diff --git a/client/index.js b/client/index.js index ce9976a..eecd128 100644 --- a/client/index.js +++ b/client/index.js @@ -286,4 +286,7 @@ function main() { schedule_draw(state, context); state.timers.offline_toast = setTimeout(() => ui_offline(), config.initial_offline_timeout); + + + test_wasm(); } diff --git a/client/speed.js b/client/speed.js index 5f3b617..9b0740c 100644 --- a/client/speed.js +++ b/client/speed.js @@ -1,3 +1,20 @@ +async function test_wasm() { + const memory = new WebAssembly.Memory({ initial: 10, maximum: 100 }); + const results = await WebAssembly.instantiateStreaming(fetch('wasm/lod.wasm')); + const numbers_offset = results.instance.exports.alloc(40); + + const numbers = new Uint32Array(results.instance.exports.memory.buffer, numbers_offset, 10); + + for (let i = 0; i < 10; ++i) { + numbers[i] = i; + } + + console.log(numbers); + + const sum = results.instance.exports.sum(numbers_offset, 10); + console.log(sum); +} + function rdp_find_max(state, zoom, coords_from, start, end) { // Finds a point from the range [start, end) with the maximum distance from the line (start--end) that is also further than EPS const EPS = 1.0 / zoom; diff --git a/client/wasm/lod.c b/client/wasm/lod.c index 947b717..db1c3c9 100644 --- a/client/wasm/lod.c +++ b/client/wasm/lod.c @@ -1,6 +1,27 @@ -float sqrtf(float x); -float fabsf(float x); +extern char __heap_base; +static int allocated; +void * +alloc(int size) +{ + void *result = &__heap_base + allocated; + allocated += size; + return(result); +} + +int +sum(int *numbers, int count) +{ + int result = 0; + + for (int i = 0; i < count; ++i) { + result += numbers[i]; + } + + return(result); +} + +#if 0 static int rdp_find_max(float *coordinates, float zoom, int coords_from, int segment_start, int segment_end) @@ -12,13 +33,13 @@ rdp_find_max(float *coordinates, float zoom, int coords_from, float ax = coordinates[coords_from + segment_start * 2 + 0]; float ay = coordinates[coords_from + segment_start * 2 + 1]; - float bx = coordinates[coords_from + segment_end * 2 + 0]; - float by = coordinates[coords_from + segment_end * 2 + 1]; + float bx = coordinates[coords_from + segment_end * 2 + 0]; + float by = coordinates[coords_from + segment_end * 2 + 1]; float dx = bx - ax; float dy = by - ay; - float dist_ab = sqrtf(dx * dx + dy * dy); + float dist_ab = __builtin_sqrtf(dx * dx + dy * dy); float dir_nx = dy / dist_ab; float dir_ny = -dx / dist_ab; @@ -29,7 +50,7 @@ rdp_find_max(float *coordinates, float zoom, int coords_from, float apx = px - ax; float apy = py - ay; - float dist = fabsf(apx * dir_nx + apy * dir_ny); + float dist = __builtin_fabsf(apx * dir_nx + apy * dir_ny); if (dist > EPS && dist > max_dist) { result = i; @@ -112,4 +133,5 @@ do_lod(int *clipped_indices, int clipped_count, float zoom, } return(segments_head); -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/client/wasm/lod.wasm b/client/wasm/lod.wasm index d78724f..e9e084a 100755 Binary files a/client/wasm/lod.wasm and b/client/wasm/lod.wasm differ