syzygy1/tb

UBSAN overflow in the Binomial calculation

Closed this issue · 1 comments

gcp commented

https://github.com/syzygy1/tb/blob/master/src/probe.c#L788

runtime error: implicit conversion from type 'int' of value -1 (32-bit, signed) to type 'unsigned long' changed the value to 18446744073709551615 (64-bit, unsigned)

I don't think this is undefined behavior. According to the C standard, signed -1 is converted to unsigned -1 + UINT64_MAX.

If -1 is a factor, then 0 is too, so the result is 0 anyway.

So I don't think this code is buggy. However, it would be more elegant to use Bin(n,k)=Bin(n-1,k-1)+Bin(n-1,k) to generate the triangle (as Stockfish does).