Add a toUint64 and toInt64 for 32-bit platform (fixes #48)

This commit is contained in:
mratsim 2018-10-06 11:28:15 +02:00 committed by tersec
parent 1c29314d8e
commit 31101a5c5b
2 changed files with 20 additions and 1 deletions

View File

@ -75,12 +75,26 @@ func to*(x: SomeUnsignedInt, T: typedesc[StUint]): T =
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 modulo 2^(sizeof(int)
num.data.least_significant_word.int 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 int. Result is modulo 2^(sizeof(int)
num.data.least_significant_word num.data.least_significant_word
func toInt64*(num: Stint or StUint): int64 {.inline.}=
# Returns as int64. Result is modulo 2^(sizeof(int)
when sizeof(uint) == 8:
cast[int64](num.data.least_significant_word)
else:
cast[int64](num.data.least_significant_two_words)
func toUint64*(num: Stint or StUint): uint64 {.inline.}=
# Returns as int. Result is modulo 2^(sizeof(int)
when sizeof(uint) == 8:
num.data.least_significant_word.uint64
else:
cast[uint64](num.data.least_significant_two_words)
func readHexChar(c: char): int8 {.inline.}= func readHexChar(c: char): int8 {.inline.}=
## Converts an hex char to an int ## Converts an hex char to an int
case c case c

View File

@ -53,6 +53,11 @@ macro second_least_significant_word*(x: UintImpl or IntImpl): untyped =
asWordsImpl(x, x, words) asWordsImpl(x, x, words)
result = words[words.len - 2] result = words[words.len - 2]
macro least_significant_two_words*(x: UintImpl or IntImpl): untyped =
var words = nnkBracket.newTree()
asWordsImpl(x, x, words)
result = nnkBracket.newTree(words[words.len - 2], words[words.len - 1])
# ######################################################################### # #########################################################################
# Iteration macros # Iteration macros