nimvm workaround for primitives

This commit is contained in:
jangko 2023-06-13 08:35:35 +07:00
parent 7ce536423a
commit 8c5a96463c
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
2 changed files with 16 additions and 7 deletions

View File

@ -107,14 +107,14 @@ func addC*(cOut: var Carry, sum: var uint32, a, b: uint32, cIn: Carry) {.inline.
## (CarryOut, Sum) <- a + b + CarryIn
when nimvm:
let dblPrec = uint64(cIn) + uint64(a) + uint64(b)
sum = (uint32)(dblPrec)
sum = uint32(dblPrec and uint32.high)
cOut = Carry(dblPrec shr 32)
else:
when X86:
cOut = addcarry_u32(cIn, a, b, sum)
else:
let dblPrec = uint64(cIn) + uint64(a) + uint64(b)
sum = (uint32)(dblPrec)
sum = uint32(dblPrec)
cOut = Carry(dblPrec shr 32)
func subB*(bOut: var Borrow, diff: var uint32, a, b: uint32, bIn: Borrow) {.inline.} =
@ -122,7 +122,7 @@ func subB*(bOut: var Borrow, diff: var uint32, a, b: uint32, bIn: Borrow) {.inli
## (BorrowOut, Diff) <- a - b - borrowIn
when nimvm:
let dblPrec = uint64(a) - uint64(b) - uint64(bIn)
diff = (uint32)(dblPrec)
diff = uint32(dblPrec and uint32.high)
# On borrow the high word will be 0b1111...1111 and needs to be masked
bOut = Borrow((dblPrec shr 32) and 1)
else:
@ -130,7 +130,7 @@ func subB*(bOut: var Borrow, diff: var uint32, a, b: uint32, bIn: Borrow) {.inli
bOut = subborrow_u32(bIn, a, b, diff)
else:
let dblPrec = uint64(a) - uint64(b) - uint64(bIn)
diff = (uint32)(dblPrec)
diff = uint32(dblPrec)
# On borrow the high word will be 0b1111...1111 and needs to be masked
bOut = Borrow((dblPrec shr 32) and 1)

View File

@ -41,7 +41,10 @@ func mul*(hi, lo: var uint32, a, b: uint32) {.inline.} =
## Extended precision multiplication
## (hi, lo) <- a*b
let dblPrec = uint64(a) * uint64(b)
lo = uint32(dblPrec)
when nimvm:
lo = uint32(dblPrec and uint32.high)
else:
lo = uint32(dblPrec)
hi = uint32(dblPrec shr 32)
func muladd1*(hi, lo: var uint32, a, b, c: uint32) {.inline.} =
@ -51,7 +54,10 @@ func muladd1*(hi, lo: var uint32, a, b, c: uint32) {.inline.} =
## Note: 0xFFFFFFFF² -> (hi: 0xFFFFFFFE, lo: 0x00000001)
## so adding any c cannot overflow
let dblPrec = uint64(a) * uint64(b) + uint64(c)
lo = uint32(dblPrec)
when nimvm:
lo = uint32(dblPrec and uint32.high)
else:
lo = uint32(dblPrec)
hi = uint32(dblPrec shr 32)
func muladd2*(hi, lo: var uint32, a, b, c1, c2: uint32) {.inline.}=
@ -63,7 +69,10 @@ func muladd2*(hi, lo: var uint32, a, b, c1, c2: uint32) {.inline.}=
## so adding 0xFFFFFFFF leads to (hi: 0xFFFFFFFF, lo: 0x00000000)
## and we have enough space to add again 0xFFFFFFFF without overflowing
let dblPrec = uint64(a) * uint64(b) + uint64(c1) + uint64(c2)
lo = uint32(dblPrec)
when nimvm:
lo = uint32(dblPrec and uint32.high)
else:
lo = uint32(dblPrec)
hi = uint32(dblPrec shr 32)
# ############################################################