diff --git a/examples/c/cart.c b/examples/c/cart.c index 3b652ae..2941636 100644 --- a/examples/c/cart.c +++ b/examples/c/cart.c @@ -16,7 +16,7 @@ void upd() { float t = time() * 63.0f; float x = (float)(i % 320 - 160); float y = (float)(i / 320 - 120); - float d = 40000.0f / sqrt(x * x + y * y + 1.0f); + float d = 40000.0f / sqrt(x * x + y * y); float u = atan2(x, y) * 512.0f / 3.141f; unsigned char c = (unsigned char)((int)(d + t * 2.0f) ^ (int)(u + t)) >> 4; FRAMEBUFFER[i] = c; diff --git a/examples/curlywas/tunnel.cwa b/examples/curlywas/tunnel.cwa new file mode 100644 index 0000000..33a85aa --- /dev/null +++ b/examples/curlywas/tunnel.cwa @@ -0,0 +1,19 @@ +import "env.memory" memory(4); + +import "env.atan2" fn atan2(f32, f32) -> f32; +import "env.time" fn time() -> f32; + +export fn upd() { + let i: i32; + loop pixels { + let inline t = time() * 63 as f32; + let lazy x = (i % 320 - 160) as f32; + let lazy y = (i / 320 - 120) as f32; + let inline d = 40000 as f32 / sqrt(x * x + y * y); + let inline u = atan2(x, y) * (512.0 / 3.141); + let inline c = ((i32.trunc_sat_f32_s(d + t * 2 as f32) ^ i32.trunc_sat_f32_s(u + t)) & 255) >> 4; + i?120 = c; + + branch_if (i := i + 1) < 320*240: pixels; + } +} \ No newline at end of file diff --git a/examples/rust/build.sh b/examples/rust/build.sh index b9893d2..b2141a5 100755 --- a/examples/rust/build.sh +++ b/examples/rust/build.sh @@ -1,4 +1,4 @@ -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 && \ +rustc --target=wasm32-unknown-unknown -C target-feature=+nontrapping-fptoint --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 --strip-producers -o tunnel.wasm tunnel.wasm && \ uw8 pack -l 9 tunnel.wasm tunnel.uw8 diff --git a/examples/rust/tunnel.rs b/examples/rust/tunnel.rs index 5f62d92..3c191a4 100644 --- a/examples/rust/tunnel.rs +++ b/examples/rust/tunnel.rs @@ -27,15 +27,6 @@ fn sqrt(v: f32) -> f32 { unsafe { core::intrinsics::sqrtf32(v) } } -fn ftoi(v: f32) -> i32 { - // The compiler is allowed to do bad things to our code if this - // ever results in a value that doesn't fit in an i32. - // (the joy of undefined behavior) - // But that would trap in wasm anyway, so we don't really - // care. - unsafe { v.to_int_unchecked() } -} - #[no_mangle] pub fn upd() { let mut i: i32 = 0; @@ -43,9 +34,9 @@ pub fn upd() { 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.); + let d = 40000 as f32 / sqrt(x * x + y * y); let u = atan2(x, y) * 512. / 3.141; - let c = (ftoi(d + t * 2.) ^ ftoi(u + t)) as u8 >> 4; + let c = ((d + t * 2.) as i32 ^ (u + t) as i32) as u8 >> 4; unsafe { *((120 + i) as *mut u8) = c; } diff --git a/examples/rust/tunnel.uw8 b/examples/rust/tunnel.uw8 index 17ff4da..8b635e3 100644 Binary files a/examples/rust/tunnel.uw8 and b/examples/rust/tunnel.uw8 differ diff --git a/examples/zig/main.zig b/examples/zig/main.zig index 4e6524a..102b6e5 100644 --- a/examples/zig/main.zig +++ b/examples/zig/main.zig @@ -9,7 +9,7 @@ export fn upd() void { var t = time() * 63.0; var x = @intToFloat(f32, (@intCast(i32, i % 320) - 160)); var y = @intToFloat(f32, (@intCast(i32, i / 320) - 120)); - var d = 40000.0 / @sqrt(x * x + y * y + 1.0); + var d = 40000.0 / @sqrt(x * x + y * y); var u = atan2(x, y) * 512.0 / 3.141; var c = @intCast(u8, (@floatToInt(i32, d + t * 2.0) ^ @floatToInt(i32, u + t)) & 255) >> 4;