Fix division and modulus for Nat

This commit is contained in:
Rúnar 2021-07-14 23:47:35 -04:00
parent a2dd77a550
commit 971c2abbba
2 changed files with 15 additions and 2 deletions

View File

@ -308,6 +308,7 @@ data UPrim1
data UPrim2
-- integral
= ADDI | SUBI | MULI | DIVI | MODI -- +,-,*,/,mod
| DIVN | MODN
| SHLI | SHRI | SHRN | POWI -- shiftl,shiftr,shiftr,pow
| EQLI | LEQI | LEQN -- ==,<=,<=
| ANDN | IORN | XORN -- and,or,xor
@ -937,9 +938,9 @@ emitPOp ANF.SUBN = emitP2 SUBI
emitPOp ANF.MULI = emitP2 MULI
emitPOp ANF.MULN = emitP2 MULI
emitPOp ANF.DIVI = emitP2 DIVI
emitPOp ANF.DIVN = emitP2 DIVI
emitPOp ANF.DIVN = emitP2 DIVN
emitPOp ANF.MODI = emitP2 MODI -- TODO: think about how these behave
emitPOp ANF.MODN = emitP2 MODI -- TODO: think about how these behave
emitPOp ANF.MODN = emitP2 MODN -- TODO: think about how these behave
emitPOp ANF.POWI = emitP2 POWI
emitPOp ANF.POWN = emitP2 POWI
emitPOp ANF.SHLI = emitP2 SHLI

View File

@ -902,6 +902,18 @@ uprim2 !ustk LEQN !i !j = do
ustk <- bump ustk
poke ustk $ if m <= n then 1 else 0
pure ustk
uprim2 !ustk DIVN !i !j = do
m <- peekOffN ustk i
n <- peekOffN ustk j
ustk <- bump ustk
pokeN ustk (m`div`n)
pure ustk
uprim2 !ustk MODN !i !j = do
m <- peekOffN ustk i
n <- peekOffN ustk j
ustk <- bump ustk
pokeN ustk (m`mod`n)
pure ustk
uprim2 !ustk ADDF !i !j = do
x <- peekOffD ustk i
y <- peekOffD ustk j