-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.go
30 lines (24 loc) · 917 Bytes
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package umap
import (
"math/bits"
"unsafe"
)
// ptrSize is the size of a pointer in bytes - unsafe.Sizeof(uintptr(0)) but as an ideal constant.
// It is also the size of the machine's native word size (that is, 4 on 32-bit systems, 8 on 64-bit).
const ptrSize = 4 << (^uintptr(0) >> 63)
func bmapPointer(p unsafe.Pointer, n uint) *bmapuint64 {
return (*bmapuint64)(add(p, unsafe.Sizeof(bmapuint64{})*uintptr(n)))
}
func nextPowerOfTwo(length int) uint {
shift := bits.Len(uint(length))
return uint(1) << (shift & (ptrSize*8 - 1)) // 1 << shift, optimized for code generation
}
//go:nosplit
func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
return unsafe.Pointer(uintptr(p) + x)
}
// growThresholdUint64 returns (capcity - capcity*loadFactor)
func growThresholdUint64(bucketmask uint64) int {
// Same as (capcity - capcity*loadFactor), but faster.
return int((bucketmask + 1) * emptyItemInBucket)
}