diff --git a/Cargo.lock b/Cargo.lock index d0d1a5e22b..a33799b9f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -188,6 +188,12 @@ version = "0.2.169" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" +[[package]] +name = "libm" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8355be11b20d696c8f18f6cc018c4e372165b1fa8126cef092399c9951984ffa" + [[package]] name = "libredox" version = "0.1.3" @@ -363,6 +369,7 @@ dependencies = [ "generic-rt", "goblin", "libc", + "libm", "md-5", "memchr", "pbkdf2", diff --git a/Cargo.toml b/Cargo.toml index f7ff066279..a6adf74745 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,14 +36,17 @@ memchr = { version = "2.2.0", default-features = false } plain = "0.2" unicode-width = "0.1" __libc_only_for_layout_checks = { package = "libc", version = "0.2.149", optional = true } -md5-crypto = { package = "md-5", version = "0.10.6", default-features = false} +md5-crypto = { package = "md-5", version = "0.10.6", default-features = false } sha-crypt = { version = "0.5", default-features = false } base64ct = { version = "1.6", default-features = false, features = ["alloc"] } -bcrypt-pbkdf = { version = "0.10", default-features = false, features = ["alloc"] } -scrypt = { version = "0.11", default-features = false, features = ["simple"]} -pbkdf2 = { version = "0.12", features = ["sha2"]} +bcrypt-pbkdf = { version = "0.10", default-features = false, features = [ + "alloc", +] } +scrypt = { version = "0.11", default-features = false, features = ["simple"] } +pbkdf2 = { version = "0.12", features = ["sha2"] } sha2 = { version = "0.10", default-features = false } generic-rt = { path = "generic-rt" } +libm = "0.2" [dependencies.goblin] version = "0.7" @@ -62,7 +65,9 @@ sc = "0.2.3" redox_syscall = "0.5.8" redox-rt = { path = "redox-rt" } redox-path = "0.2" -redox_event = { git = "https://gitlab.redox-os.org/redox-os/event.git", default-features = false, features = ["redox_syscall"] } +redox_event = { git = "https://gitlab.redox-os.org/redox-os/event.git", default-features = false, features = [ + "redox_syscall", +] } [features] default = ["check_against_libc_crate"] @@ -76,4 +81,4 @@ panic = "abort" panic = "abort" [patch.crates-io] -cc-11 = { git = "https://github.com/tea/cc-rs", branch="riscv-abi-arch-fix", package = "cc" } +cc-11 = { git = "https://github.com/tea/cc-rs", branch = "riscv-abi-arch-fix", package = "cc" } diff --git a/src/header/monetary/mod.rs b/src/header/monetary/mod.rs index 2916ad256d..733f752448 100644 --- a/src/header/monetary/mod.rs +++ b/src/header/monetary/mod.rs @@ -6,6 +6,8 @@ use alloc::string::{String, ToString}; use core::{ffi::CStr, ptr, slice, str}; +use libm::{fabs, floor, pow, round, trunc}; + extern crate alloc; mod strfmon; @@ -60,50 +62,6 @@ struct FormatFlags { international: bool, } -/// Use our own floating point implementation -/// TODO : maybe we can use num-traits crate -#[inline] -fn my_fabs(x: f64) -> f64 { - if x < 0.0 { - -x - } else { - x - } -} - -#[inline] -fn my_floor(x: f64) -> f64 { - let i = x as i64; - if x < 0.0 && x != i as f64 { - (i - 1) as f64 - } else { - i as f64 - } -} - -#[inline] -fn my_trunc(x: f64) -> f64 { - if x < 0.0 { - -my_floor(-x) - } else { - my_floor(x) - } -} - -#[inline] -fn my_round(x: f64) -> f64 { - my_floor(x + 0.5) -} - -#[inline] -fn my_pow10(n: usize) -> f64 { - let mut result = 1.0; - for _ in 0..n { - result *= 10.0; - } - result -} - /// Formats a monetary value according to the current locale. fn apply_grouping(int_str: &str, monetary: &LocaleMonetaryInfo) -> String { let mut grouped = String::with_capacity(int_str.len() * 2); @@ -131,15 +89,15 @@ fn apply_grouping(int_str: &str, monetary: &LocaleMonetaryInfo) -> String { /// Safe handling of large monetary values. Returns None if the value is too large to format fn format_value_parts(value: f64, frac_digits: usize) -> Option<(String, i64)> { - let abs_value = my_fabs(value); + let abs_value = fabs(value); if abs_value > (i64::MAX as f64) { // Check if the value is too large to format return None; } - let int_part = my_trunc(abs_value) as i64; - let scale = my_pow10(frac_digits); - let frac_part = my_round((abs_value - int_part as f64) * scale) as i64; + let int_part = trunc(abs_value) as i64; + let scale = pow(10.0, frac_digits as f64); + let frac_part = round((abs_value - int_part as f64) * scale) as i64; Some((int_part.to_string(), frac_part)) } diff --git a/src/header/monetary/strfmon.rs b/src/header/monetary/strfmon.rs index 3feda48182..d5eab2c889 100644 --- a/src/header/monetary/strfmon.rs +++ b/src/header/monetary/strfmon.rs @@ -1,8 +1,7 @@ -use super::{ - my_fabs, my_pow10, my_round, my_trunc, FormatFlags, LocaleMonetaryInfo, DEFAULT_MONETARY, -}; +use super::{FormatFlags, LocaleMonetaryInfo, DEFAULT_MONETARY}; use alloc::string::{String, ToString}; use core::{ffi::CStr, ptr, slice, str}; +use libm::{fabs, floor, pow, round, trunc}; /* The strfmon() function formats a monetary value according to the format string format and writes the result to the character @@ -156,8 +155,8 @@ fn format_monetary( ) -> Option { let mut pos = 0; // Initialize the position to 0 let is_negative = value < 0.0; // Check if the value is negative - let abs_value = my_fabs(value); // Get the absolute value of the number - // Check if the international flag is set + let abs_value = fabs(value); // Get the absolute value of the number + let frac_digits = if flags.international { flags .right_precision @@ -168,9 +167,9 @@ fn format_monetary( .unwrap_or(monetary.frac_digits as usize) }; - let scale = my_pow10(frac_digits); - let mut int_part = my_trunc(abs_value) as i64; - let mut frac_part = my_round((abs_value - int_part as f64) * scale) as i64; + let scale = pow(10.0, frac_digits as f64); + let mut int_part = trunc(abs_value) as i64; + let mut frac_part = round((abs_value - int_part as f64) * scale) as i64; // Ensure that the fractional part (frac_part) doesn’t overflow due to rounding // when abs_value is very close to the next integer value. @@ -243,6 +242,7 @@ fn format_monetary( // Add a space between the currency symbol and the value let sep_by_space = if is_negative { + //let scale = my_pow10(frac_digits); monetary.n_sep_by_space } else { monetary.p_sep_by_space