Created
April 19, 2018 15:54
-
-
Save mratsim/57ea041f2b2d62c63d90de1558fc1b39 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func `shl`*(x: MpUintImpl, y: SomeInteger): MpUintImpl {.inline.}= | |
## Compute the `shift left` operation of x and y | |
# Note: inlining this poses codegen/aliasing issue when doing `x = x shl 1` | |
# TODO: would it be better to reimplement this using an array of bytes/uint64 | |
# That opens up to endianness issues. | |
const halfSize = size_mpuintimpl(x) div 2 | |
let defect = halfSize - int(y) | |
if y == 0: | |
return x | |
elif y > size_mpuintimpl(x): | |
return | |
elif defect > 0: | |
result.hi = (x.hi shl y) or (x.lo shr defect) | |
result.lo = x.lo shl y | |
elif defect < 0: | |
result.hi = x.lo shl (-defect) | |
else: | |
result.hi = x.lo | |
func `shr`*(x: MpUintImpl, y: SomeInteger): MpUintImpl {.inline.}= | |
## Compute the `shift right` operation of x and y | |
const halfSize = size_mpuintimpl(x) div 2 | |
let defect = halfSize - int(y) | |
if y == 0: | |
return x | |
elif y > size_mpuintimpl(x): | |
return | |
elif defect > 0: | |
result.lo = (x.lo shr y) or (x.hi shr defect) | |
result.hi = x.hi shr y | |
elif defect < 0: | |
result.lo = x.lo shr (-defect) | |
else: | |
result.lo = x.hi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment