|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <array> |
| 4 | +#include <vector> |
| 5 | +#include <cassert> |
| 6 | +#include <cinttypes> |
| 7 | +#include <bitset> |
| 8 | +#include <string> |
| 9 | + |
| 10 | +#include "constants.hpp" |
| 11 | +#include "util.hpp" |
| 12 | + |
| 13 | +namespace bitboard |
| 14 | +{ |
| 15 | + typedef std::uint64_t Bitboard; |
| 16 | + |
| 17 | + constexpr bool hasBitAt(const Bitboard board, int position) |
| 18 | + { |
| 19 | + assert(position >= 0 && position < constants::SQUARES_AMOUNT); |
| 20 | + return (board >> position) & 1; |
| 21 | + } |
| 22 | + |
| 23 | + constexpr Bitboard setBitAt(Bitboard board, int position) |
| 24 | + { |
| 25 | + assert(position >= 0 && position < constants::SQUARES_AMOUNT); |
| 26 | + return board | (static_cast<uint64_t>(1ULL) << position); |
| 27 | + } |
| 28 | + |
| 29 | + constexpr Bitboard clearBitAt(Bitboard board, int position) |
| 30 | + { |
| 31 | + assert(position >= 0 && position < constants::SQUARES_AMOUNT); |
| 32 | + return board & ~(static_cast<uint64_t>(1ULL) << position); |
| 33 | + } |
| 34 | + |
| 35 | + constexpr Bitboard mask(Bitboard board, Bitboard mask) |
| 36 | + { |
| 37 | + return board & mask; |
| 38 | + } |
| 39 | + |
| 40 | + constexpr Bitboard swapMask(Bitboard board, Bitboard mask) |
| 41 | + { |
| 42 | + return board ^ mask; |
| 43 | + } |
| 44 | + |
| 45 | + inline std::string toString(Bitboard board) |
| 46 | + { |
| 47 | + std::string s; |
| 48 | + |
| 49 | + for (int rank = asInt(constants::Rank::_8); rank >= asInt(constants::Rank::_1); --rank) |
| 50 | + { |
| 51 | + for (int file = asInt(constants::File::A); file <= asInt(constants::File::H); file++) |
| 52 | + { |
| 53 | + int square = util::_120xyToSquare(file, rank); |
| 54 | + int _64_square = util::_120To64(square); |
| 55 | + |
| 56 | + if (hasBitAt(board, _64_square)) |
| 57 | + { |
| 58 | + s += "1 "; |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + s += "0 "; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + s += "\n"; |
| 67 | + } |
| 68 | + return s; |
| 69 | + } |
| 70 | + |
| 71 | +#ifdef USE_ASM |
| 72 | +# ifndef _MSC_VER |
| 73 | + inline int bitscanForward(uint64_t x) |
| 74 | + { |
| 75 | + uint64_t r; |
| 76 | + |
| 77 | + asm( |
| 78 | + "bsf %[x],%[r];" |
| 79 | + : [ r ] "=a"(r) |
| 80 | + : [ x ] "r"(x) |
| 81 | + ); |
| 82 | + |
| 83 | + return r; |
| 84 | + } |
| 85 | + |
| 86 | + inline int bitscanReverse(uint64_t x)-- |
| 87 | + { |
| 88 | + uint64_t r; |
| 89 | + |
| 90 | + asm( |
| 91 | + "bsr %[x],%[r];" |
| 92 | + : [ r ] "=a"(r) |
| 93 | + : [ x ] "r"(x)); |
| 94 | + |
| 95 | + return r; |
| 96 | + } |
| 97 | +# else |
| 98 | + |
| 99 | + inline int bitscanForward(uint64_t x) { |
| 100 | + unsigned long result = -1; |
| 101 | + _BitScanForward64(&result, x); |
| 102 | + return result; |
| 103 | + } |
| 104 | + |
| 105 | + inline int bitscanReverse(uint64_t x) { |
| 106 | + unsigned long result = -1; |
| 107 | + _BitScanReverse64(&result, x); |
| 108 | + return result; |
| 109 | + } |
| 110 | +# endif |
| 111 | +#else |
| 112 | +#pragma message("USE_ASM not defined: Using slow alternative implementation of bitscan.") |
| 113 | + |
| 114 | + inline int bitscanForward(uint64_t x) |
| 115 | + { |
| 116 | + int result = 0; |
| 117 | + while ((x & 1) == 0) |
| 118 | + { |
| 119 | + x >>= 1; |
| 120 | + ++result; |
| 121 | + } |
| 122 | + |
| 123 | + return result; |
| 124 | + } |
| 125 | + |
| 126 | + inline int bitscanReverse(uint64_t x) |
| 127 | + { |
| 128 | + int result = 0; |
| 129 | + while ((x & (0ULL | 1ULL << 63)) == 0) |
| 130 | + { |
| 131 | + x <<= 1; |
| 132 | + ++result; |
| 133 | + } |
| 134 | + |
| 135 | + return result; |
| 136 | + } |
| 137 | +#endif |
| 138 | + |
| 139 | + inline int countBits(Bitboard board) |
| 140 | + { |
| 141 | + int bit_count = 0; |
| 142 | + Bitboard current_board = board; |
| 143 | + |
| 144 | + while (current_board) |
| 145 | + { |
| 146 | + current_board = current_board >> (1 + bitscanForward(current_board)); |
| 147 | + bit_count++; |
| 148 | + } |
| 149 | + |
| 150 | + return bit_count; |
| 151 | + } |
| 152 | + |
| 153 | +} // namespace bitboard |
0 commit comments