File: uint128.go

package info (click to toggle)
docker.io 26.1.5%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,576 kB
  • sloc: sh: 5,748; makefile: 912; ansic: 664; asm: 228; python: 162
file content (62 lines) | stat: -rw-r--r-- 1,129 bytes parent folder | download
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package ipbits

import (
	"encoding/binary"
	"math/bits"
)

type uint128 struct{ hi, lo uint64 }

func uint128From16(b [16]byte) uint128 {
	return uint128{
		hi: binary.BigEndian.Uint64(b[:8]),
		lo: binary.BigEndian.Uint64(b[8:]),
	}
}

func uint128From(x uint64) uint128 {
	return uint128{lo: x}
}

func (x uint128) add(y uint128) uint128 {
	lo, carry := bits.Add64(x.lo, y.lo, 0)
	hi, _ := bits.Add64(x.hi, y.hi, carry)
	return uint128{hi: hi, lo: lo}
}

func (x uint128) lsh(n uint) uint128 {
	if n > 64 {
		return uint128{hi: x.lo << (n - 64)}
	}
	return uint128{
		hi: x.hi<<n | x.lo>>(64-n),
		lo: x.lo << n,
	}
}

func (x uint128) rsh(n uint) uint128 {
	if n > 64 {
		return uint128{lo: x.hi >> (n - 64)}
	}
	return uint128{
		hi: x.hi >> n,
		lo: x.lo>>n | x.hi<<(64-n),
	}
}

func (x uint128) and(y uint128) uint128 {
	return uint128{hi: x.hi & y.hi, lo: x.lo & y.lo}
}

func (x uint128) not() uint128 {
	return uint128{hi: ^x.hi, lo: ^x.lo}
}

func (x uint128) fill16(a *[16]byte) {
	binary.BigEndian.PutUint64(a[:8], x.hi)
	binary.BigEndian.PutUint64(a[8:], x.lo)
}

func (x uint128) uint64() uint64 {
	return x.lo
}