From c5453432f390b06866000604176e240a16701f09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Fri, 1 Jun 2018 23:05:28 +0200 Subject: [PATCH] Implement */MOD --- src/waforth.wat | 17 +++++++++++++++++ tests/index.js | 19 +++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/waforth.wat b/src/waforth.wat index 9a1d000..3964857 100644 --- a/src/waforth.wat +++ b/src/waforth.wat @@ -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) diff --git a/tests/index.js b/tests/index.js index 8a5bf8e..e015eb3 100644 --- a/tests/index.js +++ b/tests/index.js @@ -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); });