From dd15e90f4668026b03cc06cb60fab22bc668b5e2 Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Fri, 30 May 2025 00:06:11 +0000 Subject: [PATCH] Reduce the x86-specificity of the crate Currently a lot of things are gated on `cfg(target_arch = "x86")`, which is used to indicate systems with 32-bit words. Apply the following changes to make this less specific: * Replace x86-32 configuration with configuration based on `target_pointer_width`. * Where possible, replace `#[cfg]` with `cfg!` or eliminate the check, to increase the percentage of code that gets validated on any platform. * Remove some configuration that was unneeded, for the same reason. This includes things like `#[cfg(target_arch = "x86")]` on comparisons to `EXPONENT_MIN` or `EXPONENT_MAX`. The checks are only useful on 32-bit platforms, but this is a trivial compiler optimization so not much is gained by keeping the config. I have verified that the crate builds on armv7 targets (but have not tested). Fixes: https://github.com/stencillogic/astro-float/issues/26 --- astro-float-num/Cargo.toml | 1 + astro-float-num/src/common/util.rs | 57 ++++---- astro-float-num/src/conv.rs | 5 +- astro-float-num/src/defs.rs | 58 ++++---- astro-float-num/src/ext.rs | 31 ++--- astro-float-num/src/mantissa/mantissa.rs | 10 +- astro-float-num/src/num.rs | 108 +++++---------- astro-float-num/src/ops/consts/e.rs | 50 +++---- astro-float-num/src/ops/consts/ln10.rs | 51 +++---- astro-float-num/src/ops/consts/ln2.rs | 168 ++++++++++------------- astro-float-num/src/ops/consts/pi.rs | 50 +++---- astro-float-num/src/ops/tests.rs | 10 +- astro-float-num/src/parser.rs | 28 ++-- 13 files changed, 239 insertions(+), 388 deletions(-) diff --git a/astro-float-num/Cargo.toml b/astro-float-num/Cargo.toml index d31c677..d878415 100644 --- a/astro-float-num/Cargo.toml +++ b/astro-float-num/Cargo.toml @@ -23,6 +23,7 @@ serde = { version = "1.0.147", optional = true } rand = { version = "0.8.5", optional = true } lazy_static = { version = "1.4.0", default-features = false, features = [] } itertools = { version = "0.10.3", default-features = false, features = [] } +cfg-if = "1.0.0" [features] default = ["std", "random", "serde"] diff --git a/astro-float-num/src/common/util.rs b/astro-float-num/src/common/util.rs index 8bf1190..0795e27 100644 --- a/astro-float-num/src/common/util.rs +++ b/astro-float-num/src/common/util.rs @@ -1,7 +1,7 @@ //! Auxiliary functions. use crate::{ - defs::{Word, WORD_BIT_SIZE, WORD_MAX, WORD_SIGNIFICANT_BIT}, + defs::{DoubleWord, Word, WORD_BASE, WORD_BIT_SIZE, WORD_MAX, WORD_SIGNIFICANT_BIT}, RoundingMode, }; @@ -117,32 +117,28 @@ pub fn calc_sqrt_cost(p: usize, cost_mul: usize, cost_add: usize) -> usize { #[inline(always)] pub fn add_carry(a: Word, b: Word, c: Word, r: &mut Word) -> Word { + // Using #[cfg(target_arch = "x86_64")] { // platform-specific operation - unsafe { core::arch::x86_64::_addcarry_u64(c as u8, a, b, r) as Word } + return unsafe { core::arch::x86_64::_addcarry_u64(c as u8, a, b, r) as Word }; } #[cfg(target_arch = "x86")] { // platform-specific operation - unsafe { core::arch::x86::_addcarry_u32(c as u8, a, b, r) as Word } + return unsafe { core::arch::x86::_addcarry_u32(c as u8, a, b, r) as Word }; } - #[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))] - { - use crate::defs::DoubleWord; - use crate::defs::WORD_BASE; - - let mut s = c as DoubleWord + a as DoubleWord + b as DoubleWord; - if s >= WORD_BASE { - s -= WORD_BASE; - *r = s as Word; - 1 - } else { - *r = s as Word; - 0 - } + #[allow(unreachable_code)] // not used on x86 + let mut s = c as DoubleWord + a as DoubleWord + b as DoubleWord; + if s >= WORD_BASE { + s -= WORD_BASE; + *r = s as Word; + 1 + } else { + *r = s as Word; + 0 } } @@ -151,30 +147,25 @@ pub fn sub_borrow(a: Word, b: Word, c: Word, r: &mut Word) -> Word { #[cfg(target_arch = "x86_64")] { // platform-specific operation - unsafe { core::arch::x86_64::_subborrow_u64(c as u8, a, b, r) as Word } + return unsafe { core::arch::x86_64::_subborrow_u64(c as u8, a, b, r) as Word }; } #[cfg(target_arch = "x86")] { // platform-specific operation - unsafe { core::arch::x86::_subborrow_u32(c as u8, a, b, r) as Word } + return unsafe { core::arch::x86::_subborrow_u32(c as u8, a, b, r) as Word }; } - #[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))] - { - use crate::defs::DoubleWord; - use crate::defs::WORD_BASE; + #[allow(unreachable_code)] // not used on x86 + let v1 = a as DoubleWord; + let v2 = b as DoubleWord + c as DoubleWord; - let v1 = a as DoubleWord; - let v2 = b as DoubleWord + c as DoubleWord; - - if v1 < v2 { - *r = (v1 + WORD_BASE - v2) as Word; - 1 - } else { - *r = (v1 - v2) as Word; - 0 - } + if v1 < v2 { + *r = (v1 + WORD_BASE - v2) as Word; + 1 + } else { + *r = (v1 - v2) as Word; + 0 } } diff --git a/astro-float-num/src/conv.rs b/astro-float-num/src/conv.rs index bdd7c4d..a6ae8c8 100644 --- a/astro-float-num/src/conv.rs +++ b/astro-float-num/src/conv.rs @@ -57,7 +57,6 @@ impl BigFloatNumber { } } - #[cfg(target_arch = "x86")] if e < EXPONENT_MIN || e > EXPONENT_MAX { return Err(Error::InvalidArgument); } @@ -830,7 +829,7 @@ mod tests { let n = BigFloatNumber::from_f64(64, -83.591552734375).unwrap(); assert_eq!(n.cmp(&g), 0); - #[cfg(target_arch = "x86")] + #[cfg(not(target_pointer_width = "64"))] { let n = BigFloatNumber::from_raw_parts( &[2576980377, 2576980377, 2576980377], @@ -891,7 +890,7 @@ mod tests { assert!(g.cmp(&n) == 0); } - #[cfg(not(target_arch = "x86"))] + #[cfg(target_pointer_width = "64")] { let n = BigFloatNumber::from_raw_parts( &[0x9999999999999999, 0x9999999999999999, 0x9999999999999999], diff --git a/astro-float-num/src/defs.rs b/astro-float-num/src/defs.rs index 022a91f..f1f3cf5 100644 --- a/astro-float-num/src/defs.rs +++ b/astro-float-num/src/defs.rs @@ -8,48 +8,44 @@ use std::collections::TryReserveError; #[cfg(not(feature = "std"))] use alloc::collections::TryReserveError; -/// A word. -#[cfg(not(target_arch = "x86"))] -pub type Word = u64; +cfg_if::cfg_if! { + if #[cfg(target_pointer_width = "64")] { + /// A word. + pub type Word = u64; -/// Doubled word. -#[cfg(not(target_arch = "x86"))] -pub type DoubleWord = u128; + /// Doubled word. + pub type DoubleWord = u128; -/// Word with sign. -#[cfg(not(target_arch = "x86"))] -pub type SignedWord = i128; + /// Word with sign. + pub type SignedWord = i128; + } else { + /// A word. + pub type Word = u32; -/// A word. -#[cfg(target_arch = "x86")] -pub type Word = u32; + /// Doubled word. + pub type DoubleWord = u64; -/// Doubled word. -#[cfg(target_arch = "x86")] -pub type DoubleWord = u64; - -/// Word with sign. -#[cfg(target_arch = "x86")] -pub type SignedWord = i64; + /// Word with sign. + pub type SignedWord = i64; + } +} /// An exponent. pub type Exponent = i32; /// Maximum exponent value. -#[cfg(not(target_arch = "x86"))] -pub const EXPONENT_MAX: Exponent = Exponent::MAX; - -/// Maximum exponent value. -#[cfg(target_arch = "x86")] -pub const EXPONENT_MAX: Exponent = Exponent::MAX / 4; - -/// Minimum exponent value. -#[cfg(not(target_arch = "x86"))] -pub const EXPONENT_MIN: Exponent = Exponent::MIN; +pub const EXPONENT_MAX: Exponent = if cfg!(target_pointer_width = "64") { + Exponent::MAX +} else { + Exponent::MAX / 4 +}; /// Minimum exponent value. -#[cfg(target_arch = "x86")] -pub const EXPONENT_MIN: Exponent = Exponent::MIN / 4; +pub const EXPONENT_MIN: Exponent = if cfg!(target_pointer_width = "64") { + Exponent::MIN +} else { + Exponent::MIN / 4 +}; /// Maximum value of a word. pub const WORD_MAX: Word = Word::MAX; diff --git a/astro-float-num/src/ext.rs b/astro-float-num/src/ext.rs index 60822c9..c7ede9e 100644 --- a/astro-float-num/src/ext.rs +++ b/astro-float-num/src/ext.rs @@ -2158,16 +2158,10 @@ mod tests { let d1 = ONE.clone(); assert!(d1.exponent() == Some(1)); - let words: &[Word] = { - #[cfg(not(target_arch = "x86"))] - { - &[0, 0x8000000000000000] - } - #[cfg(target_arch = "x86")] - { - &[0, 0, 0, 0x80000000] - } - }; + #[cfg(target_pointer_width = "64")] + let words: &[Word] = &[0, 0x8000000000000000]; + #[cfg(not(target_pointer_width = "64"))] + let words: &[Word] = &[0, 0, 0, 0x80000000]; assert!(d1.mantissa_digits() == Some(words)); assert!(d1.mantissa_max_bit_len() == Some(DEFAULT_P)); @@ -2473,22 +2467,17 @@ mod rand_tests { use super::*; use crate::defs::EXPONENT_MAX; + use crate::defs::EXPONENT_MIN; #[test] fn test_rand() { for _ in 0..1000 { let p = rand::random::() % 1000 + DEFAULT_P; - let exp_from; - #[cfg(not(target_arch = "x86"))] - { - exp_from = rand::random::().abs(); - } - #[cfg(target_arch = "x86")] - { - use crate::defs::EXPONENT_MIN; - exp_from = - rand::random::().abs() % (EXPONENT_MAX - EXPONENT_MIN) + EXPONENT_MIN; - } + let exp_from = if cfg!(target_pointer_width = "64") { + rand::random::().abs() + } else { + rand::random::().abs() % (EXPONENT_MAX - EXPONENT_MIN) + EXPONENT_MIN + }; let exp_shift = if EXPONENT_MAX > exp_from { rand::random::().abs() % (EXPONENT_MAX as isize - exp_from as isize) as Exponent diff --git a/astro-float-num/src/mantissa/mantissa.rs b/astro-float-num/src/mantissa/mantissa.rs index 352ea0d..5ecfc39 100644 --- a/astro-float-num/src/mantissa/mantissa.rs +++ b/astro-float-num/src/mantissa/mantissa.rs @@ -717,12 +717,11 @@ impl Mantissa { let nd = m.len() - size_of::() / size_of::(); m[..nd].fill(0); - #[cfg(not(target_arch = "x86"))] + #[cfg(target_pointer_width = "64")] { m[nd] = u; } - - #[cfg(target_arch = "x86")] + #[cfg(not(target_pointer_width = "64"))] { let mut u = u; for v in &mut m[nd..] { @@ -764,12 +763,11 @@ impl Mantissa { #[cfg(test)] pub fn to_u64(&self) -> u64 { - #[cfg(not(target_arch = "x86"))] + #[cfg(target_pointer_width = "64")] { self.m[self.m.len() - 1] } - - #[cfg(target_arch = "x86")] + #[cfg(not(target_pointer_width = "64"))] { let mut ret: u64 = 0; let nd = size_of::() / size_of::(); diff --git a/astro-float-num/src/num.rs b/astro-float-num/src/num.rs index f477fb9..78e2165 100644 --- a/astro-float-num/src/num.rs +++ b/astro-float-num/src/num.rs @@ -166,12 +166,11 @@ impl BigFloatNumber { } pub(crate) fn from_u64_internal(d: u64, p: usize) -> Result { - #[cfg(not(target_arch = "x86"))] + #[cfg(target_pointer_width = "64")] { Self::from_word(d, p) } - - #[cfg(target_arch = "x86")] + #[cfg(not(target_pointer_width = "64"))] { Self::p_assertion(p)?; @@ -551,8 +550,7 @@ impl BigFloatNumber { if self.is_subnormal() { let (shift, mantissa) = self.m.normilize()?; - #[cfg(not(target_arch = "x86"))] - { + if cfg!(target_pointer_width = "64") { // checks for the case when usize is larger than exponent debug_assert!((shift as isize) < (isize::MAX / 2 + EXPONENT_MIN as isize)); @@ -673,8 +671,7 @@ impl BigFloatNumber { d3.inexact |= inexact; - #[cfg(not(target_arch = "x86"))] - { + if cfg!(target_pointer_width = "64") { debug_assert!(shift <= isize::MAX / 2 && e >= isize::MIN / 2); } e -= shift; @@ -845,7 +842,6 @@ impl BigFloatNumber { ret.m = m; ret.e = exponent - 0b1111111111 - shift as Exponent; - #[cfg(target_arch = "x86")] debug_assert!(ret.e <= EXPONENT_MAX && ret.e >= EXPONENT_MIN); Ok(ret) @@ -943,7 +939,6 @@ impl BigFloatNumber { let p = m.len() * WORD_BIT_SIZE; Self::p_assertion(p)?; - #[cfg(target_arch = "x86")] if e < EXPONENT_MIN || e > EXPONENT_MAX { return Err(Error::InvalidArgument); } @@ -989,7 +984,6 @@ impl BigFloatNumber { let p = m.len() * WORD_BIT_SIZE; Self::p_assertion(p)?; - #[cfg(target_arch = "x86")] if e < EXPONENT_MIN || e > EXPONENT_MAX { return Err(Error::InvalidArgument); } @@ -1175,7 +1169,6 @@ impl BigFloatNumber { /// Note that if `self` is subnormal, the exponent may not change, but the mantissa will shift instead. /// `e` will be clamped to the range from EXPONENT_MIN to EXPONENT_MAX if it's outside of the range. pub fn set_exponent(&mut self, e: Exponent) { - #[cfg(target_arch = "x86")] let e = if e < EXPONENT_MIN { EXPONENT_MIN } else if e > EXPONENT_MAX { @@ -1314,7 +1307,6 @@ impl BigFloatNumber { pub fn random_normal(p: usize, exp_from: Exponent, exp_to: Exponent) -> Result { Self::p_assertion(p)?; - #[cfg(target_arch = "x86")] if exp_from < EXPONENT_MIN || exp_to > EXPONENT_MAX { return Err(Error::InvalidArgument); } @@ -2287,22 +2279,16 @@ mod tests { .div(&d2, WORD_BIT_SIZE * 5, RoundingMode::ToEven) .unwrap(); - let words = { - #[cfg(not(target_arch = "x86"))] - { - [ - 12297829382473034411, - 12297829382473034410, - 12297829382473034410, - 12297829382473034410, - 12297829382473034410, - ] - } - #[cfg(target_arch = "x86")] - { - [2863311531, 2863311530, 2863311530, 2863311530, 2863311530] - } - }; + #[cfg(target_pointer_width = "64")] + let words = [ + 12297829382473034411, + 12297829382473034410, + 12297829382473034410, + 12297829382473034410, + 12297829382473034410, + ]; + #[cfg(not(target_pointer_width = "64"))] + let words = [2863311531, 2863311530, 2863311530, 2863311530, 2863311530]; assert!(d3.mantissa_max_bit_len() == WORD_BIT_SIZE * 5); assert!(d3.mantissa().digits() == words); @@ -2311,32 +2297,20 @@ mod tests { .div(&d2, WORD_BIT_SIZE * 3, RoundingMode::ToEven) .unwrap(); - let words = { - #[cfg(not(target_arch = "x86"))] - { - [12297829382473034411, 12297829382473034410, 12297829382473034410] - } - #[cfg(target_arch = "x86")] - { - [2863311531, 2863311530, 2863311530] - } - }; + #[cfg(target_pointer_width = "64")] + let words = [12297829382473034411, 12297829382473034410, 12297829382473034410]; + #[cfg(not(target_pointer_width = "64"))] + let words = [2863311531, 2863311530, 2863311530]; assert!(d3.mantissa_max_bit_len() == WORD_BIT_SIZE * 3); assert!(d3.mantissa().digits() == words); d3 = d1.div(&d2, WORD_BIT_SIZE, RoundingMode::ToEven).unwrap(); - let words = { - #[cfg(not(target_arch = "x86"))] - { - [12297829382473034411] - } - #[cfg(target_arch = "x86")] - { - [2863311531] - } - }; + #[cfg(target_pointer_width = "64")] + let words = [12297829382473034411]; + #[cfg(not(target_pointer_width = "64"))] + let words = [2863311531]; assert!(d3.mantissa_max_bit_len() == WORD_BIT_SIZE); assert!(d3.mantissa().digits() == words); @@ -2405,7 +2379,7 @@ mod tests { .unwrap(); let words = { - #[cfg(not(target_arch = "x86"))] + #[cfg(target_pointer_width = "64")] { [ 12297829382473034411, @@ -2415,7 +2389,7 @@ mod tests { 12297829382473034410, ] } - #[cfg(target_arch = "x86")] + #[cfg(not(target_pointer_width = "64"))] { [2863311531, 2863311530, 2863311530, 2863311530, 2863311530] } @@ -2425,16 +2399,10 @@ mod tests { d2 = d1.reciprocal(WORD_BIT_SIZE, RoundingMode::ToEven).unwrap(); - let words = { - #[cfg(not(target_arch = "x86"))] - { - [12297829382473034411] - } - #[cfg(target_arch = "x86")] - { - [2863311531] - } - }; + #[cfg(target_pointer_width = "64")] + let words = [12297829382473034411]; + #[cfg(not(target_pointer_width = "64"))] + let words = [2863311531]; assert!(d2.mantissa().digits() == words); @@ -2861,14 +2829,14 @@ mod tests { assert_eq!(d1.sign(), Sign::Neg); let d1 = BigFloatNumber::from_words(&[3, 1], Sign::Pos, EXPONENT_MAX).unwrap(); - #[cfg(not(target_arch = "x86"))] + #[cfg(target_pointer_width = "64")] { assert_eq!( d1.mantissa().digits(), [0x8000000000000000u64, 0x8000000000000001u64] ); } - #[cfg(target_arch = "x86")] + #[cfg(not(target_pointer_width = "64"))] { assert_eq!(d1.mantissa().digits(), [0x80000000u32, 0x80000001u32]); } @@ -2892,11 +2860,11 @@ mod tests { assert_eq!(d1.sign(), Sign::Pos); let words = { - #[cfg(not(target_arch = "x86"))] + #[cfg(target_pointer_width = "64")] { [3, 0x8000000000000000u64] } - #[cfg(target_arch = "x86")] + #[cfg(not(target_pointer_width = "64"))] { [3, 0x80000000u32] } @@ -2933,7 +2901,7 @@ mod tests { // 1 1001 let mantissas = { - #[cfg(not(target_arch = "x86"))] + #[cfg(target_pointer_width = "64")] { [ [0x8000000000000000u64, 0x8000000000000000u64], @@ -2944,7 +2912,7 @@ mod tests { [0x8000000000000019u64, 0x8000000000000000u64], ] } - #[cfg(target_arch = "x86")] + #[cfg(not(target_pointer_width = "64"))] { [ [0x80000000u32, 0x80000000u32], @@ -2958,7 +2926,7 @@ mod tests { }; let rounding_results_posnum = { - #[cfg(not(target_arch = "x86"))] + #[cfg(target_pointer_width = "64")] { [ (RoundingMode::None, mantissas), @@ -3030,7 +2998,7 @@ mod tests { ), ] } - #[cfg(target_arch = "x86")] + #[cfg(not(target_pointer_width = "64"))] { [ (RoundingMode::None, mantissas), @@ -3105,7 +3073,7 @@ mod tests { }; let rounding_results_negnum = { - #[cfg(not(target_arch = "x86"))] + #[cfg(target_pointer_width = "64")] { [ (RoundingMode::None, mantissas), @@ -3177,7 +3145,7 @@ mod tests { ), ] } - #[cfg(target_arch = "x86")] + #[cfg(not(target_pointer_width = "64"))] { [ (RoundingMode::None, mantissas), diff --git a/astro-float-num/src/ops/consts/e.rs b/astro-float-num/src/ops/consts/e.rs index 96d7a07..67c04d2 100644 --- a/astro-float-num/src/ops/consts/e.rs +++ b/astro-float-num/src/ops/consts/e.rs @@ -136,45 +136,27 @@ mod tests { use crate::{RoundingMode, Sign}; #[test] - #[cfg(target_arch = "x86")] fn test_e_const() { let mut e = ECache::new().unwrap(); let c = e.for_prec(320, RoundingMode::ToEven).unwrap(); - //println!("{:?}", c); - let r = BigFloatNumber::from_raw_parts( - &[ - 614153977, 3432226254, 342111227, 2850108993, 3459069589, 3636053379, 658324721, - 2950452768, 2730183322, 2918732888, - ], - 320, - Sign::Pos, - 2, - false, - ) - .unwrap(); - assert!(c.cmp(&r) == 0); - } - #[test] - #[cfg(not(target_arch = "x86"))] - fn test_e_const() { - let mut e = ECache::new().unwrap(); - let c = e.for_prec(320, RoundingMode::ToEven).unwrap(); + #[cfg(target_pointer_width = "64")] + let words = [ + 14741299514016743161, + 12241124915312604155, + 15616730352774362773, + 12672098147611000049, + 12535862302449814170, + ]; + + #[cfg(not(target_pointer_width = "64"))] + let words = [ + 614153977, 3432226254, 342111227, 2850108993, 3459069589, 3636053379, 658324721, + 2950452768, 2730183322, 2918732888, + ]; + //println!("{:?}", c); - let r = BigFloatNumber::from_raw_parts( - &[ - 14741299514016743161, - 12241124915312604155, - 15616730352774362773, - 12672098147611000049, - 12535862302449814170, - ], - 320, - Sign::Pos, - 2, - false, - ) - .unwrap(); + let r = BigFloatNumber::from_raw_parts(&words, 320, Sign::Pos, 2, false).unwrap(); assert!(c.cmp(&r) == 0); } } diff --git a/astro-float-num/src/ops/consts/ln10.rs b/astro-float-num/src/ops/consts/ln10.rs index d7a452f..df2c8a4 100644 --- a/astro-float-num/src/ops/consts/ln10.rs +++ b/astro-float-num/src/ops/consts/ln10.rs @@ -133,45 +133,26 @@ mod tests { use crate::{RoundingMode, Sign}; #[test] - #[cfg(target_arch = "x86")] fn test_ln10_const() { let mut ln10 = Ln10Cache::new().unwrap(); let c = ln10.for_prec(320, RoundingMode::ToEven).unwrap(); - //println!("{:?}", c.fp3(crate::Radix::Dec, RoundingMode::None)); - let r = BigFloatNumber::from_raw_parts( - &[ - 2980581469, 2519663319, 32517490, 2210799234, 3663591694, 3801083129, 2194868776, - 3931559467, 2863180822, 2472381917, - ], - 320, - Sign::Pos, - 2, - false, - ) - .unwrap(); - assert!(c.cmp(&r) == 0); - } - #[test] - #[cfg(not(target_arch = "x86"))] - fn test_ln10_const() { - let mut ln10 = Ln10Cache::new().unwrap(); - let c = ln10.for_prec(320, RoundingMode::ToEven).unwrap(); - //println!("{:?}", c); - let r = BigFloatNumber::from_raw_parts( - &[ - 10821871555016396893, - 9495310408084368754, - 16325527732095940878, - 16885919335239060008, - 10618799479599967254, - ], - 320, - Sign::Pos, - 2, - false, - ) - .unwrap(); + #[cfg(target_pointer_width = "64")] + let words = [ + 10821871555016396893, + 9495310408084368754, + 16325527732095940878, + 16885919335239060008, + 10618799479599967254, + ]; + + #[cfg(not(target_pointer_width = "64"))] + let words = [ + 2980581469, 2519663319, 32517490, 2210799234, 3663591694, 3801083129, 2194868776, + 3931559467, 2863180822, 2472381917, + ]; + + let r = BigFloatNumber::from_raw_parts(&words, 320, Sign::Pos, 2, false).unwrap(); assert!(c.cmp(&r) == 0); } } diff --git a/astro-float-num/src/ops/consts/ln2.rs b/astro-float-num/src/ops/consts/ln2.rs index 0bf5219..788cacc 100644 --- a/astro-float-num/src/ops/consts/ln2.rs +++ b/astro-float-num/src/ops/consts/ln2.rs @@ -137,103 +137,83 @@ mod tests { use super::*; #[test] - #[cfg(target_arch = "x86")] fn test_ln2_const() { let mut ln2 = Ln2Cache::new().unwrap(); let c = ln2.for_prec(3200, RoundingMode::ToEven).unwrap(); - //println!("{:?}", c); - let r = BigFloatNumber::from_raw_parts( - &[ - 3303612541, 717600284, 3199892741, 1462999970, 137959948, 6349977, 1794441986, - 1645437721, 1024185081, 96544981, 3471345337, 920660768, 4091103730, 196810490, - 3381928201, 1109492717, 2355549125, 1586635448, 1797039055, 1638923588, 2503522690, - 646601759, 4224649045, 2372882807, 579489529, 2708640721, 1931271310, 1151346004, - 816810139, 2530535123, 2409011252, 1433450182, 451048430, 1972937109, 3009774909, - 1293299123, 1348780427, 1599124760, 455979803, 126841693, 1818258609, 2922462427, - 2984344477, 2505920889, 389352748, 206047828, 1560181411, 122533377, 1578405506, - 1788641162, 895787831, 627481395, 3520465245, 1276780043, 2475905356, 3435941477, - 3027881267, 3376670514, 3683225829, 390465397, 335590294, 2100175838, 4229888533, - 3998653805, 2414170530, 1628254456, 4217109392, 133483025, 255841734, 3660421061, - 790684578, 1700766087, 942682696, 4125075133, 2640660682, 1926137777, 1985476427, - 628072684, 2973130811, 3119160259, 830224510, 449567249, 575334597, 1050069526, - 292141093, 660028201, 3241681220, 3979259445, 1257904912, 1435849467, 1844161688, - 3887625760, 2343238187, 2316113755, 1922610733, 1089684262, 66254511, 3387143064, - 3520035243, 2977044471, - ], - 3200, - Sign::Pos, - 0, - false, - ) - .unwrap(); - assert!(c.cmp(&r) == 0); - } - - #[test] - #[cfg(not(target_arch = "x86"))] - fn test_ln2_const() { - let mut ln2 = Ln2Cache::new().unwrap(); - let c = ln2.for_prec(3200, RoundingMode::ToEven).unwrap(); - //println!("{:?}", c); - let r = BigFloatNumber::from_raw_parts( - &[ - 3082069754683924605, - 6283537028398873861, - 27272943683312140, - 7067101201094214402, - 414657537012126457, - 3954207892741588665, - 845294622150838770, - 4765234938047111433, - 6814547362189857733, - 7039123212900017103, - 2777133410944596354, - 10191454057530328917, - 11633523313888349945, - 4944993435491556494, - 10868565595481147547, - 6156621654544259124, - 8473700360670835694, - 5554677440240256317, - 6868188547772629387, - 544780923660251931, - 12551880549572046001, - 10762848267602590621, - 884968683061185836, - 526276848443620003, - 7682155296647843458, - 2695012071269245751, - 5483728532390938973, - 14757256277160841548, - 14502689430025391411, - 1677036114017882341, - 9020186540394984342, - 17174087324730849813, - 6993299640500441506, - 573305231163259792, - 15721388746840462790, - 7304734722601575330, - 17717062790720533064, - 8272698762445801674, - 2697551639276418891, - 13396691306361020475, - 1930876632637913214, - 4510014273271556293, - 2834799538024855589, - 17090789181815791940, - 6166926504001936144, - 16697225500131306648, - 9947632833883994667, - 4680158270178506285, - 14547668686819489455, - 12786308645202655659, - ], - 3200, - Sign::Pos, - 0, - false, - ) - .unwrap(); + #[cfg(target_pointer_width = "64")] + let words = [ + 3082069754683924605, + 6283537028398873861, + 27272943683312140, + 7067101201094214402, + 414657537012126457, + 3954207892741588665, + 845294622150838770, + 4765234938047111433, + 6814547362189857733, + 7039123212900017103, + 2777133410944596354, + 10191454057530328917, + 11633523313888349945, + 4944993435491556494, + 10868565595481147547, + 6156621654544259124, + 8473700360670835694, + 5554677440240256317, + 6868188547772629387, + 544780923660251931, + 12551880549572046001, + 10762848267602590621, + 884968683061185836, + 526276848443620003, + 7682155296647843458, + 2695012071269245751, + 5483728532390938973, + 14757256277160841548, + 14502689430025391411, + 1677036114017882341, + 9020186540394984342, + 17174087324730849813, + 6993299640500441506, + 573305231163259792, + 15721388746840462790, + 7304734722601575330, + 17717062790720533064, + 8272698762445801674, + 2697551639276418891, + 13396691306361020475, + 1930876632637913214, + 4510014273271556293, + 2834799538024855589, + 17090789181815791940, + 6166926504001936144, + 16697225500131306648, + 9947632833883994667, + 4680158270178506285, + 14547668686819489455, + 12786308645202655659, + ]; + + #[cfg(not(target_pointer_width = "64"))] + let words = [ + 3303612541, 717600284, 3199892741, 1462999970, 137959948, 6349977, 1794441986, + 1645437721, 1024185081, 96544981, 3471345337, 920660768, 4091103730, 196810490, + 3381928201, 1109492717, 2355549125, 1586635448, 1797039055, 1638923588, 2503522690, + 646601759, 4224649045, 2372882807, 579489529, 2708640721, 1931271310, 1151346004, + 816810139, 2530535123, 2409011252, 1433450182, 451048430, 1972937109, 3009774909, + 1293299123, 1348780427, 1599124760, 455979803, 126841693, 1818258609, 2922462427, + 2984344477, 2505920889, 389352748, 206047828, 1560181411, 122533377, 1578405506, + 1788641162, 895787831, 627481395, 3520465245, 1276780043, 2475905356, 3435941477, + 3027881267, 3376670514, 3683225829, 390465397, 335590294, 2100175838, 4229888533, + 3998653805, 2414170530, 1628254456, 4217109392, 133483025, 255841734, 3660421061, + 790684578, 1700766087, 942682696, 4125075133, 2640660682, 1926137777, 1985476427, + 628072684, 2973130811, 3119160259, 830224510, 449567249, 575334597, 1050069526, + 292141093, 660028201, 3241681220, 3979259445, 1257904912, 1435849467, 1844161688, + 3887625760, 2343238187, 2316113755, 1922610733, 1089684262, 66254511, 3387143064, + 3520035243, 2977044471, + ]; + + let r = BigFloatNumber::from_raw_parts(&words, 3200, Sign::Pos, 0, false).unwrap(); assert!(c.cmp(&r) == 0); } } diff --git a/astro-float-num/src/ops/consts/pi.rs b/astro-float-num/src/ops/consts/pi.rs index 5c36a3f..7f21569 100644 --- a/astro-float-num/src/ops/consts/pi.rs +++ b/astro-float-num/src/ops/consts/pi.rs @@ -154,45 +154,27 @@ mod tests { use crate::{RoundingMode, Sign}; #[test] - #[cfg(target_arch = "x86")] fn test_pi_const() { let mut pi = PiCache::new().unwrap(); let c = pi.for_prec(320, RoundingMode::ToEven).unwrap(); - //println!("pi {:?}", c); - let r = BigFloatNumber::from_raw_parts( - &[ - 2385773790, 1363806329, 991140642, 34324134, 2322058356, 688016904, 2161908945, - 3301335691, 560513588, 3373259426, - ], - 320, - Sign::Pos, - 2, - false, - ) - .unwrap(); - assert!(c.cmp(&r) == 0); - } - #[test] - #[cfg(not(target_arch = "x86"))] - fn test_pi_const() { - let mut pi = PiCache::new().unwrap(); - let c = pi.for_prec(320, RoundingMode::ToEven).unwrap(); + #[cfg(target_pointer_width = "64")] + let words = [ + 5857503583518590174, + 147421033984662306, + 2955010104097229940, + 14179128828124470481, + 14488038916154245684, + ]; + + #[cfg(not(target_pointer_width = "64"))] + let words = [ + 2385773790, 1363806329, 991140642, 34324134, 2322058356, 688016904, 2161908945, + 3301335691, 560513588, 3373259426, + ]; + //println!("pi {:?}", c); - let r = BigFloatNumber::from_raw_parts( - &[ - 5857503583518590174, - 147421033984662306, - 2955010104097229940, - 14179128828124470481, - 14488038916154245684, - ], - 320, - Sign::Pos, - 2, - false, - ) - .unwrap(); + let r = BigFloatNumber::from_raw_parts(&words, 320, Sign::Pos, 2, false).unwrap(); assert!(c.cmp(&r) == 0); } } diff --git a/astro-float-num/src/ops/tests.rs b/astro-float-num/src/ops/tests.rs index 60f5065..d49b8cb 100644 --- a/astro-float-num/src/ops/tests.rs +++ b/astro-float-num/src/ops/tests.rs @@ -895,15 +895,7 @@ fn test_tanh_atanh() { let mut cc = Consts::new().unwrap(); - let exp_to; - #[cfg(not(target_arch = "x86"))] - { - exp_to = 5; - } - #[cfg(target_arch = "x86")] - { - exp_to = 3; - } + let exp_to = if cfg!(target_pointer_width = "64") { 5 } else { 3 }; for i in 0..1000 { let p1 = (rand::random::() % prec_rng + 1) * WORD_BIT_SIZE; diff --git a/astro-float-num/src/parser.rs b/astro-float-num/src/parser.rs index 6f0aaf7..5f56ca6 100644 --- a/astro-float-num/src/parser.rs +++ b/astro-float-num/src/parser.rs @@ -362,28 +362,20 @@ mod tests { assert!(e == -0x1f7); // large exp - let numstr; - #[cfg(not(target_arch = "x86"))] - { - numstr = "abc.def09123e_e+7FFFFFFF"; - } - #[cfg(target_arch = "x86")] - { - numstr = "abc.def09123e_e+1FFFFFFF"; - } + let numstr = if cfg!(target_pointer_width = "64") { + "abc.def09123e_e+7FFFFFFF" + } else { + "abc.def09123e_e+1FFFFFFF" + }; let ps = parse(numstr, Radix::Hex).unwrap(); assert!(ps.is_inf()); assert!(ps.sign().is_positive()); - let numstr; - #[cfg(not(target_arch = "x86"))] - { - numstr = "-abc.def09123e_e+7FFFFFFF"; - } - #[cfg(target_arch = "x86")] - { - numstr = "-abc.def09123e_e+1FFFFFFF"; - } + let numstr = if cfg!(target_pointer_width = "64") { + "-abc.def09123e_e+7FFFFFFF" + } else { + "-abc.def09123e_e+1FFFFFFF" + }; let ps = parse(numstr, Radix::Hex).unwrap(); assert!(ps.is_inf()); assert!(!ps.is_nan());