diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/c-writer.cc | 2 | ||||
-rw-r--r-- | src/prebuilt/wasm2c.include.c | 206 | ||||
-rw-r--r-- | src/prebuilt/wasm2c.include.h | 1 | ||||
-rw-r--r-- | src/wasm2c.c.tmpl | 206 | ||||
-rw-r--r-- | src/wasm2c.h.tmpl | 1 |
5 files changed, 232 insertions, 184 deletions
diff --git a/src/c-writer.cc b/src/c-writer.cc index 16a8e49d..ce247deb 100644 --- a/src/c-writer.cc +++ b/src/c-writer.cc @@ -792,6 +792,7 @@ void CWriter::WriteSourceTop() { Write(s_source_includes); Write(Newline(), "#include \"", header_name_, "\"", Newline()); Write(s_source_declarations); + Write(Newline()); } void CWriter::WriteMultivalueTypes() { @@ -2377,6 +2378,7 @@ void CWriter::WriteCHeader() { Write("#define ", guard, Newline()); Write(Newline()); Write(s_header_top); + Write(Newline()); WriteMultivalueTypes(); WriteImports(); Write("void ", module_prefix_, "_init(void);", Newline()); diff --git a/src/prebuilt/wasm2c.include.c b/src/prebuilt/wasm2c.include.c index 83d28a87..85ac55cb 100644 --- a/src/prebuilt/wasm2c.include.c +++ b/src/prebuilt/wasm2c.include.c @@ -16,21 +16,22 @@ const char SECTION_NAME(declarations)[] = "#else\n" "#define FUNC_PROLOGUE \\\n" " if (++wasm_rt_call_stack_depth > WASM_RT_MAX_CALL_STACK_DEPTH) \\\n" -" TRAP(EXHAUSTION)\n" +" TRAP(EXHAUSTION);\n" "\n" "#define FUNC_EPILOGUE --wasm_rt_call_stack_depth\n" "#endif\n" "\n" "#define UNREACHABLE TRAP(UNREACHABLE)\n" "\n" -"#define CALL_INDIRECT(table, t, ft, x, ...) \\\n" -" (LIKELY((x) < table.size && table.data[x].func && \\\n" -" table.data[x].func_type == func_types[ft]) \\\n" -" || TRAP(CALL_INDIRECT) \\\n" -" , ((t)table.data[x].func)(__VA_ARGS__))\n" +"#define CALL_INDIRECT(table, t, ft, x, ...) \\\n" +" (LIKELY((x) < table.size && table.data[x].func && \\\n" +" table.data[x].func_type == func_types[ft]) || \\\n" +" TRAP(CALL_INDIRECT), \\\n" +" ((t)table.data[x].func)(__VA_ARGS__))\n" "\n" -"#define RANGE_CHECK(mem, offset, len) \\\n" -" if (UNLIKELY(offset + (uint64_t)len > mem->size)) TRAP(OOB)\n" +"#define RANGE_CHECK(mem, offset, len) \\\n" +" if (UNLIKELY(offset + (uint64_t)len > mem->size)) \\\n" +" TRAP(OOB);\n" "\n" "#if WASM_RT_MEMCHECK_SIGNAL_HANDLER\n" "#define MEMCHECK(mem, a, t)\n" @@ -39,11 +40,11 @@ const char SECTION_NAME(declarations)[] = "#endif\n" "\n" "#if WABT_BIG_ENDIAN\n" -"static inline void load_data(void *dest, const void *src, size_t n) {\n" +"static inline void load_data(void* dest, const void* src, size_t n) {\n" " size_t i = 0;\n" -" u8 *dest_chars = dest;\n" +" u8* dest_chars = dest;\n" " memcpy(dest, src, n);\n" -" for (i = 0; i < (n>>1); i++) {\n" +" for (i = 0; i < (n >> 1); i++) {\n" " u8 cursor = dest_chars[i];\n" " dest_chars[i] = dest_chars[n - i - 1];\n" " dest_chars[n - i - 1] = cursor;\n" @@ -71,11 +72,12 @@ const char SECTION_NAME(declarations)[] = " sizeof(t1)); \\\n" " }\n" "#else\n" -"static inline void load_data(void *dest, const void *src, size_t n) {\n" +"static inline void load_data(void* dest, const void* src, size_t n) {\n" " memcpy(dest, src, n);\n" "}\n" -"#define LOAD_DATA(m, o, i, s) do { \\\n" -" RANGE_CHECK((&m), o, s); \\\n" +"#define LOAD_DATA(m, o, i, s) \\\n" +" do { \\\n" +" RANGE_CHECK((&m), o, s); \\\n" " load_data(&(m.data[o]), i, s); \\\n" " } while (0)\n" "#define DEFINE_LOAD(name, t1, t2, t3) \\\n" @@ -132,9 +134,9 @@ const char SECTION_NAME(declarations)[] = " return 63 - r;\n" " }\n" "#else\n" -" if (_BitScanReverse(&r, (unsigned long) (v >> 32))) {\n" +" if (_BitScanReverse(&r, (unsigned long)(v >> 32))) {\n" " return 31 - r;\n" -" } else if (_BitScanReverse(&r, (unsigned long) v)) {\n" +" } else if (_BitScanReverse(&r, (unsigned long)v)) {\n" " return 63 - r;\n" " }\n" "#endif\n" @@ -156,14 +158,14 @@ const char SECTION_NAME(declarations)[] = " unsigned long r = 0;\n" "#if defined(_M_AMD64) || defined(_M_ARM)\n" " _BitScanForward64(&r, v);\n" -" return (int) r;\n" +" return (int)r;\n" "#else\n" -" if (_BitScanForward(&r, (unsigned int) (v))) {\n" -" return (int) (r);\n" +" if (_BitScanForward(&r, (unsigned int)(v))) {\n" +" return (int)(r);\n" " }\n" "\n" -" _BitScanForward(&r, (unsigned int) (v >> 32));\n" -" return (int) (r + 32);\n" +" _BitScanForward(&r, (unsigned int)(v >> 32));\n" +" return (int)(r + 32);\n" "#endif\n" "}\n" "\n" @@ -173,15 +175,15 @@ const char SECTION_NAME(declarations)[] = " }\n" " unsigned long r = 0;\n" " _BitScanForward(&r, v);\n" -" return (int) r;\n" +" return (int)r;\n" "}\n" "\n" -"#define POPCOUNT_DEFINE_PORTABLE(f_n, T) \\\n" -" static inline u32 f_n(T x) { \\\n" -" x = x - ((x >> 1) & (T)~(T)0/3); \\\n" -" x = (x & (T)~(T)0/15*3) + ((x >> 2) & (T)~(T)0/15*3); \\\n" -" x = (x + (x >> 4)) & (T)~(T)0/255*15; \\\n" -" return (T)(x * ((T)~(T)0/255)) >> (sizeof(T) - 1) * 8; \\\n" +"#define POPCOUNT_DEFINE_PORTABLE(f_n, T) \\\n" +" static inline u32 f_n(T x) { \\\n" +" x = x - ((x >> 1) & (T) ~(T)0 / 3); \\\n" +" x = (x & (T) ~(T)0 / 15 * 3) + ((x >> 2) & (T) ~(T)0 / 15 * 3); \\\n" +" x = (x + (x >> 4)) & (T) ~(T)0 / 255 * 15; \\\n" +" return (T)(x * ((T) ~(T)0 / 255)) >> (sizeof(T) - 1) * 8; \\\n" " }\n" "\n" "POPCOUNT_DEFINE_PORTABLE(I32_POPCNT, u32)\n" @@ -200,15 +202,16 @@ const char SECTION_NAME(declarations)[] = "\n" "#endif\n" "\n" -"#define DIV_S(ut, min, x, y) \\\n" -" ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) \\\n" -" : (UNLIKELY((x) == min && (y) == -1)) ? TRAP(INT_OVERFLOW) \\\n" -" : (ut)((x) / (y)))\n" +"#define DIV_S(ut, min, x, y) \\\n" +" ((UNLIKELY((y) == 0)) \\\n" +" ? TRAP(DIV_BY_ZERO) \\\n" +" : (UNLIKELY((x) == min && (y) == -1)) ? TRAP(INT_OVERFLOW) \\\n" +" : (ut)((x) / (y)))\n" "\n" -"#define REM_S(ut, min, x, y) \\\n" -" ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) \\\n" -" : (UNLIKELY((x) == min && (y) == -1)) ? 0 \\\n" -" : (ut)((x) % (y)))\n" +"#define REM_S(ut, min, x, y) \\\n" +" ((UNLIKELY((y) == 0)) \\\n" +" ? TRAP(DIV_BY_ZERO) \\\n" +" : (UNLIKELY((x) == min && (y) == -1)) ? 0 : (ut)((x) % (y)))\n" "\n" "#define I32_DIV_S(x, y) DIV_S(u32, INT32_MIN, (s32)x, (s32)y)\n" "#define I64_DIV_S(x, y) DIV_S(u64, INT64_MIN, (s64)x, (s64)y)\n" @@ -216,7 +219,7 @@ const char SECTION_NAME(declarations)[] = "#define I64_REM_S(x, y) REM_S(u64, INT64_MIN, (s64)x, (s64)y)\n" "\n" "#define DIVREM_U(op, x, y) \\\n" -" ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) : ((x) op (y)))\n" +" ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) : ((x)op(y)))\n" "\n" "#define DIV_U(x, y) DIVREM_U(/, x, y)\n" "#define REM_U(x, y) DIVREM_U(%, x, y)\n" @@ -231,70 +234,91 @@ const char SECTION_NAME(declarations)[] = "#define I32_ROTR(x, y) ROTR(x, y, 31)\n" "#define I64_ROTR(x, y) ROTR(x, y, 63)\n" "\n" -"#define FMIN(x, y) \\\n" -" ((UNLIKELY((x) != (x))) ? NAN \\\n" -" : (UNLIKELY((y) != (y))) ? NAN \\\n" -" : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? x : y) \\\n" -" : (x < y) ? x : y)\n" -"\n" -"#define FMAX(x, y) \\\n" -" ((UNLIKELY((x) != (x))) ? NAN \\\n" -" : (UNLIKELY((y) != (y))) ? NAN \\\n" -" : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? y : x) \\\n" -" : (x > y) ? x : y)\n" -"\n" -"#define TRUNC_S(ut, st, ft, min, minop, max, x) \\\n" -" ((UNLIKELY((x) != (x))) ? TRAP(INVALID_CONVERSION) \\\n" -" : (UNLIKELY(!((x)minop(min) && (x) < (max)))) ? TRAP(INT_OVERFLOW) \\\n" -" : (ut)(st)(x))\n" -"\n" -"#define I32_TRUNC_S_F32(x) TRUNC_S(u32, s32, f32, (f32)INT32_MIN, >=, 2147483648.f, x)\n" -"#define I64_TRUNC_S_F32(x) TRUNC_S(u64, s64, f32, (f32)INT64_MIN, >=, (f32)INT64_MAX, x)\n" -"#define I32_TRUNC_S_F64(x) TRUNC_S(u32, s32, f64, -2147483649., >, 2147483648., x)\n" -"#define I64_TRUNC_S_F64(x) TRUNC_S(u64, s64, f64, (f64)INT64_MIN, >=, (f64)INT64_MAX, x)\n" -"\n" -"#define TRUNC_U(ut, ft, max, x) \\\n" -" ((UNLIKELY((x) != (x))) ? TRAP(INVALID_CONVERSION) \\\n" -" : (UNLIKELY(!((x) > (ft)-1 && (x) < (max)))) ? TRAP(INT_OVERFLOW) \\\n" -" : (ut)(x))\n" +"#define FMIN(x, y) \\\n" +" ((UNLIKELY((x) != (x))) \\\n" +" ? NAN \\\n" +" : (UNLIKELY((y) != (y))) \\\n" +" ? NAN \\\n" +" : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? x : y) \\\n" +" : (x < y) ? x : y)\n" +"\n" +"#define FMAX(x, y) \\\n" +" ((UNLIKELY((x) != (x))) \\\n" +" ? NAN \\\n" +" : (UNLIKELY((y) != (y))) \\\n" +" ? NAN \\\n" +" : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? y : x) \\\n" +" : (x > y) ? x : y)\n" +"\n" +"#define TRUNC_S(ut, st, ft, min, minop, max, x) \\\n" +" ((UNLIKELY((x) != (x))) \\\n" +" ? TRAP(INVALID_CONVERSION) \\\n" +" : (UNLIKELY(!((x)minop(min) && (x) < (max)))) ? TRAP(INT_OVERFLOW) \\\n" +" : (ut)(st)(x))\n" +"\n" +"#define I32_TRUNC_S_F32(x) \\\n" +" TRUNC_S(u32, s32, f32, (f32)INT32_MIN, >=, 2147483648.f, x)\n" +"#define I64_TRUNC_S_F32(x) \\\n" +" TRUNC_S(u64, s64, f32, (f32)INT64_MIN, >=, (f32)INT64_MAX, x)\n" +"#define I32_TRUNC_S_F64(x) \\\n" +" TRUNC_S(u32, s32, f64, -2147483649., >, 2147483648., x)\n" +"#define I64_TRUNC_S_F64(x) \\\n" +" TRUNC_S(u64, s64, f64, (f64)INT64_MIN, >=, (f64)INT64_MAX, x)\n" +"\n" +"#define TRUNC_U(ut, ft, max, x) \\\n" +" ((UNLIKELY((x) != (x))) \\\n" +" ? TRAP(INVALID_CONVERSION) \\\n" +" : (UNLIKELY(!((x) > (ft)-1 && (x) < (max)))) ? TRAP(INT_OVERFLOW) \\\n" +" : (ut)(x))\n" "\n" "#define I32_TRUNC_U_F32(x) TRUNC_U(u32, f32, 4294967296.f, x)\n" "#define I64_TRUNC_U_F32(x) TRUNC_U(u64, f32, (f32)UINT64_MAX, x)\n" -"#define I32_TRUNC_U_F64(x) TRUNC_U(u32, f64, 4294967296., x)\n" +"#define I32_TRUNC_U_F64(x) TRUNC_U(u32, f64, 4294967296., x)\n" "#define I64_TRUNC_U_F64(x) TRUNC_U(u64, f64, (f64)UINT64_MAX, x)\n" "\n" "#define TRUNC_SAT_S(ut, st, ft, min, smin, minop, max, smax, x) \\\n" -" ((UNLIKELY((x) != (x))) ? 0 \\\n" -" : (UNLIKELY(!((x)minop(min)))) ? smin \\\n" -" : (UNLIKELY(!((x) < (max)))) ? smax \\\n" -" : (ut)(st)(x))\n" -"\n" -"#define I32_TRUNC_SAT_S_F32(x) TRUNC_SAT_S(u32, s32, f32, (f32)INT32_MIN, INT32_MIN, >=, 2147483648.f, INT32_MAX, x)\n" -"#define I64_TRUNC_SAT_S_F32(x) TRUNC_SAT_S(u64, s64, f32, (f32)INT64_MIN, INT64_MIN, >=, (f32)INT64_MAX, INT64_MAX, x)\n" -"#define I32_TRUNC_SAT_S_F64(x) TRUNC_SAT_S(u32, s32, f64, -2147483649., INT32_MIN, >, 2147483648., INT32_MAX, x)\n" -"#define I64_TRUNC_SAT_S_F64(x) TRUNC_SAT_S(u64, s64, f64, (f64)INT64_MIN, INT64_MIN, >=, (f64)INT64_MAX, INT64_MAX, x)\n" -"\n" -"#define TRUNC_SAT_U(ut, ft, max, smax, x) \\\n" -" ((UNLIKELY((x) != (x))) ? 0 \\\n" -" : (UNLIKELY(!((x) > (ft)-1))) ? 0 \\\n" -" : (UNLIKELY(!((x) < (max)))) ? smax \\\n" -" : (ut)(x))\n" -"\n" -"#define I32_TRUNC_SAT_U_F32(x) TRUNC_SAT_U(u32, f32, 4294967296.f, UINT32_MAX, x)\n" -"#define I64_TRUNC_SAT_U_F32(x) TRUNC_SAT_U(u64, f32, (f32)UINT64_MAX, UINT64_MAX, x)\n" -"#define I32_TRUNC_SAT_U_F64(x) TRUNC_SAT_U(u32, f64, 4294967296., UINT32_MAX, x)\n" -"#define I64_TRUNC_SAT_U_F64(x) TRUNC_SAT_U(u64, f64, (f64)UINT64_MAX, UINT64_MAX, x)\n" -"\n" -"#define DEFINE_REINTERPRET(name, t1, t2) \\\n" -" static inline t2 name(t1 x) { \\\n" -" t2 result; \\\n" -" memcpy(&result, &x, sizeof(result)); \\\n" -" return result; \\\n" +" ((UNLIKELY((x) != (x))) \\\n" +" ? 0 \\\n" +" : (UNLIKELY(!((x)minop(min)))) \\\n" +" ? smin \\\n" +" : (UNLIKELY(!((x) < (max)))) ? smax : (ut)(st)(x))\n" +"\n" +"#define I32_TRUNC_SAT_S_F32(x) \\\n" +" TRUNC_SAT_S(u32, s32, f32, (f32)INT32_MIN, INT32_MIN, >=, 2147483648.f, \\\n" +" INT32_MAX, x)\n" +"#define I64_TRUNC_SAT_S_F32(x) \\\n" +" TRUNC_SAT_S(u64, s64, f32, (f32)INT64_MIN, INT64_MIN, >=, (f32)INT64_MAX, \\\n" +" INT64_MAX, x)\n" +"#define I32_TRUNC_SAT_S_F64(x) \\\n" +" TRUNC_SAT_S(u32, s32, f64, -2147483649., INT32_MIN, >, 2147483648., \\\n" +" INT32_MAX, x)\n" +"#define I64_TRUNC_SAT_S_F64(x) \\\n" +" TRUNC_SAT_S(u64, s64, f64, (f64)INT64_MIN, INT64_MIN, >=, (f64)INT64_MAX, \\\n" +" INT64_MAX, x)\n" +"\n" +"#define TRUNC_SAT_U(ut, ft, max, smax, x) \\\n" +" ((UNLIKELY((x) != (x))) ? 0 \\\n" +" : (UNLIKELY(!((x) > (ft)-1))) \\\n" +" ? 0 \\\n" +" : (UNLIKELY(!((x) < (max)))) ? smax : (ut)(x))\n" +"\n" +"#define I32_TRUNC_SAT_U_F32(x) \\\n" +" TRUNC_SAT_U(u32, f32, 4294967296.f, UINT32_MAX, x)\n" +"#define I64_TRUNC_SAT_U_F32(x) \\\n" +" TRUNC_SAT_U(u64, f32, (f32)UINT64_MAX, UINT64_MAX, x)\n" +"#define I32_TRUNC_SAT_U_F64(x) TRUNC_SAT_U(u32, f64, 4294967296., UINT32_MAX, x)\n" +"#define I64_TRUNC_SAT_U_F64(x) \\\n" +" TRUNC_SAT_U(u64, f64, (f64)UINT64_MAX, UINT64_MAX, x)\n" +"\n" +"#define DEFINE_REINTERPRET(name, t1, t2) \\\n" +" static inline t2 name(t1 x) { \\\n" +" t2 result; \\\n" +" memcpy(&result, &x, sizeof(result)); \\\n" +" return result; \\\n" " }\n" "\n" "DEFINE_REINTERPRET(f32_reinterpret_i32, u32, f32)\n" "DEFINE_REINTERPRET(i32_reinterpret_f32, f32, u32)\n" "DEFINE_REINTERPRET(f64_reinterpret_i64, u64, f64)\n" "DEFINE_REINTERPRET(i64_reinterpret_f64, f64, u64)\n" -"\n" ; diff --git a/src/prebuilt/wasm2c.include.h b/src/prebuilt/wasm2c.include.h index 239dd539..d1f300b3 100644 --- a/src/prebuilt/wasm2c.include.h +++ b/src/prebuilt/wasm2c.include.h @@ -22,7 +22,6 @@ const char SECTION_NAME(top)[] = "#ifdef __cplusplus\n" "extern \"C\" {\n" "#endif\n" -"\n" ; const char SECTION_NAME(bottom)[] = diff --git a/src/wasm2c.c.tmpl b/src/wasm2c.c.tmpl index f86aad52..ae430453 100644 --- a/src/wasm2c.c.tmpl +++ b/src/wasm2c.c.tmpl @@ -13,21 +13,22 @@ #else #define FUNC_PROLOGUE \ if (++wasm_rt_call_stack_depth > WASM_RT_MAX_CALL_STACK_DEPTH) \ - TRAP(EXHAUSTION) + TRAP(EXHAUSTION); #define FUNC_EPILOGUE --wasm_rt_call_stack_depth #endif #define UNREACHABLE TRAP(UNREACHABLE) -#define CALL_INDIRECT(table, t, ft, x, ...) \ - (LIKELY((x) < table.size && table.data[x].func && \ - table.data[x].func_type == func_types[ft]) \ - || TRAP(CALL_INDIRECT) \ - , ((t)table.data[x].func)(__VA_ARGS__)) +#define CALL_INDIRECT(table, t, ft, x, ...) \ + (LIKELY((x) < table.size && table.data[x].func && \ + table.data[x].func_type == func_types[ft]) || \ + TRAP(CALL_INDIRECT), \ + ((t)table.data[x].func)(__VA_ARGS__)) -#define RANGE_CHECK(mem, offset, len) \ - if (UNLIKELY(offset + (uint64_t)len > mem->size)) TRAP(OOB) +#define RANGE_CHECK(mem, offset, len) \ + if (UNLIKELY(offset + (uint64_t)len > mem->size)) \ + TRAP(OOB); #if WASM_RT_MEMCHECK_SIGNAL_HANDLER #define MEMCHECK(mem, a, t) @@ -36,11 +37,11 @@ #endif #if WABT_BIG_ENDIAN -static inline void load_data(void *dest, const void *src, size_t n) { +static inline void load_data(void* dest, const void* src, size_t n) { size_t i = 0; - u8 *dest_chars = dest; + u8* dest_chars = dest; memcpy(dest, src, n); - for (i = 0; i < (n>>1); i++) { + for (i = 0; i < (n >> 1); i++) { u8 cursor = dest_chars[i]; dest_chars[i] = dest_chars[n - i - 1]; dest_chars[n - i - 1] = cursor; @@ -68,11 +69,12 @@ static inline void load_data(void *dest, const void *src, size_t n) { sizeof(t1)); \ } #else -static inline void load_data(void *dest, const void *src, size_t n) { +static inline void load_data(void* dest, const void* src, size_t n) { memcpy(dest, src, n); } -#define LOAD_DATA(m, o, i, s) do { \ - RANGE_CHECK((&m), o, s); \ +#define LOAD_DATA(m, o, i, s) \ + do { \ + RANGE_CHECK((&m), o, s); \ load_data(&(m.data[o]), i, s); \ } while (0) #define DEFINE_LOAD(name, t1, t2, t3) \ @@ -129,9 +131,9 @@ static inline int I64_CLZ(unsigned long long v) { return 63 - r; } #else - if (_BitScanReverse(&r, (unsigned long) (v >> 32))) { + if (_BitScanReverse(&r, (unsigned long)(v >> 32))) { return 31 - r; - } else if (_BitScanReverse(&r, (unsigned long) v)) { + } else if (_BitScanReverse(&r, (unsigned long)v)) { return 63 - r; } #endif @@ -153,14 +155,14 @@ static inline int I64_CTZ(unsigned long long v) { unsigned long r = 0; #if defined(_M_AMD64) || defined(_M_ARM) _BitScanForward64(&r, v); - return (int) r; + return (int)r; #else - if (_BitScanForward(&r, (unsigned int) (v))) { - return (int) (r); + if (_BitScanForward(&r, (unsigned int)(v))) { + return (int)(r); } - _BitScanForward(&r, (unsigned int) (v >> 32)); - return (int) (r + 32); + _BitScanForward(&r, (unsigned int)(v >> 32)); + return (int)(r + 32); #endif } @@ -170,15 +172,15 @@ static inline int I32_CTZ(unsigned long v) { } unsigned long r = 0; _BitScanForward(&r, v); - return (int) r; + return (int)r; } -#define POPCOUNT_DEFINE_PORTABLE(f_n, T) \ - static inline u32 f_n(T x) { \ - x = x - ((x >> 1) & (T)~(T)0/3); \ - x = (x & (T)~(T)0/15*3) + ((x >> 2) & (T)~(T)0/15*3); \ - x = (x + (x >> 4)) & (T)~(T)0/255*15; \ - return (T)(x * ((T)~(T)0/255)) >> (sizeof(T) - 1) * 8; \ +#define POPCOUNT_DEFINE_PORTABLE(f_n, T) \ + static inline u32 f_n(T x) { \ + x = x - ((x >> 1) & (T) ~(T)0 / 3); \ + x = (x & (T) ~(T)0 / 15 * 3) + ((x >> 2) & (T) ~(T)0 / 15 * 3); \ + x = (x + (x >> 4)) & (T) ~(T)0 / 255 * 15; \ + return (T)(x * ((T) ~(T)0 / 255)) >> (sizeof(T) - 1) * 8; \ } POPCOUNT_DEFINE_PORTABLE(I32_POPCNT, u32) @@ -197,15 +199,16 @@ POPCOUNT_DEFINE_PORTABLE(I64_POPCNT, u64) #endif -#define DIV_S(ut, min, x, y) \ - ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) \ - : (UNLIKELY((x) == min && (y) == -1)) ? TRAP(INT_OVERFLOW) \ - : (ut)((x) / (y))) +#define DIV_S(ut, min, x, y) \ + ((UNLIKELY((y) == 0)) \ + ? TRAP(DIV_BY_ZERO) \ + : (UNLIKELY((x) == min && (y) == -1)) ? TRAP(INT_OVERFLOW) \ + : (ut)((x) / (y))) -#define REM_S(ut, min, x, y) \ - ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) \ - : (UNLIKELY((x) == min && (y) == -1)) ? 0 \ - : (ut)((x) % (y))) +#define REM_S(ut, min, x, y) \ + ((UNLIKELY((y) == 0)) \ + ? TRAP(DIV_BY_ZERO) \ + : (UNLIKELY((x) == min && (y) == -1)) ? 0 : (ut)((x) % (y))) #define I32_DIV_S(x, y) DIV_S(u32, INT32_MIN, (s32)x, (s32)y) #define I64_DIV_S(x, y) DIV_S(u64, INT64_MIN, (s64)x, (s64)y) @@ -213,7 +216,7 @@ POPCOUNT_DEFINE_PORTABLE(I64_POPCNT, u64) #define I64_REM_S(x, y) REM_S(u64, INT64_MIN, (s64)x, (s64)y) #define DIVREM_U(op, x, y) \ - ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) : ((x) op (y))) + ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) : ((x)op(y))) #define DIV_U(x, y) DIVREM_U(/, x, y) #define REM_U(x, y) DIVREM_U(%, x, y) @@ -228,69 +231,90 @@ POPCOUNT_DEFINE_PORTABLE(I64_POPCNT, u64) #define I32_ROTR(x, y) ROTR(x, y, 31) #define I64_ROTR(x, y) ROTR(x, y, 63) -#define FMIN(x, y) \ - ((UNLIKELY((x) != (x))) ? NAN \ - : (UNLIKELY((y) != (y))) ? NAN \ - : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? x : y) \ - : (x < y) ? x : y) - -#define FMAX(x, y) \ - ((UNLIKELY((x) != (x))) ? NAN \ - : (UNLIKELY((y) != (y))) ? NAN \ - : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? y : x) \ - : (x > y) ? x : y) - -#define TRUNC_S(ut, st, ft, min, minop, max, x) \ - ((UNLIKELY((x) != (x))) ? TRAP(INVALID_CONVERSION) \ - : (UNLIKELY(!((x)minop(min) && (x) < (max)))) ? TRAP(INT_OVERFLOW) \ - : (ut)(st)(x)) - -#define I32_TRUNC_S_F32(x) TRUNC_S(u32, s32, f32, (f32)INT32_MIN, >=, 2147483648.f, x) -#define I64_TRUNC_S_F32(x) TRUNC_S(u64, s64, f32, (f32)INT64_MIN, >=, (f32)INT64_MAX, x) -#define I32_TRUNC_S_F64(x) TRUNC_S(u32, s32, f64, -2147483649., >, 2147483648., x) -#define I64_TRUNC_S_F64(x) TRUNC_S(u64, s64, f64, (f64)INT64_MIN, >=, (f64)INT64_MAX, x) - -#define TRUNC_U(ut, ft, max, x) \ - ((UNLIKELY((x) != (x))) ? TRAP(INVALID_CONVERSION) \ - : (UNLIKELY(!((x) > (ft)-1 && (x) < (max)))) ? TRAP(INT_OVERFLOW) \ - : (ut)(x)) +#define FMIN(x, y) \ + ((UNLIKELY((x) != (x))) \ + ? NAN \ + : (UNLIKELY((y) != (y))) \ + ? NAN \ + : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? x : y) \ + : (x < y) ? x : y) + +#define FMAX(x, y) \ + ((UNLIKELY((x) != (x))) \ + ? NAN \ + : (UNLIKELY((y) != (y))) \ + ? NAN \ + : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? y : x) \ + : (x > y) ? x : y) + +#define TRUNC_S(ut, st, ft, min, minop, max, x) \ + ((UNLIKELY((x) != (x))) \ + ? TRAP(INVALID_CONVERSION) \ + : (UNLIKELY(!((x)minop(min) && (x) < (max)))) ? TRAP(INT_OVERFLOW) \ + : (ut)(st)(x)) + +#define I32_TRUNC_S_F32(x) \ + TRUNC_S(u32, s32, f32, (f32)INT32_MIN, >=, 2147483648.f, x) +#define I64_TRUNC_S_F32(x) \ + TRUNC_S(u64, s64, f32, (f32)INT64_MIN, >=, (f32)INT64_MAX, x) +#define I32_TRUNC_S_F64(x) \ + TRUNC_S(u32, s32, f64, -2147483649., >, 2147483648., x) +#define I64_TRUNC_S_F64(x) \ + TRUNC_S(u64, s64, f64, (f64)INT64_MIN, >=, (f64)INT64_MAX, x) + +#define TRUNC_U(ut, ft, max, x) \ + ((UNLIKELY((x) != (x))) \ + ? TRAP(INVALID_CONVERSION) \ + : (UNLIKELY(!((x) > (ft)-1 && (x) < (max)))) ? TRAP(INT_OVERFLOW) \ + : (ut)(x)) #define I32_TRUNC_U_F32(x) TRUNC_U(u32, f32, 4294967296.f, x) #define I64_TRUNC_U_F32(x) TRUNC_U(u64, f32, (f32)UINT64_MAX, x) -#define I32_TRUNC_U_F64(x) TRUNC_U(u32, f64, 4294967296., x) +#define I32_TRUNC_U_F64(x) TRUNC_U(u32, f64, 4294967296., x) #define I64_TRUNC_U_F64(x) TRUNC_U(u64, f64, (f64)UINT64_MAX, x) #define TRUNC_SAT_S(ut, st, ft, min, smin, minop, max, smax, x) \ - ((UNLIKELY((x) != (x))) ? 0 \ - : (UNLIKELY(!((x)minop(min)))) ? smin \ - : (UNLIKELY(!((x) < (max)))) ? smax \ - : (ut)(st)(x)) - -#define I32_TRUNC_SAT_S_F32(x) TRUNC_SAT_S(u32, s32, f32, (f32)INT32_MIN, INT32_MIN, >=, 2147483648.f, INT32_MAX, x) -#define I64_TRUNC_SAT_S_F32(x) TRUNC_SAT_S(u64, s64, f32, (f32)INT64_MIN, INT64_MIN, >=, (f32)INT64_MAX, INT64_MAX, x) -#define I32_TRUNC_SAT_S_F64(x) TRUNC_SAT_S(u32, s32, f64, -2147483649., INT32_MIN, >, 2147483648., INT32_MAX, x) -#define I64_TRUNC_SAT_S_F64(x) TRUNC_SAT_S(u64, s64, f64, (f64)INT64_MIN, INT64_MIN, >=, (f64)INT64_MAX, INT64_MAX, x) - -#define TRUNC_SAT_U(ut, ft, max, smax, x) \ - ((UNLIKELY((x) != (x))) ? 0 \ - : (UNLIKELY(!((x) > (ft)-1))) ? 0 \ - : (UNLIKELY(!((x) < (max)))) ? smax \ - : (ut)(x)) - -#define I32_TRUNC_SAT_U_F32(x) TRUNC_SAT_U(u32, f32, 4294967296.f, UINT32_MAX, x) -#define I64_TRUNC_SAT_U_F32(x) TRUNC_SAT_U(u64, f32, (f32)UINT64_MAX, UINT64_MAX, x) -#define I32_TRUNC_SAT_U_F64(x) TRUNC_SAT_U(u32, f64, 4294967296., UINT32_MAX, x) -#define I64_TRUNC_SAT_U_F64(x) TRUNC_SAT_U(u64, f64, (f64)UINT64_MAX, UINT64_MAX, x) - -#define DEFINE_REINTERPRET(name, t1, t2) \ - static inline t2 name(t1 x) { \ - t2 result; \ - memcpy(&result, &x, sizeof(result)); \ - return result; \ + ((UNLIKELY((x) != (x))) \ + ? 0 \ + : (UNLIKELY(!((x)minop(min)))) \ + ? smin \ + : (UNLIKELY(!((x) < (max)))) ? smax : (ut)(st)(x)) + +#define I32_TRUNC_SAT_S_F32(x) \ + TRUNC_SAT_S(u32, s32, f32, (f32)INT32_MIN, INT32_MIN, >=, 2147483648.f, \ + INT32_MAX, x) +#define I64_TRUNC_SAT_S_F32(x) \ + TRUNC_SAT_S(u64, s64, f32, (f32)INT64_MIN, INT64_MIN, >=, (f32)INT64_MAX, \ + INT64_MAX, x) +#define I32_TRUNC_SAT_S_F64(x) \ + TRUNC_SAT_S(u32, s32, f64, -2147483649., INT32_MIN, >, 2147483648., \ + INT32_MAX, x) +#define I64_TRUNC_SAT_S_F64(x) \ + TRUNC_SAT_S(u64, s64, f64, (f64)INT64_MIN, INT64_MIN, >=, (f64)INT64_MAX, \ + INT64_MAX, x) + +#define TRUNC_SAT_U(ut, ft, max, smax, x) \ + ((UNLIKELY((x) != (x))) ? 0 \ + : (UNLIKELY(!((x) > (ft)-1))) \ + ? 0 \ + : (UNLIKELY(!((x) < (max)))) ? smax : (ut)(x)) + +#define I32_TRUNC_SAT_U_F32(x) \ + TRUNC_SAT_U(u32, f32, 4294967296.f, UINT32_MAX, x) +#define I64_TRUNC_SAT_U_F32(x) \ + TRUNC_SAT_U(u64, f32, (f32)UINT64_MAX, UINT64_MAX, x) +#define I32_TRUNC_SAT_U_F64(x) TRUNC_SAT_U(u32, f64, 4294967296., UINT32_MAX, x) +#define I64_TRUNC_SAT_U_F64(x) \ + TRUNC_SAT_U(u64, f64, (f64)UINT64_MAX, UINT64_MAX, x) + +#define DEFINE_REINTERPRET(name, t1, t2) \ + static inline t2 name(t1 x) { \ + t2 result; \ + memcpy(&result, &x, sizeof(result)); \ + return result; \ } 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) - diff --git a/src/wasm2c.h.tmpl b/src/wasm2c.h.tmpl index 27db28bd..774e2e97 100644 --- a/src/wasm2c.h.tmpl +++ b/src/wasm2c.h.tmpl @@ -21,7 +21,6 @@ typedef double f64; #ifdef __cplusplus extern "C" { #endif - %%bottom #ifdef __cplusplus |