Browse Source

Successfull sum function in wasm :D

ssao
A.Olokhtonov 10 months ago
parent
commit
cf11f6c701
  1. 3
      client/index.js
  2. 17
      client/speed.js
  3. 36
      client/wasm/lod.c
  4. BIN
      client/wasm/lod.wasm

3
client/index.js

@ -286,4 +286,7 @@ function main() {
schedule_draw(state, context); schedule_draw(state, context);
state.timers.offline_toast = setTimeout(() => ui_offline(), config.initial_offline_timeout); state.timers.offline_toast = setTimeout(() => ui_offline(), config.initial_offline_timeout);
test_wasm();
} }

17
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) { 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 // 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; const EPS = 1.0 / zoom;

36
client/wasm/lod.c

@ -1,6 +1,27 @@
float sqrtf(float x); extern char __heap_base;
float fabsf(float x); 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 static int
rdp_find_max(float *coordinates, float zoom, int coords_from, rdp_find_max(float *coordinates, float zoom, int coords_from,
int segment_start, int segment_end) 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 ax = coordinates[coords_from + segment_start * 2 + 0];
float ay = coordinates[coords_from + segment_start * 2 + 1]; float ay = coordinates[coords_from + segment_start * 2 + 1];
float bx = coordinates[coords_from + segment_end * 2 + 0]; float bx = coordinates[coords_from + segment_end * 2 + 0];
float by = coordinates[coords_from + segment_end * 2 + 1]; float by = coordinates[coords_from + segment_end * 2 + 1];
float dx = bx - ax; float dx = bx - ax;
float dy = by - ay; 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_nx = dy / dist_ab;
float dir_ny = -dx / 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 apx = px - ax;
float apy = py - ay; 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) { if (dist > EPS && dist > max_dist) {
result = i; result = i;
@ -112,4 +133,5 @@ do_lod(int *clipped_indices, int clipped_count, float zoom,
} }
return(segments_head); return(segments_head);
} }
#endif

BIN
client/wasm/lod.wasm

Binary file not shown.
Loading…
Cancel
Save