^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifdef __KERNEL__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) # include <linux/crush/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) # include "hash.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Robert Jenkins' function for mixing 32-bit values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * https://burtleburtle.net/bob/hash/evahash.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * a, b = random bits, c = input and output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define crush_hashmix(a, b, c) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) a = a-b; a = a-c; a = a^(c>>13); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) b = b-c; b = b-a; b = b^(a<<8); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) c = c-a; c = c-b; c = c^(b>>13); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) a = a-b; a = a-c; a = a^(c>>12); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) b = b-c; b = b-a; b = b^(a<<16); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) c = c-a; c = c-b; c = c^(b>>5); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) a = a-b; a = a-c; a = a^(c>>3); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) b = b-c; b = b-a; b = b^(a<<10); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) c = c-a; c = c-b; c = c^(b>>15); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define crush_hash_seed 1315423911
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static __u32 crush_hash32_rjenkins1(__u32 a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) __u32 hash = crush_hash_seed ^ a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) __u32 b = a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) __u32 x = 231232;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) __u32 y = 1232;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) crush_hashmix(b, x, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) crush_hashmix(y, a, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static __u32 crush_hash32_rjenkins1_2(__u32 a, __u32 b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) __u32 hash = crush_hash_seed ^ a ^ b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) __u32 x = 231232;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) __u32 y = 1232;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) crush_hashmix(a, b, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) crush_hashmix(x, a, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) crush_hashmix(b, y, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static __u32 crush_hash32_rjenkins1_3(__u32 a, __u32 b, __u32 c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) __u32 hash = crush_hash_seed ^ a ^ b ^ c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) __u32 x = 231232;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) __u32 y = 1232;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) crush_hashmix(a, b, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) crush_hashmix(c, x, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) crush_hashmix(y, a, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) crush_hashmix(b, x, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) crush_hashmix(y, c, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static __u32 crush_hash32_rjenkins1_4(__u32 a, __u32 b, __u32 c, __u32 d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) __u32 hash = crush_hash_seed ^ a ^ b ^ c ^ d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) __u32 x = 231232;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) __u32 y = 1232;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) crush_hashmix(a, b, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) crush_hashmix(c, d, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) crush_hashmix(a, x, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) crush_hashmix(y, b, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) crush_hashmix(c, x, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) crush_hashmix(y, d, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static __u32 crush_hash32_rjenkins1_5(__u32 a, __u32 b, __u32 c, __u32 d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) __u32 e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) __u32 hash = crush_hash_seed ^ a ^ b ^ c ^ d ^ e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) __u32 x = 231232;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) __u32 y = 1232;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) crush_hashmix(a, b, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) crush_hashmix(c, d, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) crush_hashmix(e, x, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) crush_hashmix(y, a, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) crush_hashmix(b, x, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) crush_hashmix(y, c, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) crush_hashmix(d, x, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) crush_hashmix(y, e, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) __u32 crush_hash32(int type, __u32 a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) case CRUSH_HASH_RJENKINS1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return crush_hash32_rjenkins1(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) __u32 crush_hash32_2(int type, __u32 a, __u32 b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) case CRUSH_HASH_RJENKINS1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return crush_hash32_rjenkins1_2(a, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) __u32 crush_hash32_3(int type, __u32 a, __u32 b, __u32 c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) case CRUSH_HASH_RJENKINS1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return crush_hash32_rjenkins1_3(a, b, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) __u32 crush_hash32_4(int type, __u32 a, __u32 b, __u32 c, __u32 d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) case CRUSH_HASH_RJENKINS1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return crush_hash32_rjenkins1_4(a, b, c, d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) __u32 crush_hash32_5(int type, __u32 a, __u32 b, __u32 c, __u32 d, __u32 e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) case CRUSH_HASH_RJENKINS1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return crush_hash32_rjenkins1_5(a, b, c, d, e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) const char *crush_hash_name(int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) case CRUSH_HASH_RJENKINS1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return "rjenkins1";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return "unknown";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }