strconv: float functions cleaning and speed optimization (#10076)

pull/10081/head
penguindark 2021-05-11 07:09:17 +02:00 committed by GitHub
parent 3b062388ba
commit d60a55d30b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 79 deletions

View File

@ -1,6 +1,5 @@
module strconv
/*
/*=============================================================================
f32 to string
@ -18,8 +17,7 @@ Pages 270282 https://doi.org/10.1145/3192366.3192369
inspired by the Go version here:
https://github.com/cespare/ryu/tree/ba56a33f39e3bbbfa409095d0f9ae168a595feea
*/
=============================================================================*/
// pow of ten table used by n_digit reduction
const(
@ -39,11 +37,9 @@ const(
]
)
/*
Conversion Functions
*/
//=============================================================================
// Conversion Functions
//=============================================================================
const(
mantbits32 = u32(23)
expbits32 = u32(8)
@ -53,11 +49,13 @@ const(
// max 46 char
// -3.40282346638528859811704183484516925440e+38
[direct_array_access]
pub fn (d Dec32) get_string_32(neg bool, i_n_digit int, i_pad_digit int) string {
n_digit := i_n_digit + 1
pad_digit := i_pad_digit + 1
mut out := d.m
mut out_len := decimal_len_32(out)
//mut out_len := decimal_len_32(out)
mut out_len := dec_digits(out)
out_len_original := out_len
mut fw_zeros := 0
@ -119,15 +117,6 @@ pub fn (d Dec32) get_string_32(neg bool, i_n_digit int, i_pad_digit int) string
fw_zeros--
}
/*
x=0
for x<buf.len {
C.printf("d:%c\n",buf[x])
x++
}
C.printf("\n")
*/
buf[i]=`e`
i++
@ -150,13 +139,6 @@ pub fn (d Dec32) get_string_32(neg bool, i_n_digit int, i_pad_digit int) string
i++
buf[i]=0
/*
x=0
for x<buf.len {
C.printf("d:%c\n",buf[x])
x++
}
*/
return unsafe {
tos(byteptr(&buf[0]), i)
}
@ -327,6 +309,10 @@ pub fn f32_to_decimal(mant u32, exp u32) Dec32 {
return Dec32{m: out e: e10 + removed}
}
//=============================================================================
// String Functions
//=============================================================================
// f32_to_str return a string in scientific notation with max n_digit after the dot
pub fn f32_to_str(f f32, n_digit int) string {
mut u1 := Uf32{}

View File

@ -1,8 +1,7 @@
module strconv
/*=============================================================================
/*
f32 to string
f64 to string
Copyright (c) 2019-2021 Dario Deledda. All rights reserved.
Use of this source code is governed by an MIT license
@ -18,7 +17,7 @@ Pages 270282 https://doi.org/10.1145/3192366.3192369
inspired by the Go version here:
https://github.com/cespare/ryu/tree/ba56a33f39e3bbbfa409095d0f9ae168a595feea
*/
=============================================================================*/
// pow of ten table used by n_digit reduction
const(
@ -46,11 +45,9 @@ const(
]
)
/*
Conversion Functions
*/
//=============================================================================
// Conversion Functions
//=============================================================================
const(
mantbits64 = u32(52)
expbits64 = u32(11)
@ -58,12 +55,14 @@ const(
maxexp64 = 2047
)
[direct_array_access]
fn (d Dec64) get_string_64(neg bool, i_n_digit int, i_pad_digit int) string {
mut n_digit := i_n_digit + 1
pad_digit := i_pad_digit + 1
mut out := d.m
mut d_exp := d.e
mut out_len := decimal_len_64(out)
// mut out_len := decimal_len_64(out)
mut out_len := dec_digits(out)
out_len_original := out_len
mut fw_zeros := 0
@ -135,15 +134,6 @@ fn (d Dec64) get_string_64(neg bool, i_n_digit int, i_pad_digit int) string {
fw_zeros--
}
/*
x=0
for x<buf.len {
C.printf("d:%c\n",buf[x])
x++
}
C.printf("\n")
*/
buf[i]=`e`
i++
@ -172,14 +162,6 @@ fn (d Dec64) get_string_64(neg bool, i_n_digit int, i_pad_digit int) string {
i++
buf[i]=0
/*
x=0
for x<buf.len {
C.printf("d:%c\n",buf[x])
x++
}
*/
return unsafe {
tos(byteptr(&buf[0]), i)
}
@ -376,6 +358,10 @@ fn f64_to_decimal(mant u64, exp u64) Dec64 {
return Dec64{m: out, e: e10 + removed}
}
//=============================================================================
// String Functions
//=============================================================================
// f64_to_str return a string in scientific notation with max n_digit after the dot
pub fn f64_to_str(f f64, n_digit int) string {
mut u1 := Uf64{}