1
1
# # This module is for compiler internal use only. For reliable error
2
2
# # messages and range checks, the compiler needs a data type that can
3
- # # hold all from `` low(BiggestInt)`` to `` high(BiggestUInt)` `, This
3
+ # # hold all from `low(BiggestInt)` to `high(BiggestUInt)`, This
4
4
# # type is for that purpose.
5
5
6
- from math import trunc
6
+ from std / math import trunc
7
7
8
8
type
9
9
Int128 * = object
10
- udata: array [4 ,uint32 ]
10
+ udata: array [4 , uint32 ]
11
11
12
12
template sdata (arg: Int128 , idx: int ): int32 =
13
13
# udata and sdata was supposed to be in a union, but unions are
@@ -17,12 +17,12 @@ template sdata(arg: Int128, idx: int): int32 =
17
17
# encoding least significant int first (like LittleEndian)
18
18
19
19
const
20
- Zero * = Int128 (udata: [0 'u32 ,0 , 0 , 0 ])
21
- One * = Int128 (udata: [1 'u32 ,0 , 0 , 0 ])
22
- Ten * = Int128 (udata: [10 'u32 ,0 , 0 , 0 ])
23
- Min = Int128 (udata: [0 'u32 ,0 , 0 , 0x 80000000 'u32 ])
24
- Max = Int128 (udata: [high (uint32 ),high (uint32 ),high (uint32 ),uint32 (high (int32 ))])
25
- NegOne * = Int128 (udata: [0x ffffffff 'u32 ,0x ffffffff 'u32 ,0x ffffffff 'u32 ,0x ffffffff 'u32 ])
20
+ Zero * = Int128 (udata: [0 'u32 , 0 , 0 , 0 ])
21
+ One * = Int128 (udata: [1 'u32 , 0 , 0 , 0 ])
22
+ Ten * = Int128 (udata: [10 'u32 , 0 , 0 , 0 ])
23
+ Min = Int128 (udata: [0 'u32 , 0 , 0 , 0x 80000000 'u32 ])
24
+ Max = Int128 (udata: [high (uint32 ), high (uint32 ), high (uint32 ), uint32 (high (int32 ))])
25
+ NegOne * = Int128 (udata: [0x ffffffff 'u32 , 0x ffffffff 'u32 , 0x ffffffff 'u32 , 0x ffffffff 'u32 ])
26
26
27
27
template low * (t: typedesc [Int128 ]): Int128 = Min
28
28
template high * (t: typedesc [Int128 ]): Int128 = Max
@@ -57,10 +57,10 @@ template isNegative(arg: Int128): bool =
57
57
template isNegative (arg: int32 ): bool =
58
58
arg < 0
59
59
60
- proc bitconcat (a,b: uint32 ): uint64 =
60
+ proc bitconcat (a, b: uint32 ): uint64 =
61
61
(uint64 (a) shl 32 ) or uint64 (b)
62
62
63
- proc bitsplit (a: uint64 ): (uint32 ,uint32 ) =
63
+ proc bitsplit (a: uint64 ): (uint32 , uint32 ) =
64
64
(cast [uint32 ](a shr 32 ), cast [uint32 ](a))
65
65
66
66
proc toInt64 * (arg: Int128 ): int64 =
@@ -176,7 +176,6 @@ proc toHex*(arg: Int128): string =
176
176
result .addToHex (arg)
177
177
178
178
proc inc * (a: var Int128 , y: uint32 = 1 ) =
179
- let input = a
180
179
a.udata[0 ] += y
181
180
if unlikely (a.udata[0 ] < y):
182
181
a.udata[1 ].inc
@@ -186,7 +185,7 @@ proc inc*(a: var Int128, y: uint32 = 1) =
186
185
a.udata[3 ].inc
187
186
doAssert (a.sdata (3 ) != low (int32 ), " overflow" )
188
187
189
- proc cmp * (a,b: Int128 ): int =
188
+ proc cmp * (a, b: Int128 ): int =
190
189
let tmp1 = cmp (a.sdata (3 ), b.sdata (3 ))
191
190
if tmp1 != 0 : return tmp1
192
191
let tmp2 = cmp (a.udata[2 ], b.udata[2 ])
@@ -196,13 +195,13 @@ proc cmp*(a,b: Int128): int =
196
195
let tmp4 = cmp (a.udata[0 ], b.udata[0 ])
197
196
return tmp4
198
197
199
- proc `<` * (a,b: Int128 ): bool =
200
- cmp (a,b) < 0
198
+ proc `<` * (a, b: Int128 ): bool =
199
+ cmp (a, b) < 0
201
200
202
- proc `<=` * (a,b: Int128 ): bool =
203
- cmp (a,b) <= 0
201
+ proc `<=` * (a, b: Int128 ): bool =
202
+ cmp (a, b) <= 0
204
203
205
- proc `==` * (a,b: Int128 ): bool =
204
+ proc `==` * (a, b: Int128 ): bool =
206
205
if a.udata[0 ] != b.udata[0 ]: return false
207
206
if a.udata[1 ] != b.udata[1 ]: return false
208
207
if a.udata[2 ] != b.udata[2 ]: return false
@@ -221,19 +220,19 @@ proc bitnot*(a: Int128): Int128 =
221
220
result .udata[2 ] = not a.udata[2 ]
222
221
result .udata[3 ] = not a.udata[3 ]
223
222
224
- proc bitand * (a,b: Int128 ): Int128 =
223
+ proc bitand * (a, b: Int128 ): Int128 =
225
224
result .udata[0 ] = a.udata[0 ] and b.udata[0 ]
226
225
result .udata[1 ] = a.udata[1 ] and b.udata[1 ]
227
226
result .udata[2 ] = a.udata[2 ] and b.udata[2 ]
228
227
result .udata[3 ] = a.udata[3 ] and b.udata[3 ]
229
228
230
- proc bitor * (a,b: Int128 ): Int128 =
229
+ proc bitor * (a, b: Int128 ): Int128 =
231
230
result .udata[0 ] = a.udata[0 ] or b.udata[0 ]
232
231
result .udata[1 ] = a.udata[1 ] or b.udata[1 ]
233
232
result .udata[2 ] = a.udata[2 ] or b.udata[2 ]
234
233
result .udata[3 ] = a.udata[3 ] or b.udata[3 ]
235
234
236
- proc bitxor * (a,b: Int128 ): Int128 =
235
+ proc bitxor * (a, b: Int128 ): Int128 =
237
236
result .udata[0 ] = a.udata[0 ] xor b.udata[0 ]
238
237
result .udata[1 ] = a.udata[1 ] xor b.udata[1 ]
239
238
result .udata[2 ] = a.udata[2 ] xor b.udata[2 ]
@@ -288,7 +287,7 @@ proc `shl`*(a: Int128, b: int): Int128 =
288
287
result .udata[2 ] = 0
289
288
result .udata[3 ] = a.udata[0 ] shl (b and 31 )
290
289
291
- proc `+` * (a,b: Int128 ): Int128 =
290
+ proc `+` * (a, b: Int128 ): Int128 =
292
291
let tmp0 = uint64 (a.udata[0 ]) + uint64 (b.udata[0 ])
293
292
result .udata [0 ] = cast [uint32 ](tmp0)
294
293
let tmp1 = uint64 (a.udata[1 ]) + uint64 (b.udata[1 ]) + (tmp0 shr 32 )
@@ -305,7 +304,7 @@ proc `-`*(a: Int128): Int128 =
305
304
result = bitnot (a)
306
305
result .inc
307
306
308
- proc `-` * (a,b: Int128 ): Int128 =
307
+ proc `-` * (a, b: Int128 ): Int128 =
309
308
a + (- b)
310
309
311
310
proc `-=` * (a: var Int128 , b: Int128 ) =
@@ -342,7 +341,7 @@ proc `*`*(a: Int128, b: int32): Int128 =
342
341
proc `*=` * (a: var Int128 , b: int32 ): Int128 =
343
342
result = result * b
344
343
345
- proc makeInt128 (high,low: uint64 ): Int128 =
344
+ proc makeInt128 (high, low: uint64 ): Int128 =
346
345
result .udata [0 ] = cast [uint32 ](low)
347
346
result .udata [1 ] = cast [uint32 ](low shr 32 )
348
347
result .udata [2 ] = cast [uint32 ](high)
@@ -354,7 +353,7 @@ proc high64(a: Int128): uint64 =
354
353
proc low64 (a: Int128 ): uint64 =
355
354
bitconcat (a.udata[1 ], a.udata[0 ])
356
355
357
- proc `*` * (lhs,rhs: Int128 ): Int128 =
356
+ proc `*` * (lhs, rhs: Int128 ): Int128 =
358
357
let
359
358
a = cast [uint64 ](lhs.udata[0 ])
360
359
b = cast [uint64 ](lhs.udata[1 ])
@@ -379,7 +378,7 @@ proc `*`*(lhs,rhs: Int128): Int128 =
379
378
proc `*=` * (a: var Int128 , b: Int128 ) =
380
379
a = a * b
381
380
382
- import bitops
381
+ import std / bitops
383
382
384
383
proc fastLog2 * (a: Int128 ): int =
385
384
if a.udata[3 ] != 0 :
@@ -389,7 +388,7 @@ proc fastLog2*(a: Int128): int =
389
388
if a.udata[1 ] != 0 :
390
389
return 32 + fastLog2 (a.udata[1 ])
391
390
if a.udata[0 ] != 0 :
392
- return fastLog2 (a.udata[0 ])
391
+ return fastLog2 (a.udata[0 ])
393
392
394
393
proc divMod * (dividend, divisor: Int128 ): tuple [quotient, remainder: Int128 ] =
395
394
assert (divisor != Zero )
@@ -441,12 +440,12 @@ proc divMod*(dividend, divisor: Int128): tuple[quotient, remainder: Int128] =
441
440
else :
442
441
result .remainder = dividend
443
442
444
- proc `div` * (a,b: Int128 ): Int128 =
445
- let (a,b) = divMod (a,b)
443
+ proc `div` * (a, b: Int128 ): Int128 =
444
+ let (a, b) = divMod (a, b)
446
445
return a
447
446
448
- proc `mod` * (a,b: Int128 ): Int128 =
449
- let (a,b) = divMod (a,b)
447
+ proc `mod` * (a, b: Int128 ): Int128 =
448
+ let (a, b) = divMod (a, b)
450
449
return b
451
450
452
451
proc addInt128 * (result: var string ; value: Int128 ) =
@@ -477,7 +476,7 @@ proc `$`*(a: Int128): string =
477
476
478
477
proc parseDecimalInt128 * (arg: string , pos: int = 0 ): Int128 =
479
478
assert (pos < arg.len)
480
- assert (arg[pos] in {'-' ,'0' .. '9' })
479
+ assert (arg[pos] in {'-' , '0' .. '9' })
481
480
482
481
var isNegative = false
483
482
var pos = pos
@@ -497,13 +496,13 @@ proc parseDecimalInt128*(arg: string, pos: int = 0): Int128 =
497
496
# fluff
498
497
499
498
proc `<` * (a: Int128 , b: BiggestInt ): bool =
500
- cmp (a,toInt128 (b)) < 0
499
+ cmp (a, toInt128 (b)) < 0
501
500
502
501
proc `<` * (a: BiggestInt , b: Int128 ): bool =
503
502
cmp (toInt128 (a), b) < 0
504
503
505
504
proc `<=` * (a: Int128 , b: BiggestInt ): bool =
506
- cmp (a,toInt128 (b)) <= 0
505
+ cmp (a, toInt128 (b)) <= 0
507
506
508
507
proc `<=` * (a: BiggestInt , b: Int128 ): bool =
509
508
cmp (toInt128 (a), b) <= 0
@@ -539,7 +538,7 @@ proc toFloat64*(arg: Int128): float64 =
539
538
540
539
proc ldexp (x: float64 , exp: cint ): float64 {.importc : " ldexp" , header : " <math.h>" .}
541
540
542
- template bitor (a,b, c: Int128 ): Int128 = bitor (bitor (a,b), c)
541
+ template bitor (a, b, c: Int128 ): Int128 = bitor (bitor (a, b), c)
543
542
544
543
proc toInt128 * (arg: float64 ): Int128 =
545
544
let isNegative = arg < 0
0 commit comments