mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-16 02:33:28 +00:00
Improve runtime.printuint32: avoid recursion
This reduces size slightly at least on the PCA10040, and is probably faster and probably uses less stack as well.
This commit is contained in:
+14
-6
@@ -43,13 +43,21 @@ func printint16(n uint16) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func printuint32(n uint32) {
|
func printuint32(n uint32) {
|
||||||
// TODO: don't recurse, but still be compact (and don't divide/mod
|
digits := [10]byte{} // enough to hold (2^32)-1
|
||||||
// more than necessary).
|
// Fill in all 10 digits.
|
||||||
prevdigits := n / 10
|
firstdigit := 9 // digit index that isn't zero (by default, the last to handle '0' correctly)
|
||||||
if prevdigits != 0 {
|
for i := 9; i >= 0; i-- {
|
||||||
printuint32(prevdigits)
|
digit := byte(n%10 + '0')
|
||||||
|
digits[i] = digit
|
||||||
|
if digit != '0' {
|
||||||
|
firstdigit = i
|
||||||
|
}
|
||||||
|
n /= 10
|
||||||
|
}
|
||||||
|
// Print digits without the leading zeroes.
|
||||||
|
for i := firstdigit; i < 10; i++ {
|
||||||
|
putchar(digits[i])
|
||||||
}
|
}
|
||||||
putchar(byte((n % 10) + '0'))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func printint32(n int32) {
|
func printint32(n int32) {
|
||||||
|
|||||||
Reference in New Issue
Block a user