Source file
src/strconv/ftoaryu.go
Documentation: strconv
1
2
3
4
5 package strconv
6
7 import (
8 "math/bits"
9 )
10
11
12
13
14
15
16
17
18
19
20 func ryuFtoaFixed32(d *decimalSlice, mant uint32, exp int, prec int) {
21 if prec < 0 {
22 panic("ryuFtoaFixed32 called with negative prec")
23 }
24 if prec > 9 {
25 panic("ryuFtoaFixed32 called with prec > 9")
26 }
27
28 if mant == 0 {
29 d.nd, d.dp = 0, 0
30 return
31 }
32
33 e2 := exp
34 if b := bits.Len32(mant); b < 25 {
35 mant <<= uint(25 - b)
36 e2 += int(b) - 25
37 }
38
39
40
41
42
43
44 q := -mulByLog2Log10(e2+24) + prec - 1
45
46
47
48
49 exact := q <= 27 && q >= 0
50
51 di, dexp2, d0 := mult64bitPow10(mant, e2, q)
52 if dexp2 >= 0 {
53 panic("not enough significant bits after mult64bitPow10")
54 }
55
56
57
58
59 if q < 0 && q >= -10 && divisibleByPower5(uint64(mant), -q) {
60 exact = true
61 d0 = true
62 }
63
64 extra := uint(-dexp2)
65 extraMask := uint32(1<<extra - 1)
66
67 di, dfrac := di>>extra, di&extraMask
68 roundUp := false
69 if exact {
70
71
72 roundUp = dfrac > 1<<(extra-1) ||
73 (dfrac == 1<<(extra-1) && !d0) ||
74 (dfrac == 1<<(extra-1) && d0 && di&1 == 1)
75 } else {
76
77
78 roundUp = dfrac>>(extra-1) == 1
79 }
80 if dfrac != 0 {
81 d0 = false
82 }
83
84 formatDecimal(d, uint64(di), !d0, roundUp, prec)
85
86 d.dp -= q
87 }
88
89
90 func ryuFtoaFixed64(d *decimalSlice, mant uint64, exp int, prec int) {
91 if prec > 18 {
92 panic("ryuFtoaFixed64 called with prec > 18")
93 }
94
95 if mant == 0 {
96 d.nd, d.dp = 0, 0
97 return
98 }
99
100 e2 := exp
101 if b := bits.Len64(mant); b < 55 {
102 mant = mant << uint(55-b)
103 e2 += int(b) - 55
104 }
105
106
107
108
109
110
111
112
113
114 q := -mulByLog2Log10(e2+54) + prec - 1
115
116
117
118
119 exact := q <= 55 && q >= 0
120
121 di, dexp2, d0 := mult128bitPow10(mant, e2, q)
122 if dexp2 >= 0 {
123 panic("not enough significant bits after mult128bitPow10")
124 }
125
126
127
128
129 if q < 0 && q >= -22 && divisibleByPower5(mant, -q) {
130 exact = true
131 d0 = true
132 }
133
134 extra := uint(-dexp2)
135 extraMask := uint64(1<<extra - 1)
136
137 di, dfrac := di>>extra, di&extraMask
138 roundUp := false
139 if exact {
140
141
142 roundUp = dfrac > 1<<(extra-1) ||
143 (dfrac == 1<<(extra-1) && !d0) ||
144 (dfrac == 1<<(extra-1) && d0 && di&1 == 1)
145 } else {
146
147
148 roundUp = dfrac>>(extra-1) == 1
149 }
150 if dfrac != 0 {
151 d0 = false
152 }
153
154 formatDecimal(d, di, !d0, roundUp, prec)
155
156 d.dp -= q
157 }
158
159 var uint64pow10 = [...]uint64{
160 1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
161 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
162 }
163
164
165
166
167 func formatDecimal(d *decimalSlice, m uint64, trunc bool, roundUp bool, prec int) {
168 max := uint64pow10[prec]
169 trimmed := 0
170 for m >= max {
171 a, b := m/10, m%10
172 m = a
173 trimmed++
174 if b > 5 {
175 roundUp = true
176 } else if b < 5 {
177 roundUp = false
178 } else {
179
180
181 roundUp = trunc || m&1 == 1
182 }
183 if b != 0 {
184 trunc = true
185 }
186 }
187 if roundUp {
188 m++
189 }
190 if m >= max {
191
192 m /= 10
193 trimmed++
194 }
195
196 n := uint(prec)
197 d.nd = int(prec)
198 v := m
199 for v >= 100 {
200 var v1, v2 uint64
201 if v>>32 == 0 {
202 v1, v2 = uint64(uint32(v)/100), uint64(uint32(v)%100)
203 } else {
204 v1, v2 = v/100, v%100
205 }
206 n -= 2
207 d.d[n+1] = smallsString[2*v2+1]
208 d.d[n+0] = smallsString[2*v2+0]
209 v = v1
210 }
211 if v > 0 {
212 n--
213 d.d[n] = smallsString[2*v+1]
214 }
215 if v >= 10 {
216 n--
217 d.d[n] = smallsString[2*v]
218 }
219 for d.d[d.nd-1] == '0' {
220 d.nd--
221 trimmed++
222 }
223 d.dp = d.nd + trimmed
224 }
225
226
227 func ryuFtoaShortest(d *decimalSlice, mant uint64, exp int, flt *floatInfo) {
228 if mant == 0 {
229 d.nd, d.dp = 0, 0
230 return
231 }
232
233
234 if exp <= 0 && bits.TrailingZeros64(mant) >= -exp {
235 mant >>= uint(-exp)
236 ryuDigits(d, mant, mant, mant, true, false)
237 return
238 }
239 ml, mc, mu, e2 := computeBounds(mant, exp, flt)
240 if e2 == 0 {
241 ryuDigits(d, ml, mc, mu, true, false)
242 return
243 }
244
245 q := mulByLog2Log10(-e2) + 1
246
247
248
249 var dl, dc, du uint64
250 var dl0, dc0, du0 bool
251 if flt == &float32info {
252 var dl32, dc32, du32 uint32
253 dl32, _, dl0 = mult64bitPow10(uint32(ml), e2, q)
254 dc32, _, dc0 = mult64bitPow10(uint32(mc), e2, q)
255 du32, e2, du0 = mult64bitPow10(uint32(mu), e2, q)
256 dl, dc, du = uint64(dl32), uint64(dc32), uint64(du32)
257 } else {
258 dl, _, dl0 = mult128bitPow10(ml, e2, q)
259 dc, _, dc0 = mult128bitPow10(mc, e2, q)
260 du, e2, du0 = mult128bitPow10(mu, e2, q)
261 }
262 if e2 >= 0 {
263 panic("not enough significant bits after mult128bitPow10")
264 }
265
266 if q > 55 {
267
268 dl0, dc0, du0 = false, false, false
269 }
270 if q < 0 && q >= -24 {
271
272
273 if divisibleByPower5(ml, -q) {
274 dl0 = true
275 }
276 if divisibleByPower5(mc, -q) {
277 dc0 = true
278 }
279 if divisibleByPower5(mu, -q) {
280 du0 = true
281 }
282 }
283
284
285 extra := uint(-e2)
286 extraMask := uint64(1<<extra - 1)
287
288 dl, fracl := dl>>extra, dl&extraMask
289 dc, fracc := dc>>extra, dc&extraMask
290 du, fracu := du>>extra, du&extraMask
291
292
293
294
295 uok := !du0 || fracu > 0
296 if du0 && fracu == 0 {
297 uok = mant&1 == 0
298 }
299 if !uok {
300 du--
301 }
302
303
304 cup := false
305 if dc0 {
306
307
308 cup = fracc > 1<<(extra-1) ||
309 (fracc == 1<<(extra-1) && dc&1 == 1)
310 } else {
311
312
313 cup = fracc>>(extra-1) == 1
314 }
315
316
317
318 lok := dl0 && fracl == 0 && (mant&1 == 0)
319 if !lok {
320 dl++
321 }
322
323 c0 := dc0 && fracc == 0
324
325 ryuDigits(d, dl, dc, du, c0, cup)
326 d.dp -= q
327 }
328
329
330
331
332
333
334 func mulByLog2Log10(x int) int {
335
336 return (x * 78913) >> 18
337 }
338
339
340
341
342
343
344 func mulByLog10Log2(x int) int {
345
346 return (x * 108853) >> 15
347 }
348
349
350
351
352 func computeBounds(mant uint64, exp int, flt *floatInfo) (lower, central, upper uint64, e2 int) {
353 if mant != 1<<flt.mantbits || exp == flt.bias+1-int(flt.mantbits) {
354
355 lower, central, upper = 2*mant-1, 2*mant, 2*mant+1
356 e2 = exp - 1
357 return
358 } else {
359
360 lower, central, upper = 4*mant-1, 4*mant, 4*mant+2
361 e2 = exp - 2
362 return
363 }
364 }
365
366 func ryuDigits(d *decimalSlice, lower, central, upper uint64,
367 c0, cup bool) {
368 lhi, llo := divmod1e9(lower)
369 chi, clo := divmod1e9(central)
370 uhi, ulo := divmod1e9(upper)
371 if uhi == 0 {
372
373 ryuDigits32(d, llo, clo, ulo, c0, cup, 8)
374 } else if lhi < uhi {
375
376 if llo != 0 {
377 lhi++
378 }
379 c0 = c0 && clo == 0
380 cup = (clo > 5e8) || (clo == 5e8 && cup)
381 ryuDigits32(d, lhi, chi, uhi, c0, cup, 8)
382 d.dp += 9
383 } else {
384 d.nd = 0
385
386 n := uint(9)
387 for v := chi; v > 0; {
388 v1, v2 := v/10, v%10
389 v = v1
390 n--
391 d.d[n] = byte(v2 + '0')
392 }
393 d.d = d.d[n:]
394 d.nd = int(9 - n)
395
396 ryuDigits32(d, llo, clo, ulo,
397 c0, cup, d.nd+8)
398 }
399
400 for d.nd > 0 && d.d[d.nd-1] == '0' {
401 d.nd--
402 }
403
404 for d.nd > 0 && d.d[0] == '0' {
405 d.nd--
406 d.dp--
407 d.d = d.d[1:]
408 }
409 }
410
411
412 func ryuDigits32(d *decimalSlice, lower, central, upper uint32,
413 c0, cup bool, endindex int) {
414 if upper == 0 {
415 d.dp = endindex + 1
416 return
417 }
418 trimmed := 0
419
420
421 cNextDigit := 0
422 for upper > 0 {
423
424
425
426
427
428 l := (lower + 9) / 10
429 c, cdigit := central/10, central%10
430 u := upper / 10
431 if l > u {
432
433
434 break
435 }
436
437
438
439
440
441
442
443
444
445 if l == c+1 && c < u {
446 c++
447 cdigit = 0
448 cup = false
449 }
450 trimmed++
451
452 c0 = c0 && cNextDigit == 0
453 cNextDigit = int(cdigit)
454 lower, central, upper = l, c, u
455 }
456
457 if trimmed > 0 {
458 cup = cNextDigit > 5 ||
459 (cNextDigit == 5 && !c0) ||
460 (cNextDigit == 5 && c0 && central&1 == 1)
461 }
462 if central < upper && cup {
463 central++
464 }
465
466 endindex -= trimmed
467 v := central
468 n := endindex
469 for n > d.nd {
470 v1, v2 := v/100, v%100
471 d.d[n] = smallsString[2*v2+1]
472 d.d[n-1] = smallsString[2*v2+0]
473 n -= 2
474 v = v1
475 }
476 if n == d.nd {
477 d.d[n] = byte(v + '0')
478 }
479 d.nd = endindex + 1
480 d.dp = d.nd + trimmed
481 }
482
483
484
485
486
487
488
489
490
491
492 func mult64bitPow10(m uint32, e2, q int) (resM uint32, resE int, exact bool) {
493 if q == 0 {
494
495 return m << 6, e2 - 6, true
496 }
497 if q < detailedPowersOfTenMinExp10 || detailedPowersOfTenMaxExp10 < q {
498
499 panic("mult64bitPow10: power of 10 is out of range")
500 }
501 pow := detailedPowersOfTen[q-detailedPowersOfTenMinExp10][1]
502 if q < 0 {
503
504 pow += 1
505 }
506 hi, lo := bits.Mul64(uint64(m), pow)
507 e2 += mulByLog10Log2(q) - 63 + 57
508 return uint32(hi<<7 | lo>>57), e2, lo<<7 == 0
509 }
510
511
512
513
514
515
516
517
518
519
520 func mult128bitPow10(m uint64, e2, q int) (resM uint64, resE int, exact bool) {
521 if q == 0 {
522
523 return m << 8, e2 - 8, true
524 }
525 if q < detailedPowersOfTenMinExp10 || detailedPowersOfTenMaxExp10 < q {
526
527 panic("mult128bitPow10: power of 10 is out of range")
528 }
529 pow := detailedPowersOfTen[q-detailedPowersOfTenMinExp10]
530 if q < 0 {
531
532 pow[0] += 1
533 }
534 e2 += mulByLog10Log2(q) - 127 + 119
535
536
537 l1, l0 := bits.Mul64(m, pow[0])
538 h1, h0 := bits.Mul64(m, pow[1])
539 mid, carry := bits.Add64(l1, h0, 0)
540 h1 += carry
541 return h1<<9 | mid>>55, e2, mid<<9 == 0 && l0 == 0
542 }
543
544 func divisibleByPower5(m uint64, k int) bool {
545 if m == 0 {
546 return true
547 }
548 for i := 0; i < k; i++ {
549 if m%5 != 0 {
550 return false
551 }
552 m /= 5
553 }
554 return true
555 }
556
557
558
559 func divmod1e9(x uint64) (uint32, uint32) {
560 if !host32bit {
561 return uint32(x / 1e9), uint32(x % 1e9)
562 }
563
564 hi, _ := bits.Mul64(x>>1, 0x89705f4136b4a598)
565 q := hi >> 28
566 return uint32(q), uint32(x - q*1e9)
567 }
568
View as plain text