《操作系统》的实验代码。

18 lines
459 B

10 years ago
  1. #include <stdlib.h>
  2. /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
  3. #define GOLDEN_RATIO_PRIME_32 0x9e370001UL
  4. /* *
  5. * hash32 - generate a hash value in the range [0, 2^@bits - 1]
  6. * @val: the input value
  7. * @bits: the number of bits in a return value
  8. *
  9. * High bits are more random, so we use them.
  10. * */
  11. uint32_t
  12. hash32(uint32_t val, unsigned int bits) {
  13. uint32_t hash = val * GOLDEN_RATIO_PRIME_32;
  14. return (hash >> (32 - bits));
  15. }