Skip to content

Commit bb85bc7

Browse files
authored
remove a temporary variable in int128.nim (#16935)
1 parent 2b2836f commit bb85bc7

File tree

1 file changed

+34
-35
lines changed

1 file changed

+34
-35
lines changed

compiler/int128.nim

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
## This module is for compiler internal use only. For reliable error
22
## 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
44
## type is for that purpose.
55

6-
from math import trunc
6+
from std/math import trunc
77

88
type
99
Int128* = object
10-
udata: array[4,uint32]
10+
udata: array[4, uint32]
1111

1212
template sdata(arg: Int128, idx: int): int32 =
1313
# udata and sdata was supposed to be in a union, but unions are
@@ -17,12 +17,12 @@ template sdata(arg: Int128, idx: int): int32 =
1717
# encoding least significant int first (like LittleEndian)
1818

1919
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,0x80000000'u32])
24-
Max = Int128(udata: [high(uint32),high(uint32),high(uint32),uint32(high(int32))])
25-
NegOne* = Int128(udata: [0xffffffff'u32,0xffffffff'u32,0xffffffff'u32,0xffffffff'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, 0x80000000'u32])
24+
Max = Int128(udata: [high(uint32), high(uint32), high(uint32), uint32(high(int32))])
25+
NegOne* = Int128(udata: [0xffffffff'u32, 0xffffffff'u32, 0xffffffff'u32, 0xffffffff'u32])
2626

2727
template low*(t: typedesc[Int128]): Int128 = Min
2828
template high*(t: typedesc[Int128]): Int128 = Max
@@ -57,10 +57,10 @@ template isNegative(arg: Int128): bool =
5757
template isNegative(arg: int32): bool =
5858
arg < 0
5959

60-
proc bitconcat(a,b: uint32): uint64 =
60+
proc bitconcat(a, b: uint32): uint64 =
6161
(uint64(a) shl 32) or uint64(b)
6262

63-
proc bitsplit(a: uint64): (uint32,uint32) =
63+
proc bitsplit(a: uint64): (uint32, uint32) =
6464
(cast[uint32](a shr 32), cast[uint32](a))
6565

6666
proc toInt64*(arg: Int128): int64 =
@@ -176,7 +176,6 @@ proc toHex*(arg: Int128): string =
176176
result.addToHex(arg)
177177

178178
proc inc*(a: var Int128, y: uint32 = 1) =
179-
let input = a
180179
a.udata[0] += y
181180
if unlikely(a.udata[0] < y):
182181
a.udata[1].inc
@@ -186,7 +185,7 @@ proc inc*(a: var Int128, y: uint32 = 1) =
186185
a.udata[3].inc
187186
doAssert(a.sdata(3) != low(int32), "overflow")
188187

189-
proc cmp*(a,b: Int128): int =
188+
proc cmp*(a, b: Int128): int =
190189
let tmp1 = cmp(a.sdata(3), b.sdata(3))
191190
if tmp1 != 0: return tmp1
192191
let tmp2 = cmp(a.udata[2], b.udata[2])
@@ -196,13 +195,13 @@ proc cmp*(a,b: Int128): int =
196195
let tmp4 = cmp(a.udata[0], b.udata[0])
197196
return tmp4
198197

199-
proc `<`*(a,b: Int128): bool =
200-
cmp(a,b) < 0
198+
proc `<`*(a, b: Int128): bool =
199+
cmp(a, b) < 0
201200

202-
proc `<=`*(a,b: Int128): bool =
203-
cmp(a,b) <= 0
201+
proc `<=`*(a, b: Int128): bool =
202+
cmp(a, b) <= 0
204203

205-
proc `==`*(a,b: Int128): bool =
204+
proc `==`*(a, b: Int128): bool =
206205
if a.udata[0] != b.udata[0]: return false
207206
if a.udata[1] != b.udata[1]: return false
208207
if a.udata[2] != b.udata[2]: return false
@@ -221,19 +220,19 @@ proc bitnot*(a: Int128): Int128 =
221220
result.udata[2] = not a.udata[2]
222221
result.udata[3] = not a.udata[3]
223222

224-
proc bitand*(a,b: Int128): Int128 =
223+
proc bitand*(a, b: Int128): Int128 =
225224
result.udata[0] = a.udata[0] and b.udata[0]
226225
result.udata[1] = a.udata[1] and b.udata[1]
227226
result.udata[2] = a.udata[2] and b.udata[2]
228227
result.udata[3] = a.udata[3] and b.udata[3]
229228

230-
proc bitor*(a,b: Int128): Int128 =
229+
proc bitor*(a, b: Int128): Int128 =
231230
result.udata[0] = a.udata[0] or b.udata[0]
232231
result.udata[1] = a.udata[1] or b.udata[1]
233232
result.udata[2] = a.udata[2] or b.udata[2]
234233
result.udata[3] = a.udata[3] or b.udata[3]
235234

236-
proc bitxor*(a,b: Int128): Int128 =
235+
proc bitxor*(a, b: Int128): Int128 =
237236
result.udata[0] = a.udata[0] xor b.udata[0]
238237
result.udata[1] = a.udata[1] xor b.udata[1]
239238
result.udata[2] = a.udata[2] xor b.udata[2]
@@ -288,7 +287,7 @@ proc `shl`*(a: Int128, b: int): Int128 =
288287
result.udata[2] = 0
289288
result.udata[3] = a.udata[0] shl (b and 31)
290289

291-
proc `+`*(a,b: Int128): Int128 =
290+
proc `+`*(a, b: Int128): Int128 =
292291
let tmp0 = uint64(a.udata[0]) + uint64(b.udata[0])
293292
result.udata[0] = cast[uint32](tmp0)
294293
let tmp1 = uint64(a.udata[1]) + uint64(b.udata[1]) + (tmp0 shr 32)
@@ -305,7 +304,7 @@ proc `-`*(a: Int128): Int128 =
305304
result = bitnot(a)
306305
result.inc
307306

308-
proc `-`*(a,b: Int128): Int128 =
307+
proc `-`*(a, b: Int128): Int128 =
309308
a + (-b)
310309

311310
proc `-=`*(a: var Int128, b: Int128) =
@@ -342,7 +341,7 @@ proc `*`*(a: Int128, b: int32): Int128 =
342341
proc `*=`*(a: var Int128, b: int32): Int128 =
343342
result = result * b
344343

345-
proc makeInt128(high,low: uint64): Int128 =
344+
proc makeInt128(high, low: uint64): Int128 =
346345
result.udata[0] = cast[uint32](low)
347346
result.udata[1] = cast[uint32](low shr 32)
348347
result.udata[2] = cast[uint32](high)
@@ -354,7 +353,7 @@ proc high64(a: Int128): uint64 =
354353
proc low64(a: Int128): uint64 =
355354
bitconcat(a.udata[1], a.udata[0])
356355

357-
proc `*`*(lhs,rhs: Int128): Int128 =
356+
proc `*`*(lhs, rhs: Int128): Int128 =
358357
let
359358
a = cast[uint64](lhs.udata[0])
360359
b = cast[uint64](lhs.udata[1])
@@ -379,7 +378,7 @@ proc `*`*(lhs,rhs: Int128): Int128 =
379378
proc `*=`*(a: var Int128, b: Int128) =
380379
a = a * b
381380

382-
import bitops
381+
import std/bitops
383382

384383
proc fastLog2*(a: Int128): int =
385384
if a.udata[3] != 0:
@@ -389,7 +388,7 @@ proc fastLog2*(a: Int128): int =
389388
if a.udata[1] != 0:
390389
return 32 + fastLog2(a.udata[1])
391390
if a.udata[0] != 0:
392-
return fastLog2(a.udata[0])
391+
return fastLog2(a.udata[0])
393392

394393
proc divMod*(dividend, divisor: Int128): tuple[quotient, remainder: Int128] =
395394
assert(divisor != Zero)
@@ -441,12 +440,12 @@ proc divMod*(dividend, divisor: Int128): tuple[quotient, remainder: Int128] =
441440
else:
442441
result.remainder = dividend
443442

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)
446445
return a
447446

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)
450449
return b
451450

452451
proc addInt128*(result: var string; value: Int128) =
@@ -477,7 +476,7 @@ proc `$`*(a: Int128): string =
477476

478477
proc parseDecimalInt128*(arg: string, pos: int = 0): Int128 =
479478
assert(pos < arg.len)
480-
assert(arg[pos] in {'-','0'..'9'})
479+
assert(arg[pos] in {'-', '0'..'9'})
481480

482481
var isNegative = false
483482
var pos = pos
@@ -497,13 +496,13 @@ proc parseDecimalInt128*(arg: string, pos: int = 0): Int128 =
497496
# fluff
498497

499498
proc `<`*(a: Int128, b: BiggestInt): bool =
500-
cmp(a,toInt128(b)) < 0
499+
cmp(a, toInt128(b)) < 0
501500

502501
proc `<`*(a: BiggestInt, b: Int128): bool =
503502
cmp(toInt128(a), b) < 0
504503

505504
proc `<=`*(a: Int128, b: BiggestInt): bool =
506-
cmp(a,toInt128(b)) <= 0
505+
cmp(a, toInt128(b)) <= 0
507506

508507
proc `<=`*(a: BiggestInt, b: Int128): bool =
509508
cmp(toInt128(a), b) <= 0
@@ -539,7 +538,7 @@ proc toFloat64*(arg: Int128): float64 =
539538

540539
proc ldexp(x: float64, exp: cint): float64 {.importc: "ldexp", header: "<math.h>".}
541540

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)
543542

544543
proc toInt128*(arg: float64): Int128 =
545544
let isNegative = arg < 0

0 commit comments

Comments
 (0)