mirror of
https://github.com/HChaZZY/Stockfish.git
synced 2025-12-20 00:56:39 +08:00
Guard against UB in lsb/msb
lsb(b) and msb(b) are undefined when b == 0. This can lead to subtle bugs, where the resulting code behaves differently on different configurations: - It can be the home grown software LSB/MSB - It can be the compiler generated software LSB/MSB (when using compiler intrinsics without the right compiler flags to allow compiler to use hardware LSB/MSB). Which of course depends on the compiler. - It can be hardware LSB/MSB generated by the compiler. - Not to mention that hardware LSB/MSB can return different value on different hardware when b == 0. No functional change Resolves #610
This commit is contained in:
@@ -81,11 +81,13 @@ namespace {
|
||||
/// Software fall-back of lsb() and msb() for CPU lacking hardware support
|
||||
|
||||
Square lsb(Bitboard b) {
|
||||
assert(b);
|
||||
return BSFTable[bsf_index(b)];
|
||||
}
|
||||
|
||||
Square msb(Bitboard b) {
|
||||
|
||||
assert(b);
|
||||
unsigned b32;
|
||||
int result = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user