1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/strconv/utilities.v

177 lines
3.5 KiB
V
Raw Normal View History

module strconv
import math.bits
// general utilities
2020-02-25 13:12:37 +03:00
// General Utilities
[if debug_strconv ?]
2020-02-25 13:12:37 +03:00
fn assert1(t bool, msg string) {
if !t {
panic(msg)
}
}
[inline]
fn bool_to_int(b bool) int {
if b {
return 1
}
return 0
}
[inline]
fn bool_to_u32(b bool) u32 {
if b {
return u32(1)
}
return u32(0)
}
[inline]
fn bool_to_u64(b bool) u64 {
if b {
return u64(1)
}
return u64(0)
}
fn get_string_special(neg bool, expZero bool, mantZero bool) string {
if !mantZero {
2021-06-18 17:59:56 +03:00
return 'nan'
2020-02-25 13:12:37 +03:00
}
if !expZero {
if neg {
2021-06-18 17:59:56 +03:00
return '-inf'
2020-02-25 13:12:37 +03:00
} else {
2021-06-18 17:59:56 +03:00
return '+inf'
2020-02-25 13:12:37 +03:00
}
}
if neg {
2021-06-18 17:59:56 +03:00
return '-0e+00'
2020-02-25 13:12:37 +03:00
}
2021-06-18 17:59:56 +03:00
return '0e+00'
2020-02-25 13:12:37 +03:00
}
/*
2021-06-18 17:59:56 +03:00
32 bit functions
*/
2020-02-25 13:12:37 +03:00
fn mul_shift_32(m u32, mul u64, ishift int) u32 {
2020-04-02 15:26:35 +03:00
// QTODO
2021-06-18 17:59:56 +03:00
// assert ishift > 32
2020-02-25 13:12:37 +03:00
hi, lo := bits.mul_64(u64(m), mul)
2021-06-18 17:59:56 +03:00
shifted_sum := (lo >> u64(ishift)) + (hi << u64(64 - ishift))
assert1(shifted_sum <= 2147483647, 'shiftedSum <= math.max_u32')
2020-02-25 13:12:37 +03:00
return u32(shifted_sum)
}
fn mul_pow5_invdiv_pow2(m u32, q u32, j int) u32 {
return mul_shift_32(m, pow5_inv_split_32[q], j)
}
fn mul_pow5_div_pow2(m u32, i u32, j int) u32 {
return mul_shift_32(m, pow5_split_32[i], j)
}
fn pow5_factor_32(i_v u32) u32 {
mut v := i_v
2021-06-18 17:59:56 +03:00
for n := u32(0); true; n++ {
q := v / 5
r := v % 5
2020-02-25 13:12:37 +03:00
if r != 0 {
return n
}
v = q
}
return v
}
// multiple_of_power_of_five_32 reports whether v is divisible by 5^p.
fn multiple_of_power_of_five_32(v u32, p u32) bool {
return pow5_factor_32(v) >= p
}
// multiple_of_power_of_two_32 reports whether v is divisible by 2^p.
fn multiple_of_power_of_two_32(v u32, p u32) bool {
2020-03-01 15:26:09 +03:00
return u32(bits.trailing_zeros_32(v)) >= p
2020-02-25 13:12:37 +03:00
}
// log10_pow2 returns floor(log_10(2^e)).
fn log10_pow2(e int) u32 {
// The first value this approximation fails for is 2^1651
// which is just greater than 10^297.
2021-06-18 17:59:56 +03:00
assert1(e >= 0, 'e >= 0')
assert1(e <= 1650, 'e <= 1650')
2020-02-25 13:12:37 +03:00
return (u32(e) * 78913) >> 18
}
// log10_pow5 returns floor(log_10(5^e)).
fn log10_pow5(e int) u32 {
// The first value this approximation fails for is 5^2621
// which is just greater than 10^1832.
2021-06-18 17:59:56 +03:00
assert1(e >= 0, 'e >= 0')
assert1(e <= 2620, 'e <= 2620')
2020-02-25 13:12:37 +03:00
return (u32(e) * 732923) >> 20
}
// pow5_bits returns ceil(log_2(5^e)), or else 1 if e==0.
fn pow5_bits(e int) int {
// This approximation works up to the point that the multiplication
// overflows at e = 3529. If the multiplication were done in 64 bits,
// it would fail at 5^4004 which is just greater than 2^9297.
2021-06-18 17:59:56 +03:00
assert1(e >= 0, 'e >= 0')
assert1(e <= 3528, 'e <= 3528')
return int(((u32(e) * 1217359) >> 19) + 1)
2020-02-25 13:12:37 +03:00
}
/*
2021-06-18 17:59:56 +03:00
64 bit functions
*/
2021-06-18 17:59:56 +03:00
2020-02-25 13:12:37 +03:00
fn shift_right_128(v Uint128, shift int) u64 {
// The shift value is always modulo 64.
// In the current implementation of the 64-bit version
// of Ryu, the shift value is always < 64.
// (It is in the range [2, 59].)
// Check this here in case a future change requires larger shift
// values. In this case this function needs to be adjusted.
2021-06-18 17:59:56 +03:00
assert1(shift < 64, 'shift < 64')
2020-02-25 13:12:37 +03:00
return (v.hi << u64(64 - shift)) | (v.lo >> u32(shift))
}
fn mul_shift_64(m u64, mul Uint128, shift int) u64 {
hihi, hilo := bits.mul_64(m, mul.hi)
2021-06-18 17:59:56 +03:00
lohi, _ := bits.mul_64(m, mul.lo)
mut sum := Uint128{
lo: lohi + hilo
hi: hihi
}
2020-02-25 13:12:37 +03:00
if sum.lo < lohi {
sum.hi++ // overflow
}
2021-06-18 17:59:56 +03:00
return shift_right_128(sum, shift - 64)
2020-02-25 13:12:37 +03:00
}
fn pow5_factor_64(v_i u64) u32 {
mut v := v_i
2021-06-18 17:59:56 +03:00
for n := u32(0); true; n++ {
q := v / 5
r := v % 5
2020-02-25 13:12:37 +03:00
if r != 0 {
return n
}
v = q
}
return u32(0)
}
fn multiple_of_power_of_five_64(v u64, p u32) bool {
return pow5_factor_64(v) >= p
}
fn multiple_of_power_of_two_64(v u64, p u32) bool {
return u32(bits.trailing_zeros_64(v)) >= p
}