-
Notifications
You must be signed in to change notification settings - Fork 2
/
i128.go
214 lines (179 loc) · 4.83 KB
/
i128.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package goscale
import (
"bytes"
"encoding/binary"
"math/big"
"math/bits"
)
// little endian byte order
// [0] least significant bits
// [1] most significant bits
type I128 [2]U64
func NewI128[N Integer](n N) I128 {
return anyIntegerTo128Bits[I128](n)
}
func NewI128FromString(n string) (I128, error) {
return stringTo128Bits[I128](n)
}
func (n I128) Encode(buffer *bytes.Buffer) error {
err := n[0].Encode(buffer)
if err != nil {
return err
}
err = n[1].Encode(buffer)
if err != nil {
return err
}
return nil
}
func (n I128) Bytes() []byte {
return append(n[0].Bytes(), n[1].Bytes()...)
}
func DecodeI128(buffer *bytes.Buffer) (I128, error) {
low, err := DecodeU64(buffer)
if err != nil {
return I128{}, err
}
high, err := DecodeU64(buffer)
if err != nil {
return I128{}, err
}
return I128{
low,
high,
}, nil
}
func (n I128) ToBigInt() *big.Int {
isNegative := n.isNegative()
if isNegative {
n = negateI128(n)
}
bytes := make([]byte, 16)
binary.BigEndian.PutUint64(bytes[:8], uint64(n[1]))
binary.BigEndian.PutUint64(bytes[8:], uint64(n[0]))
result := big.NewInt(0).SetBytes(bytes)
if isNegative {
result.Neg(result)
}
return result
}
func (n I128) Add(other I128) I128 {
sumLow, carry := bits.Add64(uint64(n[0]), uint64(other[0]), 0)
sumHigh, _ := bits.Add64(uint64(n[1]), uint64(other[1]), carry)
return I128{U64(sumLow), U64(sumHigh)}
}
func (n I128) Sub(other I128) I128 {
diffLow, borrow := bits.Sub64(uint64(n[0]), uint64(other[0]), 0)
diffHigh, _ := bits.Sub64(uint64(n[1]), uint64(other[1]), borrow)
return I128{U64(diffLow), U64(diffHigh)}
}
func (n I128) Mul(other I128) I128 {
negA := n[1]>>(64-1) == 1
negB := other[1]>>(64-1) == 1
absA := n
if negA {
absA = negateI128(n)
}
absB := other
if negB {
absB = negateI128(other)
}
high, low := bits.Mul64(uint64(absA[0]), uint64(absB[0]))
high += uint64(absA[1])*uint64(absB[0]) + uint64(absA[0])*uint64(absB[1])
result := I128{U64(low), U64(high)}
// if one of the operands is negative the result is also negative
if negA != negB {
return negateI128(result)
}
return result
}
func (n I128) Div(other I128) I128 {
return bigIntToI128(
new(big.Int).Div(n.ToBigInt(), other.ToBigInt()),
)
}
func (n I128) Mod(other I128) I128 {
return bigIntToI128(
new(big.Int).Mod(n.ToBigInt(), other.ToBigInt()),
)
}
func (n I128) Eq(other I128) bool {
return n.ToBigInt().Cmp(other.ToBigInt()) == 0
}
func (n I128) Ne(other I128) bool {
return !n.Eq(other)
}
func (n I128) Lt(other I128) bool {
return n.ToBigInt().Cmp(other.ToBigInt()) < 0
}
func (n I128) Lte(other I128) bool {
return n.ToBigInt().Cmp(other.ToBigInt()) <= 0
}
func (n I128) Gt(other I128) bool {
return n.ToBigInt().Cmp(other.ToBigInt()) > 0
}
func (n I128) Gte(other I128) bool {
return n.ToBigInt().Cmp(other.ToBigInt()) >= 0
}
func bigIntToI128(n *big.Int) I128 {
bytes := make([]byte, 16)
n.FillBytes(bytes)
reverseSlice(bytes)
var result = I128{
U64(binary.LittleEndian.Uint64(bytes[:8])),
U64(binary.LittleEndian.Uint64(bytes[8:])),
}
if n.Sign() < 0 {
result = negateI128(result)
}
return result
}
func (n I128) isNegative() bool {
return n[1]&U64(1<<63) != 0
}
func negateI128(n I128) I128 {
// two's complement representation
negLow, carry := bits.Add64(^uint64(n[0]), 1, 0)
negHigh, _ := bits.Add64(^uint64(n[1]), 0, carry)
return I128{U64(negLow), U64(negHigh)}
}
//func (n I128) SaturatingAdd(b Numeric) Numeric {
// sumLow, carry := bits.Add64(uint64(a[0]), uint64(b.(I128)[0]), 0)
// sumHigh, _ := bits.Add64(uint64(a[1]), uint64(b.(I128)[1]), carry)
// // check for overflow
// if a[1]&(1<<63) == 0 && b.(I128)[1]&(1<<63) == 0 && sumHigh&(1<<63) != 0 {
// return MaxI128()
// }
// // check for underflow
// if a[1]&(1<<63) != 0 && b.(I128)[1]&(1<<63) != 0 && sumHigh&(1<<63) == 0 {
// return MinI128()
// }
// return I128{U64(sumLow), U64(sumHigh)}
//}
//
//func (n I128) SaturatingSub(b Numeric) Numeric {
// diffLow, borrow := bits.Sub64(uint64(a[0]), uint64(b.(I128)[0]), 0)
// diffHigh, _ := bits.Sub64(uint64(a[1]), uint64(b.(I128)[1]), borrow)
// // check for overflow
// if a[1]&(1<<63) == 0 && b.(I128)[1]&(1<<63) != 0 && diffHigh&(1<<63) != 0 {
// return MaxI128()
// }
// // check for underflow
// if a[1]&(1<<63) != 0 && b.(I128)[1]&(1<<63) == 0 && diffHigh&(1<<63) == 0 {
// return MinI128()
// }
// return I128{U64(diffLow), U64(diffHigh)}
//}
//
//func (n I128) SaturatingMul(b Numeric) Numeric {
// result := new(big.Int).Mul(a.ToBigInt(), b.(I128).ToBigInt())
// // define the maximum and minimum representable I128 values
// maxI128 := new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 127), big.NewInt(1))
// minI128 := new(big.Int).Neg(new(big.Int).Lsh(big.NewInt(1), 127))
// if result.Cmp(maxI128) > 0 {
// return bigIntToI128(maxI128)
// } else if result.Cmp(minI128) < 0 {
// return bigIntToI128(minI128)
// }
// return bigIntToI128(result)
//}