Shrinking PEXT/PDEP bitboards

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
leanchess
Posts: 181
Joined: Sun Dec 08, 2019 8:16 pm
Full name: Dmitry Shechtman

Shrinking PEXT/PDEP bitboards

Post by leanchess »

I'm not sure whether PEXT/PDEP bitboards are state-of-the-art, but I just managed to shave exactly 16 KiB off the 210.25 KiB table using the following recipe:

Code: Select all

uint32_t comp_data(uint32_t count, uint32_t *index) {
	for (uint32_t j = 0; j + count < *index; ++j) {
		if (!memcmp(&bmi2_data[j], data, count * sizeof(uint16_t))) {
			return j;
		}
	}
	memcpy(&bmi2_data[*index], data, count * sizeof(uint16_t));
	*index += count;
	return *index - count;
}

void init_attacks_sq(bmi2_pdep_t attacks[SQ_NONE], int sq, int start, int end, uint32_t *index) {
	attacks[sq].imask = get_imask(sq, start, end);
	attacks[sq].omask = get_omask(sq, start, end);
	uint32_t count = get_data(attacks, sq, start, end);
	uint32_t j = comp_data(count, index);
	attacks[sq].data = &bmi2_data[j];
}

void init_attacks_rook(uint32_t *index) {
	for (int sq = H8; sq >= A1; --sq) {
		init_attacks_sq(bmi2_attacks.rook, sq, 4, 8, index);
	}
}

void init_attacks_bishop(uint32_t *index) {
	for (int sq = A1; sq <= H8; ++sq) {
		init_attacks_sq(bmi2_attacks.bishop, sq, 0, 4, index);
	}
}

void bmi2_init_attacks() {
	uint32_t i = 0;
	init_attacks_rook(&i);
	init_attacks_bishop(&i);
}
Hopefully someone will find this useful.