mirror of
https://github.com/exoticorn/microw8.git
synced 2026-01-20 11:16:42 +01:00
move all imports to module "env", some tweaks to rust example
This commit is contained in:
@@ -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
|
||||
339 byte tunnel.wasm. Using wasm-opt this can be reduced to
|
||||
244 bytes.
|
||||
342 byte tunnel.wasm. Using wasm-opt this can be reduced to
|
||||
243 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 200 byte wasm file. Running this through
|
||||
This gives us a 199 byte wasm file. Running this through
|
||||
uw8-tool pack brings us to the final size of 137 bytes.
|
||||
@@ -1,25 +1,43 @@
|
||||
#![no_std]
|
||||
#![feature(core_intrinsics)]
|
||||
|
||||
#[link(wasm_import_module = "math")]
|
||||
extern "C" {
|
||||
fn atan2(x: f32, y: f32) -> f32;
|
||||
mod env {
|
||||
// "env" is the default module for imports, but it is still needed here
|
||||
// since there is a compiler builtin of the same name which is used
|
||||
// if we don't make it clear that this is a module import.
|
||||
#[link(wasm_import_module = "env")]
|
||||
extern "C" {
|
||||
pub fn atan2(x: f32, y: f32) -> f32;
|
||||
}
|
||||
}
|
||||
|
||||
fn atan2(x: f32, y: f32) -> f32 {
|
||||
unsafe { env::atan2(x, y) }
|
||||
}
|
||||
|
||||
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 tic(time: i32) {
|
||||
unsafe {
|
||||
for i in 0..320 * 256 {
|
||||
let t = time as f32 / 10 as f32;
|
||||
let x = (i % 320 - 160) as f32;
|
||||
let y = (i / 320 - 128) as f32;
|
||||
let d = 20000 as f32 / sqrt(x * x + y * y + 1 as f32);
|
||||
let u = atan2(x, y) * 512f32 / 3.141;
|
||||
let c = ((d + t).to_int_unchecked::<i32>() ^ (u + t).to_int_unchecked::<i32>()) as u8;
|
||||
for i in 0..320 * 256 {
|
||||
let t = time as f32 / 10 as f32;
|
||||
let x = (i % 320 - 160) as f32;
|
||||
let y = (i / 320 - 128) as f32;
|
||||
let d = 20000 as f32 / sqrt(x * x + y * y + 1 as f32);
|
||||
let u = atan2(x, y) * 512f32 / 3.141;
|
||||
let c = (ftoi(d + t) ^ ftoi(u + t)) as u8;
|
||||
unsafe {
|
||||
*((120 + i) as *mut u8) = c;
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user