diff --git a/include/BitBoard.h b/include/BitBoard.h index 896be43..c8a0d61 100644 --- a/include/BitBoard.h +++ b/include/BitBoard.h @@ -21,6 +21,13 @@ class BitBoard bool m_isBlacksTurn = true; + /* Class member functions */ + + // These are basic 32 bit rotation functions + // Details here: http://stackoverflow.com/questions/776508/best-practices-for-circular-shift-rotate-operations-in-c + uint32_t const rotl32 (uint32_t n, unsigned int c); + uint32_t const rotr32 (uint32_t n, unsigned int c); + public: }; diff --git a/src/BitBoard.cpp b/src/BitBoard.cpp index 23f1544..706cb4b 100644 --- a/src/BitBoard.cpp +++ b/src/BitBoard.cpp @@ -1 +1,19 @@ +#include +#include #include "BitBoard.h" + +uint32_t const BitBoard::rotl32 (uint32_t n, unsigned int c) +{ + const unsigned int mask = (CHAR_BIT*sizeof(n)-1); + + c &= mask; // avoid undef behaviour with NDEBUG. 0 overhead for most types / compilers + return (n<>( (-c)&mask )); +} + +uint32_t const BitBoard::rotr32 (uint32_t n, unsigned int c) +{ + const unsigned int mask = (CHAR_BIT*sizeof(n)-1); + + c &= mask; // avoid undef behaviour with NDEBUG. 0 overhead for most types / compilers + return (n>>c) | (n<<( (-c)&mask )); +}