-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathintops.nim
370 lines (290 loc) · 8.61 KB
/
intops.nim
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# Stint
# Copyright 2018-2023 Status Research & Development GmbH
# Licensed under either of
#
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import
./private/datatypes,
./private/uint_bitwise,
./private/uint_shift,
./private/uint_addsub,
./private/uint_div,
./uintops
export StInt
const
signMask = 1.Word shl (WordBitWidth - 1)
clearSignMask = not signMask
# Signedness
# --------------------------------------------------------
{.push raises: [], inline, noinit, gcsafe.}
func sign*(a: StInt): int =
## get the sign of `a`
## either -1, 0, or 1
if a.impl.isZero: return 0
if a.limbs[^1] < signMask: 1
else: -1
func isNegative*(a: StInt): bool =
a.limbs[^1] >= signMask
func isPositive*(a: StInt): bool =
a.limbs[^1] < signMask
func clearMSB(a: var StInt) =
a.limbs[^1] = a.limbs[^1] and clearSignMask
func setMSB(a: var StInt) =
a.limbs[^1] = a.limbs[^1] or signMask
func negate*(a: var StInt) =
## two complement negation
a.impl.bitnot(a.impl)
a.impl.inc
func neg*(a: StInt): StInt =
## two complement negation
result.impl.bitnot(a.impl)
result.impl.inc
func abs*(a: StInt): StInt =
## Returns the positive value of Stint
if a.isNegative:
a.neg
else:
a
func `-`*(a: StInt): StInt =
## two complement negation
a.neg
{.pop.}
# Initialization
# --------------------------------------------------------
{.push raises: [], inline, noinit, gcsafe.}
func setZero*(a: var StInt) =
## Set ``a`` to 0
a.impl.setZero
func setOne*(a: var StInt) =
## Set ``a`` to 1
a.impl.setOne
func zero*[bits: static[int]](T: typedesc[StInt[bits]]): T =
## Returns the zero of the input type
result.setZero
func one*[bits: static[int]](T: typedesc[StInt[bits]]): T =
## Returns the one of the input type
result.setOne
func high*[bits](_: typedesc[StInt[bits]]): StInt[bits] =
## Returns the highest value of Stint
# The highest signed int has representation
# 0b0111_1111_1111_1111 ....
# so we only have to unset the most significant bit.
for i in 0 ..< result.limbs.len:
result[i] = high(Word)
result.clearMSB
func low*[bits](_: typedesc[StInt[bits]]): StInt[bits] =
## Returns the lowest value of Stint
# The lowest signed int has representation
# 0b1000_0000_0000_0000 ....
# so we only have to set the most significant bit.
result.setZero
result.setMSB
{.pop.}
# Comparisons
# --------------------------------------------------------
{.push raises: [], inline, noinit, gcsafe.}
func isZero*(a: StInt): bool =
a.impl.isZero
func isOne*(a: StInt): bool =
a.impl.isOne
func `==`*(a, b: StInt): bool =
## Signed int `equal` comparison
a.impl == b.impl
func `<`*(a, b: StInt): bool =
## Signed int `less than` comparison
let
aNeg = a.isNegative
bNeg = b.isNegative
if aNeg xor bNeg:
return aNeg
a.impl < b.impl
func `<=`*(a, b: StInt): bool =
## Signed int `less or equal` comparison
not(b < a)
func isOdd*(a: StInt): bool =
## Returns true if input is odd
## false otherwise
bool(a[0] and 1)
func isEven*(a: StInt): bool =
## Returns true if input is zero
## false otherwise
not a.isOdd()
{.pop.}
# Bitwise operations
# --------------------------------------------------------
{.push raises: [], inline, noinit, gcsafe.}
func `not`*(a: StInt): StInt =
## Bitwise complement of signed integer a
## i.e. flips all bits of the input
result.impl.bitnot(a.impl)
func `or`*(a, b: StInt): StInt =
## `Bitwise or` of numbers a and b
result.impl.bitor(a.impl, b.impl)
func `and`*(a, b: StInt): StInt =
## `Bitwise and` of numbers a and b
result.impl.bitand(a.impl, b.impl)
func `xor`*(a, b: StInt): StInt =
## `Bitwise xor` of numbers x and y
result.impl.bitxor(a.impl, b.impl)
{.pop.} # End noinit
{.push raises: [], inline, gcsafe.}
func `shr`*(a: StInt, k: SomeInteger): StInt =
## Shift right by k bits, arithmetically
## value < 0 ? ~(~value >> amount) : value >> amount
if a.isNegative:
var tmp: type a
result.impl.bitnot(a.impl)
tmp.impl.shiftRight(result.impl, k)
result.impl.bitnot(tmp.impl)
else:
result.impl.shiftRight(a.impl, k)
func `shl`*(a: StInt, k: SomeInteger): StInt =
## Shift left by k bits
result.impl.shiftLeft(a.impl, k)
func setBit*(a: var StInt, k: Natural) =
## set bit at position `k`
## k = 0..a.bits-1
a.impl.setBit(k)
func clearBit*(a: var StInt, k: Natural) =
## set bit at position `k`
## k = 0..a.bits-1
a.impl.clearBit(k)
func getBit*(a: StInt, k: Natural): bool =
## set bit at position `k`
## k = 0..a.bits-1
a.impl.getBit(k)
{.pop.}
# Addsub
# --------------------------------------------------------
{.push raises: [], inline, noinit, gcsafe.}
func `+`*(a, b: StInt): StInt =
## Addition for multi-precision signed int
result.impl.sum(a.impl, b.impl)
func `+=`*(a: var StInt, b: StInt) =
## In-place addition for multi-precision signed int
a.impl.sum(a.impl, b.impl)
func `-`*(a, b: StInt): StInt =
## Substraction for multi-precision signed int
result.impl.diff(a.impl, b.impl)
func `-=`*(a: var StInt, b: StInt) =
## In-place substraction for multi-precision signed int
a.impl.diff(a.impl, b.impl)
func inc*(a: var StInt, w: Word = 1) =
a.impl.inc(w)
func `+`*(a: StInt, b: SomeUnsignedInt): StInt =
## Addition for multi-precision signed int
## with an unsigned integer
result.impl.sum(a.impl, Word(b))
func `+=`*(a: var StInt, b: SomeUnsignedInt) =
## In-place addition for multi-precision signed int
## with an unsigned integer
a.impl.inc(Word(b))
{.pop.}
# Exponentiation
# --------------------------------------------------------
{.push raises: [], noinit, gcsafe.}
func isOdd(x: Natural): bool =
bool(x and 1)
func pow*(a: StInt, e: Natural): StInt =
## Compute ``a`` to the power of ``e``,
## ``e`` must be non-negative
if a.isNegative:
let base = a.neg
result.impl = base.impl.pow(e)
if e.isOdd:
result.negate
else:
result.impl = a.impl.pow(e)
func pow*[aBits, eBits](a: StInt[aBits], e: StInt[eBits]): StInt[aBits] =
## Compute ``x`` to the power of ``y``,
## ``x`` must be non-negative
doAssert e.isNegative.not, "exponent must be non-negative"
if a.isNegative:
let base = a.neg
result.impl = base.impl.pow(e.impl)
if e.isOdd:
result.negate
else:
result.impl = a.impl.pow(e.impl)
{.pop.}
# Division & Modulo
# --------------------------------------------------------
{.push raises: [], inline, noinit, gcsafe.}
func `div`*(n, d: StInt): StInt =
## Division operation for multi-precision signed uint
var tmp{.noinit.}: StInt
if n.isPositive:
if d.isPositive:
# pos / pos
result.impl = n.impl div d.impl
return
else:
# pos / neg
tmp = d.neg
result.impl = n.impl div tmp.impl
result.negate
return
let nneg = n.neg
if d.isNegative:
# neg / neg
tmp = d.neg
result.impl = nneg.impl div tmp.impl
return
# neg / pos
result.impl = nneg.impl div d.impl
result.negate
func `mod`*(x, y: StInt): StInt =
## Remainder operation for multi-precision signed uint
## The behavior is similar to Nim's `mod` operator
## The sign of the remainder will follow the sign of left operand
let
xIn = x.abs
yIn = y.abs
result.impl = xIn.impl mod yIn.impl
if x.isNegative:
result.negate
func divmodI(x, y: StInt): tuple[quot, rem: StInt] =
## Division and remainder operations for multi-precision uint
## with StInt operands
divRem(result.quot.limbs, result.rem.limbs, x.limbs, y.limbs)
func divmod*(n, d: StInt): tuple[quot, rem: StInt] =
## Division and remainder operations for multi-precision signed uint
## The sign of the remainder will follow the sign of left operand
var tmp{.noinit.}: StInt
if n.isPositive:
if d.isPositive:
# pos / pos
return divmodI(n, d)
else:
# pos / neg
tmp = d.neg
result = divmodI(n, tmp)
result.quot.negate
return
let nneg = n.neg
if d.isNegative:
# neg / neg
tmp = d.neg
result = divmodI(nneg, tmp)
result.rem.negate
return
# neg / pos
result = divmodI(nneg, d)
result.quot.negate
result.rem.negate
{.pop.}
# Multiplication
# --------------------------------------------------------
{.push raises: [], inline, noinit, gcsafe.}
func `*`*(a, b: StInt): StInt =
## Signed integer multiplication
let
av = a.abs
bv = b.abs
result.impl = av.impl * bv.impl
if a.isNegative xor b.isNegative:
result.negate
{.pop.}