Skip to content

Commit a8a1e93

Browse files
authored
strconv,js: f64_to_str works on JS backend now; Fix BigInt usage in infix expressions (#12464)
1 parent 1d2b16d commit a8a1e93

File tree

7 files changed

+399
-66
lines changed

7 files changed

+399
-66
lines changed

vlib/strconv/f32_str.js.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module strconv

vlib/strconv/f64_str.c.v

Lines changed: 17 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,6 @@ https://github.com/cespare/ryu/tree/ba56a33f39e3bbbfa409095d0f9ae168a595feea
2020
2121
=============================================================================*/
2222

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-
5923
[direct_array_access]
6024
fn (d Dec64) get_string_64(neg bool, i_n_digit int, i_pad_digit int) string {
6125
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 {
8751
// rounding last used digit
8852
if n_digit < out_len {
8953
// 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]
9256
// 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 {
9458
d_exp++
9559
n_digit++
9660
}
@@ -170,11 +134,11 @@ fn (d Dec64) get_string_64(neg bool, i_n_digit int, i_pad_digit int) string {
170134

171135
fn f64_to_decimal_exact_int(i_mant u64, exp u64) (Dec64, bool) {
172136
mut d := Dec64{}
173-
e := exp - strconv.bias64
174-
if e > strconv.mantbits64 {
137+
e := exp - bias64
138+
if e > mantbits64 {
175139
return d, false
176140
}
177-
shift := strconv.mantbits64 - e
141+
shift := mantbits64 - e
178142
mant := i_mant | u64(0x0010_0000_0000_0000) // implicit 1
179143
// mant := i_mant | (1 << mantbits64) // implicit 1
180144
d.m = mant >> shift
@@ -195,11 +159,11 @@ fn f64_to_decimal(mant u64, exp u64) Dec64 {
195159
if exp == 0 {
196160
// We subtract 2 so that the bounds computation has
197161
// 2 additional bits.
198-
e2 = 1 - strconv.bias64 - int(strconv.mantbits64) - 2
162+
e2 = 1 - bias64 - int(mantbits64) - 2
199163
m2 = mant
200164
} 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
203167
}
204168
even := (m2 & 1) == 0
205169
accept_bounds := even
@@ -373,13 +337,13 @@ pub fn f64_to_str(f f64, n_digit int) string {
373337
u1.f = f
374338
u := unsafe { u1.u }
375339

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))
379343
// println("s:${neg} mant:${mant} exp:${exp} float:${f} byte:${u1.u:016lx}")
380344

381345
// Exit early for easy cases.
382-
if (exp == strconv.maxexp64) || (exp == 0 && mant == 0) {
346+
if (exp == maxexp64) || (exp == 0 && mant == 0) {
383347
return get_string_special(neg, exp == 0, mant == 0)
384348
}
385349

@@ -398,13 +362,13 @@ pub fn f64_to_str_pad(f f64, n_digit int) string {
398362
u1.f = f
399363
u := unsafe { u1.u }
400364

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))
404368
// println("s:${neg} mant:${mant} exp:${exp} float:${f} byte:${u1.u:016lx}")
405369

406370
// Exit early for easy cases.
407-
if (exp == strconv.maxexp64) || (exp == 0 && mant == 0) {
371+
if (exp == maxexp64) || (exp == 0 && mant == 0) {
408372
return get_string_special(neg, exp == 0, mant == 0)
409373
}
410374

0 commit comments

Comments
 (0)