less branching for shift ops

This commit is contained in:
mratsim 2018-02-16 14:30:07 +01:00
parent 35e80263a3
commit 6e27069298
1 changed files with 5 additions and 11 deletions

View File

@ -40,14 +40,11 @@ proc `shl`*[T: MpUint](x: T, y: SomeInteger): T {.noInit, noSideEffect.}=
type Sub = getSubType T type Sub = getSubType T
if y > halfSize: if y < halfSize:
result.hi = x.lo shl (y - halfSize)
result.lo = 0.Sub
elif y < halfSize:
result.hi = (x.hi shl y) or (x.lo shr (halfSize - y)) result.hi = (x.hi shl y) or (x.lo shr (halfSize - y))
result.lo = x.lo shl y result.lo = x.lo shl y
else: else:
result.hi = x.lo result.hi = x.lo shl (y - halfSize)
result.lo = 0.Sub result.lo = 0.Sub
proc `shr`*[T: MpUint](x: T, y: SomeInteger): T {.noInit, noSideEffect.}= proc `shr`*[T: MpUint](x: T, y: SomeInteger): T {.noInit, noSideEffect.}=
@ -62,12 +59,9 @@ proc `shr`*[T: MpUint](x: T, y: SomeInteger): T {.noInit, noSideEffect.}=
type Sub = getSubType T type Sub = getSubType T
if y > halfSize: if y < halfSize:
result.hi = x.hi shr (y - halfSize)
result.lo = 0.Sub
elif y < halfSize:
result.lo = (x.lo shr y) or (x.hi shl (halfSize - y)) result.lo = (x.lo shr y) or (x.hi shl (halfSize - y))
result.hi = x.hi shr y result.hi = x.hi shr y
else: else:
result.lo = x.hi result.hi = x.hi shr (y - halfSize)
result.hi = 0.Sub result.lo = 0.Sub