From 88ee0e1befdb32f15419e9d50a512a79563acd28 Mon Sep 17 00:00:00 2001 From: Dennis Ranke Date: Sun, 2 Jan 2022 13:58:56 +0100 Subject: [PATCH] port rust tunnel example to zig --- examples/rust/build.sh | 5 +++-- examples/rust/readme.txt | 10 +++++----- examples/rust/tunnel.rs | 15 ++++++++++----- examples/rust/tunnel.uw8 | Bin 119 -> 115 bytes examples/zig/.gitignore | 2 ++ examples/zig/build.zig | 37 +++++++++++++++++++++++++++++++++++++ examples/zig/main.zig | 20 ++++++++++++++++++++ 7 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 examples/zig/.gitignore create mode 100644 examples/zig/build.zig create mode 100644 examples/zig/main.zig diff --git a/examples/rust/build.sh b/examples/rust/build.sh index 0ecec33..b9893d2 100755 --- a/examples/rust/build.sh +++ b/examples/rust/build.sh @@ -1,3 +1,4 @@ -rustc --target=wasm32-unknown-unknown --crate-type cdylib -C opt-level="z" -C "link-args=--import-memory --initial-memory=262144 -zstack-size=65536" -o tunnel.wasm tunnel.rs && \ +rustc --target=wasm32-unknown-unknown --crate-type cdylib -C opt-level="z" -C "link-args=--import-memory --initial-memory=262144 -zstack-size=90000" -o tunnel.wasm tunnel.rs && \ uw8 filter-exports tunnel.wasm tunnel.wasm && \ -wasm-opt -Oz -o tunnel.wasm tunnel.wasm +wasm-opt -Oz --strip-producers -o tunnel.wasm tunnel.wasm && \ +uw8 pack -l 9 tunnel.wasm tunnel.uw8 diff --git a/examples/rust/readme.txt b/examples/rust/readme.txt index 562782f..be2774d 100644 --- a/examples/rust/readme.txt +++ b/examples/rust/readme.txt @@ -5,14 +5,14 @@ A nightly rust compiler is needed for the unstable sqrtf32 intrinsic. Simply compiling with rustc as shown in build.sh results in a -371 byte tunnel.wasm. Using wasm-opt this can be reduced to -260 bytes. +361 byte tunnel.wasm. Using wasm-opt this can be reduced to +255 bytes. When you disassemble this wasm file using wasm2wat you can see these globals and exports: -(global (;0;) i32 (i32.const 65536)) -(global (;1;) i32 (i32.const 65536)) +(global (;0;) i32 (i32.const 90000)) +(global (;1;) i32 (i32.const 90000)) (export "__data_end" (global 0)) (export "__heap_base" (global 1)) @@ -23,5 +23,5 @@ referenced by the exports and we can remove them using 'uw8 filter-exports' (preferably before running wasm-opt) which removes all exports except those used by the MicroW8 platform. -This gives us a 216 byte wasm file. Running this through +This gives us a 211 byte wasm file. Running this through uw8 pack brings us to the final size of 119 bytes. \ No newline at end of file diff --git a/examples/rust/tunnel.rs b/examples/rust/tunnel.rs index 83527fe..5f62d92 100644 --- a/examples/rust/tunnel.rs +++ b/examples/rust/tunnel.rs @@ -38,16 +38,21 @@ fn ftoi(v: f32) -> i32 { #[no_mangle] pub fn upd() { - for i in 0..320 * 240 { - let t = time() * 63 as f32; + let mut i: i32 = 0; + loop { + let t = time() * 63.; let x = (i % 320 - 160) as f32; let y = (i / 320 - 120) as f32; - let d = 40000 as f32 / sqrt(x * x + y * y + 1 as f32); - let u = atan2(x, y) * 512f32 / 3.141; - let c = (ftoi(d + t * 2 as f32) ^ ftoi(u + t)) as u8 >> 4; + let d = 40000 as f32 / sqrt(x * x + y * y + 1.); + let u = atan2(x, y) * 512. / 3.141; + let c = (ftoi(d + t * 2.) ^ ftoi(u + t)) as u8 >> 4; unsafe { *((120 + i) as *mut u8) = c; } + i += 1; + if i >= 320*240 { + break; + } } } diff --git a/examples/rust/tunnel.uw8 b/examples/rust/tunnel.uw8 index 50c01f3f3435ee5b6c8f43743f679c17cd100ffc..17ff4daa706a5c70b9740a79582df325e01dba36 100644 GIT binary patch literal 115 zcmV-(0F3_v5Z8u|Z|#{&AWW@?ahbgU8bpqy|#X0LF+0s*nH{b>t`VtLE{d)%$6?ECNRU! z^qaUOzNvK8)5AEzspQZ3njgWf=E#z09qo+t%?E+p%iI)|LBjg6m~*6PThd6Sn6$> 4; + + FRAMEBUFFER[@as(usize, i)] = c; + i += 1; + if(i >= 320*240) { break; } + } +} \ No newline at end of file