Source file
src/net/netip/uint128.go
1
2
3
4
5 package netip
6
7 import "math/bits"
8
9
10
11
12
13 type uint128 struct {
14 hi uint64
15 lo uint64
16 }
17
18
19
20 func mask6(n int) uint128 {
21 return uint128{^(^uint64(0) >> n), ^uint64(0) << (128 - n)}
22 }
23
24
25
26
27
28
29 func (u uint128) isZero() bool { return u.hi|u.lo == 0 }
30
31
32 func (u uint128) and(m uint128) uint128 {
33 return uint128{u.hi & m.hi, u.lo & m.lo}
34 }
35
36
37 func (u uint128) xor(m uint128) uint128 {
38 return uint128{u.hi ^ m.hi, u.lo ^ m.lo}
39 }
40
41
42 func (u uint128) or(m uint128) uint128 {
43 return uint128{u.hi | m.hi, u.lo | m.lo}
44 }
45
46
47 func (u uint128) not() uint128 {
48 return uint128{^u.hi, ^u.lo}
49 }
50
51
52 func (u uint128) subOne() uint128 {
53 lo, borrow := bits.Sub64(u.lo, 1, 0)
54 return uint128{u.hi - borrow, lo}
55 }
56
57
58 func (u uint128) addOne() uint128 {
59 lo, carry := bits.Add64(u.lo, 1, 0)
60 return uint128{u.hi + carry, lo}
61 }
62
63 func u64CommonPrefixLen(a, b uint64) uint8 {
64 return uint8(bits.LeadingZeros64(a ^ b))
65 }
66
67 func (u uint128) commonPrefixLen(v uint128) (n uint8) {
68 if n = u64CommonPrefixLen(u.hi, v.hi); n == 64 {
69 n += u64CommonPrefixLen(u.lo, v.lo)
70 }
71 return
72 }
73
74
75
76
77
78 func (u *uint128) halves() [2]*uint64 {
79 return [2]*uint64{&u.hi, &u.lo}
80 }
81
82
83
84 func (u uint128) bitsSetFrom(bit uint8) uint128 {
85 return u.or(mask6(int(bit)).not())
86 }
87
88
89
90 func (u uint128) bitsClearedFrom(bit uint8) uint128 {
91 return u.and(mask6(int(bit)))
92 }
93
View as plain text