hash.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. #ifndef _SELINUX_HASH_H_
  3. #define _SELINUX_HASH_H_
  4. /*
  5. * Based on MurmurHash3, written by Austin Appleby and placed in the
  6. * public domain.
  7. */
  8. static inline u32 av_hash(u32 key1, u32 key2, u32 key3, u32 mask)
  9. {
  10. static const u32 c1 = 0xcc9e2d51;
  11. static const u32 c2 = 0x1b873593;
  12. static const u32 r1 = 15;
  13. static const u32 r2 = 13;
  14. static const u32 m = 5;
  15. static const u32 n = 0xe6546b64;
  16. u32 hash = 0;
  17. #define mix(input) \
  18. do { \
  19. u32 v = input; \
  20. v *= c1; \
  21. v = (v << r1) | (v >> (32 - r1)); \
  22. v *= c2; \
  23. hash ^= v; \
  24. hash = (hash << r2) | (hash >> (32 - r2)); \
  25. hash = hash * m + n; \
  26. } while (0)
  27. mix(key1);
  28. mix(key2);
  29. mix(key3);
  30. #undef mix
  31. hash ^= hash >> 16;
  32. hash *= 0x85ebca6b;
  33. hash ^= hash >> 13;
  34. hash *= 0xc2b2ae35;
  35. hash ^= hash >> 16;
  36. return hash & mask;
  37. }
  38. #endif /* _SELINUX_HASH_H_ */