stellar

Stellar - UCI Chess engine written in C++20
git clone git://git.dimitrijedobrota.com/stellar.git
Log | Files | Refs | README | LICENSE

bit.hpp (878B)


      1 #ifndef STELLAR_BIT_H
      2 #define STELLAR_BIT_H
      3 
      4 #include "utils.hpp"
      5 #include <bit>
      6 
      7 namespace bit {
      8 
      9 inline constexpr bool get(const U64 &bitboard, uint8_t square) { return (bitboard >> square) & C64(1); }
     10 inline constexpr void set(U64 &bitboard, uint8_t square) { bitboard |= (C64(1) << square); }
     11 inline constexpr void pop(U64 &bitboard, uint8_t square) { bitboard &= ~(C64(1) << square); }
     12 
     13 inline constexpr uint8_t count(U64 bitboard) { return std::popcount(bitboard); }
     14 inline constexpr uint8_t lsb_index(U64 bitboard) { return std::countr_zero(bitboard); }
     15 inline constexpr U64 &lsb_pop(U64 &bitboard) { return bitboard = bitboard & (bitboard - 1); }
     16 
     17 #define bitboard_for_each_bit(var, bb)                                                                       \
     18     for (var = bit::lsb_index(bb); bb; bit::lsb_pop(bb), var = bit::lsb_index(bb))
     19 
     20 } // namespace bit
     21 
     22 #endif