From c7fd22a6c3c95021f56894f768bffc26c6398ae7 Mon Sep 17 00:00:00 2001 From: tjjfvi Date: Tue, 21 May 2024 15:52:07 -0400 Subject: [PATCH 1/7] add test suite --- Cargo.lock | 197 +++++++++++++++++- Cargo.toml | 3 + examples/sort_bitonic/main.bend | 2 +- examples/sort_bitonic/main.hvm | 6 +- examples/sort_radix/main.hvm | 2 +- examples/stress/main.hvm | 2 +- examples/sum_rec/main.bend | 2 +- examples/sum_rec/main.hvm | 4 +- examples/sum_tree/main.hvm | 6 +- rust-toolchain.toml | 4 + src/hvm.c | 30 +-- src/hvm.cu | 30 +-- tests/programs/f24.hvm | 40 ++++ tests/programs/i24.hvm | 29 +++ tests/programs/u24.hvm | 28 +++ tests/run.rs | 89 ++++++++ .../run__file@demo_io__main.hvm.snap | 7 + tests/snapshots/run__file@f24.hvm.snap | 7 + tests/snapshots/run__file@i24.hvm.snap | 7 + .../run__file@sort_bitonic__main.hvm.snap | 7 + .../run__file@sort_radix__main.hvm.snap | 7 + .../snapshots/run__file@stress__main.hvm.snap | 7 + .../run__file@sum_rec__main.hvm.snap | 7 + .../run__file@sum_tree__main.hvm.snap | 7 + .../run__file@tuples__tuples.hvm.snap | 7 + tests/snapshots/run__file@u24.hvm.snap | 7 + 26 files changed, 501 insertions(+), 43 deletions(-) create mode 100644 rust-toolchain.toml create mode 100644 tests/programs/f24.hvm create mode 100644 tests/programs/i24.hvm create mode 100644 tests/programs/u24.hvm create mode 100644 tests/run.rs create mode 100644 tests/snapshots/run__file@demo_io__main.hvm.snap create mode 100644 tests/snapshots/run__file@f24.hvm.snap create mode 100644 tests/snapshots/run__file@i24.hvm.snap create mode 100644 tests/snapshots/run__file@sort_bitonic__main.hvm.snap create mode 100644 tests/snapshots/run__file@sort_radix__main.hvm.snap create mode 100644 tests/snapshots/run__file@stress__main.hvm.snap create mode 100644 tests/snapshots/run__file@sum_rec__main.hvm.snap create mode 100644 tests/snapshots/run__file@sum_tree__main.hvm.snap create mode 100644 tests/snapshots/run__file@tuples__tuples.hvm.snap create mode 100644 tests/snapshots/run__file@u24.hvm.snap diff --git a/Cargo.lock b/Cargo.lock index e583c786..4d782dc0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,15 @@ dependencies = [ "highlight_error", ] +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "anstream" version = "0.6.14" @@ -60,6 +69,16 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "bstr" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05efc5cfd9110c8416e471df0e96702d58690178e206e61b7173706673c93706" +dependencies = [ + "memchr", + "serde", +] + [[package]] name = "cc" version = "1.0.97" @@ -99,6 +118,37 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" +[[package]] +name = "console" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "windows-sys", +] + +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + +[[package]] +name = "globset" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57da3b9b5b85bd66f31093f8c408b90a74431672542466497dcbdfdc02034be1" +dependencies = [ + "aho-corasick", + "bstr", + "log", + "regex-automata", + "regex-syntax", +] + [[package]] name = "hermit-abi" version = "0.3.9" @@ -113,27 +163,66 @@ checksum = "809e18805660d7b6b2e2b9f316a5099521b5998d5cba4dda11b5157a21aaef03" [[package]] name = "hvm" -version = "2.0.13" +version = "2.0.14" dependencies = [ "TSPL", "cc", "clap", "highlight_error", + "insta", "num_cpus", ] +[[package]] +name = "insta" +version = "1.39.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "810ae6042d48e2c9e9215043563a58a80b877bc863228a74cf10c49d4620a6f5" +dependencies = [ + "console", + "globset", + "lazy_static", + "linked-hash-map", + "similar", + "walkdir", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + +[[package]] +name = "log" +version = "0.4.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" + +[[package]] +name = "memchr" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" + [[package]] name = "num_cpus" version = "1.16.0" @@ -144,18 +233,124 @@ dependencies = [ "libc", ] +[[package]] +name = "proc-macro2" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ad3d49ab951a01fbaafe34f2ec74122942fe18a3f9814c3268f1bb72042131b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex-automata" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "serde" +version = "1.0.200" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddc6f9cc94d67c0e21aaf7eda3a010fd3af78ebf6e096aa6e2e13c79749cce4f" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.200" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "856f046b9400cee3c8c94ed572ecdb752444c24528c035cd35882aad6f492bcb" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "similar" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa42c91313f1d05da9b26f267f931cf178d4aba455b4c4622dd7355eb80c6640" + [[package]] name = "strsim" version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" +[[package]] +name = "syn" +version = "2.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ad3dee41f36859875573074334c200d1add8e4a87bb37113ebd31d926b7b11f" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + [[package]] name = "utf8parse" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "winapi-util" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" +dependencies = [ + "windows-sys", +] + [[package]] name = "windows-sys" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index ac77fd41..bc4375d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,3 +25,6 @@ default = [] # C and CUDA features are determined during build c = [] cuda = [] + +[dev-dependencies] +insta = { version = "1.39.0", features = ["glob"] } diff --git a/examples/sort_bitonic/main.bend b/examples/sort_bitonic/main.bend index 061ee323..d9197523 100644 --- a/examples/sort_bitonic/main.bend +++ b/examples/sort_bitonic/main.bend @@ -56,4 +56,4 @@ def sort(d, s, t): return flow(d, s, (sort(d-1, 0, t.a), sort(d-1, 1, t.b))) def main: - return sum(20, sort(20, 0, gen(20, 0))) + return sum(16, sort(16, 0, gen(16, 0))) diff --git a/examples/sort_bitonic/main.hvm b/examples/sort_bitonic/main.hvm index c93e3b2e..1a1a914b 100644 --- a/examples/sort_bitonic/main.hvm +++ b/examples/sort_bitonic/main.hvm @@ -17,13 +17,13 @@ &! @gen ~ (d (e f)) @main = a - & @sum ~ (18 (@main__C1 a)) + & @sum ~ (16 (@main__C1 a)) @main__C0 = a - & @gen ~ (18 (0 a)) + & @gen ~ (16 (0 a)) @main__C1 = a - & @sort ~ (18 (0 (@main__C0 a))) + & @sort ~ (16 (0 (@main__C0 a))) @sort = (?(((a (* a)) @sort__C0) (b (c d))) (c (b d))) diff --git a/examples/sort_radix/main.hvm b/examples/sort_radix/main.hvm index 820b51e1..39af5a7d 100644 --- a/examples/sort_radix/main.hvm +++ b/examples/sort_radix/main.hvm @@ -23,7 +23,7 @@ @main = c & @sum ~ (b c) & @sort ~ (a b) - & @gen ~ (20 (0 a)) + & @gen ~ (16 (0 a)) @merge = ((@merge$C5 (@merge$C3 (@merge$C2 a))) a) diff --git a/examples/stress/main.hvm b/examples/stress/main.hvm index 28a2584e..c575faf6 100644 --- a/examples/stress/main.hvm +++ b/examples/stress/main.hvm @@ -13,4 +13,4 @@ & @loop ~ a @main = a - & @fun ~ (18 a) + & @fun ~ (10 a) diff --git a/examples/sum_rec/main.bend b/examples/sum_rec/main.bend index 6f7f8a58..d7f33174 100644 --- a/examples/sum_rec/main.bend +++ b/examples/sum_rec/main.bend @@ -7,4 +7,4 @@ sum = λn λx switch n { (+ fst snd) } -main = (sum 30 0) +main = (sum 24 0) diff --git a/examples/sum_rec/main.hvm b/examples/sum_rec/main.hvm index fcf8b14b..c0b4fb10 100644 --- a/examples/sum_rec/main.hvm +++ b/examples/sum_rec/main.hvm @@ -1,8 +1,8 @@ @main = a - & @sum ~ (28 (0 a)) + & @sum ~ (24 (0 a)) @sum = (?(((a a) @sum__C0) b) b) @sum__C0 = ({c a} ({$([*2] $([+1] d)) $([*2] $([+0] b))} f)) - &! @sum ~ (a (b $(:[+] $(e f)))) + &! @sum ~ (a (b $([+] $(e f)))) &! @sum ~ (c (d e)) diff --git a/examples/sum_tree/main.hvm b/examples/sum_tree/main.hvm index bffd8a0a..c7203304 100644 --- a/examples/sum_tree/main.hvm +++ b/examples/sum_tree/main.hvm @@ -5,13 +5,13 @@ &!@gen ~ (d (e f)) @main = a - & @sum ~ (26 (@main__C0 a)) + & @sum ~ (20 (@main__C0 a)) @main__C0 = a - & @gen ~ (26 (0 a)) + & @gen ~ (20 (0 a)) @sum = (?(((* 1) @sum__C0) a) a) @sum__C0 = ({a c} ((b d) f)) - &!@sum ~ (a (b $(:[+] $(e f)))) + &!@sum ~ (a (b $([+] $(e f)))) &!@sum ~ (c (d e)) diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 00000000..2e4d1842 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,4 @@ +[toolchain] +profile = "minimal" +channel = "nightly-2024-01-27" +components = ["rustfmt", "clippy"] diff --git a/src/hvm.c b/src/hvm.c index 350b8619..e095cd1e 100644 --- a/src/hvm.c +++ b/src/hvm.c @@ -397,11 +397,11 @@ static inline u32 get_u24(Numb word) { // Constructor and getters for I24 (signed 24-bit integer) static inline Numb new_i24(i32 val) { - return (((u32)val & 0xFFFFFF) << 4) | TY_I24; + return ((u32)val << 5) | TY_I24; } static inline i32 get_i24(Numb word) { - return ((i32)((word >> 4) & 0xFFFFFF)) << 8 >> 8; + return ((i32)word) << 3 >> 8; } // Constructor and getters for F24 (24-bit float) @@ -1804,24 +1804,24 @@ void pretty_print_port(Net* net, Book* book, Port port) { u32 num = 0; while (len > 0) { Port cur = stack[--len]; - if (cur > 0xFFFFFF00) { - printf("%c", (char)(cur&0xFF)); - continue; - } switch (get_tag(cur)) { case CON: { Pair node = node_load(net,get_val(cur)); Port p2 = get_snd(node); Port p1 = get_fst(node); printf("("); - stack[len++] = (0xFFFFFF00) | (u32)(')'); + stack[len++] = new_port(ERA, (u32)(')')); stack[len++] = p2; - stack[len++] = (0xFFFFFF00) | (u32)(' '); + stack[len++] = new_port(ERA, (u32)(' ')); stack[len++] = p1; break; } case ERA: { - printf("*"); + if (get_val(cur) != 0) { + printf("%c", (char)get_val(cur)); + } else { + printf("*"); + } break; } case VAR: { @@ -1842,9 +1842,9 @@ void pretty_print_port(Net* net, Book* book, Port port) { Port p2 = get_snd(node); Port p1 = get_fst(node); printf("{"); - stack[len++] = (0xFFFFFF00) | (u32)('}'); + stack[len++] = new_port(ERA, (u32)('}')); stack[len++] = p2; - stack[len++] = (0xFFFFFF00) | (u32)(' '); + stack[len++] = new_port(ERA, (u32)(' ')); stack[len++] = p1; break; } @@ -1853,9 +1853,9 @@ void pretty_print_port(Net* net, Book* book, Port port) { Port p2 = get_snd(node); Port p1 = get_fst(node); printf("$("); - stack[len++] = (0xFFFFFF00) | (u32)(')'); + stack[len++] = new_port(ERA, (u32)(')')); stack[len++] = p2; - stack[len++] = (0xFFFFFF00) | (u32)(' '); + stack[len++] = new_port(ERA, (u32)(' ')); stack[len++] = p1; break; } @@ -1864,9 +1864,9 @@ void pretty_print_port(Net* net, Book* book, Port port) { Port p2 = get_snd(node); Port p1 = get_fst(node); printf("?("); - stack[len++] = (0xFFFFFF00) | (u32)(')'); + stack[len++] = new_port(ERA, (u32)(')')); stack[len++] = p2; - stack[len++] = (0xFFFFFF00) | (u32)(' '); + stack[len++] = new_port(ERA, (u32)(' ')); stack[len++] = p1; break; } diff --git a/src/hvm.cu b/src/hvm.cu index eaae0064..81ec0cb5 100644 --- a/src/hvm.cu +++ b/src/hvm.cu @@ -487,11 +487,11 @@ __device__ __host__ inline u32 get_u24(Numb word) { // Constructor and getters for I24 (signed 24-bit integer) __device__ __host__ inline Numb new_i24(i32 val) { - return (((u32)val & 0xFFFFFF) << 4) | TY_I24; + return ((u32)val << 5) | TY_I24; } __device__ __host__ inline i32 get_i24(Numb word) { - return ((i32)((word >> 4) & 0xFFFFFF)) << 8 >> 8; + return ((i32)word) << 3 >> 8; } // Constructor and getters for F24 (24-bit float) @@ -2137,24 +2137,24 @@ __device__ void pretty_print_port(Net* net, Port port) { continue; } Port cur = stack[--len]; - if (cur > 0xFFFFFF00) { - printf("%c", (char)(cur&0xFF)); - continue; - } switch (get_tag(cur)) { case CON: { Pair node = node_load(net,get_val(cur)); Port p2 = get_snd(node); Port p1 = get_fst(node); printf("("); - stack[len++] = (0xFFFFFF00) | (u32)(')'); + stack[len++] = new_port(ERA, (u32)(')')); stack[len++] = p2; - stack[len++] = (0xFFFFFF00) | (u32)(' '); + stack[len++] = new_port(ERA, (u32)(' ')); stack[len++] = p1; break; } case ERA: { - printf("*"); + if (get_val(cur) != 0) { + printf("%c", (char)get_val(cur)); + } else { + printf("*"); + } break; } case VAR: { @@ -2175,9 +2175,9 @@ __device__ void pretty_print_port(Net* net, Port port) { Port p2 = get_snd(node); Port p1 = get_fst(node); printf("{"); - stack[len++] = (0xFFFFFF00) | (u32)('}'); + stack[len++] = new_port(ERA, (u32)('}')); stack[len++] = p2; - stack[len++] = (0xFFFFFF00) | (u32)(' '); + stack[len++] = new_port(ERA, (u32)(' ')); stack[len++] = p1; break; } @@ -2186,9 +2186,9 @@ __device__ void pretty_print_port(Net* net, Port port) { Port p2 = get_snd(node); Port p1 = get_fst(node); printf("$("); - stack[len++] = (0xFFFFFF00) | (u32)(')'); + stack[len++] = new_port(ERA, (u32)(')')); stack[len++] = p2; - stack[len++] = (0xFFFFFF00) | (u32)(' '); + stack[len++] = new_port(ERA, (u32)(' ')); stack[len++] = p1; break; } @@ -2197,9 +2197,9 @@ __device__ void pretty_print_port(Net* net, Port port) { Port p2 = get_snd(node); Port p1 = get_fst(node); printf("?("); - stack[len++] = (0xFFFFFF00) | (u32)(')'); + stack[len++] = new_port(ERA, (u32)(')')); stack[len++] = p2; - stack[len++] = (0xFFFFFF00) | (u32)(' '); + stack[len++] = new_port(ERA, (u32)(' ')); stack[len++] = p1; break; } diff --git a/tests/programs/f24.hvm b/tests/programs/f24.hvm new file mode 100644 index 00000000..38a8bde6 --- /dev/null +++ b/tests/programs/f24.hvm @@ -0,0 +1,40 @@ +@half = xN & 1.0 ~ $([/] $(2.0 xN)) +@nan = xN & 0.0 ~ $([/] $(0.0 xN)) + +@main = x0 + // nan and inf divisions + & x0 ~ {n0 x1} & 1.0 ~ $([/] $(0.0 n0)) // +inf + & x1 ~ {n1 x2} & -1.0 ~ $([/] $(0.0 n1)) // -inf + & x2 ~ {n2 x3} & 0.0 ~ $([/] $(0.0 n2)) // NaN + + // general operators + & x3 ~ {n3 x4} & @half ~ $([+] $(2.0 n3)) // 2.5 + & x4 ~ {n4 x5} & @half ~ $([-] $(2.0 n4)) // -1.5 + & x5 ~ {n5 x6} & @half ~ $([*] $(2.3 n5)) // 1.15 + & x6 ~ {n6 x7} & @half ~ $([/] $(2.0 n6)) // 0.25 + & x7 ~ {n7 x8} & @half ~ $([%] $(2.0 n7)) // 0.5 + + // comparisons (returning ints) + & x8 ~ {n8 x9} & @half ~ $([=] $(2.0 n8)) // 0 + & x9 ~ {n9 xA} & @half ~ $([!] $(2.0 n9)) // 1 + & xA ~ {nA xB} & @half ~ $([<] $(2.0 nA)) // 1 + & xB ~ {nB xC} & @half ~ $([>] $(2.0 nB)) // 0 + + // ieee nan comparisons + & xC ~ {nC xD} & @nan ~ $([=] $(@nan nC)) // 0 + & xD ~ {nD xE} & @nan ~ $([<] $(@nan nD)) // 0 + & xE ~ {nE xF} & @nan ~ $([>] $(@nan nE)) // 0 + + // parsing + & xF ~ {nF xG} & +NaN ~ $([+] $(0.0 nF)) // NaN + & xG ~ {nG xH} & +inf ~ $([+] $(0.0 nG)) // inf + & xH ~ {nH xI} & -inf ~ $([+] $(0.0 nH)) // -inf + & xI ~ {nI xJ} & 1.02 ~ $([+] $(0.0 nI)) // 1.02 + + // modulo + & xJ ~ {nJ xK} & +1.2 ~ $([%] $(+1.1 nJ)) // +0.1 + & xK ~ {nK xL} & +1.2 ~ $([%] $(-1.1 nK)) // +0.1 + & xL ~ {nL xM} & -1.2 ~ $([%] $(+1.1 nL)) // -0.1 + & xM ~ {nM xN} & -1.2 ~ $([%] $(-1.1 nM)) // -0.1 + + & xN ~ * diff --git a/tests/programs/i24.hvm b/tests/programs/i24.hvm new file mode 100644 index 00000000..57778ce5 --- /dev/null +++ b/tests/programs/i24.hvm @@ -0,0 +1,29 @@ +@main = x0 + // all ops + & x0 ~ {n0 x1} & +10 ~ $([+] $(+2 n0)) // 12 + & x1 ~ {n1 x2} & +10 ~ $([-] $(+2 n1)) // 8 + & x2 ~ {n2 x3} & +10 ~ $([*] $(+2 n2)) // 20 + & x3 ~ {n3 x4} & +10 ~ $([/] $(+2 n3)) // 5 + & x4 ~ {n4 x5} & +10 ~ $([%] $(+2 n4)) // 0 + & x5 ~ {n5 x6} & +10 ~ $([=] $(+2 n5)) // 0 + & x6 ~ {n6 x7} & +10 ~ $([!] $(+2 n6)) // 1 + & x7 ~ {n7 x8} & +10 ~ $([<] $(+2 n7)) // 0 + & x8 ~ {n8 x9} & +10 ~ $([>] $(+2 n8)) // 1 + & x9 ~ {n9 xA} & +10 ~ $([&] $(+2 n9)) // 2 + & xA ~ {nA xB} & +10 ~ $([|] $(+2 nA)) // 10 + & xB ~ {nB xC} & +10 ~ $([^] $(+2 nB)) // 8 + + // underflow + & xC ~ {nC xD} & -8388608 ~ $([-] $(+1 nC)) // 8388607 + + // overflow + & xD ~ {nD xE} & +8388607 ~ $([+] $(+1 nD)) // -8388608 + + // modulo + & xE ~ {nE xF} & +3 ~ $([%] $(+2 nE)) // +1 + & xF ~ {nF xG} & +3 ~ $([%] $(-2 nF)) // +1 + & xG ~ {nG xH} & -3 ~ $([%] $(+2 nG)) // -1 + & xH ~ {nH xI} & -3 ~ $([%] $(-2 nH)) // -1 + + & xI ~ * + diff --git a/tests/programs/u24.hvm b/tests/programs/u24.hvm new file mode 100644 index 00000000..1c8ecfbb --- /dev/null +++ b/tests/programs/u24.hvm @@ -0,0 +1,28 @@ +@main = x0 + // all ops + & x0 ~ {n0 x1} & 10 ~ $([+] $(2 n0)) // 12 + & x1 ~ {n1 x2} & 10 ~ $([-] $(2 n1)) // 8 + & x2 ~ {n2 x3} & 10 ~ $([*] $(2 n2)) // 20 + & x3 ~ {n3 x4} & 10 ~ $([/] $(2 n3)) // 5 + & x4 ~ {n4 x5} & 10 ~ $([%] $(2 n4)) // 0 + & x5 ~ {n5 x6} & 10 ~ $([=] $(2 n5)) // 0 + & x6 ~ {n6 x7} & 10 ~ $([!] $(2 n6)) // 1 + & x7 ~ {n7 x8} & 10 ~ $([<] $(2 n7)) // 0 + & x8 ~ {n8 x9} & 10 ~ $([>] $(2 n8)) // 1 + & x9 ~ {n9 xA} & 10 ~ $([&] $(2 n9)) // 2 + & xA ~ {nA xB} & 10 ~ $([|] $(2 nA)) // 10 + & xB ~ {nB xC} & 10 ~ $([^] $(2 nB)) // 8 + & xC ~ {nC xD} & 10 ~ $([<<] $(2 nC)) // 40 + & xD ~ {nD xE} & 10 ~ $([>>] $(2 nD)) // 2 + + // underflow + & xE ~ {nE xF} & 0 ~ $([-] $(1 nE)) // 16777215 + + // overflow + & xF ~ {nF xG} & 16777215 ~ $([+] $(1 nF)) // 0 + + // no sign extension + & xG ~ {nG xH} & 16777215 ~ $([>>] $(22 nG)) // 3 + + & xH ~ * + diff --git a/tests/run.rs b/tests/run.rs new file mode 100644 index 00000000..0e43bd18 --- /dev/null +++ b/tests/run.rs @@ -0,0 +1,89 @@ + +use hvm::ast::Tree; +use insta::assert_snapshot; +use TSPL::Parser; +use std::{ + collections::HashMap, error::Error, ffi::OsStr, io::Read, path::{Path, PathBuf}, process::{Command, Stdio} +}; + +#[test] +fn test_run_programs() { + test_dir(&manifest_relative("tests/programs/")); +} + +#[test] +fn test_run_examples() { + test_dir(&manifest_relative("examples/")); +} + +fn test_dir(dir: &Path) { + insta::glob!(dir, "**/*.hvm", test_file) +} + +fn manifest_relative(sub: &str) -> PathBuf { + format!("{}/{}", env!("CARGO_MANIFEST_DIR"), sub).into() +} + +fn test_file(path: &Path) { + println!("testing {path:?}..."); + let rust_output = execute_hvm(&["run".as_ref(), path.as_os_str()]).unwrap(); + assert_snapshot!(rust_output); + println!(" testing {path:?}, C..."); + let c_output = execute_hvm(&["run-c".as_ref(), path.as_os_str()]).unwrap(); + assert_eq!(c_output, rust_output, "{path:?}: C output does not match rust output"); + if cfg!(feature = "cuda") { + println!(" testing {path:?}, CUDA..."); + let cuda_output = execute_hvm(&["run-cu".as_ref(), path.as_os_str()]).unwrap(); + assert_eq!(cuda_output, rust_output, "{path:?}: CUDA output does not match rust output"); + } +} + +fn execute_hvm(args: &[&OsStr]) -> Result> { + // Spawn the command + let mut child = + Command::new(env!("CARGO_BIN_EXE_hvm")).args(args).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn()?; + + // Capture the output of the command + let mut stdout = child.stdout.take().ok_or("Couldn't capture stdout!")?; + let mut stderr = child.stderr.take().ok_or("Couldn't capture stderr!")?; + + // Wait for the command to finish and get the exit status + let status = child.wait()?; + + // Read the output + let mut output = String::new(); + stdout.read_to_string(&mut output)?; + stderr.read_to_string(&mut output)?; + + Ok(if !status.success() { + format!("exited with code {status}:\n{output}") + } else { + parse_output(&output).unwrap_or_else(|err| { + panic!("error parsing output:\n{err}\n\n{output}") + }) + }) +} + +fn parse_output(output: &str) -> Result { + let mut parser = hvm::ast::CoreParser::new(output); + parser.consume("Result:")?; + let mut tree = parser.parse_tree()?; + parser.consume("- ITRS:")?; + let itrs = parser.parse_u64()?; + normalize_vars(&mut tree, &mut HashMap::new()); + Ok(format!("Result: {}\n- ITRS: {}", tree.show(), itrs)) +} + +fn normalize_vars(tree: &mut Tree, vars: &mut HashMap) { + match tree { + Tree::Var { nam } => { + let next_var = vars.len(); + *nam = format!("x{}", vars.entry(std::mem::take(nam)).or_insert(next_var)); + }, + Tree::Era | Tree::Ref { .. } | Tree::Num { .. } => {} + Tree::Con { fst, snd } | Tree::Dup { fst, snd } | Tree::Opr { fst, snd } | Tree::Swi { fst, snd } => { + normalize_vars(fst, vars); + normalize_vars(snd, vars); + } + } +} diff --git a/tests/snapshots/run__file@demo_io__main.hvm.snap b/tests/snapshots/run__file@demo_io__main.hvm.snap new file mode 100644 index 00000000..c14e7b63 --- /dev/null +++ b/tests/snapshots/run__file@demo_io__main.hvm.snap @@ -0,0 +1,7 @@ +--- +source: tests/run.rs +expression: rust_output +input_file: examples/demo_io/main.hvm +--- +Result: ((@PUTTEXT (@Main__C2 ((* ((@PUTTEXT (@Main__C0 ((* ((@DRAWIMAGE (((16711680 65280) (255 16711935)) ((* ((@SLEEP ((119 3511296) ((* ((@DONE (42 x0)) x0)) x1))) x1)) x2))) x2)) x3))) x3)) x4))) x4) +- ITRS: 120 diff --git a/tests/snapshots/run__file@f24.hvm.snap b/tests/snapshots/run__file@f24.hvm.snap new file mode 100644 index 00000000..0392c6af --- /dev/null +++ b/tests/snapshots/run__file@f24.hvm.snap @@ -0,0 +1,7 @@ +--- +source: tests/run.rs +expression: rust_output +input_file: tests/programs/f24.hvm +--- +Result: {+inf {-inf {+NaN {2.5 {-1.5 {1.1499939 {0.25 {0.5 {0 {1 {1 {0 {0 {0 {0 {+NaN {+inf {-inf {1.019989 {0.1000061 {0.1000061 {-0.1000061 {-0.1000061 *}}}}}}}}}}}}}}}}}}}}}}} +- ITRS: 95 diff --git a/tests/snapshots/run__file@i24.hvm.snap b/tests/snapshots/run__file@i24.hvm.snap new file mode 100644 index 00000000..545cb2b4 --- /dev/null +++ b/tests/snapshots/run__file@i24.hvm.snap @@ -0,0 +1,7 @@ +--- +source: tests/run.rs +expression: rust_output +input_file: tests/programs/i24.hvm +--- +Result: {+12 {+8 {+20 {+5 {+0 {+0 {+1 {+0 {+1 {+2 {+10 {+8 {+8388607 {-8388608 {+1 {+1 {-1 {-1 *}}}}}}}}}}}}}}}}}} +- ITRS: 37 diff --git a/tests/snapshots/run__file@sort_bitonic__main.hvm.snap b/tests/snapshots/run__file@sort_bitonic__main.hvm.snap new file mode 100644 index 00000000..0563e726 --- /dev/null +++ b/tests/snapshots/run__file@sort_bitonic__main.hvm.snap @@ -0,0 +1,7 @@ +--- +source: tests/run.rs +expression: rust_output +input_file: examples/sort_bitonic/main.hvm +--- +Result: 16744448 +- ITRS: 253525989 diff --git a/tests/snapshots/run__file@sort_radix__main.hvm.snap b/tests/snapshots/run__file@sort_radix__main.hvm.snap new file mode 100644 index 00000000..bd540445 --- /dev/null +++ b/tests/snapshots/run__file@sort_radix__main.hvm.snap @@ -0,0 +1,7 @@ +--- +source: tests/run.rs +expression: rust_output +input_file: examples/sort_radix/main.hvm +--- +Result: 16744448 +- ITRS: 110821371 diff --git a/tests/snapshots/run__file@stress__main.hvm.snap b/tests/snapshots/run__file@stress__main.hvm.snap new file mode 100644 index 00000000..94f2aa27 --- /dev/null +++ b/tests/snapshots/run__file@stress__main.hvm.snap @@ -0,0 +1,7 @@ +--- +source: tests/run.rs +expression: rust_output +input_file: examples/stress/main.hvm +--- +Result: 0 +- ITRS: 402675702 diff --git a/tests/snapshots/run__file@sum_rec__main.hvm.snap b/tests/snapshots/run__file@sum_rec__main.hvm.snap new file mode 100644 index 00000000..e9fc27c6 --- /dev/null +++ b/tests/snapshots/run__file@sum_rec__main.hvm.snap @@ -0,0 +1,7 @@ +--- +source: tests/run.rs +expression: rust_output +input_file: examples/sum_rec/main.hvm +--- +Result: 8388608 +- ITRS: 402653167 diff --git a/tests/snapshots/run__file@sum_tree__main.hvm.snap b/tests/snapshots/run__file@sum_tree__main.hvm.snap new file mode 100644 index 00000000..d92e1968 --- /dev/null +++ b/tests/snapshots/run__file@sum_tree__main.hvm.snap @@ -0,0 +1,7 @@ +--- +source: tests/run.rs +expression: rust_output +input_file: examples/sum_tree/main.hvm +--- +Result: 1048576 +- ITRS: 44040165 diff --git a/tests/snapshots/run__file@tuples__tuples.hvm.snap b/tests/snapshots/run__file@tuples__tuples.hvm.snap new file mode 100644 index 00000000..3986b4ca --- /dev/null +++ b/tests/snapshots/run__file@tuples__tuples.hvm.snap @@ -0,0 +1,7 @@ +--- +source: tests/run.rs +expression: rust_output +input_file: examples/tuples/tuples.hvm +--- +Result: ((1 (2 (3 (4 (5 (6 (7 (8 x0)))))))) x0) +- ITRS: 170000018 diff --git a/tests/snapshots/run__file@u24.hvm.snap b/tests/snapshots/run__file@u24.hvm.snap new file mode 100644 index 00000000..2682b0d9 --- /dev/null +++ b/tests/snapshots/run__file@u24.hvm.snap @@ -0,0 +1,7 @@ +--- +source: tests/run.rs +expression: rust_output +input_file: tests/programs/u24.hvm +--- +Result: {12 {8 {20 {5 {0 {0 {1 {0 {1 {2 {10 {8 {40 {2 {16777215 {0 {3 *}}}}}}}}}}}}}}}}} +- ITRS: 35 From f09adf38e61dcedf8571eba835aa7b5733f5a21a Mon Sep 17 00:00:00 2001 From: tjjfvi Date: Tue, 21 May 2024 16:01:55 -0400 Subject: [PATCH 2/7] add checks workflow --- .github/workflows/checks.yml | 48 ++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 .github/workflows/checks.yml diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml new file mode 100644 index 00000000..a122a137 --- /dev/null +++ b/.github/workflows/checks.yml @@ -0,0 +1,48 @@ +name: Checks + +on: + pull_request: + merge_group: + push: + branches: + - main + +jobs: + check: + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@v3 + - uses: dsherret/rust-toolchain-file@v1 + - uses: actions/cache@v2 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: ${{ runner.os }}-check-${{ hashFiles('**/Cargo.lock') }} + - run: RUSTFLAGS="-D warnings" cargo check --all-targets + test: + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@v3 + - uses: dsherret/rust-toolchain-file@v1 + - uses: actions/cache@v2 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: ${{ runner.os }}-test-${{ hashFiles('**/Cargo.lock') }} + - run: cargo test --release + test-cuda: + needs: test # don't bother the cuda machine if other tests are failing + runs-on: [self-hosted, cuda] + timeout-minutes: 10 + steps: + - uses: actions/checkout@v3 + - uses: dsherret/rust-toolchain-file@v1 + - run: cargo test --release + shell: bash -l {0} + From b33cba9f08f76f9b8278c7fcfcc1e7f279417911 Mon Sep 17 00:00:00 2001 From: tjjfvi Date: Tue, 21 May 2024 16:34:18 -0400 Subject: [PATCH 3/7] make numeric tests cuda-friendly --- tests/programs/f24.hvm | 75 +++++++++++++------------- tests/programs/i24.hvm | 49 ++++++++--------- tests/programs/u24.hvm | 48 ++++++++--------- tests/snapshots/run__file@f24.hvm.snap | 2 +- tests/snapshots/run__file@i24.hvm.snap | 2 +- tests/snapshots/run__file@u24.hvm.snap | 2 +- 6 files changed, 90 insertions(+), 88 deletions(-) diff --git a/tests/programs/f24.hvm b/tests/programs/f24.hvm index 38a8bde6..2433b5f3 100644 --- a/tests/programs/f24.hvm +++ b/tests/programs/f24.hvm @@ -1,40 +1,41 @@ @half = xN & 1.0 ~ $([/] $(2.0 xN)) @nan = xN & 0.0 ~ $([/] $(0.0 xN)) -@main = x0 - // nan and inf divisions - & x0 ~ {n0 x1} & 1.0 ~ $([/] $(0.0 n0)) // +inf - & x1 ~ {n1 x2} & -1.0 ~ $([/] $(0.0 n1)) // -inf - & x2 ~ {n2 x3} & 0.0 ~ $([/] $(0.0 n2)) // NaN - - // general operators - & x3 ~ {n3 x4} & @half ~ $([+] $(2.0 n3)) // 2.5 - & x4 ~ {n4 x5} & @half ~ $([-] $(2.0 n4)) // -1.5 - & x5 ~ {n5 x6} & @half ~ $([*] $(2.3 n5)) // 1.15 - & x6 ~ {n6 x7} & @half ~ $([/] $(2.0 n6)) // 0.25 - & x7 ~ {n7 x8} & @half ~ $([%] $(2.0 n7)) // 0.5 - - // comparisons (returning ints) - & x8 ~ {n8 x9} & @half ~ $([=] $(2.0 n8)) // 0 - & x9 ~ {n9 xA} & @half ~ $([!] $(2.0 n9)) // 1 - & xA ~ {nA xB} & @half ~ $([<] $(2.0 nA)) // 1 - & xB ~ {nB xC} & @half ~ $([>] $(2.0 nB)) // 0 - - // ieee nan comparisons - & xC ~ {nC xD} & @nan ~ $([=] $(@nan nC)) // 0 - & xD ~ {nD xE} & @nan ~ $([<] $(@nan nD)) // 0 - & xE ~ {nE xF} & @nan ~ $([>] $(@nan nE)) // 0 - - // parsing - & xF ~ {nF xG} & +NaN ~ $([+] $(0.0 nF)) // NaN - & xG ~ {nG xH} & +inf ~ $([+] $(0.0 nG)) // inf - & xH ~ {nH xI} & -inf ~ $([+] $(0.0 nH)) // -inf - & xI ~ {nI xJ} & 1.02 ~ $([+] $(0.0 nI)) // 1.02 - - // modulo - & xJ ~ {nJ xK} & +1.2 ~ $([%] $(+1.1 nJ)) // +0.1 - & xK ~ {nK xL} & +1.2 ~ $([%] $(-1.1 nK)) // +0.1 - & xL ~ {nL xM} & -1.2 ~ $([%] $(+1.1 nL)) // -0.1 - & xM ~ {nM xN} & -1.2 ~ $([%] $(-1.1 nM)) // -0.1 - - & xN ~ * +@main = x & @t0 ~ (* x) + +// nan and inf divisions +@t0 = (* {n x}) & @t1 ~ (* x) & 1.0 ~ $([/] $(0.0 n)) // +inf +@t1 = (* {n x}) & @t2 ~ (* x) & -1.0 ~ $([/] $(0.0 n)) // -inf +@t2 = (* {n x}) & @t3 ~ (* x) & 0.0 ~ $([/] $(0.0 n)) // NaN + +// general operators +@t3 = (* {n x}) & @t4 ~ (* x) & @half ~ $([+] $(2.0 n)) // 2.5 +@t4 = (* {n x}) & @t5 ~ (* x) & @half ~ $([-] $(2.0 n)) // -1.5 +@t5 = (* {n x}) & @t6 ~ (* x) & @half ~ $([*] $(2.3 n)) // 1.15 +@t6 = (* {n x}) & @t7 ~ (* x) & @half ~ $([/] $(2.0 n)) // 0.25 +@t7 = (* {n x}) & @t8 ~ (* x) & @half ~ $([%] $(2.0 n)) // 0.5 + +// comparisons (returning ints) +@t8 = (* {n x}) & @t9 ~ (* x) & @half ~ $([=] $(2.0 n)) // 0 +@t9 = (* {n x}) & @tA ~ (* x) & @half ~ $([!] $(2.0 n)) // 1 +@tA = (* {n x}) & @tB ~ (* x) & @half ~ $([<] $(2.0 n)) // 1 +@tB = (* {n x}) & @tC ~ (* x) & @half ~ $([>] $(2.0 n)) // 0 + +// ieee nan comparisons +@tC = (* {n x}) & @tD ~ (* x) & @nan ~ $([=] $(@nan n)) // 0 +@tD = (* {n x}) & @tE ~ (* x) & @nan ~ $([<] $(@nan n)) // 0 +@tE = (* {n x}) & @tF ~ (* x) & @nan ~ $([>] $(@nan n)) // 0 + +// parsing +@tF = (* {n x}) & @tG ~ (* x) & +NaN ~ $([+] $(0.0 n)) // NaN +@tG = (* {n x}) & @tH ~ (* x) & +inf ~ $([+] $(0.0 n)) // inf +@tH = (* {n x}) & @tI ~ (* x) & -inf ~ $([+] $(0.0 n)) // -inf +@tI = (* {n x}) & @tJ ~ (* x) & 1.02 ~ $([+] $(0.0 n)) // 1.02 + +// modulo +@tJ = (* {n x}) & @tK ~ (* x) & +1.2 ~ $([%] $(+1.1 n)) // +0.1 +@tK = (* {n x}) & @tL ~ (* x) & +1.2 ~ $([%] $(-1.1 n)) // +0.1 +@tL = (* {n x}) & @tM ~ (* x) & -1.2 ~ $([%] $(+1.1 n)) // -0.1 +@tM = (* {n x}) & @tN ~ (* x) & -1.2 ~ $([%] $(-1.1 n)) // -0.1 + +@tN = * diff --git a/tests/programs/i24.hvm b/tests/programs/i24.hvm index 57778ce5..99b4ea7f 100644 --- a/tests/programs/i24.hvm +++ b/tests/programs/i24.hvm @@ -1,29 +1,30 @@ -@main = x0 - // all ops - & x0 ~ {n0 x1} & +10 ~ $([+] $(+2 n0)) // 12 - & x1 ~ {n1 x2} & +10 ~ $([-] $(+2 n1)) // 8 - & x2 ~ {n2 x3} & +10 ~ $([*] $(+2 n2)) // 20 - & x3 ~ {n3 x4} & +10 ~ $([/] $(+2 n3)) // 5 - & x4 ~ {n4 x5} & +10 ~ $([%] $(+2 n4)) // 0 - & x5 ~ {n5 x6} & +10 ~ $([=] $(+2 n5)) // 0 - & x6 ~ {n6 x7} & +10 ~ $([!] $(+2 n6)) // 1 - & x7 ~ {n7 x8} & +10 ~ $([<] $(+2 n7)) // 0 - & x8 ~ {n8 x9} & +10 ~ $([>] $(+2 n8)) // 1 - & x9 ~ {n9 xA} & +10 ~ $([&] $(+2 n9)) // 2 - & xA ~ {nA xB} & +10 ~ $([|] $(+2 nA)) // 10 - & xB ~ {nB xC} & +10 ~ $([^] $(+2 nB)) // 8 +@main = x & @t0 ~ (* x) + +// all ops +@t0 = (* {n x}) & @t1 ~ (* x) & +10 ~ $([+] $(+2 n)) // 12 +@t1 = (* {n x}) & @t2 ~ (* x) & +10 ~ $([-] $(+2 n)) // 8 +@t2 = (* {n x}) & @t3 ~ (* x) & +10 ~ $([*] $(+2 n)) // 20 +@t3 = (* {n x}) & @t4 ~ (* x) & +10 ~ $([/] $(+2 n)) // 5 +@t4 = (* {n x}) & @t5 ~ (* x) & +10 ~ $([%] $(+2 n)) // 0 +@t5 = (* {n x}) & @t6 ~ (* x) & +10 ~ $([=] $(+2 n)) // 0 +@t6 = (* {n x}) & @t7 ~ (* x) & +10 ~ $([!] $(+2 n)) // 1 +@t7 = (* {n x}) & @t8 ~ (* x) & +10 ~ $([<] $(+2 n)) // 0 +@t8 = (* {n x}) & @t9 ~ (* x) & +10 ~ $([>] $(+2 n)) // 1 +@t9 = (* {n x}) & @tA ~ (* x) & +10 ~ $([&] $(+2 n)) // 2 +@tA = (* {n x}) & @tB ~ (* x) & +10 ~ $([|] $(+2 n)) // 10 +@tB = (* {n x}) & @tC ~ (* x) & +10 ~ $([^] $(+2 n)) // 8 - // underflow - & xC ~ {nC xD} & -8388608 ~ $([-] $(+1 nC)) // 8388607 +// underflow +@tC = (* {n x}) & @tD ~ (* x) & -8388608 ~ $([-] $(+1 n)) // 8388607 - // overflow - & xD ~ {nD xE} & +8388607 ~ $([+] $(+1 nD)) // -8388608 +// overflow +@tD = (* {n x}) & @tE ~ (* x) & +8388607 ~ $([+] $(+1 n)) // -8388608 - // modulo - & xE ~ {nE xF} & +3 ~ $([%] $(+2 nE)) // +1 - & xF ~ {nF xG} & +3 ~ $([%] $(-2 nF)) // +1 - & xG ~ {nG xH} & -3 ~ $([%] $(+2 nG)) // -1 - & xH ~ {nH xI} & -3 ~ $([%] $(-2 nH)) // -1 +// modulo +@tE = (* {n x}) & @tF ~ (* x) & +3 ~ $([%] $(+2 n)) // +1 +@tF = (* {n x}) & @tG ~ (* x) & +3 ~ $([%] $(-2 n)) // +1 +@tG = (* {n x}) & @tH ~ (* x) & -3 ~ $([%] $(+2 n)) // -1 +@tH = (* {n x}) & @tI ~ (* x) & -3 ~ $([%] $(-2 n)) // -1 - & xI ~ * +@tI = * diff --git a/tests/programs/u24.hvm b/tests/programs/u24.hvm index 1c8ecfbb..a5e17b60 100644 --- a/tests/programs/u24.hvm +++ b/tests/programs/u24.hvm @@ -1,28 +1,28 @@ -@main = x0 - // all ops - & x0 ~ {n0 x1} & 10 ~ $([+] $(2 n0)) // 12 - & x1 ~ {n1 x2} & 10 ~ $([-] $(2 n1)) // 8 - & x2 ~ {n2 x3} & 10 ~ $([*] $(2 n2)) // 20 - & x3 ~ {n3 x4} & 10 ~ $([/] $(2 n3)) // 5 - & x4 ~ {n4 x5} & 10 ~ $([%] $(2 n4)) // 0 - & x5 ~ {n5 x6} & 10 ~ $([=] $(2 n5)) // 0 - & x6 ~ {n6 x7} & 10 ~ $([!] $(2 n6)) // 1 - & x7 ~ {n7 x8} & 10 ~ $([<] $(2 n7)) // 0 - & x8 ~ {n8 x9} & 10 ~ $([>] $(2 n8)) // 1 - & x9 ~ {n9 xA} & 10 ~ $([&] $(2 n9)) // 2 - & xA ~ {nA xB} & 10 ~ $([|] $(2 nA)) // 10 - & xB ~ {nB xC} & 10 ~ $([^] $(2 nB)) // 8 - & xC ~ {nC xD} & 10 ~ $([<<] $(2 nC)) // 40 - & xD ~ {nD xE} & 10 ~ $([>>] $(2 nD)) // 2 - - // underflow - & xE ~ {nE xF} & 0 ~ $([-] $(1 nE)) // 16777215 +@main = x & @t0 ~ (* x) - // overflow - & xF ~ {nF xG} & 16777215 ~ $([+] $(1 nF)) // 0 +// all ops +@t0 = (* {n x}) & @t1 ~ (* x) & 10 ~ $([+] $(2 n)) // 12 +@t1 = (* {n x}) & @t2 ~ (* x) & 10 ~ $([-] $(2 n)) // 8 +@t2 = (* {n x}) & @t3 ~ (* x) & 10 ~ $([*] $(2 n)) // 20 +@t3 = (* {n x}) & @t4 ~ (* x) & 10 ~ $([/] $(2 n)) // 5 +@t4 = (* {n x}) & @t5 ~ (* x) & 10 ~ $([%] $(2 n)) // 0 +@t5 = (* {n x}) & @t6 ~ (* x) & 10 ~ $([=] $(2 n)) // 0 +@t6 = (* {n x}) & @t7 ~ (* x) & 10 ~ $([!] $(2 n)) // 1 +@t7 = (* {n x}) & @t8 ~ (* x) & 10 ~ $([<] $(2 n)) // 0 +@t8 = (* {n x}) & @t9 ~ (* x) & 10 ~ $([>] $(2 n)) // 1 +@t9 = (* {n x}) & @tA ~ (* x) & 10 ~ $([&] $(2 n)) // 2 +@tA = (* {n x}) & @tB ~ (* x) & 10 ~ $([|] $(2 n)) // 10 +@tB = (* {n x}) & @tC ~ (* x) & 10 ~ $([^] $(2 n)) // 8 +@tC = (* {n x}) & @tD ~ (* x) & 10 ~ $([<<] $(2 n)) // 40 +@tD = (* {n x}) & @tE ~ (* x) & 10 ~ $([>>] $(2 n)) // 2 - // no sign extension - & xG ~ {nG xH} & 16777215 ~ $([>>] $(22 nG)) // 3 +// underflow +@tE = (* {n x}) & @tF ~ (* x) & 0 ~ $([-] $(1 n)) // 16777215 - & xH ~ * +// overflow +@tF = (* {n x}) & @tG ~ (* x) & 16777215 ~ $([+] $(1 n)) // 0 +// no sign extension +@tG = (* {n x}) & @tH ~ (* x) & 16777215 ~ $([>>] $(22 n)) // 3 + +@tH = * diff --git a/tests/snapshots/run__file@f24.hvm.snap b/tests/snapshots/run__file@f24.hvm.snap index 0392c6af..708da379 100644 --- a/tests/snapshots/run__file@f24.hvm.snap +++ b/tests/snapshots/run__file@f24.hvm.snap @@ -4,4 +4,4 @@ expression: rust_output input_file: tests/programs/f24.hvm --- Result: {+inf {-inf {+NaN {2.5 {-1.5 {1.1499939 {0.25 {0.5 {0 {1 {1 {0 {0 {0 {0 {+NaN {+inf {-inf {1.019989 {0.1000061 {0.1000061 {-0.1000061 {-0.1000061 *}}}}}}}}}}}}}}}}}}}}}}} -- ITRS: 95 +- ITRS: 167 diff --git a/tests/snapshots/run__file@i24.hvm.snap b/tests/snapshots/run__file@i24.hvm.snap index 545cb2b4..52c7072f 100644 --- a/tests/snapshots/run__file@i24.hvm.snap +++ b/tests/snapshots/run__file@i24.hvm.snap @@ -4,4 +4,4 @@ expression: rust_output input_file: tests/programs/i24.hvm --- Result: {+12 {+8 {+20 {+5 {+0 {+0 {+1 {+0 {+1 {+2 {+10 {+8 {+8388607 {-8388608 {+1 {+1 {-1 {-1 *}}}}}}}}}}}}}}}}}} -- ITRS: 37 +- ITRS: 94 diff --git a/tests/snapshots/run__file@u24.hvm.snap b/tests/snapshots/run__file@u24.hvm.snap index 2682b0d9..7ccdb398 100644 --- a/tests/snapshots/run__file@u24.hvm.snap +++ b/tests/snapshots/run__file@u24.hvm.snap @@ -4,4 +4,4 @@ expression: rust_output input_file: tests/programs/u24.hvm --- Result: {12 {8 {20 {5 {0 {0 {1 {0 {1 {2 {10 {8 {40 {2 {16777215 {0 {3 *}}}}}}}}}}}}}}}}} -- ITRS: 35 +- ITRS: 89 From d2b14d76f500cd39d5f2f06197422c97b1db0752 Mon Sep 17 00:00:00 2001 From: tjjfvi Date: Tue, 21 May 2024 16:56:15 -0400 Subject: [PATCH 4/7] dont round nan to zero --- src/hvm.c | 6 ++++-- src/hvm.cu | 6 ++++-- src/hvm.rs | 7 +++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/hvm.c b/src/hvm.c index e095cd1e..b18bbb86 100644 --- a/src/hvm.c +++ b/src/hvm.c @@ -409,8 +409,10 @@ static inline Numb new_f24(float val) { u32 bits = *(u32*)&val; u32 shifted_bits = bits >> 8; u32 lost_bits = bits & 0xFF; - shifted_bits += (lost_bits - ((lost_bits >> 7) & !shifted_bits)) >> 7; // round ties to even - shifted_bits |= ((bits & 0x7F800000) == 0x7F800000) && (bits << 9 != 0); // ensure NaNs don't become infinities + // round ties to even + shifted_bits += (!isnan(val)) & ((lost_bits - ((lost_bits >> 7) & !shifted_bits)) >> 7); + // ensure NaNs don't become infinities + shifted_bits |= isnan(val); return (shifted_bits << 5) | TY_F24; } diff --git a/src/hvm.cu b/src/hvm.cu index 81ec0cb5..cf3a5dc4 100644 --- a/src/hvm.cu +++ b/src/hvm.cu @@ -499,8 +499,10 @@ __device__ __host__ inline Numb new_f24(f32 val) { u32 bits = *(u32*)&val; u32 shifted_bits = bits >> 8; u32 lost_bits = bits & 0xFF; - shifted_bits += (lost_bits - ((lost_bits >> 7) & !shifted_bits)) >> 7; // round ties to even - shifted_bits |= ((bits & 0x7F800000) == 0x7F800000) && (bits << 9 != 0); // ensure NaNs don't become infinities + // round ties to even + shifted_bits += (!isnan(val)) & ((lost_bits - ((lost_bits >> 7) & !shifted_bits)) >> 7); + // ensure NaNs don't become infinities + shifted_bits |= isnan(val); return (shifted_bits << 5) | TY_F24; } diff --git a/src/hvm.rs b/src/hvm.rs index 773f4478..44ec973b 100644 --- a/src/hvm.rs +++ b/src/hvm.rs @@ -254,8 +254,10 @@ impl Numb { let bits = val.to_bits(); let mut shifted_bits = bits >> 8; let lost_bits = bits & 0xFF; - shifted_bits += (lost_bits - ((lost_bits >> 7) & !shifted_bits)) >> 7; // round ties to even - shifted_bits |= u32::from((bits & 0x7F800000 == 0x7F800000) && (bits << 9 != 0)); // ensure NaNs don't become infinities + // round ties to even + shifted_bits += u32::from(!val.is_nan()) & ((lost_bits - ((lost_bits >> 7) & !shifted_bits)) >> 7); + // ensure NaNs don't become infinities + shifted_bits |= u32::from(val.is_nan()); Numb((shifted_bits << 5) as Val | (TY_F24 as Val)) } @@ -1156,4 +1158,5 @@ fn test_f24() { // Test that NaNs are not turned into infinities assert!(Numb::new_f24(f32::from_bits(0b0_11111111_000000000000000_00000001)).get_f24().is_nan()); assert!(Numb::new_f24(f32::from_bits(0b1_11111111_000000000000000_00000001)).get_f24().is_nan()); + assert!(Numb::new_f24(f32::from_bits(0b0_11111111_111111111111111_11111111)).get_f24().is_nan()); } From aa1d7ac94a23e61da1c3c942e647d39a3769251b Mon Sep 17 00:00:00 2001 From: tjjfvi Date: Tue, 21 May 2024 17:08:42 -0400 Subject: [PATCH 5/7] fix get_sym --- src/hvm.cu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hvm.cu b/src/hvm.cu index cf3a5dc4..59510df7 100644 --- a/src/hvm.cu +++ b/src/hvm.cu @@ -473,7 +473,7 @@ __device__ __host__ inline Numb new_sym(u32 val) { } __device__ __host__ inline u32 get_sym(Numb word) { - return (word >> 5) & 0xF; + return (word >> 5); } // Constructor and getters for U24 (unsigned 24-bit integer) From 7d930ac017185c236614f6be17b84d04a5594f3c Mon Sep 17 00:00:00 2001 From: tjjfvi Date: Tue, 21 May 2024 17:14:36 -0400 Subject: [PATCH 6/7] remove iteration count from snapshot --- tests/run.rs | 8 +++++--- tests/snapshots/run__file@demo_io__main.hvm.snap | 1 - tests/snapshots/run__file@f24.hvm.snap | 1 - tests/snapshots/run__file@i24.hvm.snap | 1 - tests/snapshots/run__file@sort_bitonic__main.hvm.snap | 1 - tests/snapshots/run__file@sort_radix__main.hvm.snap | 1 - tests/snapshots/run__file@stress__main.hvm.snap | 1 - tests/snapshots/run__file@sum_rec__main.hvm.snap | 1 - tests/snapshots/run__file@sum_tree__main.hvm.snap | 1 - tests/snapshots/run__file@tuples__tuples.hvm.snap | 1 - tests/snapshots/run__file@u24.hvm.snap | 1 - 11 files changed, 5 insertions(+), 13 deletions(-) diff --git a/tests/run.rs b/tests/run.rs index 0e43bd18..3c94f5dc 100644 --- a/tests/run.rs +++ b/tests/run.rs @@ -68,10 +68,12 @@ fn parse_output(output: &str) -> Result { let mut parser = hvm::ast::CoreParser::new(output); parser.consume("Result:")?; let mut tree = parser.parse_tree()?; - parser.consume("- ITRS:")?; - let itrs = parser.parse_u64()?; normalize_vars(&mut tree, &mut HashMap::new()); - Ok(format!("Result: {}\n- ITRS: {}", tree.show(), itrs)) + // TODO: include iteration count in snapshot once consistent + // parser.consume("- ITRS:")?; + // let itrs = parser.parse_u64()?; + // Ok(format!("Result: {}\n- ITRS: {}", tree.show(), itrs)) + Ok(format!("Result: {}", tree.show())) } fn normalize_vars(tree: &mut Tree, vars: &mut HashMap) { diff --git a/tests/snapshots/run__file@demo_io__main.hvm.snap b/tests/snapshots/run__file@demo_io__main.hvm.snap index c14e7b63..fe036376 100644 --- a/tests/snapshots/run__file@demo_io__main.hvm.snap +++ b/tests/snapshots/run__file@demo_io__main.hvm.snap @@ -4,4 +4,3 @@ expression: rust_output input_file: examples/demo_io/main.hvm --- Result: ((@PUTTEXT (@Main__C2 ((* ((@PUTTEXT (@Main__C0 ((* ((@DRAWIMAGE (((16711680 65280) (255 16711935)) ((* ((@SLEEP ((119 3511296) ((* ((@DONE (42 x0)) x0)) x1))) x1)) x2))) x2)) x3))) x3)) x4))) x4) -- ITRS: 120 diff --git a/tests/snapshots/run__file@f24.hvm.snap b/tests/snapshots/run__file@f24.hvm.snap index 708da379..f528dbd4 100644 --- a/tests/snapshots/run__file@f24.hvm.snap +++ b/tests/snapshots/run__file@f24.hvm.snap @@ -4,4 +4,3 @@ expression: rust_output input_file: tests/programs/f24.hvm --- Result: {+inf {-inf {+NaN {2.5 {-1.5 {1.1499939 {0.25 {0.5 {0 {1 {1 {0 {0 {0 {0 {+NaN {+inf {-inf {1.019989 {0.1000061 {0.1000061 {-0.1000061 {-0.1000061 *}}}}}}}}}}}}}}}}}}}}}}} -- ITRS: 167 diff --git a/tests/snapshots/run__file@i24.hvm.snap b/tests/snapshots/run__file@i24.hvm.snap index 52c7072f..4f8c624a 100644 --- a/tests/snapshots/run__file@i24.hvm.snap +++ b/tests/snapshots/run__file@i24.hvm.snap @@ -4,4 +4,3 @@ expression: rust_output input_file: tests/programs/i24.hvm --- Result: {+12 {+8 {+20 {+5 {+0 {+0 {+1 {+0 {+1 {+2 {+10 {+8 {+8388607 {-8388608 {+1 {+1 {-1 {-1 *}}}}}}}}}}}}}}}}}} -- ITRS: 94 diff --git a/tests/snapshots/run__file@sort_bitonic__main.hvm.snap b/tests/snapshots/run__file@sort_bitonic__main.hvm.snap index 0563e726..ad761bb8 100644 --- a/tests/snapshots/run__file@sort_bitonic__main.hvm.snap +++ b/tests/snapshots/run__file@sort_bitonic__main.hvm.snap @@ -4,4 +4,3 @@ expression: rust_output input_file: examples/sort_bitonic/main.hvm --- Result: 16744448 -- ITRS: 253525989 diff --git a/tests/snapshots/run__file@sort_radix__main.hvm.snap b/tests/snapshots/run__file@sort_radix__main.hvm.snap index bd540445..bf9f0324 100644 --- a/tests/snapshots/run__file@sort_radix__main.hvm.snap +++ b/tests/snapshots/run__file@sort_radix__main.hvm.snap @@ -4,4 +4,3 @@ expression: rust_output input_file: examples/sort_radix/main.hvm --- Result: 16744448 -- ITRS: 110821371 diff --git a/tests/snapshots/run__file@stress__main.hvm.snap b/tests/snapshots/run__file@stress__main.hvm.snap index 94f2aa27..28987638 100644 --- a/tests/snapshots/run__file@stress__main.hvm.snap +++ b/tests/snapshots/run__file@stress__main.hvm.snap @@ -4,4 +4,3 @@ expression: rust_output input_file: examples/stress/main.hvm --- Result: 0 -- ITRS: 402675702 diff --git a/tests/snapshots/run__file@sum_rec__main.hvm.snap b/tests/snapshots/run__file@sum_rec__main.hvm.snap index e9fc27c6..ccd079ee 100644 --- a/tests/snapshots/run__file@sum_rec__main.hvm.snap +++ b/tests/snapshots/run__file@sum_rec__main.hvm.snap @@ -4,4 +4,3 @@ expression: rust_output input_file: examples/sum_rec/main.hvm --- Result: 8388608 -- ITRS: 402653167 diff --git a/tests/snapshots/run__file@sum_tree__main.hvm.snap b/tests/snapshots/run__file@sum_tree__main.hvm.snap index d92e1968..87588a2f 100644 --- a/tests/snapshots/run__file@sum_tree__main.hvm.snap +++ b/tests/snapshots/run__file@sum_tree__main.hvm.snap @@ -4,4 +4,3 @@ expression: rust_output input_file: examples/sum_tree/main.hvm --- Result: 1048576 -- ITRS: 44040165 diff --git a/tests/snapshots/run__file@tuples__tuples.hvm.snap b/tests/snapshots/run__file@tuples__tuples.hvm.snap index 3986b4ca..d6bf57ae 100644 --- a/tests/snapshots/run__file@tuples__tuples.hvm.snap +++ b/tests/snapshots/run__file@tuples__tuples.hvm.snap @@ -4,4 +4,3 @@ expression: rust_output input_file: examples/tuples/tuples.hvm --- Result: ((1 (2 (3 (4 (5 (6 (7 (8 x0)))))))) x0) -- ITRS: 170000018 diff --git a/tests/snapshots/run__file@u24.hvm.snap b/tests/snapshots/run__file@u24.hvm.snap index 7ccdb398..966d2e41 100644 --- a/tests/snapshots/run__file@u24.hvm.snap +++ b/tests/snapshots/run__file@u24.hvm.snap @@ -4,4 +4,3 @@ expression: rust_output input_file: tests/programs/u24.hvm --- Result: {12 {8 {20 {5 {0 {0 {1 {0 {1 {2 {10 {8 {40 {2 {16777215 {0 {3 *}}}}}}}}}}}}}}}}} -- ITRS: 89 From b414c03d57ee783302a7f3ae1dfd1a1590da8693 Mon Sep 17 00:00:00 2001 From: tjjfvi Date: Tue, 21 May 2024 17:21:28 -0400 Subject: [PATCH 7/7] simplify tuple example for CUDA --- examples/tuples/tuples.hvm | 2 +- tests/snapshots/run__file@tuples__tuples.hvm.snap | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/tuples/tuples.hvm b/examples/tuples/tuples.hvm index 5bdf0a7b..e112c0e3 100644 --- a/examples/tuples/tuples.hvm +++ b/examples/tuples/tuples.hvm @@ -9,7 +9,7 @@ & @app ~ (a (b (d e))) @main = b - & @app ~ (5000000 (@rot (a b))) + & @app ~ (20 (@rot (a b))) & @Tup8/New ~ (1 (2 (3 (4 (5 (6 (7 (8 a)))))))) @rot = ((@rot__C0 a) a) diff --git a/tests/snapshots/run__file@tuples__tuples.hvm.snap b/tests/snapshots/run__file@tuples__tuples.hvm.snap index d6bf57ae..89bfe598 100644 --- a/tests/snapshots/run__file@tuples__tuples.hvm.snap +++ b/tests/snapshots/run__file@tuples__tuples.hvm.snap @@ -3,4 +3,4 @@ source: tests/run.rs expression: rust_output input_file: examples/tuples/tuples.hvm --- -Result: ((1 (2 (3 (4 (5 (6 (7 (8 x0)))))))) x0) +Result: ((5 (6 (7 (8 (1 (2 (3 (4 x0)))))))) x0)