From 5bf058a0a61df64f6b150443926fd1456dafb432 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sat, 29 Sep 2018 16:16:38 +0200 Subject: [PATCH] nrf: fix off-by-one in modulo of runtime.ticks This code: foo & 0xffffff Is equivalent to this code: foo % 0x1000000 However, to drop the high 8 bits, this calculation was used: foo % 0xffffff This is far more expensive (and incorrect), as it needs an actual modulo operation which increases code size and probably reduces speed on a Cortex-M4 and needs library functions for a Cortex-M0 increasing code size by a much bigger amount. --- src/runtime/runtime_nrf.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/runtime_nrf.go b/src/runtime/runtime_nrf.go index 1cccce97..8ec08404 100644 --- a/src/runtime/runtime_nrf.go +++ b/src/runtime/runtime_nrf.go @@ -75,7 +75,7 @@ var ( // handling the overflow event. func ticks() timeUnit { rtcCounter := uint32(nrf.RTC0.COUNTER) - offset := (rtcCounter - rtcLastCounter) % 0xffffff // change since last measurement + offset := (rtcCounter - rtcLastCounter) & 0xffffff // change since last measurement rtcLastCounter = rtcCounter timestamp += timeUnit(offset) // TODO: not precise return timestamp