Debian Codegraph @

replaydetector/fixedbig.go

raw symbols SCIP
1 package replaydetector
2
3 import (
4 "fmt"
5 )
6
7 // fixedBigInt is the fix-sized multi-word integer.
8 type fixedBigInt struct {
9 bits []uint64
10 n uint
11 msbMask uint64
12 }
13
14 // newFixedBigInt creates a new fix-sized multi-word int.
15 func newFixedBigInt(n uint) *fixedBigInt {
16 chunkSize := (n + 63) / 64
17 if chunkSize == 0 {
18 chunkSize = 1
19 }
20 return &fixedBigInt{
21 bits: make([]uint64, chunkSize),
22 n: n,
23 msbMask: (1 << (64 - n%64)) - 1,
24 }
25 }
26
27 // Lsh is the left shift operation.
28 func (s *fixedBigInt) Lsh(n uint) {
29 if n == 0 {
30 return
31 }
32 nChunk := int(n / 64)
33 nN := n % 64
34
35 for i := len(s.bits) - 1; i >= 0; i-- {
36 var carry uint64
37 if i-nChunk >= 0 {
38 carry = s.bits[i-nChunk] << nN
39 if i-nChunk-1 >= 0 {
40 carry |= s.bits[i-nChunk-1] >> (64 - nN)
41 }
42 }
43 s.bits[i] = (s.bits[i] << n) | carry
44 }
45 s.bits[len(s.bits)-1] &= s.msbMask
46 }
47
48 // Bit returns i-th bit of the fixedBigInt.
49 func (s *fixedBigInt) Bit(i uint) uint {
50 if i >= s.n {
51 return 0
52 }
53 chunk := i / 64
54 pos := i % 64
55 if s.bits[chunk]&(1<<pos) != 0 {
56 return 1
57 }
58 return 0
59 }
60
61 // SetBit sets i-th bit to 1.
62 func (s *fixedBigInt) SetBit(i uint) {
63 if i >= s.n {
64 return
65 }
66 chunk := i / 64
67 pos := i % 64
68 s.bits[chunk] |= 1 << pos
69 }
70
71 // String returns string representation of fixedBigInt.
72 func (s *fixedBigInt) String() string {
73 var out string
74 for i := len(s.bits) - 1; i >= 0; i-- {
75 out += fmt.Sprintf("%016X", s.bits[i])
76 }
77 return out
78 }