github.com/saintwish/kv@v1.0.4/swiss/bits.go (about) 1 // From https://github.com/dolthub/swiss 2 // Copyright 2023 Dolthub, Inc. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 //go:build !amd64 || nosimd 17 18 package swiss 19 20 import ( 21 "math/bits" 22 "unsafe" 23 ) 24 25 const ( 26 groupSize = 8 27 maxAvgGroupLoad = 7 28 29 loBits uint64 = 0x0101010101010101 30 hiBits uint64 = 0x8080808080808080 31 ) 32 33 type bitset uint64 34 35 func metaMatchH2(m *metadata, h h2) bitset { 36 // https://graphics.stanford.edu/~seander/bithacks.html##ValueInWord 37 return hasZeroByte(castUint64(m) ^ (loBits * uint64(h))) 38 } 39 40 func metaMatchEmpty(m *metadata) bitset { 41 return hasZeroByte(castUint64(m) ^ hiBits) 42 } 43 44 func nextMatch(b *bitset) uint32 { 45 s := uint32(bits.TrailingZeros64(uint64(*b))) 46 *b &= ^(1 << s) // clear bit |s| 47 return s >> 3 // div by 8 48 } 49 50 func hasZeroByte(x uint64) bitset { 51 return bitset(((x - loBits) & ^(x)) & hiBits) 52 } 53 54 func castUint64(m *metadata) uint64 { 55 return *(*uint64)((unsafe.Pointer)(m)) 56 } 57 58 //go:linkname fastrand runtime.fastrand 59 func fastrand() uint32