Debian Codegraph @

floatfmt.go

raw
1 package schema
2
3 import (
4 "math"
5 "math/bits"
6 "strconv"
7
8 utils "github.com/gofiber/utils/v2"
9 )
10
11 // encFloatPrec is the number of fractional digits the encoder emits for
12 // float fields.
13 const encFloatPrec = 6
14
15 // scaleFixed is 10^encFloatPrec, the factor that turns a value into the
16 // integer the formatter prints.
17 const scaleFixed = 1_000_000
18
19 // fastFixedLimit bounds the magnitude the exact-integer path accepts: below
20 // it |value|*10^6 stays under 10^18, so the scaled result and a round-up's +1
21 // both fit a uint64.
22 const fastFixedLimit = 1e12
23
24 // formatFloatFixed is strconv.FormatFloat(f, 'f', encFloatPrec, bitSize).
25 //
26 // strconv has no Ryƫ fast path for the 'f' verb with a fixed precision (see
27 // the `fmt != 'f'` guard in its ftoa), so every such call runs the
28 // multi-precision decimal path. A float's exact value is mant*2^exp, so for
29 // exponents in range the printed digits are exactly
30 // round-half-to-even(mant*10^6 * 2^exp), which a 128-bit multiply and a shift
31 // compute directly; utils.AppendUint then lays down the digits. Inf, NaN and
32 // magnitudes outside the fast range fall back to strconv.
33 func formatFloatFixed(f float64, bitSize int) string {
34 if bitSize == 32 {
35 // strconv formats the float32 value, which is what the decomposition
36 // below must see.
37 f = float64(float32(f))
38 }
39
40 u := math.Float64bits(f)
41 neg := u>>63 != 0
42 biasedExp := int(u>>52) & 0x7FF
43 mant := u & (1<<52 - 1)
44 if biasedExp == 0x7FF {
45 // Inf or NaN.
46 return strconv.FormatFloat(f, 'f', encFloatPrec, bitSize)
47 }
48 if biasedExp == 0 {
49 biasedExp = 1 // subnormal: no implicit leading bit
50 } else {
51 mant |= 1 << 52
52 }
53 // f == mant * 2^exp exactly.
54 exp := biasedExp - 1023 - 52
55 // exp >= 0 means |f| >= 2^52, where f*10^6 overflows a uint64; the
56 // magnitude check bounds the scaled value for everything else.
57 if exp >= 0 || f > fastFixedLimit || f < -fastFixedLimit {
58 return strconv.FormatFloat(f, 'f', encFloatPrec, bitSize)
59 }
60
61 // n = round-half-to-even(mant*10^6 / 2^k), computed on the exact
62 // 128-bit product so the tie test sees the value's true remainder.
63 k := uint(-exp)
64 hi, lo := bits.Mul64(mant, scaleFixed)
65 var n uint64
66 switch {
67 case k >= 74:
68 // mant*10^6 < 2^73 <= 2^(k-1), i.e. below half a unit: rounds to 0.
69 n = 0
70 case k < 64:
71 n = lo>>k | hi<<(64-k)
72 half := uint64(1) << (k - 1)
73 if r := lo & (half<<1 - 1); r > half || (r == half && n&1 != 0) {
74 n++
75 }
76 case k == 64:
77 n = hi
78 if lo > 1<<63 || (lo == 1<<63 && n&1 != 0) {
79 n++
80 }
81 default:
82 s := k - 64
83 n = hi >> s
84 half := uint64(1) << (s - 1)
85 // The discarded bits are hi's low s bits followed by all of lo, so
86 // a tie needs the half bit set and every lower bit clear.
87 if r := hi & (half<<1 - 1); r > half || (r == half && (lo != 0 || n&1 != 0)) {
88 n++
89 }
90 }
91
92 // Sign, integer digits (at most 13), '.', then six fractional digits.
93 var buf [24]byte
94 dst := buf[:0]
95 if neg {
96 dst = append(dst, '-')
97 }
98 dst = utils.AppendUint(dst, n/scaleFixed)
99 dst = append(dst, '.')
100 // Formatting 10^6+frac and dropping its leading '1' zero-pads the
101 // fraction without a per-digit loop.
102 var frac [8]byte
103 dst = append(dst, utils.AppendUint(frac[:0], scaleFixed+n%scaleFixed)[1:]...)
104 return string(dst)
105 }