@@ -20,42 +20,6 @@ https://github.com/cespare/ryu/tree/ba56a33f39e3bbbfa409095d0f9ae168a595feea
20
20
21
21
=============================================================================*/
22
22
23
- // pow of ten table used by n_digit reduction
24
- const (
25
- ten_pow_table_64 = [
26
- u64 (1 ),
27
- u64 (10 ),
28
- u64 (100 ),
29
- u64 (1000 ),
30
- u64 (10000 ),
31
- u64 (100000 ),
32
- u64 (1000000 ),
33
- u64 (10000000 ),
34
- u64 (100000000 ),
35
- u64 (1000000000 ),
36
- u64 (10000000000 ),
37
- u64 (100000000000 ),
38
- u64 (1000000000000 ),
39
- u64 (10000000000000 ),
40
- u64 (100000000000000 ),
41
- u64 (1000000000000000 ),
42
- u64 (10000000000000000 ),
43
- u64 (100000000000000000 ),
44
- u64 (1000000000000000000 ),
45
- u64 (10000000000000000000 ),
46
- ]
47
- )
48
-
49
- // =============================================================================
50
- // Conversion Functions
51
- // =============================================================================
52
- const (
53
- mantbits64 = u32 (52 )
54
- expbits64 = u32 (11 )
55
- bias64 = 1023 // f64 exponent bias
56
- maxexp64 = 2047
57
- )
58
-
59
23
[direct_array_access ]
60
24
fn (d Dec64) get_string_64 (neg bool , i_n_digit int , i_pad_digit int ) string {
61
25
mut n_digit := i_n_digit + 1
@@ -87,10 +51,10 @@ fn (d Dec64) get_string_64(neg bool, i_n_digit int, i_pad_digit int) string {
87
51
// rounding last used digit
88
52
if n_digit < out_len {
89
53
// println("out:[$out]")
90
- out + = strconv. ten_pow_table_64 [out_len - n_digit - 1 ] * 5 // round to up
91
- out / = strconv. ten_pow_table_64 [out_len - n_digit]
54
+ out + = ten_pow_table_64 [out_len - n_digit - 1 ] * 5 // round to up
55
+ out / = ten_pow_table_64 [out_len - n_digit]
92
56
// println("out1:[$out] ${d.m / ten_pow_table_64[out_len - n_digit ]}")
93
- if d.m / strconv. ten_pow_table_64 [out_len - n_digit] < out {
57
+ if d.m / ten_pow_table_64 [out_len - n_digit] < out {
94
58
d_exp++
95
59
n_digit++
96
60
}
@@ -170,11 +134,11 @@ fn (d Dec64) get_string_64(neg bool, i_n_digit int, i_pad_digit int) string {
170
134
171
135
fn f64_to_decimal_exact_int (i_mant u64 , exp u64 ) (Dec64 , bool ) {
172
136
mut d := Dec64 {}
173
- e := exp - strconv. bias64
174
- if e > strconv. mantbits64 {
137
+ e := exp - bias64
138
+ if e > mantbits64 {
175
139
return d, false
176
140
}
177
- shift := strconv. mantbits64 - e
141
+ shift := mantbits64 - e
178
142
mant := i_mant | u64 (0x0010_0000_0000_0000 ) // implicit 1
179
143
// mant := i_mant | (1 << mantbits64) // implicit 1
180
144
d.m = mant >> shift
@@ -195,11 +159,11 @@ fn f64_to_decimal(mant u64, exp u64) Dec64 {
195
159
if exp == 0 {
196
160
// We subtract 2 so that the bounds computation has
197
161
// 2 additional bits.
198
- e2 = 1 - strconv. bias64 - int (strconv. mantbits64 ) - 2
162
+ e2 = 1 - bias64 - int (mantbits64 ) - 2
199
163
m2 = mant
200
164
} else {
201
- e2 = int (exp) - strconv. bias64 - int (strconv. mantbits64 ) - 2
202
- m2 = (u64 (1 ) << strconv. mantbits64 ) | mant
165
+ e2 = int (exp) - bias64 - int (mantbits64 ) - 2
166
+ m2 = (u64 (1 ) << mantbits64 ) | mant
203
167
}
204
168
even := (m2 & 1 ) == 0
205
169
accept_bounds := even
@@ -373,13 +337,13 @@ pub fn f64_to_str(f f64, n_digit int) string {
373
337
u1 .f = f
374
338
u := unsafe { u1 .u }
375
339
376
- neg := (u >> (strconv. mantbits64 + strconv. expbits64 )) != 0
377
- mant := u & ((u64 (1 ) << strconv. mantbits64 ) - u64 (1 ))
378
- exp := (u >> strconv. mantbits64 ) & ((u64 (1 ) << strconv. expbits64 ) - u64 (1 ))
340
+ neg := (u >> (mantbits64 + expbits64 )) != 0
341
+ mant := u & ((u64 (1 ) << mantbits64 ) - u64 (1 ))
342
+ exp := (u >> mantbits64 ) & ((u64 (1 ) << expbits64 ) - u64 (1 ))
379
343
// println("s:${neg} mant:${mant} exp:${exp} float:${f} byte:${u1.u:016lx}")
380
344
381
345
// Exit early for easy cases.
382
- if (exp == strconv. maxexp64 ) || (exp == 0 && mant == 0 ) {
346
+ if (exp == maxexp64 ) || (exp == 0 && mant == 0 ) {
383
347
return get_string_special (neg , exp == 0 , mant == 0 )
384
348
}
385
349
@@ -398,13 +362,13 @@ pub fn f64_to_str_pad(f f64, n_digit int) string {
398
362
u1 .f = f
399
363
u := unsafe { u1 .u }
400
364
401
- neg := (u >> (strconv. mantbits64 + strconv. expbits64 )) != 0
402
- mant := u & ((u64 (1 ) << strconv. mantbits64 ) - u64 (1 ))
403
- exp := (u >> strconv. mantbits64 ) & ((u64 (1 ) << strconv. expbits64 ) - u64 (1 ))
365
+ neg := (u >> (mantbits64 + expbits64 )) != 0
366
+ mant := u & ((u64 (1 ) << mantbits64 ) - u64 (1 ))
367
+ exp := (u >> mantbits64 ) & ((u64 (1 ) << expbits64 ) - u64 (1 ))
404
368
// println("s:${neg} mant:${mant} exp:${exp} float:${f} byte:${u1.u:016lx}")
405
369
406
370
// Exit early for easy cases.
407
- if (exp == strconv. maxexp64 ) || (exp == 0 && mant == 0 ) {
371
+ if (exp == maxexp64 ) || (exp == 0 && mant == 0 ) {
408
372
return get_string_special (neg , exp == 0 , mant == 0 )
409
373
}
410
374
0 commit comments