Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 40 additions & 52 deletions src/prebuilt/wasm2c_source_declarations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ R"w2c_template(// A pointer for an object of size n.
)w2c_template"
R"w2c_template(#if WABT_BIG_ENDIAN
)w2c_template"
R"w2c_template(#define MEM_ADDR(mem, addr, n) &(mem)->data[(mem)->size - (addr) - (n)]
R"w2c_template(#define MEM_ADDR(mem, addr, n) ((mem)->data_end - (addr) - (n))
)w2c_template"
R"w2c_template(#else
)w2c_template"
R"w2c_template(#define MEM_ADDR(mem, addr, n) &(mem)->data[addr]
R"w2c_template(#define MEM_ADDR(mem, addr, n) &((mem)->data[addr])
)w2c_template"
R"w2c_template(#endif
)w2c_template"
Expand Down Expand Up @@ -194,7 +194,7 @@ R"w2c_template( if (UNLIKELY(add_overflow(offset, len, &res))) \
)w2c_template"
R"w2c_template( TRAP(OOB); \
)w2c_template"
R"w2c_template( if (UNLIKELY(res > mem->size)) \
R"w2c_template( if (UNLIKELY(res > (mem)->size)) \
)w2c_template"
R"w2c_template( TRAP(OOB); \
)w2c_template"
Expand Down Expand Up @@ -624,24 +624,22 @@ R"w2c_template(
#endif
)w2c_template"
R"w2c_template(
#define DIV_S(ut, min, x, y) \
#define DIV_S(ut, min, x, y) \
)w2c_template"
R"w2c_template( ((UNLIKELY((y) == 0)) \
R"w2c_template( ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) \
)w2c_template"
R"w2c_template( ? TRAP(DIV_BY_ZERO) \
R"w2c_template( : (UNLIKELY((x) == min && (y) == -1)) ? TRAP(INT_OVERFLOW) \
)w2c_template"
R"w2c_template( : (UNLIKELY((x) == min && (y) == -1)) ? TRAP(INT_OVERFLOW) \
)w2c_template"
R"w2c_template( : (ut)((x) / (y)))
R"w2c_template( : (ut)((x) / (y)))
)w2c_template"
R"w2c_template(
#define REM_S(ut, min, x, y) \
#define REM_S(ut, min, x, y) \
)w2c_template"
R"w2c_template( ((UNLIKELY((y) == 0)) \
R"w2c_template( ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) \
)w2c_template"
R"w2c_template( ? TRAP(DIV_BY_ZERO) \
R"w2c_template( : (UNLIKELY((x) == min && (y) == -1)) ? 0 \
)w2c_template"
R"w2c_template( : (UNLIKELY((x) == min && (y) == -1)) ? 0 : (ut)((x) % (y)))
R"w2c_template( : (ut)((x) % (y)))
)w2c_template"
R"w2c_template(
#define I32_DIV_S(x, y) DIV_S(u32, INT32_MIN, (s32)x, (s32)y)
Expand Down Expand Up @@ -681,45 +679,39 @@ R"w2c_template(#define I32_ROTR(x, y) ROTR(x, y, 31)
R"w2c_template(#define I64_ROTR(x, y) ROTR(x, y, 63)
)w2c_template"
R"w2c_template(
#define FMIN(x, y) \
)w2c_template"
R"w2c_template( ((UNLIKELY((x) != (x))) \
#define FMIN(x, y) \
)w2c_template"
R"w2c_template( ? NAN \
R"w2c_template( ((UNLIKELY((x) != (x))) ? NAN \
)w2c_template"
R"w2c_template( : (UNLIKELY((y) != (y))) \
R"w2c_template( : (UNLIKELY((y) != (y))) ? NAN \
)w2c_template"
R"w2c_template( ? NAN \
R"w2c_template( : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? x : y) \
)w2c_template"
R"w2c_template( : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? x : y) \
R"w2c_template( : (x < y) ? x \
)w2c_template"
R"w2c_template( : (x < y) ? x : y)
R"w2c_template( : y)
)w2c_template"
R"w2c_template(
#define FMAX(x, y) \
)w2c_template"
R"w2c_template( ((UNLIKELY((x) != (x))) \
#define FMAX(x, y) \
)w2c_template"
R"w2c_template( ? NAN \
R"w2c_template( ((UNLIKELY((x) != (x))) ? NAN \
)w2c_template"
R"w2c_template( : (UNLIKELY((y) != (y))) \
R"w2c_template( : (UNLIKELY((y) != (y))) ? NAN \
)w2c_template"
R"w2c_template( ? NAN \
R"w2c_template( : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? y : x) \
)w2c_template"
R"w2c_template( : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? y : x) \
R"w2c_template( : (x > y) ? x \
)w2c_template"
R"w2c_template( : (x > y) ? x : y)
R"w2c_template( : y)
)w2c_template"
R"w2c_template(
#define TRUNC_S(ut, st, ft, min, minop, max, x) \
#define TRUNC_S(ut, st, ft, min, minop, max, x) \
)w2c_template"
R"w2c_template( ((UNLIKELY((x) != (x))) \
R"w2c_template( ((UNLIKELY((x) != (x))) ? TRAP(INVALID_CONVERSION) \
)w2c_template"
R"w2c_template( ? TRAP(INVALID_CONVERSION) \
R"w2c_template( : (UNLIKELY(!((x)minop(min) && (x) < (max)))) ? TRAP(INT_OVERFLOW) \
)w2c_template"
R"w2c_template( : (UNLIKELY(!((x)minop(min) && (x) < (max)))) ? TRAP(INT_OVERFLOW) \
)w2c_template"
R"w2c_template( : (ut)(st)(x))
R"w2c_template( : (ut)(st)(x))
)w2c_template"
R"w2c_template(
#define I32_TRUNC_S_F32(x) \
Expand All @@ -739,15 +731,13 @@ R"w2c_template(#define I64_TRUNC_S_F64(x) \
R"w2c_template( TRUNC_S(u64, s64, f64, (f64)INT64_MIN, >=, (f64)INT64_MAX, x)
)w2c_template"
R"w2c_template(
#define TRUNC_U(ut, ft, max, x) \
)w2c_template"
R"w2c_template( ((UNLIKELY((x) != (x))) \
#define TRUNC_U(ut, ft, max, x) \
)w2c_template"
R"w2c_template( ? TRAP(INVALID_CONVERSION) \
R"w2c_template( ((UNLIKELY((x) != (x))) ? TRAP(INVALID_CONVERSION) \
)w2c_template"
R"w2c_template( : (UNLIKELY(!((x) > (ft)-1 && (x) < (max)))) ? TRAP(INT_OVERFLOW) \
R"w2c_template( : (UNLIKELY(!((x) > (ft) - 1 && (x) < (max)))) ? TRAP(INT_OVERFLOW) \
)w2c_template"
R"w2c_template( : (ut)(x))
R"w2c_template( : (ut)(x))
)w2c_template"
R"w2c_template(
#define I32_TRUNC_U_F32(x) TRUNC_U(u32, f32, 4294967296.f, x)
Expand All @@ -761,15 +751,13 @@ R"w2c_template(#define I64_TRUNC_U_F64(x) TRUNC_U(u64, f64, (f64)UINT64_MAX, x)
R"w2c_template(
#define TRUNC_SAT_S(ut, st, ft, min, smin, minop, max, smax, x) \
)w2c_template"
R"w2c_template( ((UNLIKELY((x) != (x))) \
)w2c_template"
R"w2c_template( ? 0 \
R"w2c_template( ((UNLIKELY((x) != (x))) ? 0 \
)w2c_template"
R"w2c_template( : (UNLIKELY(!((x)minop(min)))) \
R"w2c_template( : (UNLIKELY(!((x)minop(min)))) ? smin \
)w2c_template"
R"w2c_template( ? smin \
R"w2c_template( : (UNLIKELY(!((x) < (max)))) ? smax \
)w2c_template"
R"w2c_template( : (UNLIKELY(!((x) < (max)))) ? smax : (ut)(st)(x))
R"w2c_template( : (ut)(st)(x))
)w2c_template"
R"w2c_template(
#define I32_TRUNC_SAT_S_F32(x) \
Expand Down Expand Up @@ -797,15 +785,15 @@ R"w2c_template( TRUNC_SAT_S(u64, s64, f64, (f64)INT64_MIN, INT64_MIN, >=, (f64)
R"w2c_template( INT64_MAX, x)
)w2c_template"
R"w2c_template(
#define TRUNC_SAT_U(ut, ft, max, smax, x) \
#define TRUNC_SAT_U(ut, ft, max, smax, x) \
)w2c_template"
R"w2c_template( ((UNLIKELY((x) != (x))) ? 0 \
R"w2c_template( ((UNLIKELY((x) != (x))) ? 0 \
)w2c_template"
R"w2c_template( : (UNLIKELY(!((x) > (ft)-1))) \
R"w2c_template( : (UNLIKELY(!((x) > (ft) - 1))) ? 0 \
)w2c_template"
R"w2c_template( ? 0 \
R"w2c_template( : (UNLIKELY(!((x) < (max)))) ? smax \
)w2c_template"
R"w2c_template( : (UNLIKELY(!((x) < (max)))) ? smax : (ut)(x))
R"w2c_template( : (ut)(x))
)w2c_template"
R"w2c_template(
#define I32_TRUNC_SAT_U_F32(x) \
Expand Down
90 changes: 42 additions & 48 deletions src/template/wasm2c.declarations.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
// Result:
// A pointer for an object of size n.
#if WABT_BIG_ENDIAN
#define MEM_ADDR(mem, addr, n) &(mem)->data[(mem)->size - (addr) - (n)]
#define MEM_ADDR(mem, addr, n) ((mem)->data_end - (addr) - (n))
#else
#define MEM_ADDR(mem, addr, n) &(mem)->data[addr]
#define MEM_ADDR(mem, addr, n) &((mem)->data[addr])
#endif

// We can only use Segue for this module if it uses a single unshared imported
Expand Down Expand Up @@ -104,7 +104,7 @@ static inline bool add_overflow(uint64_t a, uint64_t b, uint64_t* resptr) {
uint64_t res; \
if (UNLIKELY(add_overflow(offset, len, &res))) \
TRAP(OOB); \
if (UNLIKELY(res > mem->size)) \
if (UNLIKELY(res > (mem)->size)) \
TRAP(OOB); \
} while (0);

Expand Down Expand Up @@ -332,16 +332,15 @@ 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)
Expand All @@ -364,27 +363,24 @@ 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 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)
Expand All @@ -395,23 +391,21 @@ POPCOUNT_DEFINE_PORTABLE(I64_POPCNT, u64)
#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 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 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))
((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, \
Expand All @@ -426,11 +420,11 @@ POPCOUNT_DEFINE_PORTABLE(I64_POPCNT, u64)
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 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)
Expand Down
Loading
Loading