From 6a759884893f984776e0aa5d6deba2ac2e9797da Mon Sep 17 00:00:00 2001 From: Dennis Ranke Date: Sat, 1 Jan 2022 15:51:18 +0100 Subject: [PATCH] update rust example --- examples/rust/readme.txt | 8 ++++---- examples/rust/tunnel.rs | 14 +++++++++++--- examples/rust/tunnel.uw8 | Bin 137 -> 119 bytes 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/examples/rust/readme.txt b/examples/rust/readme.txt index 450e4dd..b21a897 100644 --- a/examples/rust/readme.txt +++ b/examples/rust/readme.txt @@ -5,8 +5,8 @@ A nightly rust compiler is needed for the unstable sqrtf32 intrinsic. Simply compiling with rustc as shown in build.sh results in a -342 byte tunnel.wasm. Using wasm-opt this can be reduced to -243 bytes. +371 byte tunnel.wasm. Using wasm-opt this can be reduced to +260 bytes. When you disassemble this wasm file using wasm2wat you can see these globals and exports: @@ -21,5 +21,5 @@ values that are not simple scalars (i32, f32, etc.). Since our code doesn't actually use any of that, we can just delete them in a text editor and assemble the code again with wat2wasm. -This gives us a 199 byte wasm file. Running this through -uw8-tool pack brings us to the final size of 137 bytes. \ No newline at end of file +This gives us a 216 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 5a7dc5d..83527fe 100644 --- a/examples/rust/tunnel.rs +++ b/examples/rust/tunnel.rs @@ -9,12 +9,20 @@ mod env { extern "C" { pub fn atan2(x: f32, y: f32) -> f32; } + + extern "C" { + pub fn time() -> f32; + } } fn atan2(x: f32, y: f32) -> f32 { unsafe { env::atan2(x, y) } } +fn time() -> f32 { + unsafe { env::time() } +} + fn sqrt(v: f32) -> f32 { unsafe { core::intrinsics::sqrtf32(v) } } @@ -29,14 +37,14 @@ fn ftoi(v: f32) -> i32 { } #[no_mangle] -pub fn tic(time: i32) { +pub fn upd() { for i in 0..320 * 240 { - let t = time as f32 / 10 as f32; + let t = time() * 63 as f32; 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; + let c = (ftoi(d + t * 2 as f32) ^ ftoi(u + t)) as u8 >> 4; unsafe { *((120 + i) as *mut u8) = c; } diff --git a/examples/rust/tunnel.uw8 b/examples/rust/tunnel.uw8 index 21c1c16a12e9549578ec6e0f2706482ff6b8f994..50c01f3f3435ee5b6c8f43743f679c17cd100ffc 100644 GIT binary patch literal 119 zcmV--0Eqtr1#ia}{*%7ra>hbgU8bpqy|#X0LF+0s*nH{b>t`VtLE{d)%$6?ECNRU! z^qaUOzNvK8)5AEzspQZ3njgWf=E#z09qo+t%?E+p%iI)|LBjg6m~*6PThd6Sn6$Vq{{jWvo|V*yPN>px`)Fk