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 50c01f3..17ff4da 100644 Binary files a/examples/rust/tunnel.uw8 and b/examples/rust/tunnel.uw8 differ diff --git a/examples/zig/.gitignore b/examples/zig/.gitignore new file mode 100644 index 0000000..7608bcb --- /dev/null +++ b/examples/zig/.gitignore @@ -0,0 +1,2 @@ +/zig-cache/ +/zig-out/ \ No newline at end of file diff --git a/examples/zig/build.zig b/examples/zig/build.zig new file mode 100644 index 0000000..8deb34c --- /dev/null +++ b/examples/zig/build.zig @@ -0,0 +1,37 @@ +const std = @import("std"); + +pub fn build(b: *std.build.Builder) void { + const mode = std.builtin.Mode.ReleaseSmall; + + const lib = b.addSharedLibrary("cart", "main.zig", .unversioned); + lib.setBuildMode(mode); + lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); + lib.import_memory = true; + lib.initial_memory = 262144; + lib.max_memory = 262144; + lib.global_base = 81920; + lib.stack_size = 8192; + lib.install(); + + if (lib.install_step) |install_step| { + const run_filter_exports = b.addSystemCommand(&[_][]const u8{ + "uw8", "filter-exports", "zig-out/lib/cart.wasm", "zig-out/lib/cart-filtered.wasm" + }); + run_filter_exports.step.dependOn(&install_step.step); + + const run_wasm_opt = b.addSystemCommand(&[_][]const u8{ + "wasm-opt", "-Oz", "-o", "zig-out/cart.wasm", "zig-out/lib/cart-filtered.wasm" + }); + run_wasm_opt.step.dependOn(&run_filter_exports.step); + + const run_uw8_pack = b.addSystemCommand(&[_][]const u8{ + "uw8", "pack", "-l", "9", "zig-out/cart.wasm", "zig-out/cart.uw8" + }); + run_uw8_pack.step.dependOn(&run_wasm_opt.step); + + const make_opt = b.step("make_opt", "make size optimized cart"); + make_opt.dependOn(&run_uw8_pack.step); + + b.default_step = make_opt; + } +} diff --git a/examples/zig/main.zig b/examples/zig/main.zig new file mode 100644 index 0000000..4e6524a --- /dev/null +++ b/examples/zig/main.zig @@ -0,0 +1,20 @@ +extern fn atan2(x: f32, y: f32) f32; +extern fn time() f32; + +pub const FRAMEBUFFER: *[320*240]u8 = @intToPtr(*[320*240]u8, 120); + +export fn upd() void { + var i: u32 = 0; + while(true) { + 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 u = atan2(x, y) * 512.0 / 3.141; + var c = @intCast(u8, (@floatToInt(i32, d + t * 2.0) ^ @floatToInt(i32, u + t)) & 255) >> 4; + + FRAMEBUFFER[@as(usize, i)] = c; + i += 1; + if(i >= 320*240) { break; } + } +} \ No newline at end of file