github.com/comwrg/go/src@v0.0.0-20220319063731-c238d0440370/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 // wyhash: https://github.com/wangyi-fudan/wyhash/blob/ceb019b530e2c1c14d70b79bfa2bc49de7d95bc1/Modern%20Non-Cryptographic%20Hash%20Function%20and%20Pseudorandom%20Number%20Generator.pdf 7 8 //go:build 386 || arm || mips || mipsle 9 // +build 386 arm mips mipsle 10 11 package runtime 12 13 import "unsafe" 14 15 func memhash32Fallback(p unsafe.Pointer, seed uintptr) uintptr { 16 a, b := mix32(uint32(seed), uint32(4^hashkey[0])) 17 t := readUnaligned32(p) 18 a ^= t 19 b ^= t 20 a, b = mix32(a, b) 21 a, b = mix32(a, b) 22 return uintptr(a ^ b) 23 } 24 25 func memhash64Fallback(p unsafe.Pointer, seed uintptr) uintptr { 26 a, b := mix32(uint32(seed), uint32(8^hashkey[0])) 27 a ^= readUnaligned32(p) 28 b ^= readUnaligned32(add(p, 4)) 29 a, b = mix32(a, b) 30 a, b = mix32(a, b) 31 return uintptr(a ^ b) 32 } 33 34 func memhashFallback(p unsafe.Pointer, seed, s uintptr) uintptr { 35 36 a, b := mix32(uint32(seed), uint32(s^hashkey[0])) 37 if s == 0 { 38 return uintptr(a ^ b) 39 } 40 for ; s > 8; s -= 8 { 41 a ^= readUnaligned32(p) 42 b ^= readUnaligned32(add(p, 4)) 43 a, b = mix32(a, b) 44 p = add(p, 8) 45 } 46 if s >= 4 { 47 a ^= readUnaligned32(p) 48 b ^= readUnaligned32(add(p, s-4)) 49 } else { 50 t := uint32(*(*byte)(p)) 51 t |= uint32(*(*byte)(add(p, s>>1))) << 8 52 t |= uint32(*(*byte)(add(p, s-1))) << 16 53 b ^= t 54 } 55 a, b = mix32(a, b) 56 a, b = mix32(a, b) 57 return uintptr(a ^ b) 58 } 59 60 func mix32(a, b uint32) (uint32, uint32) { 61 c := uint64(a^uint32(hashkey[1])) * uint64(b^uint32(hashkey[2])) 62 return uint32(c), uint32(c >> 32) 63 }