From 20365a0dd0de89a762222509d8077a9b2834476e Mon Sep 17 00:00:00 2001 From: Dennis Ranke Date: Sun, 2 Jan 2022 15:22:11 +0100 Subject: [PATCH] port tunnel example to C --- examples/c/build.sh | 6 ++++++ examples/c/cart.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100755 examples/c/build.sh create mode 100644 examples/c/cart.c diff --git a/examples/c/build.sh b/examples/c/build.sh new file mode 100755 index 0000000..c23d192 --- /dev/null +++ b/examples/c/build.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +clang -O2 -Wno-incompatible-library-redeclaration --no-standard-libraries -ffast-math -Wl,--no-entry -Wl,--export-all -Wl,--import-memory -Wl,--initial-memory=262144 -Wl,-zstack-size=90000 -o cart.wasm cart.c --target=wasm32 && \ +uw8 filter-exports cart.wasm cart.wasm && \ +wasm-opt -Oz --fast-math --strip-producers -o cart.wasm cart.wasm && \ +uw8 pack -l 9 cart.wasm cart.uw8 \ No newline at end of file diff --git a/examples/c/cart.c b/examples/c/cart.c new file mode 100644 index 0000000..1f065fe --- /dev/null +++ b/examples/c/cart.c @@ -0,0 +1,31 @@ +#define IMPORT(MODULE, NAME) __attribute__((import_module(MODULE), import_name(NAME))) + +IMPORT("env", "atan2") extern float atan2(float, float); +IMPORT("env", "time") extern float time(); + +int ftoi(float v) { + return __builtin_wasm_trunc_s_i32_f32(v); +} + +float sqrt(float v) { + return __builtin_sqrt(v); +} + +#define FRAMEBUFFER ((unsigned char*)120) + +void upd() { + int i = 0; + + for( ;; ) { + 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 u = atan2(x, y) * 512.0f / 3.141f; + unsigned char c = (unsigned char)(ftoi(d + t * 2.0f) ^ ftoi(u + t)) >> 4; + FRAMEBUFFER[i] = c; + + i += 1; + if(i >= 320*240) break; + } +} \ No newline at end of file