mirror of
https://github.com/status-im/nim-stint.git
synced 2025-02-21 11:28:23 +00:00
Fix doc of toInt/toUint + add more docs about undefined behaviours
This commit is contained in:
parent
7fa6329d4c
commit
7bbe7d8f9f
10
stint/io.nim
10
stint/io.nim
@ -74,22 +74,24 @@ func to*(x: SomeUnsignedInt, T: typedesc[StUint]): T =
|
|||||||
stuint(x, result.bits)
|
stuint(x, result.bits)
|
||||||
|
|
||||||
func toInt*(num: Stint or StUint): int {.inline.}=
|
func toInt*(num: Stint or StUint): int {.inline.}=
|
||||||
# Returns as int. Result is modulo 2^(sizeof(int)
|
# Returns as int.
|
||||||
|
# Result is undefined if input does not fit in an int64
|
||||||
cast[int](num.data.least_significant_word)
|
cast[int](num.data.least_significant_word)
|
||||||
|
|
||||||
func toUint*(num: Stint or StUint): uint {.inline.}=
|
func toUint*(num: Stint or StUint): uint {.inline.}=
|
||||||
# Returns as int. Result is modulo 2^(sizeof(int)
|
# Returns as uint. Result is modulo 2^(sizeof(uint))
|
||||||
num.data.least_significant_word
|
num.data.least_significant_word
|
||||||
|
|
||||||
func toInt64*(num: Stint or StUint): int64 {.inline.}=
|
func toInt64*(num: Stint or StUint): int64 {.inline.}=
|
||||||
# Returns as int64. Result is modulo 2^(sizeof(int)
|
# Returns as int64.
|
||||||
|
# Result is undefined if input does not fit in an int64
|
||||||
when sizeof(uint) == 8:
|
when sizeof(uint) == 8:
|
||||||
cast[int64](num.data.least_significant_word)
|
cast[int64](num.data.least_significant_word)
|
||||||
else:
|
else:
|
||||||
cast[int64](num.data.least_significant_two_words)
|
cast[int64](num.data.least_significant_two_words)
|
||||||
|
|
||||||
func toUint64*(num: Stint or StUint): uint64 {.inline.}=
|
func toUint64*(num: Stint or StUint): uint64 {.inline.}=
|
||||||
# Returns as int. Result is modulo 2^(sizeof(int)
|
# Returns as uint64. Result is modulo 2^64.
|
||||||
when sizeof(uint) == 8:
|
when sizeof(uint) == 8:
|
||||||
num.data.least_significant_word.uint64
|
num.data.least_significant_word.uint64
|
||||||
else:
|
else:
|
||||||
|
@ -59,6 +59,8 @@ func `shl`*(x: UintImpl, y: SomeInteger): UintImpl {.inline.}=
|
|||||||
|
|
||||||
func `shr`*(x: UintImpl, y: SomeInteger): UintImpl {.inline.}=
|
func `shr`*(x: UintImpl, y: SomeInteger): UintImpl {.inline.}=
|
||||||
## Compute the `shift right` operation of x and y
|
## Compute the `shift right` operation of x and y
|
||||||
|
## Similar to C standard, result is undefined if y is bigger
|
||||||
|
## than the number of bits in x.
|
||||||
const halfSize: type(y) = getSize(x) div 2
|
const halfSize: type(y) = getSize(x) div 2
|
||||||
|
|
||||||
if y == 0:
|
if y == 0:
|
||||||
|
@ -104,6 +104,8 @@ func `shr`*(x: StUint, y: SomeInteger): StUint {.inline.} =
|
|||||||
result.data = x.data shr y
|
result.data = x.data shr y
|
||||||
func `shl`*(x: StUint, y: SomeInteger): StUint {.inline.} =
|
func `shl`*(x: StUint, y: SomeInteger): StUint {.inline.} =
|
||||||
## Logical shift right
|
## Logical shift right
|
||||||
|
## Similar to C standard, result is undefined if y is bigger
|
||||||
|
## than the number of bits in x.
|
||||||
result.data = x.data shl y
|
result.data = x.data shl y
|
||||||
|
|
||||||
import ./private/uint_highlow
|
import ./private/uint_highlow
|
||||||
|
Loading…
x
Reference in New Issue
Block a user