2018-03-02 10:48:08 +00:00
|
|
|
# Mpint
|
|
|
|
# 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-03-26 10:45:10 +00:00
|
|
|
import ./uint_type, bitops
|
2018-02-17 11:02:51 +00:00
|
|
|
|
2018-03-26 10:45:10 +00:00
|
|
|
proc bit_length*(x: SomeInteger): int {.inline, noSideEffect.}=
|
|
|
|
if x == 0: 0
|
|
|
|
else: fastlog2(x)
|
2018-02-17 11:02:51 +00:00
|
|
|
|
2018-03-26 09:46:24 +00:00
|
|
|
proc bit_length*(n: MpUintImpl): int {.noSideEffect.}=
|
2018-02-17 11:02:51 +00:00
|
|
|
## Calculates how many bits are necessary to represent the number
|
|
|
|
|
|
|
|
const maxHalfRepr = n.lo.type.sizeof * 8 - 1
|
|
|
|
|
2018-02-17 16:57:26 +00:00
|
|
|
# Changing the following to an if expression somehow transform the whole ASM to 5 branches
|
|
|
|
# instead of the 4 expected (with the inline ASM from bit_length_impl)
|
|
|
|
# Also there does not seems to be a way to generate a conditional mov
|
2018-02-17 11:02:51 +00:00
|
|
|
if n.hi.bit_length == 0:
|
|
|
|
n.lo.bit_length
|
|
|
|
else:
|
2018-03-02 10:48:08 +00:00
|
|
|
n.hi.bit_length + maxHalfRepr
|