From cf11f6c701322738a33f308e5ff276068ba17f5e Mon Sep 17 00:00:00 2001 From: "A.Olokhtonov" Date: Sat, 13 Jan 2024 00:59:33 +0300 Subject: [PATCH] Successfull sum function in wasm :D --- client/index.js | 3 +++ client/speed.js | 17 +++++++++++++++++ client/wasm/lod.c | 36 +++++++++++++++++++++++++++++------- client/wasm/lod.wasm | Bin 4528 -> 541 bytes 4 files changed, 49 insertions(+), 7 deletions(-) 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 d78724f6d643fb64a609e06672717d6e82778c7c..e9e084ae24d32198bde8f0b38b2229797a887b24 100755 GIT binary patch delta 353 zcmXYtzfQwI494xd1Q6G#iclw(3o!Hn5CdJvgv3iY*Cc8P`GZKIV!8|+I=0HpGr)84 z6gWKw+xOW&pY-|X`pcVv>1qT3oXP|sL5nsK2R(b-?>uE8{xAL6D7MGEciGL|pmzmE z2-WE~1}Al1w{M?f2*V5h*f&+xrhtm5$jvJ)O_xiQ-FppULX>qa6{g9mT#9p+W^E{M zGAWtw)+T+Ss@+JBNvDeERU!R=XjQdKQyqS9EpLLlL9{W%FcA+WDsJnj9>yv!NO}youifz5^eZ+l(IES Q2Oav#xfG+V;XeHM14O4)i2wiq literal 4528 zcmZvg2Ygi362^DV1`^mHpb?^?d;?Jy2nd#SRTQG3D2OQ7fn`IMz)d5`ML~%Ph)A!& zLK6fF*b9h)z1L^gXYc*ldwt*B-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