Skip to content

Commit

Permalink
Use em_math.h functions in jsmath.c (#23147)
Browse files Browse the repository at this point in the history
Unlike the current EM_JS implementations, the `em_math.h` functions map
directly to `Math.xxx` without a wrapper function.

The other advantage of doing it this way is that we avoid duplicate
implementations of all these functions.

Fixes: #19284
  • Loading branch information
sbc100 authored Dec 13, 2024
1 parent 53c8132 commit 5efa98e
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions system/lib/jsmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,37 @@
// See JS_MATH setting in settings.js for details.
//

#include <emscripten.h>
#include <emscripten/em_math.h>
#include <emscripten/em_js.h>
#include <math.h>
#include <stdlib.h>

#define CALL_JS_1(cname, jsname, type) \
EM_JS(type, JS_##cname, (type x), { return jsname(x) }); \
type cname(type x) { return JS_##cname(x); }

#define CALL_JS_1_TRIPLE(cname, jsname) \
CALL_JS_1(cname, jsname, double) \
CALL_JS_1(cname##f, jsname, float)

CALL_JS_1_TRIPLE(cos, Math.cos)
CALL_JS_1_TRIPLE(sin, Math.sin)
CALL_JS_1_TRIPLE(tan, Math.tan)
CALL_JS_1_TRIPLE(acos, Math.acos)
CALL_JS_1_TRIPLE(asin, Math.asin)
CALL_JS_1_TRIPLE(atan, Math.atan)
CALL_JS_1_TRIPLE(exp, Math.exp)
CALL_JS_1_TRIPLE(log, Math.log)
CALL_JS_1_TRIPLE(sqrt, Math.sqrt)
type cname(type x) { return (type)emscripten_math_##jsname(x); }

#define CALL_JS_1_TRIPLE(name) \
CALL_JS_1(name, name, double) \
CALL_JS_1(name##f, name, float)

CALL_JS_1_TRIPLE(cos)
CALL_JS_1_TRIPLE(sin)
CALL_JS_1_TRIPLE(tan)
CALL_JS_1_TRIPLE(acos)
CALL_JS_1_TRIPLE(asin)
CALL_JS_1_TRIPLE(atan)
CALL_JS_1_TRIPLE(exp)
CALL_JS_1_TRIPLE(log)
CALL_JS_1_TRIPLE(sqrt)

#define CALL_JS_2(cname, jsname, type) \
EM_JS(type, JS_##cname, (type x, type y), { return jsname(x, y) }); \
type cname(type x, type y) { return JS_##cname(x, y); }
type cname(type x, type y) { return (type)emscripten_math_##jsname(x, y); }

#define CALL_JS_2_TRIPLE(cname, jsname) \
CALL_JS_2(cname, jsname, double) \
CALL_JS_2(cname##f, jsname, float)
#define CALL_JS_2_TRIPLE(name) \
CALL_JS_2(name, name, double) \
CALL_JS_2(name##f, name, float)

CALL_JS_2_TRIPLE(atan2, Math.atan2)
CALL_JS_2_TRIPLE(pow, Math.pow)
CALL_JS_2_TRIPLE(atan2)
CALL_JS_2_TRIPLE(pow)

#define CALL_JS_1_IMPL(cname, type, impl) \
EM_JS(type, JS_##cname, (type x), impl); \
Expand Down

0 comments on commit 5efa98e

Please sign in to comment.