Fix FM/MOD + Implement SM/REM, UM/MOD.

This commit is contained in:
Remko Tronçon 2022-04-30 17:42:45 +02:00
parent 9eab504ad6
commit 504ae3e93e
3 changed files with 222 additions and 154 deletions

View file

@ -342,8 +342,8 @@ T{ 2 S>D 2 FM/MOD -> 0 1 }T
T{ -1 S>D -1 FM/MOD -> 0 1 }T
T{ -2 S>D -2 FM/MOD -> 0 1 }T
T{ 7 S>D 3 FM/MOD -> 1 2 }T
\ TODO T{ 7 S>D -3 FM/MOD -> -2 -3 }T
\ TODO T{ -7 S>D 3 FM/MOD -> 2 -3 }T
T{ 7 S>D -3 FM/MOD -> -2 -3 }T
T{ -7 S>D 3 FM/MOD -> 2 -3 }T
T{ -7 S>D -3 FM/MOD -> -1 2 }T
T{ MAX-INT S>D 1 FM/MOD -> 0 MAX-INT }T
T{ MIN-INT S>D 1 FM/MOD -> 0 MIN-INT }T
@ -363,44 +363,44 @@ T{ MIN-INT MAX-INT M* MIN-INT FM/MOD -> 0 MAX-INT }T
T{ MIN-INT MAX-INT M* MAX-INT FM/MOD -> 0 MIN-INT }T
T{ MAX-INT MAX-INT M* MAX-INT FM/MOD -> 0 MAX-INT }T
\ TODO T{ 0 S>D 1 SM/REM -> 0 0 }T
\ TODO T{ 1 S>D 1 SM/REM -> 0 1 }T
\ TODO T{ 2 S>D 1 SM/REM -> 0 2 }T
\ TODO T{ -1 S>D 1 SM/REM -> 0 -1 }T
\ TODO T{ -2 S>D 1 SM/REM -> 0 -2 }T
\ TODO T{ 0 S>D -1 SM/REM -> 0 0 }T
\ TODO T{ 1 S>D -1 SM/REM -> 0 -1 }T
\ TODO T{ 2 S>D -1 SM/REM -> 0 -2 }T
\ TODO T{ -1 S>D -1 SM/REM -> 0 1 }T
\ TODO T{ -2 S>D -1 SM/REM -> 0 2 }T
\ TODO T{ 2 S>D 2 SM/REM -> 0 1 }T
\ TODO T{ -1 S>D -1 SM/REM -> 0 1 }T
\ TODO T{ -2 S>D -2 SM/REM -> 0 1 }T
\ TODO T{ 7 S>D 3 SM/REM -> 1 2 }T
\ TODO T{ 7 S>D -3 SM/REM -> 1 -2 }T
\ TODO T{ -7 S>D 3 SM/REM -> -1 -2 }T
\ TODO T{ -7 S>D -3 SM/REM -> -1 2 }T
\ TODO T{ MAX-INT S>D 1 SM/REM -> 0 MAX-INT }T
\ TODO T{ MIN-INT S>D 1 SM/REM -> 0 MIN-INT }T
\ TODO T{ MAX-INT S>D MAX-INT SM/REM -> 0 1 }T
\ TODO T{ MIN-INT S>D MIN-INT SM/REM -> 0 1 }T
\ TODO T{ 1S 1 4 SM/REM -> 3 MAX-INT }T
\ TODO T{ 2 MIN-INT M* 2 SM/REM -> 0 MIN-INT }T
\ TODO T{ 2 MIN-INT M* MIN-INT SM/REM -> 0 2 }T
\ TODO T{ 2 MAX-INT M* 2 SM/REM -> 0 MAX-INT }T
\ TODO T{ 2 MAX-INT M* MAX-INT SM/REM -> 0 2 }T
\ TODO T{ MIN-INT MIN-INT M* MIN-INT SM/REM -> 0 MIN-INT }T
\ TODO T{ MIN-INT MAX-INT M* MIN-INT SM/REM -> 0 MAX-INT }T
\ TODO T{ MIN-INT MAX-INT M* MAX-INT SM/REM -> 0 MIN-INT }T
\ TODO T{ MAX-INT MAX-INT M* MAX-INT SM/REM -> 0 MAX-INT }T
\ TODO
\ TODO T{ 0 0 1 UM/MOD -> 0 0 }T
\ TODO T{ 1 0 1 UM/MOD -> 0 1 }T
\ TODO T{ 1 0 2 UM/MOD -> 1 0 }T
\ TODO T{ 3 0 2 UM/MOD -> 1 1 }T
\ TODO T{ MAX-UINT 2 UM* 2 UM/MOD -> 0 MAX-UINT }T
\ TODO T{ MAX-UINT 2 UM* MAX-UINT UM/MOD -> 0 2 }T
\ TODO T{ MAX-UINT MAX-UINT UM* MAX-UINT UM/MOD -> 0 MAX-UINT }T
T{ 0 S>D 1 SM/REM -> 0 0 }T
T{ 1 S>D 1 SM/REM -> 0 1 }T
T{ 2 S>D 1 SM/REM -> 0 2 }T
T{ -1 S>D 1 SM/REM -> 0 -1 }T
T{ -2 S>D 1 SM/REM -> 0 -2 }T
T{ 0 S>D -1 SM/REM -> 0 0 }T
T{ 1 S>D -1 SM/REM -> 0 -1 }T
T{ 2 S>D -1 SM/REM -> 0 -2 }T
T{ -1 S>D -1 SM/REM -> 0 1 }T
T{ -2 S>D -1 SM/REM -> 0 2 }T
T{ 2 S>D 2 SM/REM -> 0 1 }T
T{ -1 S>D -1 SM/REM -> 0 1 }T
T{ -2 S>D -2 SM/REM -> 0 1 }T
T{ 7 S>D 3 SM/REM -> 1 2 }T
T{ 7 S>D -3 SM/REM -> 1 -2 }T
T{ -7 S>D 3 SM/REM -> -1 -2 }T
T{ -7 S>D -3 SM/REM -> -1 2 }T
T{ MAX-INT S>D 1 SM/REM -> 0 MAX-INT }T
T{ MIN-INT S>D 1 SM/REM -> 0 MIN-INT }T
T{ MAX-INT S>D MAX-INT SM/REM -> 0 1 }T
T{ MIN-INT S>D MIN-INT SM/REM -> 0 1 }T
T{ 1S 1 4 SM/REM -> 3 MAX-INT }T
T{ 2 MIN-INT M* 2 SM/REM -> 0 MIN-INT }T
T{ 2 MIN-INT M* MIN-INT SM/REM -> 0 2 }T
T{ 2 MAX-INT M* 2 SM/REM -> 0 MAX-INT }T
T{ 2 MAX-INT M* MAX-INT SM/REM -> 0 2 }T
T{ MIN-INT MIN-INT M* MIN-INT SM/REM -> 0 MIN-INT }T
T{ MIN-INT MAX-INT M* MIN-INT SM/REM -> 0 MAX-INT }T
T{ MIN-INT MAX-INT M* MAX-INT SM/REM -> 0 MIN-INT }T
T{ MAX-INT MAX-INT M* MAX-INT SM/REM -> 0 MAX-INT }T
T{ 0 0 1 UM/MOD -> 0 0 }T
T{ 1 0 1 UM/MOD -> 0 1 }T
T{ 1 0 2 UM/MOD -> 1 0 }T
T{ 3 0 2 UM/MOD -> 1 1 }T
T{ MAX-UINT 2 UM* 2 UM/MOD -> 0 MAX-UINT }T
T{ MAX-UINT 2 UM* MAX-UINT UM/MOD -> 0 2 }T
T{ MAX-UINT MAX-UINT UM* MAX-UINT UM/MOD -> 0 MAX-UINT }T
: IFFLOORED
[ -3 2 / -2 = INVERT ] LITERAL IF POSTPONE \ THEN ;
@ -422,111 +422,111 @@ IFSYM : TMOD T/MOD DROP ;
IFSYM : T*/MOD >R M* R> SM/REM ;
IFSYM : T*/ T*/MOD SWAP DROP ;
\ TODO T{ 0 1 /MOD -> 0 1 T/MOD }T
\ TODO T{ 1 1 /MOD -> 1 1 T/MOD }T
\ TODO T{ 2 1 /MOD -> 2 1 T/MOD }T
\ TODO T{ -1 1 /MOD -> -1 1 T/MOD }T
\ TODO T{ -2 1 /MOD -> -2 1 T/MOD }T
\ TODO T{ 0 -1 /MOD -> 0 -1 T/MOD }T
\ TODO T{ 1 -1 /MOD -> 1 -1 T/MOD }T
\ TODO T{ 2 -1 /MOD -> 2 -1 T/MOD }T
\ TODO T{ -1 -1 /MOD -> -1 -1 T/MOD }T
\ TODO T{ -2 -1 /MOD -> -2 -1 T/MOD }T
\ TODO T{ 2 2 /MOD -> 2 2 T/MOD }T
\ TODO T{ -1 -1 /MOD -> -1 -1 T/MOD }T
\ TODO T{ -2 -2 /MOD -> -2 -2 T/MOD }T
\ TODO T{ 7 3 /MOD -> 7 3 T/MOD }T
\ TODO T{ 7 -3 /MOD -> 7 -3 T/MOD }T
\ TODO T{ -7 3 /MOD -> -7 3 T/MOD }T
\ TODO T{ -7 -3 /MOD -> -7 -3 T/MOD }T
\ TODO T{ MAX-INT 1 /MOD -> MAX-INT 1 T/MOD }T
\ TODO T{ MIN-INT 1 /MOD -> MIN-INT 1 T/MOD }T
\ TODO T{ MAX-INT MAX-INT /MOD -> MAX-INT MAX-INT T/MOD }T
\ TODO T{ MIN-INT MIN-INT /MOD -> MIN-INT MIN-INT T/MOD }T
\ TODO
\ TODO T{ 0 1 / -> 0 1 T/ }T
\ TODO T{ 1 1 / -> 1 1 T/ }T
\ TODO T{ 2 1 / -> 2 1 T/ }T
\ TODO T{ -1 1 / -> -1 1 T/ }T
\ TODO T{ -2 1 / -> -2 1 T/ }T
\ TODO T{ 0 -1 / -> 0 -1 T/ }T
\ TODO T{ 1 -1 / -> 1 -1 T/ }T
\ TODO T{ 2 -1 / -> 2 -1 T/ }T
\ TODO T{ -1 -1 / -> -1 -1 T/ }T
\ TODO T{ -2 -1 / -> -2 -1 T/ }T
\ TODO T{ 2 2 / -> 2 2 T/ }T
\ TODO T{ -1 -1 / -> -1 -1 T/ }T
\ TODO T{ -2 -2 / -> -2 -2 T/ }T
\ TODO T{ 7 3 / -> 7 3 T/ }T
\ TODO T{ 7 -3 / -> 7 -3 T/ }T
\ TODO T{ -7 3 / -> -7 3 T/ }T
\ TODO T{ -7 -3 / -> -7 -3 T/ }T
\ TODO T{ MAX-INT 1 / -> MAX-INT 1 T/ }T
\ TODO T{ MIN-INT 1 / -> MIN-INT 1 T/ }T
\ TODO T{ MAX-INT MAX-INT / -> MAX-INT MAX-INT T/ }T
\ TODO T{ MIN-INT MIN-INT / -> MIN-INT MIN-INT T/ }T
\ TODO
\ TODO T{ 0 1 MOD -> 0 1 TMOD }T
\ TODO T{ 1 1 MOD -> 1 1 TMOD }T
\ TODO T{ 2 1 MOD -> 2 1 TMOD }T
\ TODO T{ -1 1 MOD -> -1 1 TMOD }T
\ TODO T{ -2 1 MOD -> -2 1 TMOD }T
\ TODO T{ 0 -1 MOD -> 0 -1 TMOD }T
\ TODO T{ 1 -1 MOD -> 1 -1 TMOD }T
\ TODO T{ 2 -1 MOD -> 2 -1 TMOD }T
\ TODO T{ -1 -1 MOD -> -1 -1 TMOD }T
\ TODO T{ -2 -1 MOD -> -2 -1 TMOD }T
\ TODO T{ 2 2 MOD -> 2 2 TMOD }T
\ TODO T{ -1 -1 MOD -> -1 -1 TMOD }T
\ TODO T{ -2 -2 MOD -> -2 -2 TMOD }T
\ TODO T{ 7 3 MOD -> 7 3 TMOD }T
\ TODO T{ 7 -3 MOD -> 7 -3 TMOD }T
\ TODO T{ -7 3 MOD -> -7 3 TMOD }T
\ TODO T{ -7 -3 MOD -> -7 -3 TMOD }T
\ TODO T{ MAX-INT 1 MOD -> MAX-INT 1 TMOD }T
\ TODO T{ MIN-INT 1 MOD -> MIN-INT 1 TMOD }T
\ TODO T{ MAX-INT MAX-INT MOD -> MAX-INT MAX-INT TMOD }T
\ TODO T{ MIN-INT MIN-INT MOD -> MIN-INT MIN-INT TMOD }T
\ TODO
\ TODO T{ 0 2 1 */ -> 0 2 1 T*/ }T
\ TODO T{ 1 2 1 */ -> 1 2 1 T*/ }T
\ TODO T{ 2 2 1 */ -> 2 2 1 T*/ }T
\ TODO T{ -1 2 1 */ -> -1 2 1 T*/ }T
\ TODO T{ -2 2 1 */ -> -2 2 1 T*/ }T
\ TODO T{ 0 2 -1 */ -> 0 2 -1 T*/ }T
\ TODO T{ 1 2 -1 */ -> 1 2 -1 T*/ }T
\ TODO T{ 2 2 -1 */ -> 2 2 -1 T*/ }T
\ TODO T{ -1 2 -1 */ -> -1 2 -1 T*/ }T
\ TODO T{ -2 2 -1 */ -> -2 2 -1 T*/ }T
\ TODO T{ 2 2 2 */ -> 2 2 2 T*/ }T
\ TODO T{ -1 2 -1 */ -> -1 2 -1 T*/ }T
\ TODO T{ -2 2 -2 */ -> -2 2 -2 T*/ }T
\ TODO T{ 7 2 3 */ -> 7 2 3 T*/ }T
\ TODO T{ 7 2 -3 */ -> 7 2 -3 T*/ }T
\ TODO T{ -7 2 3 */ -> -7 2 3 T*/ }T
\ TODO T{ -7 2 -3 */ -> -7 2 -3 T*/ }T
\ TODO T{ MAX-INT 2 MAX-INT */ -> MAX-INT 2 MAX-INT T*/ }T
\ TODO T{ MIN-INT 2 MIN-INT */ -> MIN-INT 2 MIN-INT T*/ }T
\ TODO
\ TODO T{ 0 2 1 */MOD -> 0 2 1 T*/MOD }T
\ TODO T{ 1 2 1 */MOD -> 1 2 1 T*/MOD }T
\ TODO T{ 2 2 1 */MOD -> 2 2 1 T*/MOD }T
\ TODO T{ -1 2 1 */MOD -> -1 2 1 T*/MOD }T
\ TODO T{ -2 2 1 */MOD -> -2 2 1 T*/MOD }T
\ TODO T{ 0 2 -1 */MOD -> 0 2 -1 T*/MOD }T
\ TODO T{ 1 2 -1 */MOD -> 1 2 -1 T*/MOD }T
\ TODO T{ 2 2 -1 */MOD -> 2 2 -1 T*/MOD }T
\ TODO T{ -1 2 -1 */MOD -> -1 2 -1 T*/MOD }T
\ TODO T{ -2 2 -1 */MOD -> -2 2 -1 T*/MOD }T
\ TODO T{ 2 2 2 */MOD -> 2 2 2 T*/MOD }T
\ TODO T{ -1 2 -1 */MOD -> -1 2 -1 T*/MOD }T
\ TODO T{ -2 2 -2 */MOD -> -2 2 -2 T*/MOD }T
\ TODO T{ 7 2 3 */MOD -> 7 2 3 T*/MOD }T
\ TODO T{ 7 2 -3 */MOD -> 7 2 -3 T*/MOD }T
\ TODO T{ -7 2 3 */MOD -> -7 2 3 T*/MOD }T
\ TODO T{ -7 2 -3 */MOD -> -7 2 -3 T*/MOD }T
\ TODO T{ MAX-INT 2 MAX-INT */MOD -> MAX-INT 2 MAX-INT T*/MOD }T
\ TODO T{ MIN-INT 2 MIN-INT */MOD -> MIN-INT 2 MIN-INT T*/MOD }T
T{ 0 1 /MOD -> 0 1 T/MOD }T
T{ 1 1 /MOD -> 1 1 T/MOD }T
T{ 2 1 /MOD -> 2 1 T/MOD }T
T{ -1 1 /MOD -> -1 1 T/MOD }T
T{ -2 1 /MOD -> -2 1 T/MOD }T
T{ 0 -1 /MOD -> 0 -1 T/MOD }T
T{ 1 -1 /MOD -> 1 -1 T/MOD }T
T{ 2 -1 /MOD -> 2 -1 T/MOD }T
T{ -1 -1 /MOD -> -1 -1 T/MOD }T
T{ -2 -1 /MOD -> -2 -1 T/MOD }T
T{ 2 2 /MOD -> 2 2 T/MOD }T
T{ -1 -1 /MOD -> -1 -1 T/MOD }T
T{ -2 -2 /MOD -> -2 -2 T/MOD }T
T{ 7 3 /MOD -> 7 3 T/MOD }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 1 T*/MOD }T
T{ -2 2 1 */MOD -> -2 2 1 T*/MOD }T
T{ 0 2 -1 */MOD -> 0 2 -1 T*/MOD }T
T{ 1 2 -1 */MOD -> 1 2 -1 T*/MOD }T
T{ 2 2 -1 */MOD -> 2 2 -1 T*/MOD }T
T{ -1 2 -1 */MOD -> -1 2 -1 T*/MOD }T
T{ -2 2 -1 */MOD -> -2 2 -1 T*/MOD }T
T{ 2 2 2 */MOD -> 2 2 2 T*/MOD }T
T{ -1 2 -1 */MOD -> -1 2 -1 T*/MOD }T
T{ -2 2 -2 */MOD -> -2 2 -2 T*/MOD }T
T{ 7 2 3 */MOD -> 7 2 3 T*/MOD }T
T{ 7 2 -3 */MOD -> 7 2 -3 T*/MOD }T
T{ -7 2 3 */MOD -> -7 2 3 T*/MOD }T
T{ -7 2 -3 */MOD -> -7 2 -3 T*/MOD }T
T{ MAX-INT 2 MAX-INT */MOD -> MAX-INT 2 MAX-INT T*/MOD }T
T{ MIN-INT 2 MIN-INT */MOD -> MIN-INT 2 MIN-INT T*/MOD }T
\ ------------------------------------------------------------------------
TESTING HERE , @ ! CELL+ CELLS C, C@ C! CHARS 2@ 2! ALIGN ALIGNED +! ALLOT

View file

@ -1044,12 +1044,25 @@
(local $btos i32)
(local $bbbtos i32)
(local $n1 i64)
(local $n2 i32)
(i32.store (local.tee $bbbtos (i32.sub (local.get $tos) (i32.const 12)))
(i32.wrap_i64 (i64.rem_s (local.tee $n1 (i64.load (local.get $bbbtos)))
(i64.extend_i32_s (local.tee $n2 (i32.load (local.tee $btos (i32.sub (local.get $tos) (i32.const 4)))))))))
(i32.store (i32.sub (local.get $tos) (i32.const 8))
(i32.wrap_i64 (i64.div_s (local.get $n1) (i64.extend_i32_s (local.get $n2)))))
(local $n2 i64)
(local $n2_32 i32)
(local $q i32)
(local $mod i32)
(local.set $mod
(i32.wrap_i64
(i64.rem_s
(local.tee $n1 (i64.load (local.tee $bbbtos (i32.sub (local.get $tos) (i32.const 12)))))
(local.tee $n2 (i64.extend_i32_s (local.tee $n2_32 (i32.load (local.tee $btos (i32.sub (local.get $tos) (i32.const 4))))))))))
(local.set $q (i32.wrap_i64 (i64.div_s (local.get $n1) (local.get $n2))))
(if
(i32.and
(i32.ne (local.get $mod (i32.const 0)))
(i64.lt_s (i64.xor (local.get $n1) (local.get $n2)) (i64.const 0)))
(then
(local.set $q (i32.sub (local.get $q) (i32.const 1)))
(local.set $mod (i32.add (local.get $mod) (local.get $n2_32)))))
(i32.store (local.get $bbbtos) (local.get $mod))
(i32.store (i32.sub (local.get $tos) (i32.const 8)) (local.get $q))
(local.get $btos))
(data (i32.const 136268) "<\14\02\00\06FM/MOD\00^\00\00\00")
(elem (i32.const 0x5e) $FM/MOD)
@ -1119,7 +1132,6 @@
(data (i32.const 136400) "\c4\14\02\00\85LEAVE\00\00g\00\00\00")
(elem (i32.const 0x67) $LEAVE) ;; immediate
;; 6.1.1780
(func $LITERAL (param $tos i32) (result i32)
(local.get $tos)
@ -1357,13 +1369,30 @@
(data (i32.const 136704) "\f4\15\02\00\03S>D|\00\00\00")
(elem (i32.const 0x7c) $S>D)
;; 6.1.2210
(func $SIGN (param $tos i32) (result i32)
(call $fail (local.get $tos) (i32.const 0x20084))) ;; not implemented
(data (i32.const 136716) "\00\16\02\00\04SIGN\00\00\00}\00\00\00")
(elem (i32.const 0x7d) $SIGN)
;; 6.1.2214
;; See e.g. https://www.nimblemachines.com/symmetric-division-considered-harmful/
(func $SM/REM (param $tos i32) (result i32)
(call $fail (local.get $tos) (i32.const 0x20084))) ;; not implemented
(local $btos i32)
(local $bbbtos i32)
(local $n1 i64)
(local $n2 i64)
(i32.store
(local.tee $bbbtos (i32.sub (local.get $tos) (i32.const 12)))
(i32.wrap_i64
(i64.rem_s
(local.tee $n1 (i64.load (local.get $bbbtos)))
(local.tee $n2 (i64.extend_i32_s (i32.load (local.tee $btos (i32.sub (local.get $tos) (i32.const 4)))))))))
(i32.store
(i32.sub (local.get $tos) (i32.const 8))
(i32.wrap_i64
(i64.div_s (local.get $n1) (local.get $n2))))
(local.get $btos))
(data (i32.const 136732) "\0c\16\02\00\06SM/REM\00~\00\00\00")
(elem (i32.const 0x7e) $SM/REM)
@ -1472,8 +1501,23 @@
(data (i32.const 136884) "\a8\16\02\00\03UM*\88\00\00\00")
(elem (i32.const 0x88) $UM*)
;; 6.1.2370
(func $UM/MOD (param $tos i32) (result i32)
(call $fail (local.get $tos) (i32.const 0x20084))) ;; not implemented
(local $btos i32)
(local $bbbtos i32)
(local $n1 i64)
(local $n2 i64)
(i32.store
(local.tee $bbbtos (i32.sub (local.get $tos) (i32.const 12)))
(i32.wrap_i64
(i64.rem_u
(local.tee $n1 (i64.load (local.get $bbbtos)))
(local.tee $n2 (i64.extend_i32_u (i32.load (local.tee $btos (i32.sub (local.get $tos) (i32.const 4)))))))))
(i32.store
(i32.sub (local.get $tos) (i32.const 8))
(i32.wrap_i64
(i64.div_u (local.get $n1) (local.get $n2))))
(local.get $btos))
(data (i32.const 136896) "\b4\16\02\00\06UM/MOD\00\89\00\00\00")
(elem (i32.const 0x89) $UM/MOD) ;; TODO: Rename

View file

@ -368,6 +368,30 @@ function loadTests() {
});
});
describe("FM/MOD", () => {
it("should work", () => {
run("7 S>D 3 FM/MOD");
expect(stackValues()).to.eql([1, 2]);
});
it("should work with negative divisor", () => {
run("7 S>D -3 FM/MOD");
expect(stackValues()).to.eql([-2, -3]);
});
it("should work with large dividend", () => {
run("0 INVERT 1 4 FM/MOD");
expect(stackValues()).to.eql([3, 2147483647]);
});
});
describe("UM/MOD", () => {
it("should work", () => {
run("1 0 2 UM/MOD");
expect(stackValues()).to.eql([1, 0]);
});
});
describe("*/", () => {
it("should work with small numbers", () => {
run("10 3 5 */ 5");