Skip to content

Commit 70affd9

Browse files
stinosdpgeorge
authored andcommitted
all: Fix implicit floating point to integer conversions.
These are found when building with -Wfloat-conversion.
1 parent bcf01d1 commit 70affd9

File tree

14 files changed

+25
-23
lines changed

14 files changed

+25
-23
lines changed

extmod/modlwip.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,7 @@ STATIC mp_obj_t lwip_socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) {
13161316
timeout = -1;
13171317
} else {
13181318
#if MICROPY_PY_BUILTINS_FLOAT
1319-
timeout = 1000 * mp_obj_get_float(timeout_in);
1319+
timeout = (mp_uint_t)(MICROPY_FLOAT_CONST(1000.0) * mp_obj_get_float(timeout_in));
13201320
#else
13211321
timeout = 1000 * mp_obj_get_int(timeout_in);
13221322
#endif

lib/libm/math.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ float expf(float x)
442442
/* argument reduction */
443443
if (hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */
444444
if (hx > 0x3f851592) /* if |x| > 1.5 ln2 */
445-
k = invln2*x + half[sign];
445+
k = (int)(invln2*x + half[sign]);
446446
else
447447
k = 1 - sign - sign;
448448
hi = x - k*ln2hi; /* k*ln2hi is exact here */
@@ -533,7 +533,7 @@ float expm1f(float x)
533533
k = -1;
534534
}
535535
} else {
536-
k = invln2*x + (sign ? -0.5f : 0.5f);
536+
k = (int)(invln2*x + (sign ? -0.5f : 0.5f));
537537
t = k;
538538
hi = x - t*ln2_hi; /* t*ln2_hi is exact here */
539539
lo = t*ln2_lo;

ports/esp32/modsocket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ STATIC mp_obj_t socket_settimeout(const mp_obj_t arg0, const mp_obj_t arg1) {
457457
_socket_settimeout(self, UINT64_MAX);
458458
} else {
459459
#if MICROPY_PY_BUILTINS_FLOAT
460-
_socket_settimeout(self, mp_obj_get_float(arg1) * 1000L);
460+
_socket_settimeout(self, (uint64_t)(mp_obj_get_float(arg1) * MICROPY_FLOAT_CONST(1000.0)));
461461
#else
462462
_socket_settimeout(self, mp_obj_get_int(arg1) * 1000);
463463
#endif

ports/stm32/machine_timer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ STATIC mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, size_t n_ar
5656
if (args[ARG_freq].u_obj != mp_const_none) {
5757
// Frequency specified in Hz
5858
#if MICROPY_PY_BUILTINS_FLOAT
59-
self->delta_ms = 1000 / mp_obj_get_float(args[ARG_freq].u_obj);
59+
self->delta_ms = (uint32_t)(MICROPY_FLOAT_CONST(1000.0) / mp_obj_get_float(args[ARG_freq].u_obj));
6060
#else
6161
self->delta_ms = 1000 / mp_obj_get_int(args[ARG_freq].u_obj);
6262
#endif

ports/stm32/modusocket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ STATIC mp_obj_t socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) {
319319
timeout = -1;
320320
} else {
321321
#if MICROPY_PY_BUILTINS_FLOAT
322-
timeout = 1000 * mp_obj_get_float(timeout_in);
322+
timeout = (mp_uint_t)(MICROPY_FLOAT_CONST(1000.0) * mp_obj_get_float(timeout_in));
323323
#else
324324
timeout = 1000 * mp_obj_get_int(timeout_in);
325325
#endif

ports/stm32/servo.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ STATIC mp_obj_t pyb_servo_angle(size_t n_args, const mp_obj_t *args) {
278278
return mp_obj_new_int((self->pulse_cur - self->pulse_centre) * 90 / self->pulse_angle_90);
279279
} else {
280280
#if MICROPY_PY_BUILTINS_FLOAT
281-
self->pulse_dest = self->pulse_centre + self->pulse_angle_90 * mp_obj_get_float(args[1]) / 90.0;
281+
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));
282282
#else
283283
self->pulse_dest = self->pulse_centre + self->pulse_angle_90 * mp_obj_get_int(args[1]) / 90;
284284
#endif
@@ -308,7 +308,7 @@ STATIC mp_obj_t pyb_servo_speed(size_t n_args, const mp_obj_t *args) {
308308
return mp_obj_new_int((self->pulse_cur - self->pulse_centre) * 100 / self->pulse_speed_100);
309309
} else {
310310
#if MICROPY_PY_BUILTINS_FLOAT
311-
self->pulse_dest = self->pulse_centre + self->pulse_speed_100 * mp_obj_get_float(args[1]) / 100.0;
311+
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));
312312
#else
313313
self->pulse_dest = self->pulse_centre + self->pulse_speed_100 * mp_obj_get_int(args[1]) / 100;
314314
#endif

ports/stm32/timer.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ STATIC uint32_t compute_prescaler_period_from_freq(pyb_timer_obj_t *self, mp_obj
287287
}
288288
while (freq < 1 && prescaler < 6553) {
289289
prescaler *= 10;
290-
freq *= 10;
290+
freq *= 10.0f;
291291
}
292-
period = (float)source_freq / freq;
292+
period = (uint32_t)((float)source_freq / freq);
293293
#endif
294294
} else {
295295
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
382382
} else if (percent >= 100.0) {
383383
cmp = period;
384384
} else {
385-
cmp = percent / 100.0 * ((mp_float_t)period);
385+
cmp = (uint32_t)(percent / MICROPY_FLOAT_CONST(100.0) * ((mp_float_t)period));
386386
}
387387
#endif
388388
} else {

ports/unix/modtime.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ STATIC mp_obj_t mod_time_sleep(mp_obj_t arg) {
9696
struct timeval tv;
9797
mp_float_t val = mp_obj_get_float(arg);
9898
mp_float_t ipart;
99-
tv.tv_usec = MICROPY_FLOAT_C_FUN(round)(MICROPY_FLOAT_C_FUN(modf)(val, &ipart) * MICROPY_FLOAT_CONST(1000000.));
100-
tv.tv_sec = ipart;
99+
tv.tv_usec = (time_t)MICROPY_FLOAT_C_FUN(round)(MICROPY_FLOAT_C_FUN(modf)(val, &ipart) * MICROPY_FLOAT_CONST(1000000.));
100+
tv.tv_sec = (suseconds_t)ipart;
101101
int res;
102102
while (1) {
103103
MP_THREAD_GIL_EXIT();

ports/unix/modusocket.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ STATIC mp_obj_t socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) {
379379
#if MICROPY_PY_BUILTINS_FLOAT
380380
mp_float_t val = mp_obj_get_float(timeout_in);
381381
mp_float_t ipart;
382-
tv.tv_usec = MICROPY_FLOAT_C_FUN(round)(MICROPY_FLOAT_C_FUN(modf)(val, &ipart) * MICROPY_FLOAT_CONST(1000000.));
383-
tv.tv_sec = ipart;
382+
tv.tv_usec = (time_t)MICROPY_FLOAT_C_FUN(round)(MICROPY_FLOAT_C_FUN(modf)(val, &ipart) * MICROPY_FLOAT_CONST(1000000.));
383+
tv.tv_sec = (suseconds_t)ipart;
384384
#else
385385
tv.tv_sec = mp_obj_get_int(timeout_in);
386386
#endif

ports/windows/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ typedef int mp_int_t; // must be pointer size
160160
typedef unsigned int mp_uint_t; // must be pointer size
161161
#endif
162162

163+
typedef long suseconds_t;
164+
163165
// Just assume Windows is little-endian - mingw32 gcc doesn't
164166
// define standard endianness macros.
165167
#define MP_ENDIANNESS_LITTLE (1)

0 commit comments

Comments
 (0)