Source file src/internal/runtime/maps/runtime_hash32.go

     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 || wasm || (gccgo && (ppc || s390))
     9  
    10  package maps
    11  
    12  import "unsafe"
    13  
    14  func memHash32Fallback(k uint32, seed uintptr) uintptr {
    15  	a, b := mix32(uint32(seed), uint32(4^hashkey[0]))
    16  	a ^= k
    17  	b ^= k
    18  	a, b = mix32(a, b)
    19  	a, b = mix32(a, b)
    20  	return uintptr(a ^ b)
    21  }
    22  
    23  func memHash64Fallback(k uint64, seed uintptr) uintptr {
    24  	a, b := mix32(uint32(seed), uint32(8^hashkey[0]))
    25  	a ^= uint32(k)
    26  	b ^= uint32(k >> 32)
    27  	a, b = mix32(a, b)
    28  	a, b = mix32(a, b)
    29  	return uintptr(a ^ b)
    30  }
    31  
    32  func memHashFallback(p unsafe.Pointer, seed, s uintptr) uintptr {
    33  	a, b := mix32(uint32(seed), uint32(s^hashkey[0]))
    34  	if s == 0 {
    35  		return uintptr(a ^ b)
    36  	}
    37  	for ; s > 8; s -= 8 {
    38  		a ^= readUnaligned32(p)
    39  		b ^= readUnaligned32(add(p, 4))
    40  		a, b = mix32(a, b)
    41  		p = add(p, 8)
    42  	}
    43  	if s >= 4 {
    44  		a ^= readUnaligned32(p)
    45  		b ^= readUnaligned32(add(p, s-4))
    46  	} else {
    47  		t := uint32(*(*byte)(p))
    48  		t |= uint32(*(*byte)(add(p, s>>1))) << 8
    49  		t |= uint32(*(*byte)(add(p, s-1))) << 16
    50  		b ^= t
    51  	}
    52  	a, b = mix32(a, b)
    53  	a, b = mix32(a, b)
    54  	return uintptr(a ^ b)
    55  }
    56  
    57  func mix32(a, b uint32) (uint32, uint32) {
    58  	c := uint64(a^uint32(hashkey[1])) * uint64(b^uint32(hashkey[2]))
    59  	return uint32(c), uint32(c >> 32)
    60  }
    61  

View as plain text