Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit 9b345a9

Browse files
author
Paul Sokolovsky
committed
extmod/utime_mphal: ticks_diff/ticks_add: Don't hardcode 32-bit types.
Use normal mp_int_t/mp_uint_t types, algorithms (hm, formulas) can work with any type width.
1 parent 3272afe commit 9b345a9

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

extmod/utime_mphal.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,20 @@ MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_cpu_obj, time_ticks_cpu);
8787

8888
STATIC mp_obj_t time_ticks_diff(mp_obj_t end_in, mp_obj_t start_in) {
8989
// we assume that the arguments come from ticks_xx so are small ints
90-
uint32_t start = MP_OBJ_SMALL_INT_VALUE(start_in);
91-
uint32_t end = MP_OBJ_SMALL_INT_VALUE(end_in);
90+
mp_uint_t start = MP_OBJ_SMALL_INT_VALUE(start_in);
91+
mp_uint_t end = MP_OBJ_SMALL_INT_VALUE(end_in);
9292
// Optimized formula avoiding if conditions. We adjust difference "forward",
9393
// wrap it around and adjust back.
94-
int32_t diff = ((end - start + MICROPY_PY_UTIME_TICKS_PERIOD / 2) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1))
94+
mp_int_t diff = ((end - start + MICROPY_PY_UTIME_TICKS_PERIOD / 2) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1))
9595
- MICROPY_PY_UTIME_TICKS_PERIOD / 2;
9696
return MP_OBJ_NEW_SMALL_INT(diff);
9797
}
9898
MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_diff_obj, time_ticks_diff);
9999

100100
STATIC mp_obj_t time_ticks_add(mp_obj_t ticks_in, mp_obj_t delta_in) {
101101
// we assume that first argument come from ticks_xx so is small int
102-
uint32_t ticks = MP_OBJ_SMALL_INT_VALUE(ticks_in);
103-
uint32_t delta = (uint32_t)mp_obj_get_int(delta_in);
102+
mp_uint_t ticks = MP_OBJ_SMALL_INT_VALUE(ticks_in);
103+
mp_uint_t delta = mp_obj_get_int(delta_in);
104104
return MP_OBJ_NEW_SMALL_INT((ticks + delta) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1));
105105
}
106106
MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_add_obj, time_ticks_add);

0 commit comments

Comments
 (0)