Skip to content

Commit a2110bd

Browse files
dlechdpgeorge
authored andcommitted
all: Fix implicit casts of float/double, and signed comparison.
These were found by buiding the unix coverage variant on macOS (so clang compiler). Mostly, these are fixing implicit cast of float/double to mp_float_t which is one of those two and one mp_int_t to size_t fix for good measure.
1 parent 3a0f64f commit a2110bd

File tree

6 files changed

+12
-12
lines changed

6 files changed

+12
-12
lines changed

extmod/moductypes.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,9 @@ STATIC mp_obj_t get_aligned(uint val_type, void *p, mp_int_t index) {
360360
return mp_obj_new_int_from_ll(((int64_t *)p)[index]);
361361
#if MICROPY_PY_BUILTINS_FLOAT
362362
case FLOAT32:
363-
return mp_obj_new_float(((float *)p)[index]);
363+
return mp_obj_new_float((mp_float_t)((float *)p)[index]);
364364
case FLOAT64:
365-
return mp_obj_new_float(((double *)p)[index]);
365+
return mp_obj_new_float((mp_float_t)((double *)p)[index]);
366366
#endif
367367
default:
368368
assert(0);

ports/unix/modffi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@ STATIC mp_obj_t return_ffi_value(ffi_arg val, char type) {
167167
union { ffi_arg ffi;
168168
float flt;
169169
} val_union = { .ffi = val };
170-
return mp_obj_new_float(val_union.flt);
170+
return mp_obj_new_float((mp_float_t)val_union.flt);
171171
}
172172
case 'd': {
173173
double *p = (double *)&val;
174-
return mp_obj_new_float(*p);
174+
return mp_obj_new_float((mp_float_t)*p);
175175
}
176176
#endif
177177
case 'O':

ports/unix/modtime.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static inline int msec_sleep_tv(struct timeval *tv) {
6161
#endif
6262

6363
#if defined(MP_CLOCKS_PER_SEC)
64-
#define CLOCK_DIV (MP_CLOCKS_PER_SEC / 1000.0F)
64+
#define CLOCK_DIV (MP_CLOCKS_PER_SEC / MICROPY_FLOAT_CONST(1000.0))
6565
#else
6666
#error Unsupported clock() implementation
6767
#endif
@@ -84,7 +84,7 @@ STATIC mp_obj_t mod_time_clock(void) {
8484
// float cannot represent full range of int32 precisely, so we pre-divide
8585
// int to reduce resolution, and then actually do float division hoping
8686
// to preserve integer part resolution.
87-
return mp_obj_new_float((float)(clock() / 1000) / CLOCK_DIV);
87+
return mp_obj_new_float((mp_float_t)(clock() / 1000) / CLOCK_DIV);
8888
#else
8989
return mp_obj_new_int((mp_int_t)clock());
9090
#endif

py/binary.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, size_t index) {
176176
#endif
177177
#if MICROPY_PY_BUILTINS_FLOAT
178178
case 'f':
179-
return mp_obj_new_float(((float *)p)[index]);
179+
return mp_obj_new_float((mp_float_t)((float *)p)[index]);
180180
case 'd':
181-
return mp_obj_new_float(((double *)p)[index]);
181+
return mp_obj_new_float((mp_float_t)((double *)p)[index]);
182182
#endif
183183
// Extension to CPython: array of objects
184184
case 'O':
@@ -244,12 +244,12 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte *p_base, byte *
244244
union { uint32_t i;
245245
float f;
246246
} fpu = {val};
247-
return mp_obj_new_float(fpu.f);
247+
return mp_obj_new_float((mp_float_t)fpu.f);
248248
} else if (val_type == 'd') {
249249
union { uint64_t i;
250250
double f;
251251
} fpu = {val};
252-
return mp_obj_new_float(fpu.f);
252+
return mp_obj_new_float((mp_float_t)fpu.f);
253253
#endif
254254
} else if (is_signed(val_type)) {
255255
if ((long long)MP_SMALL_INT_MIN <= val && val <= (long long)MP_SMALL_INT_MAX) {

py/objarray.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
445445
}
446446
#endif
447447
if (len_adj > 0) {
448-
if (len_adj > o->free) {
448+
if ((size_t)len_adj > o->free) {
449449
// TODO: alloc policy; at the moment we go conservative
450450
o->items = m_renew(byte, o->items, (o->len + o->free) * item_sz, (o->len + len_adj) * item_sz);
451451
o->free = len_adj;

py/parsenum.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool
218218
if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') {
219219
// inf
220220
str += 3;
221-
dec_val = INFINITY;
221+
dec_val = (mp_float_t)INFINITY;
222222
if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') {
223223
// infinity
224224
str += 5;

0 commit comments

Comments
 (0)