runtime: fix sleep duration for long sleeps

Long sleep durations weren't implemented correctly, by simply casting to
a smaller number of bits. What we want is a saturating downcast, which
is what I've used here instead.

This should fix https://github.com/tinygo-org/tinygo/issues/3356.
This commit is contained in:
Ayke van Laethem
2025-10-01 11:56:18 +02:00
committed by Ron Evans
parent faf455283d
commit 8872d5ebfc
5 changed files with 14 additions and 2 deletions
+2 -2
View File
@@ -43,8 +43,8 @@ func TestBinarySize(t *testing.T) {
tests := []sizeTest{
// microcontrollers
{"hifive1b", "examples/echo", 3884, 280, 0, 2268},
{"microbit", "examples/serial", 2852, 360, 8, 2272},
{"wioterminal", "examples/pininterrupt", 7337, 1491, 116, 6912},
{"microbit", "examples/serial", 2844, 360, 8, 2272},
{"wioterminal", "examples/pininterrupt", 7349, 1491, 116, 6912},
// TODO: also check wasm. Right now this is difficult, because
// wasm binaries are run through wasm-opt and therefore the
+3
View File
@@ -273,6 +273,9 @@ func nanosecondsToTicks(ns int64) timeUnit {
func sleepTicks(d timeUnit) {
for d != 0 {
ticks := uint32(d)
if d > 0xffff_ffff {
ticks = 0xffff_ffff
}
if !timerSleep(ticks) {
// Bail out early to handle a non-time interrupt.
return
+3
View File
@@ -266,6 +266,9 @@ func nanosecondsToTicks(ns int64) timeUnit {
func sleepTicks(d timeUnit) {
for d != 0 {
ticks := uint32(d)
if d > 0xffff_ffff {
ticks = 0xffff_ffff
}
if !timerSleep(ticks) {
return
}
+3
View File
@@ -78,6 +78,9 @@ func buffered() int {
func sleepTicks(d timeUnit) {
for d != 0 {
ticks := uint32(d) & 0x7fffff // 23 bits (to be on the safe side)
if d > 0x7fffff {
ticks = 0x7fffff
}
rtc_sleep(ticks)
d -= timeUnit(ticks)
}
+3
View File
@@ -81,6 +81,9 @@ func buffered() int {
func sleepTicks(d timeUnit) {
for d != 0 {
ticks := uint32(d) & 0x7fffff // 23 bits (to be on the safe side)
if d > 0x7fffff {
ticks = 0x7fffff
}
rtc_sleep(ticks)
d -= timeUnit(ticks)
}