32-bit input / arbitrary bit output hash function

A hash function from a 32-bit value to a specified bit length. From the linux kernel.

unsigned long hash_long(unsigned long val, unsigned int bits)
{
  unsigned long hash = val * 0x9e370001UL;
  return hash >> (32 - bits);
}

For the magic number 0x9e370001 (16) / 2654404609 (10), a prime number close to the golden ratio 2 ^ 32 * (sqrt (5) -1) / 2 = 2654435769.5 of the 32-bit value is selected.

// 2^32 * (sqrt(5) - 1) / 2 = 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 = 2654404609

Recommended Posts

32-bit input / arbitrary bit output hash function
python input and output
Python audio input / output
Standard input / output summary