/
conversion.go
77 lines (59 loc) · 2.05 KB
/
conversion.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
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package managedbigint
// SetBytes interprets buf as the bytes of a big-endian unsigned
// integer, sets dest to that value, and returns dest.
func (c *BigIntContainer) SetBytes(dest BigIntHandle, buf []byte) BigIntHandle {
c.loadBigInt(dest, c.destination)
destDataBefore := c.destination.Bits()
c.destination = c.destination.SetBytes(buf)
destDataAfter := c.destination.Bits()
if bigIntDataMoved(destDataBefore, destDataAfter) {
return c.Insert(c.destination)
}
dest.negative = false
return dest
}
// GetBytes returns the absolute value of x as a big-endian byte slice.
func (c *BigIntContainer) GetBytes(x BigIntHandle) []byte {
c.loadBigInt(x, c.register1)
return c.register1.Bytes()
}
// BitLen returns the length of the absolute value of x in bits.
// The bit length of 0 is 0.
func (c *BigIntContainer) BitLen(x BigIntHandle) int {
c.loadBigInt(x, c.register1)
return c.register1.BitLen()
}
// ByteLen returns the minimum number of bytes required to represent the absolute value of x.
// The byte length of 0 is 0.
func (c *BigIntContainer) ByteLen(x BigIntHandle) int {
c.loadBigInt(x, c.register1)
bitLen := c.register1.BitLen()
if bitLen == 0 {
return 0
}
return (bitLen-1)/8 + 1
}
// IsInt64 reports whether x can be represented as an int64.
func (c *BigIntContainer) IsInt64(x BigIntHandle) bool {
c.loadBigInt(x, c.register1)
return c.register1.IsInt64()
}
// ToInt64 returns the int64 representation of x.
// If x cannot be represented in an int64, the result is undefined.
func (c *BigIntContainer) ToInt64(x BigIntHandle) int64 {
c.loadBigInt(x, c.register1)
return c.register1.Int64()
}
// SetInt64 sets value to dest.
func (c *BigIntContainer) SetInt64(dest BigIntHandle, value int64) BigIntHandle {
c.loadBigInt(dest, c.destination)
destDataBefore := c.destination.Bits()
c.destination = c.destination.SetInt64(value)
destDataAfter := c.destination.Bits()
if bigIntDataMoved(destDataBefore, destDataAfter) {
return c.Insert(c.destination)
}
// maybe dest changed sign
dest.negative = c.destination.Sign() < 0
return dest
}