2018-04-25 19:21:25 +00:00
|
|
|
# Stint
|
2018-03-02 10:48:08 +00: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-02-17 11:02:51 +00:00
|
|
|
|
2018-06-18 14:44:29 +00:00
|
|
|
import ./datatypes, ./conversion, stdlib_bitops, as_words
|
2018-04-21 10:12:05 +00:00
|
|
|
export stdlib_bitops
|
2018-03-26 12:46:38 +00:00
|
|
|
|
2018-04-21 10:12:05 +00:00
|
|
|
# We reuse bitops from Nim standard lib, and expand it for multi-precision int.
|
|
|
|
# MpInt rely on no undefined behaviour as often we scan 0. (if 1 is stored in a uint128 for example)
|
|
|
|
# Also countLeadingZeroBits must return the size of the type and not 0 like in the stdlib
|
2018-03-26 12:46:38 +00:00
|
|
|
|
2018-04-25 10:52:00 +00:00
|
|
|
func countLeadingZeroBits*(n: UintImpl): int {.inline.} =
|
2018-03-28 18:45:39 +00:00
|
|
|
## Returns the number of leading zero bits in integer.
|
2018-04-20 09:13:47 +00:00
|
|
|
|
2018-04-21 10:12:05 +00:00
|
|
|
const maxHalfRepr = getSize(n) div 2
|
2018-04-20 09:13:47 +00:00
|
|
|
|
2018-04-21 10:12:05 +00:00
|
|
|
let hi_clz = n.hi.countLeadingZeroBits
|
2018-04-20 09:13:47 +00:00
|
|
|
|
|
|
|
result = if hi_clz == maxHalfRepr:
|
2018-04-21 10:12:05 +00:00
|
|
|
n.lo.countLeadingZeroBits + maxHalfRepr
|
2018-04-20 09:13:47 +00:00
|
|
|
else: hi_clz
|
2018-04-25 13:41:59 +00:00
|
|
|
|
2018-06-18 10:54:25 +00:00
|
|
|
func isMsbSet*[T: SomeInteger](n: T): bool {.inline.}=
|
2018-04-25 13:41:59 +00:00
|
|
|
## Returns the most significant bit of an integer.
|
|
|
|
const msb_pos = sizeof(T) * 8 - 1
|
2018-06-18 14:44:29 +00:00
|
|
|
result = bool(n.toUint shr msb_pos)
|
2018-04-25 13:41:59 +00:00
|
|
|
|
2018-06-18 10:54:25 +00:00
|
|
|
func isMsbSet*(n: UintImpl or IntImpl): bool {.inline.}=
|
2018-04-25 13:41:59 +00:00
|
|
|
## Returns the most significant bit of an arbitrary precision integer.
|
2018-06-18 10:54:25 +00:00
|
|
|
result = isMsbSet most_significant_word(n)
|