hash.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2023 Agustina Arzille.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Hash functions for integers and byte strings.
  18. *
  19. * Integer hashing functions are based on this article by Chris Wellons:
  20. * https://nullprogram.com/blog/2018/07/31/
  21. *
  22. * String hashing is based on Bruno Haible's implementation:
  23. * https://www.haible.de/bruno/hashfunc.html
  24. *
  25. */
  26. #ifndef KERN_HASH_H
  27. #define KERN_HASH_H
  28. #include <assert.h>
  29. #include <stdbool.h>
  30. #include <stdint.h>
  31. static inline uint32_t
  32. hash_bytes (const void *ptr, size_t len)
  33. {
  34. uint32_t ret = (uint32_t)len;
  35. for (size_t i = 0; i < len; ++i)
  36. {
  37. ret = (ret << 9) | (ret >> 23);
  38. ret += ((const unsigned char *)ptr)[i];
  39. }
  40. return (ret ?: ~(uint32_t)0);
  41. }
  42. static inline uint32_t
  43. hash_mix (uint32_t h1, uint32_t h2)
  44. {
  45. return (h2 ^ ((h1 << 5) | (h1 >> 27)));
  46. }
  47. static inline uint32_t
  48. hash_u32 (uint32_t x)
  49. {
  50. x ^= x >> 16;
  51. x *= 0x21f0aaad;
  52. x ^= x >> 15;
  53. x *= 0x735a2d97;
  54. x ^= x >> 15;
  55. return (x);
  56. }
  57. static inline uint32_t
  58. hash_u64 (uint64_t x)
  59. {
  60. uint32_t lo = (uint32_t)x, hi = (uint32_t)(x >> 32);
  61. return (hash_mix (hash_u32 (lo), hash_u32 (hi)));
  62. }
  63. static inline uint32_t
  64. hash_uptr (uintptr_t x)
  65. {
  66. #ifdef __LP64__
  67. return (hash_u64 (x));
  68. #else
  69. return (hash_u32 (x));
  70. #endif
  71. }
  72. #endif