<commit_before>// Copyright (c) 2013-2014 by <NAME>. All Rights Reserved.
// Use of this source code is governed by a MIT-style license that can
// be found in the LICENSE file.

package donna

import ()

var exchangeScores = []int{
	valuePawn.midgame, 	// 2->0
	valueKnight.midgame, 	// 4->1
	valueBishop.midgame, 	// 6->2
	valueRook.midgame, 	// 8->3
	valueQueen.midgame, 	//10->4
	valueQueen.midgame * 8, //12->5
}

func (p *Position) exchangeScore(to, color, score, extra int, board Bitmask) int {
	attackers := p.attackers(to, color, board) & board
	if attackers == 0 {
		return score
	}

	from, best := 0, Checkmate
	for attackers != 0 {
		square := attackers.pop()
		index := p.pieces[square].kind() / 2 - 1
		if exchangeScores[index] < best {
			from = square
			best = exchangeScores[index]
		}
	}

	if best != Checkmate {
		board ^= bit[from]
	}

	return Max(score, -p.exchangeScore(to, color^1, -(score + extra), best, board))
}

func (p *Position) exchange(move Move) int {
	from, to, piece, capture := move.split()

	score, color := 0, move.piece().color()
	if capture != 0 {
		score = exchangeScores[capture.kind() / 2 - 1]
	}
	if promo := move.promo(); promo != 0 {
		score += exchangeScores[promo / 2 - 1] - exchangeScores[0] // Pawn
		piece = promo
	}

	board := p.board ^ bit[from]
	return -p.exchangeScore(to, color^1, -score, exchangeScores[piece.kind() / 2 - 1], board)
}
<commit_msg>Optimized static exchange evaluation<commit_after>// Copyright (c) 2013-2014 by <NAME>. All Rights Reserved.
// Use of this source code is governed by a MIT-style license that can
// be found in the LICENSE file.

package donna

import ()

var exchangeScores = []int{
	0, 0, 						// Zero score for non-capture moves.
	valuePawn.midgame, valuePawn.midgame, 		// Pawn/BlackPawn captures.
	valueKnight.midgame, valueKnight.midgame, 	// Knight/BlackKinght captures.
	valueBishop.midgame, valueBishop.midgame, 	// Bishop/BlackBishop captures.
	valueRook.midgame, valueRook.midgame, 		// Rook/BlackRook captures.
	valueQueen.midgame, valueQueen.midgame, 	// Queen/BlackQueen captures.
	valueQueen.midgame * 8, valueQueen.midgame * 8, // King/BlackKing specials.
}

// Static exchange evaluation.
func (p *Position) exchange(move Move) int {
	from, to, piece, capture := move.split()

	score := exchangeScores[capture]
	if promo := move.promo(); promo != 0 {
		score += exchangeScores[promo] - exchangeScores[Pawn]
		piece = promo
	}

	board := p.board ^ bit[from]
	return -p.exchangeScore(to, piece.color()^1, -score, exchangeScores[piece], board)
}

// Recursive helper method for the static exchange evaluation.
func (p *Position) exchangeScore(to, color, score, extra int, board Bitmask) int {
	attackers := p.attackers(to, color, board) & board
	if attackers == 0 {
		return score
	}

	from, best := 0, Checkmate
	for attackers != 0 {
		square := attackers.pop()
		if index := p.pieces[square]; exchangeScores[index] < best {
			from = square
			best = exchangeScores[index]
		}
	}

	if best != Checkmate {
		board ^= bit[from]
	}

	return Max(score, -p.exchangeScore(to, color^1, -(score + extra), best, board))
}


{'fraction_non_alphanumeric': 0.14198717948717948, 'fraction_numerical': 0.01858974358974359, 'mean_word_length': 3.5495626822157433, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2056733', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1078, 'n_words': 312}