github.com/mhmtszr/concurrent-swiss-map@v1.0.8/swiss/bits.go (about)

     1  // Copyright 2023 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //go:build !amd64 || nosimd
    15  
    16  //nolint:all
    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