aboutsummaryrefslogtreecommitdiff
path: root/ports
diff options
context:
space:
mode:
authorstijn2020-04-09 09:05:48 +0200
committerDamien George2020-04-18 22:36:14 +1000
commit0ba68f8a1dae84de950420aebf8ad46582a38e66 (patch)
treec996a9b3d26f58b286ef6f9deb508de141b66574 /ports
parentb909e8b2dd007d8e7d61547768518b29bb4f833c (diff)
all: Fix implicit floating point promotion.
Initially some of these were found building the unix coverage variant on MacOS because that build uses clang and has -Wdouble-promotion enabled, and clang performs more vigorous promotion checks than gcc. Additionally the codebase has been compiled with clang and msvc (the latter with warning level 3), and with MICROPY_FLOAT_IMPL_FLOAT to find the rest of the conversions. Fixes are implemented either as explicit casts, or by using the correct type, or by using one of the utility functions to handle floating point casting; these have been moved from nativeglue.c to the public API.
Diffstat (limited to 'ports')
-rw-r--r--ports/unix/modffi.c4
-rw-r--r--ports/unix/modtime.c8
-rw-r--r--ports/unix/modusocket.c4
3 files changed, 8 insertions, 8 deletions
diff --git a/ports/unix/modffi.c b/ports/unix/modffi.c
index 3a9a7eca6..e5bd092cc 100644
--- a/ports/unix/modffi.c
+++ b/ports/unix/modffi.c
@@ -167,7 +167,7 @@ STATIC mp_obj_t return_ffi_value(ffi_arg val, char type) {
union { ffi_arg ffi;
float flt;
} val_union = { .ffi = val };
- return mp_obj_new_float(val_union.flt);
+ return mp_obj_new_float_from_f(val_union.flt);
}
case 'd': {
double *p = (double *)&val;
@@ -381,7 +381,7 @@ STATIC mp_obj_t ffifunc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const
*p = mp_obj_get_float(a);
} else if (*argtype == 'd') {
double *p = (double *)&values[i];
- *p = mp_obj_get_float(a);
+ *p = mp_obj_get_float_to_d(a);
#endif
} else if (a == mp_const_none) {
values[i] = 0;
diff --git a/ports/unix/modtime.c b/ports/unix/modtime.c
index 126aec30c..16152152a 100644
--- a/ports/unix/modtime.c
+++ b/ports/unix/modtime.c
@@ -61,7 +61,7 @@ static inline int msec_sleep_tv(struct timeval *tv) {
#endif
#if defined(MP_CLOCKS_PER_SEC)
-#define CLOCK_DIV (MP_CLOCKS_PER_SEC / 1000.0F)
+#define CLOCK_DIV (MP_CLOCKS_PER_SEC / MICROPY_FLOAT_CONST(1000.0))
#else
#error Unsupported clock() implementation
#endif
@@ -84,7 +84,7 @@ STATIC mp_obj_t mod_time_clock(void) {
// float cannot represent full range of int32 precisely, so we pre-divide
// int to reduce resolution, and then actually do float division hoping
// to preserve integer part resolution.
- return mp_obj_new_float((float)(clock() / 1000) / CLOCK_DIV);
+ return mp_obj_new_float((clock() / 1000) / CLOCK_DIV);
#else
return mp_obj_new_int((mp_int_t)clock());
#endif
@@ -95,8 +95,8 @@ STATIC mp_obj_t mod_time_sleep(mp_obj_t arg) {
#if MICROPY_PY_BUILTINS_FLOAT
struct timeval tv;
mp_float_t val = mp_obj_get_float(arg);
- double ipart;
- tv.tv_usec = round(modf(val, &ipart) * 1000000);
+ 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;
int res;
while (1) {
diff --git a/ports/unix/modusocket.c b/ports/unix/modusocket.c
index 8bc453d42..f87a717c9 100644
--- a/ports/unix/modusocket.c
+++ b/ports/unix/modusocket.c
@@ -378,8 +378,8 @@ STATIC mp_obj_t socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) {
if (timeout_in != mp_const_none) {
#if MICROPY_PY_BUILTINS_FLOAT
mp_float_t val = mp_obj_get_float(timeout_in);
- double ipart;
- tv.tv_usec = round(modf(val, &ipart) * 1000000);
+ 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;
#else
tv.tv_sec = mp_obj_get_int(timeout_in);