From 70affd9ba22e7f62666a9a2fafc2a3c0be9ef95a Mon Sep 17 00:00:00 2001 From: stijn Date: Mon, 13 Apr 2020 20:56:31 +0200 Subject: [PATCH] all: Fix implicit floating point to integer conversions. These are found when building with -Wfloat-conversion. --- extmod/modlwip.c | 2 +- lib/libm/math.c | 4 ++-- ports/esp32/modsocket.c | 2 +- ports/stm32/machine_timer.c | 2 +- ports/stm32/modusocket.c | 2 +- ports/stm32/servo.c | 4 ++-- ports/stm32/timer.c | 6 +++--- ports/unix/modtime.c | 4 ++-- ports/unix/modusocket.c | 4 ++-- ports/windows/mpconfigport.h | 2 ++ py/binary.c | 4 ++-- py/modbuiltins.c | 2 +- py/obj.c | 4 ++-- py/runtime.c | 6 +++--- 14 files changed, 25 insertions(+), 23 deletions(-) diff --git a/extmod/modlwip.c b/extmod/modlwip.c index 9fbd783b2f..216e81749d 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -1316,7 +1316,7 @@ STATIC mp_obj_t lwip_socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) { timeout = -1; } else { #if MICROPY_PY_BUILTINS_FLOAT - timeout = 1000 * mp_obj_get_float(timeout_in); + timeout = (mp_uint_t)(MICROPY_FLOAT_CONST(1000.0) * mp_obj_get_float(timeout_in)); #else timeout = 1000 * mp_obj_get_int(timeout_in); #endif diff --git a/lib/libm/math.c b/lib/libm/math.c index 2c52edd1be..3dfd925655 100644 --- a/lib/libm/math.c +++ b/lib/libm/math.c @@ -442,7 +442,7 @@ float expf(float x) /* argument reduction */ if (hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */ if (hx > 0x3f851592) /* if |x| > 1.5 ln2 */ - k = invln2*x + half[sign]; + k = (int)(invln2*x + half[sign]); else k = 1 - sign - sign; hi = x - k*ln2hi; /* k*ln2hi is exact here */ @@ -533,7 +533,7 @@ float expm1f(float x) k = -1; } } else { - k = invln2*x + (sign ? -0.5f : 0.5f); + k = (int)(invln2*x + (sign ? -0.5f : 0.5f)); t = k; hi = x - t*ln2_hi; /* t*ln2_hi is exact here */ lo = t*ln2_lo; diff --git a/ports/esp32/modsocket.c b/ports/esp32/modsocket.c index dcb979b9c6..69a74ec256 100644 --- a/ports/esp32/modsocket.c +++ b/ports/esp32/modsocket.c @@ -457,7 +457,7 @@ STATIC mp_obj_t socket_settimeout(const mp_obj_t arg0, const mp_obj_t arg1) { _socket_settimeout(self, UINT64_MAX); } else { #if MICROPY_PY_BUILTINS_FLOAT - _socket_settimeout(self, mp_obj_get_float(arg1) * 1000L); + _socket_settimeout(self, (uint64_t)(mp_obj_get_float(arg1) * MICROPY_FLOAT_CONST(1000.0))); #else _socket_settimeout(self, mp_obj_get_int(arg1) * 1000); #endif diff --git a/ports/stm32/machine_timer.c b/ports/stm32/machine_timer.c index daf84caa49..e9b16ab72f 100644 --- a/ports/stm32/machine_timer.c +++ b/ports/stm32/machine_timer.c @@ -56,7 +56,7 @@ STATIC mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, size_t n_ar if (args[ARG_freq].u_obj != mp_const_none) { // Frequency specified in Hz #if MICROPY_PY_BUILTINS_FLOAT - self->delta_ms = 1000 / mp_obj_get_float(args[ARG_freq].u_obj); + self->delta_ms = (uint32_t)(MICROPY_FLOAT_CONST(1000.0) / mp_obj_get_float(args[ARG_freq].u_obj)); #else self->delta_ms = 1000 / mp_obj_get_int(args[ARG_freq].u_obj); #endif diff --git a/ports/stm32/modusocket.c b/ports/stm32/modusocket.c index f9de017e0b..2732d472b5 100644 --- a/ports/stm32/modusocket.c +++ b/ports/stm32/modusocket.c @@ -319,7 +319,7 @@ STATIC mp_obj_t socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) { timeout = -1; } else { #if MICROPY_PY_BUILTINS_FLOAT - timeout = 1000 * mp_obj_get_float(timeout_in); + timeout = (mp_uint_t)(MICROPY_FLOAT_CONST(1000.0) * mp_obj_get_float(timeout_in)); #else timeout = 1000 * mp_obj_get_int(timeout_in); #endif diff --git a/ports/stm32/servo.c b/ports/stm32/servo.c index c17bf63304..a347c6bbd0 100644 --- a/ports/stm32/servo.c +++ b/ports/stm32/servo.c @@ -278,7 +278,7 @@ STATIC mp_obj_t pyb_servo_angle(size_t n_args, const mp_obj_t *args) { return mp_obj_new_int((self->pulse_cur - self->pulse_centre) * 90 / self->pulse_angle_90); } else { #if MICROPY_PY_BUILTINS_FLOAT - self->pulse_dest = self->pulse_centre + self->pulse_angle_90 * mp_obj_get_float(args[1]) / 90.0; + self->pulse_dest = self->pulse_centre + (uint16_t)((mp_float_t)self->pulse_angle_90 * mp_obj_get_float(args[1]) / MICROPY_FLOAT_CONST(90.0)); #else self->pulse_dest = self->pulse_centre + self->pulse_angle_90 * mp_obj_get_int(args[1]) / 90; #endif @@ -308,7 +308,7 @@ STATIC mp_obj_t pyb_servo_speed(size_t n_args, const mp_obj_t *args) { return mp_obj_new_int((self->pulse_cur - self->pulse_centre) * 100 / self->pulse_speed_100); } else { #if MICROPY_PY_BUILTINS_FLOAT - self->pulse_dest = self->pulse_centre + self->pulse_speed_100 * mp_obj_get_float(args[1]) / 100.0; + self->pulse_dest = self->pulse_centre + (uint16_t)((mp_float_t)self->pulse_speed_100 * mp_obj_get_float(args[1]) / MICROPY_FLOAT_CONST(100.0)); #else self->pulse_dest = self->pulse_centre + self->pulse_speed_100 * mp_obj_get_int(args[1]) / 100; #endif diff --git a/ports/stm32/timer.c b/ports/stm32/timer.c index 84a47cb6d1..6006a3f666 100644 --- a/ports/stm32/timer.c +++ b/ports/stm32/timer.c @@ -287,9 +287,9 @@ STATIC uint32_t compute_prescaler_period_from_freq(pyb_timer_obj_t *self, mp_obj } while (freq < 1 && prescaler < 6553) { prescaler *= 10; - freq *= 10; + freq *= 10.0f; } - period = (float)source_freq / freq; + period = (uint32_t)((float)source_freq / freq); #endif } else { mp_int_t freq = mp_obj_get_int(freq_in); @@ -382,7 +382,7 @@ STATIC uint32_t compute_pwm_value_from_percent(uint32_t period, mp_obj_t percent } else if (percent >= 100.0) { cmp = period; } else { - cmp = percent / 100.0 * ((mp_float_t)period); + cmp = (uint32_t)(percent / MICROPY_FLOAT_CONST(100.0) * ((mp_float_t)period)); } #endif } else { diff --git a/ports/unix/modtime.c b/ports/unix/modtime.c index 16152152ac..f9052ecce6 100644 --- a/ports/unix/modtime.c +++ b/ports/unix/modtime.c @@ -96,8 +96,8 @@ STATIC mp_obj_t mod_time_sleep(mp_obj_t arg) { struct timeval tv; mp_float_t val = mp_obj_get_float(arg); mp_float_t ipart; - tv.tv_usec = MICROPY_FLOAT_C_FUN(round)(MICROPY_FLOAT_C_FUN(modf)(val, &ipart) * MICROPY_FLOAT_CONST(1000000.)); - tv.tv_sec = ipart; + tv.tv_usec = (time_t)MICROPY_FLOAT_C_FUN(round)(MICROPY_FLOAT_C_FUN(modf)(val, &ipart) * MICROPY_FLOAT_CONST(1000000.)); + tv.tv_sec = (suseconds_t)ipart; int res; while (1) { MP_THREAD_GIL_EXIT(); diff --git a/ports/unix/modusocket.c b/ports/unix/modusocket.c index f87a717c9e..248d721ed2 100644 --- a/ports/unix/modusocket.c +++ b/ports/unix/modusocket.c @@ -379,8 +379,8 @@ STATIC mp_obj_t socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) { #if MICROPY_PY_BUILTINS_FLOAT mp_float_t val = mp_obj_get_float(timeout_in); mp_float_t ipart; - tv.tv_usec = MICROPY_FLOAT_C_FUN(round)(MICROPY_FLOAT_C_FUN(modf)(val, &ipart) * MICROPY_FLOAT_CONST(1000000.)); - tv.tv_sec = ipart; + tv.tv_usec = (time_t)MICROPY_FLOAT_C_FUN(round)(MICROPY_FLOAT_C_FUN(modf)(val, &ipart) * MICROPY_FLOAT_CONST(1000000.)); + tv.tv_sec = (suseconds_t)ipart; #else tv.tv_sec = mp_obj_get_int(timeout_in); #endif diff --git a/ports/windows/mpconfigport.h b/ports/windows/mpconfigport.h index e447e692e9..057061a5b9 100644 --- a/ports/windows/mpconfigport.h +++ b/ports/windows/mpconfigport.h @@ -160,6 +160,8 @@ typedef int mp_int_t; // must be pointer size typedef unsigned int mp_uint_t; // must be pointer size #endif +typedef long suseconds_t; + // Just assume Windows is little-endian - mingw32 gcc doesn't // define standard endianness macros. #define MP_ENDIANNESS_LITTLE (1) diff --git a/py/binary.c b/py/binary.c index a53b8847cc..d0f72ec23c 100644 --- a/py/binary.c +++ b/py/binary.c @@ -419,10 +419,10 @@ void mp_binary_set_val_array_from_int(char typecode, void *p, size_t index, mp_i #endif #if MICROPY_PY_BUILTINS_FLOAT case 'f': - ((float *)p)[index] = val; + ((float *)p)[index] = (float)val; break; case 'd': - ((double *)p)[index] = val; + ((double *)p)[index] = (double)val; break; #endif // Extension to CPython: array of pointers diff --git a/py/modbuiltins.c b/py/modbuiltins.c index c1f3f771f0..85d30ab66d 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -509,7 +509,7 @@ STATIC mp_obj_t mp_builtin_round(size_t n_args, const mp_obj_t *args) { mp_float_t val = mp_obj_get_float(o_in); if (n_args > 1) { mp_int_t num_dig = mp_obj_get_int(args[1]); - mp_float_t mult = MICROPY_FLOAT_C_FUN(pow)(10, num_dig); + mp_float_t mult = MICROPY_FLOAT_C_FUN(pow)(10, (mp_float_t)num_dig); // TODO may lead to overflow mp_float_t rounded = MICROPY_FLOAT_C_FUN(nearbyint)(val * mult) / mult; return mp_obj_new_float(rounded); diff --git a/py/obj.c b/py/obj.c index b3995f9ca0..07b1612552 100644 --- a/py/obj.c +++ b/py/obj.c @@ -340,7 +340,7 @@ bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value) { } else if (arg == mp_const_true) { val = 1; } else if (mp_obj_is_small_int(arg)) { - val = MP_OBJ_SMALL_INT_VALUE(arg); + val = (mp_float_t)MP_OBJ_SMALL_INT_VALUE(arg); #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE } else if (mp_obj_is_type(arg, &mp_type_int)) { val = mp_obj_int_as_float_impl(arg); @@ -379,7 +379,7 @@ void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) { *real = 1; *imag = 0; } else if (mp_obj_is_small_int(arg)) { - *real = MP_OBJ_SMALL_INT_VALUE(arg); + *real = (mp_float_t)MP_OBJ_SMALL_INT_VALUE(arg); *imag = 0; #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE } else if (mp_obj_is_type(arg, &mp_type_int)) { diff --git a/py/runtime.c b/py/runtime.c index 1fa9e73f2a..79ca45fb18 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -473,7 +473,7 @@ mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) { case MP_BINARY_OP_INPLACE_POWER: if (rhs_val < 0) { #if MICROPY_PY_BUILTINS_FLOAT - return mp_obj_float_binary_op(op, lhs_val, rhs); + return mp_obj_float_binary_op(op, (mp_float_t)lhs_val, rhs); #else mp_raise_ValueError(MP_ERROR_TEXT("negative power with no float support")); #endif @@ -535,7 +535,7 @@ mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) { } #if MICROPY_PY_BUILTINS_FLOAT } else if (mp_obj_is_float(rhs)) { - mp_obj_t res = mp_obj_float_binary_op(op, lhs_val, rhs); + mp_obj_t res = mp_obj_float_binary_op(op, (mp_float_t)lhs_val, rhs); if (res == MP_OBJ_NULL) { goto unsupported_op; } else { @@ -544,7 +544,7 @@ mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) { #endif #if MICROPY_PY_BUILTINS_COMPLEX } else if (mp_obj_is_type(rhs, &mp_type_complex)) { - mp_obj_t res = mp_obj_complex_binary_op(op, lhs_val, 0, rhs); + mp_obj_t res = mp_obj_complex_binary_op(op, (mp_float_t)lhs_val, 0, rhs); if (res == MP_OBJ_NULL) { goto unsupported_op; } else {