Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/system/lib/jsmath.c
4150 views
1
//
2
// See JS_MATH setting in settings.js for details.
3
//
4
5
#include <emscripten/em_math.h>
6
#include <emscripten/em_js.h>
7
#include <math.h>
8
#include <stdlib.h>
9
10
#define CALL_JS_1(cname, jsname, type) \
11
type cname(type x) { return (type)emscripten_math_##jsname(x); }
12
13
#define CALL_JS_1_TRIPLE(name) \
14
CALL_JS_1(name, name, double) \
15
CALL_JS_1(name##f, name, float)
16
17
CALL_JS_1_TRIPLE(cos)
18
CALL_JS_1_TRIPLE(sin)
19
CALL_JS_1_TRIPLE(tan)
20
CALL_JS_1_TRIPLE(acos)
21
CALL_JS_1_TRIPLE(asin)
22
CALL_JS_1_TRIPLE(atan)
23
CALL_JS_1_TRIPLE(exp)
24
CALL_JS_1_TRIPLE(log)
25
CALL_JS_1_TRIPLE(sqrt)
26
27
#define CALL_JS_2(cname, jsname, type) \
28
type cname(type x, type y) { return (type)emscripten_math_##jsname(x, y); }
29
30
#define CALL_JS_2_TRIPLE(name) \
31
CALL_JS_2(name, name, double) \
32
CALL_JS_2(name##f, name, float)
33
34
CALL_JS_2_TRIPLE(atan2)
35
CALL_JS_2_TRIPLE(pow)
36
37
#define CALL_JS_1_IMPL(cname, type, impl) \
38
EM_JS(type, JS_##cname, (type x), impl); \
39
type cname(type x) { return JS_##cname(x); }
40
41
#define CALL_JS_1_IMPL_TRIPLE(cname, impl) \
42
CALL_JS_1_IMPL(cname, double, impl) \
43
CALL_JS_1_IMPL(cname##f, float, impl)
44
45
CALL_JS_1_IMPL_TRIPLE(round, {
46
return x >= 0 ? Math.floor(x + 0.5) : Math.ceil(x - 0.5);
47
})
48
49