mirror of
https://github.com/status-im/nim-stint.git
synced 2025-02-19 18:38:13 +00:00
NEP-1 (camelCase)
This commit is contained in:
parent
65729901db
commit
0f5fd20679
26
stint/io.nim
26
stint/io.nim
@ -24,9 +24,9 @@ template static_check_size(T: typedesc[SomeInteger], bits: static[int]) =
|
||||
"\nUse a smaller input type instead. This is a compile-time check" &
|
||||
" to avoid a costly run-time bit_length check at each StUint initialization."
|
||||
|
||||
template assign_least_significant_words[T: SomeInteger](result: var (Stuint|Stint), n: T) =
|
||||
template lsw_result: untyped = least_significant_word(result.data)
|
||||
template slsw_result: untyped = second_least_significant_word(result.data)
|
||||
template assign_leastSignificantWords[T: SomeInteger](result: var (Stuint|Stint), n: T) =
|
||||
template lsw_result: untyped = leastSignificantWord(result.data)
|
||||
template slsw_result: untyped = secondLeastSignificantWord(result.data)
|
||||
|
||||
const wordSize = lsw_result.getSize
|
||||
when sizeof(T) * 8 <= wordSize:
|
||||
@ -47,7 +47,7 @@ func stuint*[T: SomeInteger](n: T, bits: static[int]): StUint[bits] {.inline.}=
|
||||
assert n >= 0.T
|
||||
when result.data is UintImpl:
|
||||
static_check_size(T, bits)
|
||||
assign_least_significant_words(result, n)
|
||||
assign_leastSignificantWords(result, n)
|
||||
else:
|
||||
result.data = (type result.data)(n)
|
||||
|
||||
@ -58,12 +58,12 @@ func stint*[T: SomeInteger](n: T, bits: static[int]): StInt[bits] {.inline.}=
|
||||
static_check_size(T, bits)
|
||||
when T is SomeSignedInt:
|
||||
if n < 0:
|
||||
assign_least_significant_words(result, -n)
|
||||
assign_leastSignificantWords(result, -n)
|
||||
result = -result
|
||||
else:
|
||||
assign_least_significant_words(result, n)
|
||||
assign_leastSignificantWords(result, n)
|
||||
else:
|
||||
assign_least_significant_words(result, n)
|
||||
assign_leastSignificantWords(result, n)
|
||||
else:
|
||||
result.data = (type result.data)(n)
|
||||
|
||||
@ -76,26 +76,26 @@ func to*(x: SomeUnsignedInt, T: typedesc[StUint]): T =
|
||||
func toInt*(num: Stint or StUint): int {.inline.}=
|
||||
## 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.leastSignificantWord)
|
||||
|
||||
func toUint*(num: Stint or StUint): uint {.inline.}=
|
||||
## Returns as uint. Result is modulo 2^(sizeof(uint))
|
||||
num.data.least_significant_word.uint
|
||||
num.data.leastSignificantWord.uint
|
||||
|
||||
func toInt64*(num: Stint or StUint): int64 {.inline.}=
|
||||
## Returns as int64.
|
||||
## Result is undefined if input does not fit in an int64
|
||||
when sizeof(uint) == 8:
|
||||
cast[int64](num.data.least_significant_word)
|
||||
cast[int64](num.data.leastSignificantWord)
|
||||
else:
|
||||
cast[int64](num.data.least_significant_two_words)
|
||||
cast[int64](num.data.leastSignificantTwoWords)
|
||||
|
||||
func toUint64*(num: Stint or StUint): uint64 {.inline.}=
|
||||
## Returns as uint64. Result is modulo 2^64.
|
||||
when sizeof(uint) == 8:
|
||||
num.data.least_significant_word.uint64
|
||||
num.data.leastSignificantWord.uint64
|
||||
else:
|
||||
cast[uint64](num.data.least_significant_two_words)
|
||||
cast[uint64](num.data.leastSignificantTwoWords)
|
||||
|
||||
func readHexChar(c: char): int8 {.inline.}=
|
||||
## Converts an hex char to an int
|
||||
|
@ -43,17 +43,17 @@ macro asWords(x: UintImpl or IntImpl, idx: static[int]): untyped =
|
||||
macro most_significant_word*(x: UintImpl or IntImpl): untyped =
|
||||
result = getAST(asWords(x, 0))
|
||||
|
||||
macro least_significant_word*(x: UintImpl or IntImpl): untyped =
|
||||
macro leastSignificantWord*(x: UintImpl or IntImpl): untyped =
|
||||
var words = nnkBracket.newTree()
|
||||
asWordsImpl(x, x, words)
|
||||
result = words[words.len - 1]
|
||||
|
||||
macro second_least_significant_word*(x: UintImpl or IntImpl): untyped =
|
||||
macro secondLeastSignificantWord*(x: UintImpl or IntImpl): untyped =
|
||||
var words = nnkBracket.newTree()
|
||||
asWordsImpl(x, x, words)
|
||||
result = words[words.len - 2]
|
||||
|
||||
macro least_significant_two_words*(x: UintImpl or IntImpl): untyped =
|
||||
macro leastSignificantTwoWords*(x: UintImpl or IntImpl): untyped =
|
||||
var words = nnkBracket.newTree()
|
||||
asWordsImpl(x, x, words)
|
||||
when system.cpuEndian == bigEndian:
|
||||
|
@ -13,7 +13,7 @@ func zero*(T: typedesc): T {.inline.} =
|
||||
discard
|
||||
|
||||
func one*(T: typedesc[UintImpl or IntImpl]): T {.inline.} =
|
||||
least_significant_word(result) = 1
|
||||
leastSignificantWord(result) = 1
|
||||
|
||||
func one*(T: typedesc[SomeInteger]): T {.inline.} =
|
||||
1
|
||||
|
@ -53,7 +53,7 @@ func `<=`*(x, y: IntImpl): bool {.inline.}=
|
||||
return true # they're equal
|
||||
|
||||
func isOdd*(x: IntImpl): bool {.inline.}=
|
||||
bool(x.least_significant_word and 1)
|
||||
bool(x.leastSignificantWord and 1)
|
||||
|
||||
func isEven*(x: IntImpl): bool {.inline.}=
|
||||
not x.isOdd
|
||||
|
@ -48,7 +48,7 @@ func `<=`*(x, y: UintImpl): bool {.inline.}=
|
||||
return true # they're equal
|
||||
|
||||
func isOdd*(x: UintImpl): bool {.inline.}=
|
||||
bool(x.least_significant_word and 1)
|
||||
bool(x.leastSignificantWord and 1)
|
||||
|
||||
func isEven*(x: UintImpl): bool {.inline.}=
|
||||
not x.isOdd
|
||||
|
Loading…
x
Reference in New Issue
Block a user