github.com/gidoBOSSftw5731/go/src@v0.0.0-20210226122457-d24b0edbf019/runtime/hash32.go (about) 1 // Copyright 2014 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Hashing algorithm inspired by 6 // xxhash: https://code.google.com/p/xxhash/ 7 // cityhash: https://code.google.com/p/cityhash/ 8 9 //go:build 386 || arm || mips || mipsle 10 // +build 386 arm mips mipsle 11 12 package runtime 13 14 import "unsafe" 15 16 const ( 17 // Constants for multiplication: four random odd 32-bit numbers. 18 m1 = 3168982561 19 m2 = 3339683297 20 m3 = 832293441 21 m4 = 2336365089 22 ) 23 24 func memhashFallback(p unsafe.Pointer, seed, s uintptr) uintptr { 25 h := uint32(seed + s*hashkey[0]) 26 tail: 27 switch { 28 case s == 0: 29 case s < 4: 30 h ^= uint32(*(*byte)(p)) 31 h ^= uint32(*(*byte)(add(p, s>>1))) << 8 32 h ^= uint32(*(*byte)(add(p, s-1))) << 16 33 h = rotl_15(h*m1) * m2 34 case s == 4: 35 h ^= readUnaligned32(p) 36 h = rotl_15(h*m1) * m2 37 case s <= 8: 38 h ^= readUnaligned32(p) 39 h = rotl_15(h*m1) * m2 40 h ^= readUnaligned32(add(p, s-4)) 41 h = rotl_15(h*m1) * m2 42 case s <= 16: 43 h ^= readUnaligned32(p) 44 h = rotl_15(h*m1) * m2 45 h ^= readUnaligned32(add(p, 4)) 46 h = rotl_15(h*m1) * m2 47 h ^= readUnaligned32(add(p, s-8)) 48 h = rotl_15(h*m1) * m2 49 h ^= readUnaligned32(add(p, s-4)) 50 h = rotl_15(h*m1) * m2 51 default: 52 v1 := h 53 v2 := uint32(seed * hashkey[1]) 54 v3 := uint32(seed * hashkey[2]) 55 v4 := uint32(seed * hashkey[3]) 56 for s >= 16 { 57 v1 ^= readUnaligned32(p) 58 v1 = rotl_15(v1*m1) * m2 59 p = add(p, 4) 60 v2 ^= readUnaligned32(p) 61 v2 = rotl_15(v2*m2) * m3 62 p = add(p, 4) 63 v3 ^= readUnaligned32(p) 64 v3 = rotl_15(v3*m3) * m4 65 p = add(p, 4) 66 v4 ^= readUnaligned32(p) 67 v4 = rotl_15(v4*m4) * m1 68 p = add(p, 4) 69 s -= 16 70 } 71 h = v1 ^ v2 ^ v3 ^ v4 72 goto tail 73 } 74 h ^= h >> 17 75 h *= m3 76 h ^= h >> 13 77 h *= m4 78 h ^= h >> 16 79 return uintptr(h) 80 } 81 82 func memhash32Fallback(p unsafe.Pointer, seed uintptr) uintptr { 83 h := uint32(seed + 4*hashkey[0]) 84 h ^= readUnaligned32(p) 85 h = rotl_15(h*m1) * m2 86 h ^= h >> 17 87 h *= m3 88 h ^= h >> 13 89 h *= m4 90 h ^= h >> 16 91 return uintptr(h) 92 } 93 94 func memhash64Fallback(p unsafe.Pointer, seed uintptr) uintptr { 95 h := uint32(seed + 8*hashkey[0]) 96 h ^= readUnaligned32(p) 97 h = rotl_15(h*m1) * m2 98 h ^= readUnaligned32(add(p, 4)) 99 h = rotl_15(h*m1) * m2 100 h ^= h >> 17 101 h *= m3 102 h ^= h >> 13 103 h *= m4 104 h ^= h >> 16 105 return uintptr(h) 106 } 107 108 // Note: in order to get the compiler to issue rotl instructions, we 109 // need to constant fold the shift amount by hand. 110 // TODO: convince the compiler to issue rotl instructions after inlining. 111 func rotl_15(x uint32) uint32 { 112 return (x << 15) | (x >> (32 - 15)) 113 }