mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
wasm2c: Implement nontrapping_fptoint support.
This commit is contained in:
parent
ea1502974d
commit
35e804bfb8
2 changed files with 93 additions and 4 deletions
|
|
@ -599,9 +599,8 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
|
||||||
.target = b.resolveTargetQuery(std.Target.Query.parse(.{
|
.target = b.resolveTargetQuery(std.Target.Query.parse(.{
|
||||||
.arch_os_abi = "wasm32-wasi",
|
.arch_os_abi = "wasm32-wasi",
|
||||||
// * `extended_const` is not supported by the `wasm-opt` version in CI.
|
// * `extended_const` is not supported by the `wasm-opt` version in CI.
|
||||||
// * `nontrapping_fptoint` is not supported by `wasm2c`.
|
|
||||||
// * `nontrapping_bulk_memory_len0` is supported by `wasm2c`.
|
// * `nontrapping_bulk_memory_len0` is supported by `wasm2c`.
|
||||||
.cpu_features = "baseline-extended_const-nontrapping_fptoint+nontrapping_bulk_memory_len0",
|
.cpu_features = "baseline-extended_const+nontrapping_bulk_memory_len0",
|
||||||
}) catch unreachable),
|
}) catch unreachable),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -645,6 +644,7 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
|
||||||
"-Oz",
|
"-Oz",
|
||||||
"--enable-bulk-memory",
|
"--enable-bulk-memory",
|
||||||
"--enable-mutable-globals",
|
"--enable-mutable-globals",
|
||||||
|
"--enable-nontrapping-float-to-int",
|
||||||
"--enable-sign-ext",
|
"--enable-sign-ext",
|
||||||
});
|
});
|
||||||
run_opt.addArtifactArg(exe);
|
run_opt.addArtifactArg(exe);
|
||||||
|
|
|
||||||
|
|
@ -109,7 +109,8 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
FILE *out = fopen(argv[2], "wb");
|
FILE *out = fopen(argv[2], "wb");
|
||||||
if (out == NULL) panic("unable to open output file");
|
if (out == NULL) panic("unable to open output file");
|
||||||
fputs("#include <math.h>\n"
|
fputs("#include <float.h>\n"
|
||||||
|
"#include <math.h>\n"
|
||||||
"#include <stdint.h>\n"
|
"#include <stdint.h>\n"
|
||||||
"#include <stdlib.h>\n"
|
"#include <stdlib.h>\n"
|
||||||
"#include <string.h>\n"
|
"#include <string.h>\n"
|
||||||
|
|
@ -273,6 +274,47 @@ int main(int argc, char **argv) {
|
||||||
" return dst;\n"
|
" return dst;\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"static uint32_t i32_trunc_sat_f32(const float src) {\n"
|
||||||
|
" if (isnan(src)) return 0;\n"
|
||||||
|
" if (isinf(src)) return (uint32_t)(signbit(src) == 0 ? INT32_MAX : INT32_MIN);\n"
|
||||||
|
" return (uint32_t)(int32_t)src;\n"
|
||||||
|
"}\n"
|
||||||
|
"static uint32_t u32_trunc_sat_f32(const float src) {\n"
|
||||||
|
" if (isnan(src)) return 0;\n"
|
||||||
|
" if (isinf(src)) return signbit(src) == 0 ? UINT32_MAX : 0;\n"
|
||||||
|
" return (uint32_t)src;\n"
|
||||||
|
"}\n"
|
||||||
|
"static uint32_t i32_trunc_sat_f64(const double src) {\n"
|
||||||
|
" if (isnan(src)) return 0;\n"
|
||||||
|
" if (isinf(src)) return (uint32_t)(signbit(src) == 0 ? INT32_MAX : INT32_MIN);\n"
|
||||||
|
" return (uint32_t)(int32_t)src;\n"
|
||||||
|
"}\n"
|
||||||
|
"static uint32_t u32_trunc_sat_f64(const double src) {\n"
|
||||||
|
" if (isnan(src)) return 0;\n"
|
||||||
|
" if (isinf(src)) return signbit(src) == 0 ? UINT32_MAX : 0;\n"
|
||||||
|
" return (uint32_t)src;\n"
|
||||||
|
"}\n"
|
||||||
|
"static uint64_t i64_trunc_sat_f32(const float src) {\n"
|
||||||
|
" if (isnan(src)) return 0;\n"
|
||||||
|
" if (isinf(src)) return (uint64_t)(signbit(src) == 0 ? INT64_MAX : INT64_MIN);\n"
|
||||||
|
" return (uint64_t)(int64_t)src;\n"
|
||||||
|
"}\n"
|
||||||
|
"static uint64_t u64_trunc_sat_f32(const float src) {\n"
|
||||||
|
" if (isnan(src)) return 0;\n"
|
||||||
|
" if (isinf(src)) return signbit(src) == 0 ? UINT64_MAX : 0;\n"
|
||||||
|
" return (uint64_t)src;\n"
|
||||||
|
"}\n"
|
||||||
|
"static uint64_t i64_trunc_sat_f64(const double src) {\n"
|
||||||
|
" if (isnan(src)) return 0;\n"
|
||||||
|
" if (isinf(src)) return (uint64_t)(signbit(src) == 0 ? INT64_MAX : INT64_MIN);\n"
|
||||||
|
" return (uint64_t)(int64_t)src;\n"
|
||||||
|
"}\n"
|
||||||
|
"static uint64_t u64_trunc_sat_f64(const double src) {\n"
|
||||||
|
" if (isnan(src)) return 0;\n"
|
||||||
|
" if (isinf(src)) return signbit(src) == 0 ? UINT64_MAX : 0;\n"
|
||||||
|
" return (uint64_t)src;\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
"static uint32_t memory_grow(uint8_t **m, uint32_t *p, uint32_t *c, uint32_t n) {\n"
|
"static uint32_t memory_grow(uint8_t **m, uint32_t *p, uint32_t *c, uint32_t n) {\n"
|
||||||
" uint8_t *new_m = *m;\n"
|
" uint8_t *new_m = *m;\n"
|
||||||
" uint32_t r = *p;\n"
|
" uint32_t r = *p;\n"
|
||||||
|
|
@ -2074,14 +2116,61 @@ int main(int argc, char **argv) {
|
||||||
case WasmOpcode_prefixed:
|
case WasmOpcode_prefixed:
|
||||||
switch (InputStream_readLeb128_u32(&in)) {
|
switch (InputStream_readLeb128_u32(&in)) {
|
||||||
case WasmPrefixedOpcode_i32_trunc_sat_f32_s:
|
case WasmPrefixedOpcode_i32_trunc_sat_f32_s:
|
||||||
|
if (unreachable_depth == 0) {
|
||||||
|
uint32_t lhs = FuncGen_stackPop(&fg);
|
||||||
|
FuncGen_stackPush(&fg, out, WasmValType_i32);
|
||||||
|
fprintf(out, "i32_trunc_sat_f32(l%" PRIu32 ");\n", lhs);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case WasmPrefixedOpcode_i32_trunc_sat_f32_u:
|
case WasmPrefixedOpcode_i32_trunc_sat_f32_u:
|
||||||
|
if (unreachable_depth == 0) {
|
||||||
|
uint32_t lhs = FuncGen_stackPop(&fg);
|
||||||
|
FuncGen_stackPush(&fg, out, WasmValType_i32);
|
||||||
|
fprintf(out, "u32_trunc_sat_f32(l%" PRIu32 ");\n", lhs);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case WasmPrefixedOpcode_i32_trunc_sat_f64_s:
|
case WasmPrefixedOpcode_i32_trunc_sat_f64_s:
|
||||||
|
if (unreachable_depth == 0) {
|
||||||
|
uint32_t lhs = FuncGen_stackPop(&fg);
|
||||||
|
FuncGen_stackPush(&fg, out, WasmValType_i32);
|
||||||
|
fprintf(out, "i32_trunc_sat_f64(l%" PRIu32 ");\n", lhs);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case WasmPrefixedOpcode_i32_trunc_sat_f64_u:
|
case WasmPrefixedOpcode_i32_trunc_sat_f64_u:
|
||||||
|
if (unreachable_depth == 0) {
|
||||||
|
uint32_t lhs = FuncGen_stackPop(&fg);
|
||||||
|
FuncGen_stackPush(&fg, out, WasmValType_i32);
|
||||||
|
fprintf(out, "u32_trunc_sat_f64(l%" PRIu32 ");\n", lhs);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case WasmPrefixedOpcode_i64_trunc_sat_f32_s:
|
case WasmPrefixedOpcode_i64_trunc_sat_f32_s:
|
||||||
|
if (unreachable_depth == 0) {
|
||||||
|
uint32_t lhs = FuncGen_stackPop(&fg);
|
||||||
|
FuncGen_stackPush(&fg, out, WasmValType_i32);
|
||||||
|
fprintf(out, "i64_trunc_sat_f32(l%" PRIu32 ");\n", lhs);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case WasmPrefixedOpcode_i64_trunc_sat_f32_u:
|
case WasmPrefixedOpcode_i64_trunc_sat_f32_u:
|
||||||
|
if (unreachable_depth == 0) {
|
||||||
|
uint32_t lhs = FuncGen_stackPop(&fg);
|
||||||
|
FuncGen_stackPush(&fg, out, WasmValType_i32);
|
||||||
|
fprintf(out, "u64_trunc_sat_f32(l%" PRIu32 ");\n", lhs);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case WasmPrefixedOpcode_i64_trunc_sat_f64_s:
|
case WasmPrefixedOpcode_i64_trunc_sat_f64_s:
|
||||||
|
if (unreachable_depth == 0) {
|
||||||
|
uint32_t lhs = FuncGen_stackPop(&fg);
|
||||||
|
FuncGen_stackPush(&fg, out, WasmValType_i32);
|
||||||
|
fprintf(out, "i64_trunc_sat_f64(l%" PRIu32 ");\n", lhs);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case WasmPrefixedOpcode_i64_trunc_sat_f64_u:
|
case WasmPrefixedOpcode_i64_trunc_sat_f64_u:
|
||||||
if (unreachable_depth == 0) panic("unimplemented opcode");
|
if (unreachable_depth == 0) {
|
||||||
|
uint32_t lhs = FuncGen_stackPop(&fg);
|
||||||
|
FuncGen_stackPush(&fg, out, WasmValType_i32);
|
||||||
|
fprintf(out, "u64_trunc_sat_f64(l%" PRIu32 ");\n", lhs);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case WasmPrefixedOpcode_memory_init:
|
case WasmPrefixedOpcode_memory_init:
|
||||||
(void)InputStream_readLeb128_u32(&in);
|
(void)InputStream_readLeb128_u32(&in);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue