scoop:scoop.core:0.1.0
src/floating_text.scoop
Selected production source · src/floating_text.scoop
1
// Ryu owns shortest digit generation. Core owns the decimal/scientific spelling2
// and constructs exactly one String from an initialized, stable stack buffer.3
internal fun coreFloatToString(value: Float): String {4
var raw = (0uL, 0uL, 0uL, 0uL)5
var text = (0uL, 0uL, 0uL, 0uL)6
@Unsafe {7
val input = addressOf(raw).cast<Byte>()8
val output = addressOf(text).cast<Byte>()9
val length = coreFloatShortest(value, input).toLong()10
val size = coreFormatShortest(input, length, output)11
return coreStringCopyUtf8Unchecked(output, size)12
}13
}15
internal fun coreDoubleToString(value: Double): String {16
var raw = (0uL, 0uL, 0uL, 0uL)17
var text = (0uL, 0uL, 0uL, 0uL)18
@Unsafe {19
val input = addressOf(raw).cast<Byte>()20
val output = addressOf(text).cast<Byte>()21
val length = coreDoubleShortest(value, input).toLong()22
val size = coreFormatShortest(input, length, output)23
return coreStringCopyUtf8Unchecked(output, size)24
}25
}27
@GCLeaf @Extern(name = "f2s_buffered_n")28
private fun coreFloatShortest(value: Float, bytes: Ptr<Byte>): Int30
@GCLeaf @Extern(name = "d2s_buffered_n")31
private fun coreDoubleShortest(value: Double, bytes: Ptr<Byte>): Int33
// Input is Ryu's at-most-24-byte output. All loops have a fixed small bound;34
// output has 32 bytes, with no terminator required by String construction.35
@NoGC @Unsafe36
private fun coreFormatShortest(raw: Ptr<Byte>, length: Long, out: Ptr<Byte>): Long {37
var exponentStart = 0L38
while (exponentStart < length && raw.load(exponentStart) != 69.toByte()) { exponentStart += 1L }39
if (exponentStart == length) {40
var index = 0L41
while (index < length) { out.store(index, raw.load(index)); index += 1L }42
return length43
}44
var written = 0L45
var start = 0L46
if (raw.load() == 45.toByte()) { out.store(45.toByte()); written = 1L; start = 1L }47
var digitStorage = (0uL, 0uL, 0uL)48
val digits = addressOf(digitStorage).cast<Byte>()49
var count = 0L50
var index = start51
while (index < exponentStart) {52
val byte = raw.load(index)53
if (byte != 46.toByte()) { digits.store(count, byte); count += 1L }54
index += 1L55
}56
index = exponentStart + 1L57
var negative = false58
if (raw.load(index) == 45.toByte()) { negative = true; index += 1L }59
else if (raw.load(index) == 43.toByte()) { index += 1L }60
var exponent = 0L61
while (index < length) {62
exponent = exponent * 10L + raw.load(index).toLong() - 48L63
index += 1L64
}65
if (negative) { exponent = -exponent }66
if (exponent >= -3L && exponent < 7L) {67
val point = exponent + 1L68
if (point <= 0L) {69
out.store(written, 48.toByte()); out.store(written + 1L, 46.toByte())70
written += 2L71
index = point72
while (index < 0L) { out.store(written, 48.toByte()); written += 1L; index += 1L }73
index = 0L74
} else {75
index = 0L76
while (index < point) {77
out.store(written, if (index < count) digits.load(index) else 48.toByte())78
written += 1L; index += 1L79
}80
out.store(written, 46.toByte()); written += 1L81
if (point >= count) { out.store(written, 48.toByte()); return written + 1L }82
}83
while (index < count) { out.store(written, digits.load(index)); written += 1L; index += 1L }84
return written85
}86
out.store(written, digits.load()); out.store(written + 1L, 46.toByte())87
written += 2L88
if (count == 1L) { out.store(written, 48.toByte()); written += 1L }89
else {90
index = 1L91
while (index < count) { out.store(written, digits.load(index)); written += 1L; index += 1L }92
}93
out.store(written, 101.toByte()); written += 1L94
if (exponent < 0L) { out.store(written, 45.toByte()); written += 1L; exponent = -exponent }95
// Binary64's exponent magnitude is at most 324. Bounded subtraction avoids96
// the throwing integer-division operation in this NoGC formatter.97
var hundreds = 0L98
while (exponent >= 100L) { exponent -= 100L; hundreds += 1L }99
if (hundreds != 0L) { out.store(written, (48L + hundreds).toByte()); written += 1L }100
var tens = 0L101
while (exponent >= 10L) { exponent -= 10L; tens += 1L }102
if (hundreds != 0L || tens != 0L) { out.store(written, (48L + tens).toByte()); written += 1L }103
out.store(written, (48L + exponent).toByte()); written += 1L104
return written105
}