github.com/pidato/unsafe@v0.1.4/memory/hash/fnv.go (about) 1 package hash 2 3 ///////////////////////////////////////////////////////////////////////////////////////////////////////// 4 // fnv 5 ///////////////////////////////////////////////////////////////////////////////////////////////////////// 6 7 func FNV32a(v uint32) uint32 { 8 const ( 9 offset32 = uint32(2166136261) 10 prime32 = uint32(16777619) 11 ) 12 hash := offset32 13 hash ^= uint32(byte(v)) 14 hash *= prime32 15 hash ^= uint32(byte(v >> 8)) 16 hash *= prime32 17 hash ^= uint32(byte(v >> 16)) 18 hash *= prime32 19 hash ^= uint32(byte(v >> 24)) 20 hash *= prime32 21 return hash 22 } 23 24 func FNV32(v uint32) uint32 { 25 const ( 26 offset32 = uint32(2166136261) 27 prime32 = uint32(16777619) 28 ) 29 hash := offset32 30 hash *= prime32 31 hash ^= uint32(byte(v)) 32 hash *= prime32 33 hash ^= uint32(byte(v >> 8)) 34 hash *= prime32 35 hash ^= uint32(byte(v >> 16)) 36 hash *= prime32 37 hash ^= uint32(byte(v >> 24)) 38 return hash 39 } 40 41 func FNV64a(v uint64) uint64 { 42 const ( 43 offset64 = uint64(14695981039346656037) 44 prime64 = uint64(1099511628211) 45 ) 46 //return (v * prime64) ^ offset64 47 hash := offset64 48 hash ^= uint64(byte(v)) 49 hash *= prime64 50 hash ^= uint64(byte(v >> 8)) 51 hash *= prime64 52 hash ^= uint64(byte(v >> 16)) 53 hash *= prime64 54 hash ^= uint64(byte(v >> 24)) 55 hash *= prime64 56 hash ^= uint64(byte(v >> 32)) 57 hash *= prime64 58 hash ^= uint64(byte(v >> 40)) 59 hash *= prime64 60 hash ^= uint64(byte(v >> 48)) 61 hash *= prime64 62 hash ^= uint64(byte(v >> 56)) 63 hash *= prime64 64 return hash 65 } 66 67 func FNV64(v uint64) uint64 { 68 const ( 69 offset64 = uint64(14695981039346656037) 70 prime64 = uint64(1099511628211) 71 ) 72 hash := offset64 73 hash *= prime64 74 hash ^= uint64(byte(v)) 75 hash *= prime64 76 hash ^= uint64(byte(v >> 8)) 77 hash *= prime64 78 hash ^= uint64(byte(v >> 16)) 79 hash *= prime64 80 hash ^= uint64(byte(v >> 24)) 81 hash *= prime64 82 hash ^= uint64(byte(v >> 32)) 83 hash *= prime64 84 hash ^= uint64(byte(v >> 40)) 85 hash *= prime64 86 hash ^= uint64(byte(v >> 48)) 87 hash *= prime64 88 hash ^= uint64(byte(v >> 56)) 89 return hash 90 }