mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-18 11:33:59 +00:00
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.
This commit is contained in:
@@ -75,7 +75,7 @@ var (
|
|||||||
// handling the overflow event.
|
// handling the overflow event.
|
||||||
func ticks() timeUnit {
|
func ticks() timeUnit {
|
||||||
rtcCounter := uint32(nrf.RTC0.COUNTER)
|
rtcCounter := uint32(nrf.RTC0.COUNTER)
|
||||||
offset := (rtcCounter - rtcLastCounter) % 0xffffff // change since last measurement
|
offset := (rtcCounter - rtcLastCounter) & 0xffffff // change since last measurement
|
||||||
rtcLastCounter = rtcCounter
|
rtcLastCounter = rtcCounter
|
||||||
timestamp += timeUnit(offset) // TODO: not precise
|
timestamp += timeUnit(offset) // TODO: not precise
|
||||||
return timestamp
|
return timestamp
|
||||||
|
|||||||
Reference in New Issue
Block a user