Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
+ Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
Stockfish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
-
Stockfish is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// lsb() and msb() return the least/most significant bit in a non-zero bitboard
-#ifdef USE_BSFQ
-
-# if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
+#if defined(__GNUC__)
inline Square lsb(Bitboard b) {
- unsigned long idx;
- _BitScanForward64(&idx, b);
- return (Square) idx;
+ assert(b);
+ return Square(__builtin_ctzll(b));
}
inline Square msb(Bitboard b) {
- unsigned long idx;
- _BitScanReverse64(&idx, b);
- return (Square) idx;
-}
-
-# elif defined(__arm__)
-
-inline int lsb32(uint32_t v) {
- __asm__("rbit %0, %1" : "=r"(v) : "r"(v));
- return __builtin_clz(v);
+ assert(b);
+ return Square(63 - __builtin_clzll(b));
}
-inline Square msb(Bitboard b) {
- return (Square) (63 - __builtin_clzll(b));
-}
+#elif defined(_WIN64) && defined(_MSC_VER)
inline Square lsb(Bitboard b) {
- return (Square) (uint32_t(b) ? lsb32(uint32_t(b)) : 32 + lsb32(uint32_t(b >> 32)));
-}
-
-# else // Assumed gcc or compatible compiler
-
-inline Square lsb(Bitboard b) { // Assembly code by Heinz van Saanen
- Bitboard idx;
- __asm__("bsfq %1, %0": "=r"(idx): "rm"(b) );
+ assert(b);
+ unsigned long idx;
+ _BitScanForward64(&idx, b);
return (Square) idx;
}
inline Square msb(Bitboard b) {
- Bitboard idx;
- __asm__("bsrq %1, %0": "=r"(idx): "rm"(b) );
+ assert(b);
+ unsigned long idx;
+ _BitScanReverse64(&idx, b);
return (Square) idx;
}
-# endif
+#else
-#else // ifdef(USE_BSFQ)
+#define NO_BSF // Fallback on software implementation for other cases
Square lsb(Bitboard b);
Square msb(Bitboard b);