فهرست منبع

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
TianlongLiang 4 ماه پیش
والد
کامیت
d7afa4c0cf

+ 3 - 0
build-scripts/warnings.cmake

@@ -20,6 +20,9 @@ else ()
     $<$<COMPILE_LANGUAGE:C>:-Wincompatible-pointer-types>
     $<$<COMPILE_LANGUAGE:C>:-Wimplicit-function-declaration>
   )
+  add_compile_options (
+    -Wdouble-promotion
+  )
   # waivers
   add_compile_options (
     -Wno-unused

+ 2 - 2
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

+ 6 - 6
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;
         }
     }
 

+ 1 - 1
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)
 

+ 1 - 1
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;
     }

+ 2 - 2
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

+ 1 - 1
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;

+ 11 - 5
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

+ 2 - 2
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

+ 6 - 6
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;
         }
     }
 

+ 6 - 2
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,

+ 31 - 2
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);
 }

+ 6 - 2
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
 

+ 6 - 2
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);