aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/jackc/puddle/v2/log.go
blob: b21b9463052b9b120f6b26926502b4a3afa6bdd2 (plain) (blame)
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
31
32
package puddle

import "unsafe"

type ints interface {
	int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64
}

// log2Int returns log2 of an integer. This function panics if val < 0. For val
// == 0, returns 0.
func log2Int[T ints](val T) uint8 {
	if val <= 0 {
		panic("log2 of non-positive number does not exist")
	}

	return log2IntRange(val, 0, uint8(8*unsafe.Sizeof(val)))
}

func log2IntRange[T ints](val T, begin, end uint8) uint8 {
	length := end - begin
	if length == 1 {
		return begin
	}

	delim := begin + length/2
	mask := T(1) << delim
	if mask > val {
		return log2IntRange(val, begin, delim)
	} else {
		return log2IntRange(val, delim, end)
	}
}