diff --git a/lib/std/private/digitsutils.nim b/lib/std/private/digitsutils.nim index 045d90f8bafb0..ecf642d8c4ddf 100644 --- a/lib/std/private/digitsutils.nim +++ b/lib/std/private/digitsutils.nim @@ -94,33 +94,21 @@ func digits10*(x: uint32): int {.inline.} = elif x >= firstPow10(2): 2 # 1, 2 else: 1 -# proc numToString*(result: var openArray[char], num: uint64) {.inline.} = -# var x = num -# var i = result.len - 2 -# while i >= 0: -# let xi = (x mod 100) shl 1 -# x = x div 100 -# when defined(nimHasDragonBox): # pending bootstrap >= 1.4.0, nimHasDragonBox isn't relevant -# cast[ptr uint16](result[i].addr)[] = cast[ptr uint16](digits100[xi].unsafeAddr)[] -# else: -# result[i] = digits100[xi] -# result[i+1] = digits100[xi+1] -# i = i - 2 -# if i == - 1: -# result[0] = chr(ord('0') + x) - -proc numToString*(result: var string, num: uint64, start: int) {.inline.} = +proc addIntImpl*(result: var string, num: uint64, start: int) {.inline.} = # pending bug #15952, use `(result: var openArray[char], num: uint64)` var x = num var i = result.len - 2 while i >= start: let xi = (x mod 100) shl 1 x = x div 100 - when defined(nimHasDragonBox): # pending bootstrap >= 1.4.0, nimHasDragonBox isn't relevant - cast[ptr uint16](result[i].addr)[] = cast[ptr uint16](digits100[xi].unsafeAddr)[] - else: + template fallback = result[i] = digits100[xi] result[i+1] = digits100[xi+1] + when nimvm: fallback() + else: + when defined(nimHasDragonBox): # pending bootstrap >= 1.4.0, nimHasDragonBox isn't relevant + cast[ptr uint16](result[i].addr)[] = cast[ptr uint16](digits100[xi].unsafeAddr)[] # faster + else: fallback() i = i - 2 if i == start - 1: result[start] = chr(ord('0') + x) diff --git a/lib/system/dollars.nim b/lib/system/dollars.nim index 8b4fe94d3605a..7ad4e576b41f7 100644 --- a/lib/system/dollars.nim +++ b/lib/system/dollars.nim @@ -8,7 +8,7 @@ proc `$`*(x: int): string {.magic: "IntToStr", noSideEffect.} template dollarImpl(x: uint | uint64, result: var string) = setLen(result, digits10(x)) - numToString(result, x, 0) + addIntImpl(result, x, 0) when defined(js): import std/private/since diff --git a/lib/system/strmantle.nim b/lib/system/strmantle.nim index 44d6c36a0e98d..70710080c4620 100644 --- a/lib/system/strmantle.nim +++ b/lib/system/strmantle.nim @@ -67,8 +67,7 @@ proc addInt*(result: var string; x: int64) = num = uint64(x) length = digits10(num) setLen(result, base + length) - # numToString(toOpenArray(result, result.len - length, result.len - 1), num) - numToString(result, num, result.len - length) + addIntImpl(result, num, result.len - length) proc nimIntToStr(x: int): string {.compilerRtl.} = result = newStringOfCap(sizeof(x)*4)