From d7afa4c0cfe36eba9ce2a5581d5d48e325103564 Mon Sep 17 00:00:00 2001 From: TianlongLiang <111852609+TianlongLiang@users.noreply.github.com> Date: Fri, 12 Sep 2025 08:44:42 +0800 Subject: [PATCH] Enable -Wdouble-promotion by default and fix related warnings (#4603) - fix float to double implicit conversion - add isnanf and signbitf macros, map them to existing double version when float versions are absent - enable -Wdouble-promotion by default - define isnan as macro for platform use core\sahred\platform\common\math.c --- build-scripts/warnings.cmake | 3 ++ core/iwasm/aot/aot_intrinsic.c | 4 +-- core/iwasm/aot/aot_runtime.c | 12 +++---- core/iwasm/compilation/aot_compiler.h | 2 +- core/iwasm/compilation/simd/simd_common.c | 2 +- core/iwasm/fast-jit/fe/jit_emit_numberic.c | 4 +-- core/iwasm/fast-jit/jit_dump.c | 2 +- core/iwasm/interpreter/wasm_interp_classic.c | 16 ++++++--- core/iwasm/interpreter/wasm_interp_fast.c | 4 +-- core/iwasm/interpreter/wasm_runtime.c | 12 +++---- .../shared/platform/alios/platform_internal.h | 8 +++-- core/shared/platform/common/math/math.c | 33 +++++++++++++++++-- core/shared/platform/riot/platform_internal.h | 8 +++-- .../platform/zephyr/platform_internal.h | 8 +++-- 14 files changed, 84 insertions(+), 34 deletions(-) diff --git a/build-scripts/warnings.cmake b/build-scripts/warnings.cmake index 14abf74a3..5e8df43f1 100644 --- a/build-scripts/warnings.cmake +++ b/build-scripts/warnings.cmake @@ -20,6 +20,9 @@ else () $<$:-Wincompatible-pointer-types> $<$:-Wimplicit-function-declaration> ) + add_compile_options ( + -Wdouble-promotion + ) # waivers add_compile_options ( -Wno-unused diff --git a/core/iwasm/aot/aot_intrinsic.c b/core/iwasm/aot/aot_intrinsic.c index a0e59e1d2..f296b4a25 100644 --- a/core/iwasm/aot/aot_intrinsic.c +++ b/core/iwasm/aot/aot_intrinsic.c @@ -152,7 +152,7 @@ float64 aot_intrinsic_fmin_f64(float64 a, float64 b) { if (isnan(a) || isnan(b)) - return NAN; + return (float64)NAN; else if (a == 0 && a == b) return signbit(a) ? a : b; else @@ -174,7 +174,7 @@ float64 aot_intrinsic_fmax_f64(float64 a, float64 b) { if (isnan(a) || isnan(b)) - return NAN; + return (float64)NAN; else if (a == 0 && a == b) return signbit(a) ? b : a; else diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 876e4b1f0..3bf33e127 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -4607,16 +4607,16 @@ aot_dump_perf_profiling(const AOTModuleInstance *module_inst) os_printf( " func %s, execution time: %.3f ms, execution count: %" PRIu32 " times, children execution time: %.3f ms\n", - func_name, perf_prof->total_exec_time / 1000.0f, + func_name, perf_prof->total_exec_time / 1000.0, perf_prof->total_exec_cnt, - perf_prof->children_exec_time / 1000.0f); + perf_prof->children_exec_time / 1000.0); else os_printf(" func %" PRIu32 ", execution time: %.3f ms, execution count: %" PRIu32 " times, children execution time: %.3f ms\n", - i, perf_prof->total_exec_time / 1000.0f, + i, perf_prof->total_exec_time / 1000.0, perf_prof->total_exec_cnt, - perf_prof->children_exec_time / 1000.0f); + perf_prof->children_exec_time / 1000.0); } } @@ -4632,7 +4632,7 @@ aot_summarize_wasm_execute_time(const AOTModuleInstance *inst) AOTFuncPerfProfInfo *perf_prof = (AOTFuncPerfProfInfo *)inst->func_perf_profilings + i; ret += (perf_prof->total_exec_time - perf_prof->children_exec_time) - / 1000.0f; + / 1000.0; } return ret; @@ -4651,7 +4651,7 @@ aot_get_wasm_func_exec_time(const AOTModuleInstance *inst, AOTFuncPerfProfInfo *perf_prof = (AOTFuncPerfProfInfo *)inst->func_perf_profilings + i; return (perf_prof->total_exec_time - perf_prof->children_exec_time) - / 1000.0f; + / 1000.0; } } diff --git a/core/iwasm/compilation/aot_compiler.h b/core/iwasm/compilation/aot_compiler.h index 889e2304b..70d01c578 100644 --- a/core/iwasm/compilation/aot_compiler.h +++ b/core/iwasm/compilation/aot_compiler.h @@ -668,7 +668,7 @@ set_local_gc_ref(AOTCompFrame *frame, int n, LLVMValueRef value, uint8 ref_type) #define I32_CONST(v) LLVMConstInt(I32_TYPE, v, true) #define I64_CONST(v) LLVMConstInt(I64_TYPE, v, true) -#define F32_CONST(v) LLVMConstReal(F32_TYPE, v) +#define F32_CONST(v) LLVMConstReal(F32_TYPE, (double)(v)) #define F64_CONST(v) LLVMConstReal(F64_TYPE, v) #define I8_CONST(v) LLVMConstInt(INT8_TYPE, v, true) diff --git a/core/iwasm/compilation/simd/simd_common.c b/core/iwasm/compilation/simd/simd_common.c index 95bcdfdb0..c495ee410 100644 --- a/core/iwasm/compilation/simd/simd_common.c +++ b/core/iwasm/compilation/simd/simd_common.c @@ -137,7 +137,7 @@ simd_build_splat_const_float_vector(const AOTCompContext *comp_ctx, return NULL; } - if (!(element = LLVMConstReal(element_type, element_value))) { + if (!(element = LLVMConstReal(element_type, (double)element_value))) { HANDLE_FAILURE("LLVMConstReal"); goto fail; } diff --git a/core/iwasm/fast-jit/fe/jit_emit_numberic.c b/core/iwasm/fast-jit/fe/jit_emit_numberic.c index 00f608f84..347c21827 100644 --- a/core/iwasm/fast-jit/fe/jit_emit_numberic.c +++ b/core/iwasm/fast-jit/fe/jit_emit_numberic.c @@ -1564,7 +1564,7 @@ static float64 f64_min(float64 a, float64 b) { if (isnan(a) || isnan(b)) - return NAN; + return (float64)NAN; else if (a == 0 && a == b) return signbit(a) ? a : b; else @@ -1575,7 +1575,7 @@ static float64 f64_max(float64 a, float64 b) { if (isnan(a) || isnan(b)) - return NAN; + return (float64)NAN; else if (a == 0 && a == b) return signbit(a) ? b : a; else diff --git a/core/iwasm/fast-jit/jit_dump.c b/core/iwasm/fast-jit/jit_dump.c index d61ed5dc7..7e45dd83d 100644 --- a/core/iwasm/fast-jit/jit_dump.c +++ b/core/iwasm/fast-jit/jit_dump.c @@ -40,7 +40,7 @@ jit_dump_reg(JitCompContext *cc, JitReg reg) case JIT_REG_KIND_F32: if (jit_reg_is_const(reg)) - os_printf("%f", jit_cc_get_const_F32(cc, reg)); + os_printf("%f", (double)jit_cc_get_const_F32(cc, reg)); else os_printf("f%d", no); break; diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index edc473f2c..8f5f1821a 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -223,7 +223,7 @@ static inline float64 f64_min(float64 a, float64 b) { if (isnan(a) || isnan(b)) - return NAN; + return (float64)NAN; else if (a == 0 && a == b) return signbit(a) ? a : b; else @@ -234,7 +234,7 @@ static inline float64 f64_max(float64 a, float64 b) { if (isnan(a) || isnan(b)) - return NAN; + return (float64)NAN; else if (a == 0 && a == b) return signbit(a) ? b : a; else @@ -1685,7 +1685,10 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, goto got_exception; } - HANDLE_OP(WASM_OP_NOP) { HANDLE_OP_END(); } + HANDLE_OP(WASM_OP_NOP) + { + HANDLE_OP_END(); + } #if WASM_ENABLE_EXCE_HANDLING != 0 HANDLE_OP(WASM_OP_RETHROW) @@ -5622,7 +5625,10 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, HANDLE_OP(WASM_OP_I32_REINTERPRET_F32) HANDLE_OP(WASM_OP_I64_REINTERPRET_F64) HANDLE_OP(WASM_OP_F32_REINTERPRET_I32) - HANDLE_OP(WASM_OP_F64_REINTERPRET_I64) { HANDLE_OP_END(); } + HANDLE_OP(WASM_OP_F64_REINTERPRET_I64) + { + HANDLE_OP_END(); + } HANDLE_OP(WASM_OP_I32_EXTEND8_S) { @@ -5697,7 +5703,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, true); break; case WASM_OP_I64_TRUNC_SAT_U_F64: - DEF_OP_TRUNC_SAT_F64(-1.0f, 18446744073709551616.0, + DEF_OP_TRUNC_SAT_F64(-1.0, 18446744073709551616.0, false, false); break; #if WASM_ENABLE_BULK_MEMORY != 0 diff --git a/core/iwasm/interpreter/wasm_interp_fast.c b/core/iwasm/interpreter/wasm_interp_fast.c index 36d4538ff..5cbb3cbe9 100644 --- a/core/iwasm/interpreter/wasm_interp_fast.c +++ b/core/iwasm/interpreter/wasm_interp_fast.c @@ -164,7 +164,7 @@ static inline float64 f64_min(float64 a, float64 b) { if (isnan(a) || isnan(b)) - return NAN; + return (float64)NAN; else if (a == 0 && a == b) return signbit(a) ? a : b; else @@ -175,7 +175,7 @@ static inline float64 f64_max(float64 a, float64 b) { if (isnan(a) || isnan(b)) - return NAN; + return (float64)NAN; else if (a == 0 && a == b) return signbit(a) ? b : a; else diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 55e65142a..3642adf9b 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -3744,16 +3744,16 @@ wasm_dump_perf_profiling(const WASMModuleInstance *module_inst) os_printf( " func %s, execution time: %.3f ms, execution count: %" PRIu32 " times, children execution time: %.3f ms\n", - func_name, func_inst->total_exec_time / 1000.0f, + func_name, func_inst->total_exec_time / 1000.0, func_inst->total_exec_cnt, - func_inst->children_exec_time / 1000.0f); + func_inst->children_exec_time / 1000.0); else os_printf(" func %" PRIu32 ", execution time: %.3f ms, execution count: %" PRIu32 " times, children execution time: %.3f ms\n", - i, func_inst->total_exec_time / 1000.0f, + i, func_inst->total_exec_time / 1000.0, func_inst->total_exec_cnt, - func_inst->children_exec_time / 1000.0f); + func_inst->children_exec_time / 1000.0); } } @@ -3765,7 +3765,7 @@ wasm_summarize_wasm_execute_time(const WASMModuleInstance *inst) unsigned i; for (i = 0; i < inst->e->function_count; i++) { WASMFunctionInstance *func = inst->e->functions + i; - ret += (func->total_exec_time - func->children_exec_time) / 1000.0f; + ret += (func->total_exec_time - func->children_exec_time) / 1000.0; } return ret; @@ -3780,7 +3780,7 @@ wasm_get_wasm_func_exec_time(const WASMModuleInstance *inst, char *name_in_wasm = get_func_name_from_index(inst, i); if (name_in_wasm && strcmp(name_in_wasm, func_name) == 0) { WASMFunctionInstance *func = inst->e->functions + i; - return (func->total_exec_time - func->children_exec_time) / 1000.0f; + return (func->total_exec_time - func->children_exec_time) / 1000.0; } } diff --git a/core/shared/platform/alios/platform_internal.h b/core/shared/platform/alios/platform_internal.h index 125aa0b63..c5032f9b2 100644 --- a/core/shared/platform/alios/platform_internal.h +++ b/core/shared/platform/alios/platform_internal.h @@ -66,8 +66,12 @@ float fmaxf(float x, float y); float rintf(float x); float fabsf(float x); float truncf(float x); -int signbit(double x); -int isnan(double x); +int isnan_double(double x); +int isnan_float(float x); +int signbit_double(double x); +int signbit_float(float x); +#define isnan(x) (sizeof(x) == sizeof(double) ? isnan_double((double)x) : isnan_float(x)) +#define signbit(x) (sizeof(x) == sizeof(double) ? signbit_double((double)x) : signbit_float(x)) /* clang-format on */ /* The below types are used in platform_api_extension.h, diff --git a/core/shared/platform/common/math/math.c b/core/shared/platform/common/math/math.c index 2ba9f4d28..ac0c40221 100644 --- a/core/shared/platform/common/math/math.c +++ b/core/shared/platform/common/math/math.c @@ -1005,6 +1005,21 @@ freebsd_isnan(double d) } } +static int +freebsd_isnanf(float f) +{ + if (is_little_endian()) { + IEEEf2bits_L u; + u.f = f; + return (u.bits.exp == 0xff && u.bits.man != 0); + } + else { + IEEEf2bits_B u; + u.f = f; + return (u.bits.exp == 0xff && u.bits.man != 0); + } +} + static float freebsd_fabsf(float x) { @@ -1601,7 +1616,13 @@ fabs(double x) } int -isnan(double x) +isnan_float(float x) +{ + return freebsd_isnanf(x); +} + +int +isnan_double(double x) { return freebsd_isnan(x); } @@ -1613,7 +1634,15 @@ trunc(double x) } int -signbit(double x) +signbit_float(float x) +{ + unsigned int i; + GET_FLOAT_WORD(i, x); + return (int)(i >> 31); +} + +int +signbit_double(double x) { return ((__HI(x) & 0x80000000) >> 31); } diff --git a/core/shared/platform/riot/platform_internal.h b/core/shared/platform/riot/platform_internal.h index fd9e40da7..6eaae2caf 100644 --- a/core/shared/platform/riot/platform_internal.h +++ b/core/shared/platform/riot/platform_internal.h @@ -86,8 +86,12 @@ float fmaxf(float x, float y); float rintf(float x); float fabsf(float x); float truncf(float x); -int signbit(double x); -int isnan(double x); +int isnan_double(double x); +int isnan_float(float x); +int signbit_double(double x); +int signbit_float(float x); +#define isnan(x) (sizeof(x) == sizeof(double) ? isnan_double((double)x) : isnan_float(x)) +#define signbit(x) (sizeof(x) == sizeof(double) ? signbit_double((double)x) : signbit_float(x)) /* clang-format on */ #endif diff --git a/core/shared/platform/zephyr/platform_internal.h b/core/shared/platform/zephyr/platform_internal.h index 7e59faa74..d5f0c80d8 100644 --- a/core/shared/platform/zephyr/platform_internal.h +++ b/core/shared/platform/zephyr/platform_internal.h @@ -189,12 +189,16 @@ float fmaxf(float x, float y); float rintf(float x); float fabsf(float x); float truncf(float x); -int isnan(double x); +int isnan_double(double x); +int isnan_float(float x); +#define isnan(x) (sizeof(x) == sizeof(double) ? isnan_double((double)x) : isnan_float(x)) double pow(double x, double y); double scalbn(double x, int n); #ifndef BH_HAS_SIGNBIT -int signbit(double x); +int signbit_double(double x); +int signbit_float(float x); +#define signbit(x) (sizeof(x) == sizeof(double) ? signbit_double((double)x) : signbit_float(x)) #endif unsigned long long int strtoull(const char *nptr, char **endptr, int base);