passing compile-time bitwise tests (but not runtime :?)

This commit is contained in:
Mamy André-Ratsimbazafy 2020-06-13 16:54:54 +02:00 committed by jangko
parent 777a84e9f5
commit 195480d58a
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
3 changed files with 3 additions and 7 deletions

View File

@ -40,7 +40,7 @@ func shrLarge*(r: var Limbs, a: Limbs, w, shift: SomeInteger) =
when cpuEndian == littleEndian: when cpuEndian == littleEndian:
for i in w ..< a.len-1: for i in w ..< a.len-1:
r[i-w] = (a[i] shr shift) or (a[i+1] shl (WordBitWidth - shift)) r[i-w] = (a[i] shr shift) or (a[i+1] shl (WordBitWidth - shift))
r[^w] = a[^1] shr shift r[^(1+w)] = a[^1] shr shift
else: else:
for i in countdown(a.len-1, 1+w): for i in countdown(a.len-1, 1+w):
r[i-w] = (a[i] shr shift) or (a[i-1] shl (WordBitWidth - k)) r[i-w] = (a[i] shr shift) or (a[i-1] shl (WordBitWidth - k))

View File

@ -166,6 +166,7 @@ func `shr`*(a: Stuint, k: SomeInteger): Stuint {.inline.} =
if k < WordBitWidth: if k < WordBitWidth:
result.limbs.shrSmall(a.limbs, k) result.limbs.shrSmall(a.limbs, k)
return return
# w = k div WordBitWidth, shift = k mod WordBitWidth # w = k div WordBitWidth, shift = k mod WordBitWidth
let w = k shr static(log2trunc(uint32(WordBitWidth))) let w = k shr static(log2trunc(uint32(WordBitWidth)))
let shift = k and (WordBitWidth - 1) let shift = k and (WordBitWidth - 1)
@ -181,6 +182,7 @@ func `shl`*(a: Stuint, k: SomeInteger): Stuint {.inline.} =
result.limbs.shlSmall(a.limbs, k) result.limbs.shlSmall(a.limbs, k)
result.clearExtraBits() result.clearExtraBits()
return return
# w = k div WordBitWidth, shift = k mod WordBitWidth # w = k div WordBitWidth, shift = k mod WordBitWidth
let w = k shr static(log2trunc(uint32(WordBitWidth))) let w = k shr static(log2trunc(uint32(WordBitWidth)))
let shift = k and (WordBitWidth - 1) let shift = k and (WordBitWidth - 1)

View File

@ -337,12 +337,6 @@ suite "Testing unsigned int bitwise operations":
check: cast[uint16](b) == z # Sanity check check: cast[uint16](b) == z # Sanity check
check: cast[uint16](b shl 8) == z shl 8 check: cast[uint16](b shl 8) == z shl 8
block: # Testing shl for nested UintImpl
let p2_64 = UintImpl[uint64](hi:1, lo:0)
let p = 1.stuint(128) shl 64
check: p == cast[StUint[128]](p2_64)
test "Shift right - by less than half the size of the integer": test "Shift right - by less than half the size of the integer":
check: cast[uint16](b) == z # Sanity check check: cast[uint16](b) == z # Sanity check
check: cast[uint16](b shr 2) == z shr 2 check: cast[uint16](b shr 2) == z shr 2