Stockfish 国际象棋引擎需要存储残局得分和中局得分以进行评估。
它将它们打包成一个 ,而不是单独存储它们int。中局得分存储在低 16 位中。如果中局得分为正,则残局得分存储在高 16 位中;如果中局得分为负,则减一。
这样做的优点是可以并行地对两个数字进行运算(加法、减法、求反和乘法)。
/// Score enum stores a middlegame and an endgame value in a single integer (enum).
/// The least significant 16 bits are used to store the middlegame value and the
/// upper 16 bits are used to store the endgame value. We have to take care to
/// avoid left-shifting a signed int to avoid undefined behavior.
enum Score : int { SCORE_ZERO };
constexpr Score make_score(int mg, …Run Code Online (Sandbox Code Playgroud)