Win executable on cpu which do not support __popcnt64

Discussion of chess software programming and technical issues.

Moderator: Ras

benb
Posts: 16
Joined: Sat Dec 12, 2020 5:29 am
Full name: Ben Bradley

Re: Win executable on cpu which do not support __popcnt64

Post by benb »

Here's some code right in the middle of this video:


There's also several popcount routines in the book Hacker's Delight. There's a 2nd Edition, but I've got the 1st in front of me. Chapter 5 has pages of algorithms and code to do this, apparently with tradeoffs between code size and speed. On page 71 is a fast-looking one that uses a 256-byte array with each entry being the number of bits at its address, and uses that for howevermany bytes of a word you need. Here's the gist:

Code: Select all

int pop (unsigned x)
{
Static char table[256] = {0, 1, 1, 2, 1, 2, 2, 3, ...};

return table (x & 0xff] +
	table ((x >> 8) & 0xff] +
	table ((x >> 16) & 0xff] +
	table ((x >> 24) & 0xff]);
}
For a 64bit word you can call that twice (once with a 32bit shift) or rewrite it to use eight lookups to directly handle a uint64_t.