summaryrefslogtreecommitdiff
path: root/src/template/wasm2c.declarations.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/template/wasm2c.declarations.c')
-rw-r--r--src/template/wasm2c.declarations.c130
1 files changed, 130 insertions, 0 deletions
diff --git a/src/template/wasm2c.declarations.c b/src/template/wasm2c.declarations.c
index bd66cedb..3861c52d 100644
--- a/src/template/wasm2c.declarations.c
+++ b/src/template/wasm2c.declarations.c
@@ -31,6 +31,12 @@
#define MEMCHECK(mem, a, t) RANGE_CHECK(mem, a, sizeof(t))
#endif
+#ifdef __GNUC__
+#define wasm_asm __asm__
+#else
+#define wasm_asm(X)
+#endif
+
#if WABT_BIG_ENDIAN
static inline void load_data(void* dest, const void* src, size_t n) {
size_t i = 0;
@@ -53,6 +59,7 @@ static inline void load_data(void* dest, const void* src, size_t n) {
t1 result; \
wasm_rt_memcpy(&result, &mem->data[mem->size - addr - sizeof(t1)], \
sizeof(t1)); \
+ wasm_asm("" ::"r"(result)); \
return (t3)(t2)result; \
}
@@ -77,6 +84,7 @@ static inline void load_data(void* dest, const void* src, size_t n) {
MEMCHECK(mem, addr, t1); \
t1 result; \
wasm_rt_memcpy(&result, &mem->data[addr], sizeof(t1)); \
+ wasm_asm("" ::"r"(result)); \
return (t3)(t2)result; \
}
@@ -313,3 +321,125 @@ DEFINE_REINTERPRET(f32_reinterpret_i32, u32, f32)
DEFINE_REINTERPRET(i32_reinterpret_f32, f32, u32)
DEFINE_REINTERPRET(f64_reinterpret_i64, u64, f64)
DEFINE_REINTERPRET(i64_reinterpret_f64, f64, u64)
+
+static float quiet_nanf(float x) {
+ uint32_t tmp;
+ memcpy(&tmp, &x, 4);
+ tmp |= 0x7fc00000lu;
+ memcpy(&x, &tmp, 4);
+ return x;
+}
+
+static double quiet_nan(double x) {
+ uint64_t tmp;
+ memcpy(&tmp, &x, 8);
+ tmp |= 0x7ff8000000000000llu;
+ memcpy(&x, &tmp, 8);
+ return x;
+}
+
+static double wasm_quiet(double x) {
+ if (UNLIKELY(isnan(x))) {
+ return quiet_nan(x);
+ }
+ return x;
+}
+
+static float wasm_quietf(float x) {
+ if (UNLIKELY(isnan(x))) {
+ return quiet_nanf(x);
+ }
+ return x;
+}
+
+static double wasm_floor(double x) {
+ if (UNLIKELY(isnan(x))) {
+ return quiet_nan(x);
+ }
+ return floor(x);
+}
+
+static float wasm_floorf(float x) {
+ if (UNLIKELY(isnan(x))) {
+ return quiet_nanf(x);
+ }
+ return floorf(x);
+}
+
+static double wasm_ceil(double x) {
+ if (UNLIKELY(isnan(x))) {
+ return quiet_nan(x);
+ }
+ return ceil(x);
+}
+
+static float wasm_ceilf(float x) {
+ if (UNLIKELY(isnan(x))) {
+ return quiet_nanf(x);
+ }
+ return ceilf(x);
+}
+
+static double wasm_trunc(double x) {
+ if (UNLIKELY(isnan(x))) {
+ return quiet_nan(x);
+ }
+ return trunc(x);
+}
+
+static float wasm_truncf(float x) {
+ if (UNLIKELY(isnan(x))) {
+ return quiet_nanf(x);
+ }
+ return truncf(x);
+}
+
+static float wasm_nearbyintf(float x) {
+ if (UNLIKELY(isnan(x))) {
+ return quiet_nanf(x);
+ }
+ return nearbyintf(x);
+}
+
+static double wasm_nearbyint(double x) {
+ if (UNLIKELY(isnan(x))) {
+ return quiet_nan(x);
+ }
+ return nearbyint(x);
+}
+
+static float wasm_fabsf(float x) {
+ if (UNLIKELY(isnan(x))) {
+ uint32_t tmp;
+ memcpy(&tmp, &x, 4);
+ tmp = tmp & ~(1 << 31);
+ memcpy(&x, &tmp, 4);
+ return x;
+ }
+ return fabsf(x);
+}
+
+static double wasm_fabs(double x) {
+ if (UNLIKELY(isnan(x))) {
+ uint64_t tmp;
+ memcpy(&tmp, &x, 8);
+ tmp = tmp & ~(1ll << 63);
+ memcpy(&x, &tmp, 8);
+ return x;
+ }
+ return fabs(x);
+}
+
+static double wasm_sqrt(double x) {
+ if (UNLIKELY(isnan(x))) {
+ return quiet_nan(x);
+ }
+ return sqrt(x);
+}
+
+static float wasm_sqrtf(float x) {
+ if (UNLIKELY(isnan(x))) {
+ return quiet_nanf(x);
+ }
+ return sqrtf(x);
+}