github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/lib/kv/fast_hash_match.go (about)

     1  //go:build !amd64 || nosimd
     2  // +build !amd64 nosimd
     3  
     4  package kv
     5  
     6  // It is inconvenient to use SIMD on non-AMD64 platforms.
     7  // https://graphics.stanford.edu/~seander/bithacks.html##ValueInWord
     8  // bit manipulation is inconvenient for [16]int8{}, there
     9  // is not exists uint128 in Go.
    10  // Otherwise, we can check the hash value like:
    11  // (0x0101_0101_0101_0101_0101_0101_0101_0101 * hash) ^ uint128([16]int8)
    12  // Then convert it to uint16 by byte manipulation.
    13  func Fast16WayHashMatch(md *[16]int8, hash int8) uint16 {
    14  	res := uint16(0)
    15  	for i := 0; i < 16; i++ {
    16  		if md[i] == hash { // XOR byte.
    17  			res |= 1 << uint(i)
    18  		}
    19  	}
    20  	return res
    21  }