Introduce bitcount.h

It will be used for POPCNT intrinsics.

For now no bianry and functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba
2009-05-21 15:29:28 +02:00
parent 1b0888708d
commit 3376c68f4b
8 changed files with 192 additions and 90 deletions

View File

@@ -35,6 +35,7 @@
#include <iostream> #include <iostream>
#include "bitboard.h" #include "bitboard.h"
#include "bitcount.h"
#include "direction.h" #include "direction.h"
@@ -460,7 +461,7 @@ namespace {
Bitboard index_to_bitboard(int index, Bitboard mask) { Bitboard index_to_bitboard(int index, Bitboard mask) {
int i, j, bits = count_1s(mask); int i, j, bits = count_1s<false>(mask);
Bitboard result = 0ULL; Bitboard result = 0ULL;
for(i = 0; i < bits; i++) { for(i = 0; i < bits; i++) {
j = pop_1st_bit(&mask); j = pop_1st_bit(&mask);

View File

@@ -22,7 +22,6 @@
#if !defined(BITBOARD_H_INCLUDED) #if !defined(BITBOARD_H_INCLUDED)
#define BITBOARD_H_INCLUDED #define BITBOARD_H_INCLUDED
//// ////
//// Defines //// Defines
//// ////
@@ -47,15 +46,10 @@
//#define USE_32BIT_ATTACKS //#define USE_32BIT_ATTACKS
#define USE_FOLDED_BITSCAN #define USE_FOLDED_BITSCAN
#define BITCOUNT_SWAR_64
//#define BITCOUNT_SWAR_32
//#define BITCOUNT_LOOP
#else #else
#define USE_32BIT_ATTACKS #define USE_32BIT_ATTACKS
#define USE_FOLDED_BITSCAN #define USE_FOLDED_BITSCAN
#define BITCOUNT_SWAR_32
#endif #endif
@@ -429,65 +423,6 @@ inline Bitboard isolated_pawn_mask(Square s) {
} }
/// count_1s() counts the number of nonzero bits in a bitboard.
#if defined(BITCOUNT_LOOP)
inline int count_1s(Bitboard b) {
int r;
for(r = 0; b; r++, b &= b - 1);
return r;
}
inline int count_1s_max_15(Bitboard b) {
return count_1s(b);
}
#elif defined(BITCOUNT_SWAR_32)
inline int count_1s(Bitboard b) {
unsigned w = unsigned(b >> 32), v = unsigned(b);
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
w -= (w >> 1) & 0x55555555;
v = ((v >> 2) & 0x33333333) + (v & 0x33333333); // 0-4 in 4 bits
w = ((w >> 2) & 0x33333333) + (w & 0x33333333);
v = ((v >> 4) + v) & 0x0F0F0F0F; // 0-8 in 8 bits
v += (((w >> 4) + w) & 0x0F0F0F0F); // 0-16 in 8 bits
v *= 0x01010101; // mul is fast on amd procs
return int(v >> 24);
}
inline int count_1s_max_15(Bitboard b) {
unsigned w = unsigned(b >> 32), v = unsigned(b);
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
w -= (w >> 1) & 0x55555555;
v = ((v >> 2) & 0x33333333) + (v & 0x33333333); // 0-4 in 4 bits
w = ((w >> 2) & 0x33333333) + (w & 0x33333333);
v += w; // 0-8 in 4 bits
v *= 0x11111111;
return int(v >> 28);
}
#elif defined(BITCOUNT_SWAR_64)
inline int count_1s(Bitboard b) {
b -= ((b>>1) & 0x5555555555555555ULL);
b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
b = ((b>>4) + b) & 0x0F0F0F0F0F0F0F0FULL;
b *= 0x0101010101010101ULL;
return int(b >> 56);
}
inline int count_1s_max_15(Bitboard b) {
b -= (b>>1) & 0x5555555555555555ULL;
b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
b *= 0x1111111111111111ULL;
return int(b >> 60);
}
#endif // BITCOUNT
//// ////
//// Prototypes //// Prototypes
//// ////

162
src/bitcount.h Normal file
View File

@@ -0,0 +1,162 @@
/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
Copyright (C) 2008-2009 Marco Costalba
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
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if !defined(BITCOUNT_H_INCLUDED)
#define BITCOUNT_H_INCLUDED
#include "bitboard.h"
// Select type of software bit count function to use
#if !defined(AUTO_CONFIGURATION) || defined(IS_64BIT)
//#define USE_COMPACT_ROOK_ATTACKS
//#define USE_32BIT_ATTACKS
#define USE_FOLDED_BITSCAN
#define BITCOUNT_SWAR_64
//#define BITCOUNT_SWAR_32
//#define BITCOUNT_LOOP
#else
#define USE_32BIT_ATTACKS
#define USE_FOLDED_BITSCAN
#define BITCOUNT_SWAR_32
#endif
// Select type of intrinsic bit count instruction to use
#if defined(_MSC_VER) // Microsoft compiler
#include <intrin.h>
inline bool cpu_has_popcnt() {
int CPUInfo[4] = {-1};
__cpuid(CPUInfo, 0x00000001);
return (CPUInfo[2] >> 23) & 1;
}
#define POPCNT_INTRINSIC(x) __popcnt64(x)
#elif defined(__INTEL_COMPILER) && (defined(__x86_64) || defined(_M_X64)) // Intel compiler
#include <nmmintrin.h>
inline bool cpu_has_popcnt() {
int CPUInfo[4] = {-1};
__cpuid(CPUInfo, 0x00000001);
return (CPUInfo[2] >> 23) & 1;
}
#define POPCNT_INTRINSIC(x) _mm_popcnt_u64(x)
#else // Safe fallback for unsupported compilers
inline bool cpu_has_popcnt() { return false; }
#define POPCNT_INTRINSIC(x) sw_count_1s(x)
#endif
/// Software implementation of bit count functions
#if defined(BITCOUNT_LOOP)
inline int sw_count_1s(Bitboard b) {
int r;
for(r = 0; b; r++, b &= b - 1);
return r;
}
inline int sw_count_1s_max_15(Bitboard b) {
return count_1s(b);
}
#elif defined(BITCOUNT_SWAR_32)
inline int sw_count_1s(Bitboard b) {
unsigned w = unsigned(b >> 32), v = unsigned(b);
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
w -= (w >> 1) & 0x55555555;
v = ((v >> 2) & 0x33333333) + (v & 0x33333333); // 0-4 in 4 bits
w = ((w >> 2) & 0x33333333) + (w & 0x33333333);
v = ((v >> 4) + v) & 0x0F0F0F0F; // 0-8 in 8 bits
v += (((w >> 4) + w) & 0x0F0F0F0F); // 0-16 in 8 bits
v *= 0x01010101; // mul is fast on amd procs
return int(v >> 24);
}
inline int sw_count_1s_max_15(Bitboard b) {
unsigned w = unsigned(b >> 32), v = unsigned(b);
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
w -= (w >> 1) & 0x55555555;
v = ((v >> 2) & 0x33333333) + (v & 0x33333333); // 0-4 in 4 bits
w = ((w >> 2) & 0x33333333) + (w & 0x33333333);
v += w; // 0-8 in 4 bits
v *= 0x11111111;
return int(v >> 28);
}
#elif defined(BITCOUNT_SWAR_64)
inline int sw_count_1s(Bitboard b) {
b -= ((b>>1) & 0x5555555555555555ULL);
b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
b = ((b>>4) + b) & 0x0F0F0F0F0F0F0F0FULL;
b *= 0x0101010101010101ULL;
return int(b >> 56);
}
inline int sw_count_1s_max_15(Bitboard b) {
b -= (b>>1) & 0x5555555555555555ULL;
b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
b *= 0x1111111111111111ULL;
return int(b >> 60);
}
#endif // BITCOUNT
/// count_1s() counts the number of nonzero bits in a bitboard.
/// If template parameter is true an intrinsic is called, otherwise
/// we fallback on a software implementation.
template<bool UseIntrinsic>
inline int count_1s(Bitboard b) {
return UseIntrinsic ? POPCNT_INTRINSIC(b) : sw_count_1s(b);
}
template<bool UseIntrinsic>
inline int count_1s_max_15(Bitboard b) {
return UseIntrinsic ? POPCNT_INTRINSIC(b) : sw_count_1s_max_15(b);
}
#endif // !defined(BITCOUNT_H_INCLUDED)

View File

@@ -25,6 +25,7 @@
#include <cassert> #include <cassert>
#include "bitbase.h" #include "bitbase.h"
#include "bitcount.h"
#include "endgame.h" #include "endgame.h"
@@ -377,7 +378,7 @@ Value EvaluationFunction<KBBKN>::apply(const Position& pos) {
result += Value(square_distance(bksq, nsq) * 32); result += Value(square_distance(bksq, nsq) * 32);
// Bonus for restricting the knight's mobility // Bonus for restricting the knight's mobility
result += Value((8 - count_1s_max_15(pos.piece_attacks<KNIGHT>(nsq))) * 8); result += Value((8 - count_1s_max_15<false>(pos.piece_attacks<KNIGHT>(nsq))) * 8);
return (strongerSide == pos.side_to_move() ? result : -result); return (strongerSide == pos.side_to_move() ? result : -result);
} }

View File

@@ -25,6 +25,7 @@
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
#include "bitcount.h"
#include "evaluate.h" #include "evaluate.h"
#include "material.h" #include "material.h"
#include "pawns.h" #include "pawns.h"
@@ -339,8 +340,8 @@ Value evaluate(const Position &pos, EvalInfo &ei, int threadID) {
// Initialize pawn attack bitboards for both sides // Initialize pawn attack bitboards for both sides
ei.attackedBy[WHITE][PAWN] = ((pos.pawns(WHITE) << 9) & ~FileABB) | ((pos.pawns(WHITE) << 7) & ~FileHBB); ei.attackedBy[WHITE][PAWN] = ((pos.pawns(WHITE) << 9) & ~FileABB) | ((pos.pawns(WHITE) << 7) & ~FileHBB);
ei.attackedBy[BLACK][PAWN] = ((pos.pawns(BLACK) >> 7) & ~FileABB) | ((pos.pawns(BLACK) >> 9) & ~FileHBB); ei.attackedBy[BLACK][PAWN] = ((pos.pawns(BLACK) >> 7) & ~FileABB) | ((pos.pawns(BLACK) >> 9) & ~FileHBB);
ei.kingAttackersCount[WHITE] = count_1s_max_15(ei.attackedBy[WHITE][PAWN] & ei.attackedBy[BLACK][KING])/2; ei.kingAttackersCount[WHITE] = count_1s_max_15<false>(ei.attackedBy[WHITE][PAWN] & ei.attackedBy[BLACK][KING])/2;
ei.kingAttackersCount[BLACK] = count_1s_max_15(ei.attackedBy[BLACK][PAWN] & ei.attackedBy[WHITE][KING])/2; ei.kingAttackersCount[BLACK] = count_1s_max_15<false>(ei.attackedBy[BLACK][PAWN] & ei.attackedBy[WHITE][KING])/2;
// Evaluate pieces // Evaluate pieces
for (Color c = WHITE; c <= BLACK; c++) for (Color c = WHITE; c <= BLACK; c++)
@@ -481,8 +482,8 @@ void init_eval(int threads) {
for (Bitboard b = 0ULL; b < 256ULL; b++) for (Bitboard b = 0ULL; b < 256ULL; b++)
{ {
assert(count_1s(b) == int(uint8_t(count_1s(b)))); assert(count_1s<false>(b) == int(uint8_t(count_1s<false>(b))));
BitCount8Bit[b] = (uint8_t)count_1s(b); BitCount8Bit[b] = (uint8_t)count_1s<false>(b);
} }
} }
@@ -547,15 +548,15 @@ namespace {
ei.kingAttackersWeight[us] += AttackWeight[Piece]; ei.kingAttackersWeight[us] += AttackWeight[Piece];
Bitboard bb = (b & ei.attackedBy[them][KING]); Bitboard bb = (b & ei.attackedBy[them][KING]);
if (bb) if (bb)
ei.kingAdjacentZoneAttacksCount[us] += count_1s_max_15(bb); ei.kingAdjacentZoneAttacksCount[us] += count_1s_max_15<false>(bb);
} }
// Remove squares protected by enemy pawns // Remove squares protected by enemy pawns
Bitboard bb = (b & ~ei.attackedBy[them][PAWN]); Bitboard bb = (b & ~ei.attackedBy[them][PAWN]);
// Mobility // Mobility
int mob = (Piece != QUEEN ? count_1s_max_15(bb & ~p.pieces_of_color(us)) int mob = (Piece != QUEEN ? count_1s_max_15<false>(bb & ~p.pieces_of_color(us))
: count_1s(bb & ~p.pieces_of_color(us))); : count_1s<false>(bb & ~p.pieces_of_color(us)));
ei.mgMobility += Sign[us] * MgBonus[Piece][mob]; ei.mgMobility += Sign[us] * MgBonus[Piece][mob];
ei.egMobility += Sign[us] * EgBonus[Piece][mob]; ei.egMobility += Sign[us] * EgBonus[Piece][mob];
@@ -744,7 +745,7 @@ namespace {
// quality of the pawn shelter. // quality of the pawn shelter.
int attackUnits = int attackUnits =
Min((ei.kingAttackersCount[them] * ei.kingAttackersWeight[them]) / 2, 25) Min((ei.kingAttackersCount[them] * ei.kingAttackersWeight[them]) / 2, 25)
+ (ei.kingAdjacentZoneAttacksCount[them] + count_1s_max_15(undefended)) * 3 + (ei.kingAdjacentZoneAttacksCount[them] + count_1s_max_15<false>(undefended)) * 3
+ InitKingDanger[relative_square(us, s)] - (shelter >> 5); + InitKingDanger[relative_square(us, s)] - (shelter >> 5);
// Analyse safe queen contact checks // Analyse safe queen contact checks
@@ -760,7 +761,7 @@ namespace {
{ {
// The bitboard b now contains the squares available for safe queen // The bitboard b now contains the squares available for safe queen
// contact checks. // contact checks.
int count = count_1s_max_15(b); int count = count_1s_max_15<false>(b);
attackUnits += QueenContactCheckBonus * count * (sente ? 2 : 1); attackUnits += QueenContactCheckBonus * count * (sente ? 2 : 1);
// Is there a mate threat? // Is there a mate threat?
@@ -800,12 +801,12 @@ namespace {
// Queen checks // Queen checks
b2 = b & ei.attacked_by(them, QUEEN); b2 = b & ei.attacked_by(them, QUEEN);
if( b2) if( b2)
attackUnits += QueenCheckBonus * count_1s_max_15(b2); attackUnits += QueenCheckBonus * count_1s_max_15<false>(b2);
// Rook checks // Rook checks
b2 = b & ei.attacked_by(them, ROOK); b2 = b & ei.attacked_by(them, ROOK);
if (b2) if (b2)
attackUnits += RookCheckBonus * count_1s_max_15(b2); attackUnits += RookCheckBonus * count_1s_max_15<false>(b2);
} }
if (QueenCheckBonus > 0 || BishopCheckBonus > 0) if (QueenCheckBonus > 0 || BishopCheckBonus > 0)
{ {
@@ -814,12 +815,12 @@ namespace {
// Queen checks // Queen checks
b2 = b & ei.attacked_by(them, QUEEN); b2 = b & ei.attacked_by(them, QUEEN);
if (b2) if (b2)
attackUnits += QueenCheckBonus * count_1s_max_15(b2); attackUnits += QueenCheckBonus * count_1s_max_15<false>(b2);
// Bishop checks // Bishop checks
b2 = b & ei.attacked_by(them, BISHOP); b2 = b & ei.attacked_by(them, BISHOP);
if (b2) if (b2)
attackUnits += BishopCheckBonus * count_1s_max_15(b2); attackUnits += BishopCheckBonus * count_1s_max_15<false>(b2);
} }
if (KnightCheckBonus > 0) if (KnightCheckBonus > 0)
{ {
@@ -828,7 +829,7 @@ namespace {
// Knight checks // Knight checks
b2 = b & ei.attacked_by(them, KNIGHT); b2 = b & ei.attacked_by(them, KNIGHT);
if (b2) if (b2)
attackUnits += KnightCheckBonus * count_1s_max_15(b2); attackUnits += KnightCheckBonus * count_1s_max_15<false>(b2);
} }
// Analyse discovered checks (only for non-pawns right now, consider // Analyse discovered checks (only for non-pawns right now, consider
@@ -837,7 +838,7 @@ namespace {
{ {
b = p.discovered_check_candidates(them) & ~p.pawns(); b = p.discovered_check_candidates(them) & ~p.pawns();
if (b) if (b)
attackUnits += DiscoveredCheckBonus * count_1s_max_15(b) * (sente? 2 : 1); attackUnits += DiscoveredCheckBonus * count_1s_max_15<false>(b) * (sente? 2 : 1);
} }
// Has a mate threat been found? We don't do anything here if the // Has a mate threat been found? We don't do anything here if the
@@ -972,7 +973,7 @@ namespace {
if (d < 0) if (d < 0)
{ {
int mtg = RANK_8 - relative_rank(us, s); int mtg = RANK_8 - relative_rank(us, s);
int blockerCount = count_1s_max_15(squares_in_front_of(us,s) & pos.occupied_squares()); int blockerCount = count_1s_max_15<false>(squares_in_front_of(us,s) & pos.occupied_squares());
mtg += blockerCount; mtg += blockerCount;
d += blockerCount; d += blockerCount;
if (d < 0) if (d < 0)
@@ -1133,8 +1134,8 @@ namespace {
behindFriendlyPawns |= (behindFriendlyPawns << 16); behindFriendlyPawns |= (behindFriendlyPawns << 16);
} }
int space = count_1s_max_15(safeSquares) int space = count_1s_max_15<false>(safeSquares)
+ count_1s_max_15(behindFriendlyPawns & safeSquares); + count_1s_max_15<false>(behindFriendlyPawns & safeSquares);
ei.mgValue += Sign[us] * apply_weight(Value(space * ei.mi->space_weight()), WeightSpace); ei.mgValue += Sign[us] * apply_weight(Value(space * ei.mi->space_weight()), WeightSpace);
} }

View File

@@ -18,7 +18,7 @@
*/ */
// To profile with callgrind uncomment following line // To profile with callgrind uncomment following line
//#define USE_CALLGRIND #define USE_CALLGRIND
//// ////

View File

@@ -25,6 +25,7 @@
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
#include "bitcount.h"
#include "pawns.h" #include "pawns.h"
#include "position.h" #include "position.h"
@@ -329,8 +330,8 @@ PawnInfo *PawnInfoTable::get_pawn_info(const Position &pos) {
// Test for candidate passed pawn // Test for candidate passed pawn
candidate = !passed candidate = !passed
&& pos.file_is_half_open(them, f) && pos.file_is_half_open(them, f)
&& ( count_1s_max_15(neighboring_files_bb(f) & (behind_bb(us, r) | rank_bb(r)) & ourPawns) && ( count_1s_max_15<false>(neighboring_files_bb(f) & (behind_bb(us, r) | rank_bb(r)) & ourPawns)
- count_1s_max_15(neighboring_files_bb(f) & in_front_bb(us, r) & theirPawns) - count_1s_max_15<false>(neighboring_files_bb(f) & in_front_bb(us, r) & theirPawns)
>= 0); >= 0);
// In order to prevent doubled passed pawns from receiving a too big // In order to prevent doubled passed pawns from receiving a too big

View File

@@ -27,6 +27,7 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include "bitcount.h"
#include "mersenne.h" #include "mersenne.h"
#include "movegen.h" #include "movegen.h"
#include "movepick.h" #include "movepick.h"
@@ -2090,7 +2091,7 @@ bool Position::is_ok(int* failedStep) const {
// Is there more than 2 checkers? // Is there more than 2 checkers?
if (failedStep) (*failedStep)++; if (failedStep) (*failedStep)++;
if (debugCheckerCount && count_1s(st->checkersBB) > 2) if (debugCheckerCount && count_1s<false>(st->checkersBB) > 2)
return false; return false;
// Bitboards OK? // Bitboards OK?
@@ -2165,7 +2166,7 @@ bool Position::is_ok(int* failedStep) const {
if (debugPieceCounts) if (debugPieceCounts)
for (Color c = WHITE; c <= BLACK; c++) for (Color c = WHITE; c <= BLACK; c++)
for (PieceType pt = PAWN; pt <= KING; pt++) for (PieceType pt = PAWN; pt <= KING; pt++)
if (pieceCount[c][pt] != count_1s(pieces_of_color_and_type(c, pt))) if (pieceCount[c][pt] != count_1s<false>(pieces_of_color_and_type(c, pt)))
return false; return false;
if (failedStep) (*failedStep)++; if (failedStep) (*failedStep)++;