make use of nontrapping-fptoint, add tunnel.cwa as reference

This commit is contained in:
2022-01-02 16:34:11 +01:00
parent acea5cb6e0
commit 10ba7ed3bb
6 changed files with 24 additions and 14 deletions

View File

@@ -16,7 +16,7 @@ void upd() {
float t = time() * 63.0f; float t = time() * 63.0f;
float x = (float)(i % 320 - 160); float x = (float)(i % 320 - 160);
float y = (float)(i / 320 - 120); 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; float u = atan2(x, y) * 512.0f / 3.141f;
unsigned char c = (unsigned char)((int)(d + t * 2.0f) ^ (int)(u + t)) >> 4; unsigned char c = (unsigned char)((int)(d + t * 2.0f) ^ (int)(u + t)) >> 4;
FRAMEBUFFER[i] = c; FRAMEBUFFER[i] = c;

View File

@@ -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;
}
}

View File

@@ -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 && \ uw8 filter-exports tunnel.wasm tunnel.wasm && \
wasm-opt -Oz --strip-producers -o tunnel.wasm tunnel.wasm && \ wasm-opt -Oz --strip-producers -o tunnel.wasm tunnel.wasm && \
uw8 pack -l 9 tunnel.wasm tunnel.uw8 uw8 pack -l 9 tunnel.wasm tunnel.uw8

View File

@@ -27,15 +27,6 @@ fn sqrt(v: f32) -> f32 {
unsafe { core::intrinsics::sqrtf32(v) } 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] #[no_mangle]
pub fn upd() { pub fn upd() {
let mut i: i32 = 0; let mut i: i32 = 0;
@@ -43,9 +34,9 @@ pub fn upd() {
let t = time() * 63.; let t = time() * 63.;
let x = (i % 320 - 160) as f32; let x = (i % 320 - 160) as f32;
let y = (i / 320 - 120) 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 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 { unsafe {
*((120 + i) as *mut u8) = c; *((120 + i) as *mut u8) = c;
} }

Binary file not shown.

View File

@@ -9,7 +9,7 @@ export fn upd() void {
var t = time() * 63.0; var t = time() * 63.0;
var x = @intToFloat(f32, (@intCast(i32, i % 320) - 160)); var x = @intToFloat(f32, (@intCast(i32, i % 320) - 160));
var y = @intToFloat(f32, (@intCast(i32, i / 320) - 120)); 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 u = atan2(x, y) * 512.0 / 3.141;
var c = @intCast(u8, (@floatToInt(i32, d + t * 2.0) ^ @floatToInt(i32, u + t)) & 255) >> 4; var c = @intCast(u8, (@floatToInt(i32, d + t * 2.0) ^ @floatToInt(i32, u + t)) & 255) >> 4;