Sorry, here I went wrong.syzygy wrote:It is actually worse, and therefore more obvious, than I thought. The sort that you inserted (too early) sorts from highest ptwist[] to lowest. The sort that you removed sorts from lowest ptwist[] to highest.
The removed sort also sorts from highest ptwist[] to lowest. The binomial sum formula then goes from the last element to the first. You did not change this.
So the only problem is that flipping does break the sorting order in case of two symmetric non-leading pawns. Breaking the sorting order results in the binomial sum formula not calculating the right value. (There is only one right value; any other value corresponds to a different position or no position at all. There is absolutely no point in returning the TB value for another position than the one being probed, as should be obvious.)
Non-leading pawns on d2, e2 should give Bin(10, 1) + Bin(11, 2) = 65. Getting the order wrong gives Bin(11, 1) + Bin(10, 2) = 56. But 56 corresponds to Bin(1, 1) + Bin(11, 2), which means pawns on d7 and d2. If there are pawns on d2 and e2, you do not want to return the value for a position with pawns on d2 and d7.