2018-04-25 21:21:25 +02:00
|
|
|
# Stint
|
2018-03-02 11:48:08 +01:00
|
|
|
# Copyright 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
#
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
|
|
|
#
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
2018-05-06 22:29:08 +02:00
|
|
|
|
2018-04-25 14:27:55 +02:00
|
|
|
import ./datatypes, ./as_words
|
2018-02-16 11:33:11 +01:00
|
|
|
|
|
|
|
|
2018-05-06 22:29:08 +02:00
|
|
|
func `not`*(x: UintImpl): UintImpl {.inline.}=
|
2018-02-16 11:33:11 +01:00
|
|
|
## Bitwise complement of unsigned integer x
|
2018-04-21 17:21:14 +02:00
|
|
|
m_asWordsZip(result, x, ignoreEndianness = true):
|
|
|
|
result = not x
|
2018-02-16 11:33:11 +01:00
|
|
|
|
2018-05-06 22:29:08 +02:00
|
|
|
func `or`*(x, y: UintImpl): UintImpl {.inline.}=
|
2018-02-16 11:33:11 +01:00
|
|
|
## `Bitwise or` of numbers x and y
|
2018-04-21 17:21:14 +02:00
|
|
|
m_asWordsZip(result, x, y, ignoreEndianness = true):
|
|
|
|
result = x or y
|
2018-02-16 11:33:11 +01:00
|
|
|
|
2018-05-06 22:29:08 +02:00
|
|
|
func `and`*(x, y: UintImpl): UintImpl {.inline.}=
|
2018-02-16 11:33:11 +01:00
|
|
|
## `Bitwise and` of numbers x and y
|
2018-04-21 17:21:14 +02:00
|
|
|
m_asWordsZip(result, x, y, ignoreEndianness = true):
|
|
|
|
result = x and y
|
2018-02-16 11:33:11 +01:00
|
|
|
|
2018-05-06 22:29:08 +02:00
|
|
|
func `xor`*(x, y: UintImpl): UintImpl {.inline.}=
|
2018-02-16 11:33:11 +01:00
|
|
|
## `Bitwise xor` of numbers x and y
|
2018-04-21 17:21:14 +02:00
|
|
|
m_asWordsZip(result, x, y, ignoreEndianness = true):
|
|
|
|
result = x xor y
|
2018-02-16 13:54:38 +01:00
|
|
|
|
2018-04-25 12:52:00 +02:00
|
|
|
func `shr`*(x: UintImpl, y: SomeInteger): UintImpl {.inline.}
|
2018-04-20 11:13:47 +02:00
|
|
|
# Forward declaration
|
|
|
|
|
2018-04-25 12:52:00 +02:00
|
|
|
func `shl`*(x: UintImpl, y: SomeInteger): UintImpl {.inline.}=
|
2018-02-16 13:54:38 +01:00
|
|
|
## Compute the `shift left` operation of x and y
|
2018-02-17 12:44:51 +01:00
|
|
|
# Note: inlining this poses codegen/aliasing issue when doing `x = x shl 1`
|
2018-04-20 11:13:47 +02:00
|
|
|
|
|
|
|
# TODO: would it be better to reimplement this using an array of bytes/uint64
|
|
|
|
# That opens up to endianness issues.
|
2018-05-08 16:38:11 +02:00
|
|
|
const halfSize: type(y) = getSize(x) div 2
|
2018-02-16 13:54:38 +01:00
|
|
|
|
2018-04-20 11:13:47 +02:00
|
|
|
if y == 0:
|
|
|
|
return x
|
|
|
|
elif y == halfSize:
|
|
|
|
result.hi = x.lo
|
|
|
|
elif y < halfSize:
|
|
|
|
result.hi = (x.hi shl y) or (x.lo shr (halfSize - y))
|
2018-03-28 20:45:39 +02:00
|
|
|
result.lo = x.lo shl y
|
2018-04-20 11:13:47 +02:00
|
|
|
else:
|
|
|
|
result.hi = x.lo shl (y - halfSize)
|
2018-02-16 13:54:38 +01:00
|
|
|
|
2018-04-25 12:52:00 +02:00
|
|
|
func `shr`*(x: UintImpl, y: SomeInteger): UintImpl {.inline.}=
|
2018-02-16 13:54:38 +01:00
|
|
|
## Compute the `shift right` operation of x and y
|
2018-05-08 16:38:11 +02:00
|
|
|
const halfSize: type(y) = getSize(x) div 2
|
2018-02-16 13:54:38 +01:00
|
|
|
|
2018-04-20 11:13:47 +02:00
|
|
|
if y == 0:
|
|
|
|
return x
|
|
|
|
elif y == halfSize:
|
|
|
|
result.lo = x.hi
|
|
|
|
elif y < halfSize:
|
|
|
|
result.lo = (x.lo shr y) or (x.hi shl (halfSize - y))
|
2018-03-28 20:45:39 +02:00
|
|
|
result.hi = x.hi shr y
|
2018-04-20 11:13:47 +02:00
|
|
|
else:
|
|
|
|
result.lo = x.hi shr (y - halfSize)
|
|
|
|
|