Implement */MOD

This commit is contained in:
Remko Tronçon 2018-06-01 23:05:28 +02:00
parent 8140811ead
commit c5453432f3
2 changed files with 34 additions and 2 deletions

View file

@ -194,6 +194,23 @@
(set_global $tos (get_local $bbtos)))
(!def_word "*/" "$*/")
;; 6.1.0110
(func $*/MOD (param i32)
(local $btos i32)
(local $bbtos i32)
(local $bbbtos i32)
(local $x1 i64)
(local $x2 i64)
(i32.store (tee_local $bbbtos (i32.sub (get_global $tos) (i32.const 12)))
(i32.wrap/i64
(i64.rem_s
(tee_local $x1 (i64.mul (i64.extend_s/i32 (i32.load (get_local $bbbtos)))
(i64.extend_s/i32 (i32.load (tee_local $bbtos (i32.sub (get_global $tos) (i32.const 8)))))))
(tee_local $x2 (i64.extend_s/i32 (i32.load (tee_local $btos (i32.sub (get_global $tos) (i32.const 4)))))))))
(i32.store (get_local $bbtos) (i32.wrap/i64 (i64.div_s (get_local $x1) (get_local $x2))))
(set_global $tos (get_local $btos)))
(!def_word "*/MOD" "$*/MOD")
;; 6.1.0120
(func $plus (param i32)
(local $btos i32)

View file

@ -306,6 +306,21 @@ describe("WAForth", () => {
});
});
describe("*/MOD", () => {
it("should work with small numbers", () => {
run("9 3 5 */MOD 7");
expect(stack[0]).to.eql(2);
expect(stack[1]).to.eql(5);
expect(stack[2]).to.eql(7);
});
it("should work with large numbers", () => {
run("268435455 1000 3333 */MOD");
expect(stack[0]).to.eql(1230);
expect(stack[1]).to.eql(80538690);
});
});
describe("1+", () => {
it("should work with positive numbers", () => {
run("3");
@ -796,7 +811,7 @@ describe("WAForth", () => {
forth.read("DUP");
core.WORD();
core.FIND();
expect(stack[0]).to.eql(131740);
expect(stack[0]).to.eql(131756);
expect(stack[1]).to.eql(-1);
});
@ -812,7 +827,7 @@ describe("WAForth", () => {
forth.read("+LOOP");
core.WORD();
core.FIND();
expect(stack[0]).to.eql(131144);
expect(stack[0]).to.eql(131160);
expect(stack[1]).to.eql(1);
});