diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-11-02 02:50:48 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-11-02 02:50:48 +0300 |
commit | 10bde6933e45289eeb8b0a564b088fadc4309c17 (patch) | |
tree | ca0b1ba415a265f1d403e98ae649c46c7e63d24d | |
parent | 5fae91432646637c4f8d10c6102b76068f889226 (diff) | |
download | micropython-10bde6933e45289eeb8b0a564b088fadc4309c17.tar.gz micropython-10bde6933e45289eeb8b0a564b088fadc4309c17.zip |
extmod/utime_mphal: ticks_diff(): Optimize to avoid if conditions.
-rw-r--r-- | extmod/utime_mphal.c | 10 |
1 files changed, 4 insertions, 6 deletions
diff --git a/extmod/utime_mphal.c b/extmod/utime_mphal.c index 9aa2f13245..a563b17576 100644 --- a/extmod/utime_mphal.c +++ b/extmod/utime_mphal.c @@ -89,12 +89,10 @@ STATIC mp_obj_t time_ticks_diff(mp_obj_t end_in, mp_obj_t start_in) { // we assume that the arguments come from ticks_xx so are small ints uint32_t start = MP_OBJ_SMALL_INT_VALUE(start_in); uint32_t end = MP_OBJ_SMALL_INT_VALUE(end_in); - int32_t diff = end - start; - if (diff < (signed)-(MICROPY_PY_UTIME_TICKS_PERIOD / 2)) { - diff += MICROPY_PY_UTIME_TICKS_PERIOD; - } else if (diff >= (signed)(MICROPY_PY_UTIME_TICKS_PERIOD / 2)) { - diff -= MICROPY_PY_UTIME_TICKS_PERIOD; - } + // Optimized formula avoiding if conditions. We adjust difference "forward", + // wrap it around and adjust back. + int32_t diff = ((end - start + MICROPY_PY_UTIME_TICKS_PERIOD / 2) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1)) + - MICROPY_PY_UTIME_TICKS_PERIOD / 2; return MP_OBJ_NEW_SMALL_INT(diff); } MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_diff_obj, time_ticks_diff); |