diff --git a/Cargo.toml b/Cargo.toml index 3f3d730..24e2d57 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ exclude = ["src/float/*", "src/tests", "TODO.txt", "lit-parser/*"] # TODO: make [features] default = [] nightly = [] +float = [] serde = ["dep:serde", "serde-big-array"] numtraits = ["num-integer", "num-traits"] diff --git a/bench/benches/float.rs b/bench/benches/float.rs index 7813c6e..aadf709 100644 --- a/bench/benches/float.rs +++ b/bench/benches/float.rs @@ -63,7 +63,7 @@ fn bench_add(c: &mut Criterion) { // })); group.bench_with_input(BenchmarkId::new("Iterative", "old"), &big_inputs, |b, inputs| b.iter(|| { inputs.iter().cloned().for_each(|(a, b)| { - let _ = black_box(black_box(a).overflowing_shl(black_box(b))); + let _ = black_box(black_box(a).count_ones()); }) })); group.finish(); diff --git a/src/buint/cast.rs b/src/buint/cast.rs index 62dfabd..6f9c77c 100644 --- a/src/buint/cast.rs +++ b/src/buint/cast.rs @@ -92,28 +92,28 @@ use crate::cast::CastFrom; use crate::doc; use crate::ExpType; -// #[cfg(feature = "float")] -// impl FloatMantissa for BUintD8 { -// const ZERO: Self = Self::ZERO; -// const ONE: Self = Self::ONE; -// const TWO: Self = Self::TWO; -// const MAX: Self = Self::MAX; +#[cfg(feature = "float")] +impl FloatMantissa for BUintD8 { + const ZERO: Self = Self::ZERO; + const ONE: Self = Self::ONE; + const TWO: Self = Self::TWO; + const MAX: Self = Self::MAX; -// #[inline] -// fn leading_zeros(self) -> ExpType { -// Self::leading_zeros(self) -// } + #[inline] + fn leading_zeros(self) -> ExpType { + Self::leading_zeros(self) + } -// #[inline] -// fn checked_shr(self, n: ExpType) -> Option { -// Self::checked_shr(self, n) -// } + #[inline] + fn checked_shr(self, n: ExpType) -> Option { + Self::checked_shr(self, n) + } -// #[inline] -// fn is_power_of_two(self) -> bool { -// Self::is_power_of_two(self) -// } -// } + #[inline] + fn is_power_of_two(self) -> bool { + Self::is_power_of_two(self) + } +} impl CastUintFromFloatHelper for BUintD8 { const MAX: Self = Self::MAX; diff --git a/src/buint/mod.rs b/src/buint/mod.rs index 3d1ca50..667fcdc 100644 --- a/src/buint/mod.rs +++ b/src/buint/mod.rs @@ -262,14 +262,14 @@ impl BUintD8 { } #[doc = doc::doc_comment! { - U 256, - "Returns `true` if and only if `self == 2^k` for some integer `k`.", - - "let n = " stringify!(U256) "::from(1u16 << 14);\n" - "assert!(n.is_power_of_two());\n" - "let m = " stringify!(U256) "::from(100u8);\n" - "assert!(!m.is_power_of_two());" - }] + U 256, + "Returns `true` if and only if `self == 2^k` for some integer `k`.", + + "let n = " stringify!(U256) "::from(1u16 << 14);\n" + "assert!(n.is_power_of_two());\n" + "let m = " stringify!(U256) "::from(100u8);\n" + "assert!(!m.is_power_of_two());" + }] #[must_use] #[inline] pub const fn is_power_of_two(self) -> bool { diff --git a/src/doc/consts.rs b/src/doc/consts.rs index aa0f8b0..15acaed 100644 --- a/src/doc/consts.rs +++ b/src/doc/consts.rs @@ -92,20 +92,20 @@ macro_rules! value_desc { pub(crate) use value_desc; -// #[cfg(feature = "float")] -// crate::doc::link_doc_comment_constant!( -// RADIX, -// MANTISSA_DIGITS, -// DIGITS, -// EPSILON, -// MIN, -// MIN_POSITIVE, -// MAX, -// MIN_EXP, -// MAX_EXP, -// // MIN_10_EXP, -// // MAX_10_EXP, -// NAN, -// INFINITY, -// NEG_INFINITY -// ); +#[cfg(feature = "float")] +crate::doc::link_doc_comment_constant!( + RADIX, + MANTISSA_DIGITS, + DIGITS, + EPSILON, + MIN, + MIN_POSITIVE, + MAX, + MIN_EXP, + MAX_EXP, + // MIN_10_EXP, + // MAX_10_EXP, + NAN, + INFINITY, + NEG_INFINITY +); diff --git a/src/doc/mod.rs b/src/doc/mod.rs index 63837f8..5e83e0e 100644 --- a/src/doc/mod.rs +++ b/src/doc/mod.rs @@ -1,18 +1,18 @@ pub mod bigint_helpers; pub mod checked; -// #[cfg(feature = "float")] -// pub mod classify; -// #[cfg(feature = "float")] -// pub mod cmp; +#[cfg(feature = "float")] +pub mod classify; +#[cfg(feature = "float")] +pub mod cmp; pub mod const_trait_fillers; pub mod consts; pub mod endian; -// #[cfg(feature = "float")] -// pub mod math; +#[cfg(feature = "float")] +pub mod math; pub mod overflowing; pub mod radix; -// #[cfg(feature = "float")] -// pub mod rounding; +#[cfg(feature = "float")] +pub mod rounding; pub mod saturating; pub mod strict; pub mod unchecked; diff --git a/src/float/cast.rs b/src/float/cast.rs index 34a9ef7..c671446 100644 --- a/src/float/cast.rs +++ b/src/float/cast.rs @@ -3,7 +3,7 @@ use crate::cast::float::{FloatCastHelper, FloatMantissa}; use super::{Float, FloatExponent}; use crate::cast::CastFrom; use crate::doc; -use crate::{BUintD8, BUintD16, BUintD32, BUint, BIntD8, BIntD16, BIntD32, BInt}; +use crate::{BUintD8, BIntD8}; use crate::ExpType; macro_rules! uint_as_float { @@ -20,7 +20,7 @@ macro_rules! uint_as_float { }; } -uint_as_float!(u8, u16, u32, u64, u128, usize, BUintD8, BUintD16, BUintD32, BUint); +uint_as_float!(u8, u16, u32, u64, u128, usize, BUintD8); macro_rules! int_as_float { ($($int: ty), *) => { @@ -41,29 +41,19 @@ macro_rules! int_as_float { int_as_float!(i8, i16, i32, i64, i128, isize); -macro_rules! bint_as_float { - ($(BIntD8: ident), *) => { - $( - impl CastFrom> for Float { - fn cast_from(from: BIntD8) -> Self { - let pos_cast = Self::cast_from(from.unsigned_abs()); - if from.is_negative() { - -pos_cast - } else { - pos_cast - } - } - } - )* - }; +impl CastFrom> for Float { + fn cast_from(from: BIntD8) -> Self { + let pos_cast = Self::cast_from(from.unsigned_abs()); + if from.is_negative() { + -pos_cast + } else { + pos_cast + } + } } -bint_as_float!(BIntD8, BIntD16, BIntD32, BInt); - - impl CastFrom> for BIntD8 { - crate::bint::cast::bint_cast_from_float!(Float, BUintD8); - } - }; +impl CastFrom> for BIntD8 { + crate::bint::cast::bint_cast_from_float!(Float); } macro_rules! float_as_int { @@ -111,7 +101,7 @@ macro_rules! float_as_uint { }; } -float_as_uint!(BUintD8, BUintD16, BUintD32, BUint, u8, u16, u32, u64, u128, usize); +float_as_uint!(BUintD8, u8, u16, u32, u64, u128, usize); impl FloatCastHelper for Float { const BITS: ExpType = Self::BITS; diff --git a/src/float/consts.rs b/src/float/consts.rs index bfc7bb6..58d2c36 100644 --- a/src/float/consts.rs +++ b/src/float/consts.rs @@ -6,8 +6,8 @@ const fn buint_from_usize(u: usize) -> BUintD8 { const UINT_BITS: usize = ::BITS as usize; let mut out = BUintD8::ZERO; let mut i = 0; - while i << crate::digit::u8::BIT_SHIFT < UINT_BITS { - let d = (u >> (i << crate::digit::u8::BIT_SHIFT)) as u8; + while i << crate::digit::BIT_SHIFT < UINT_BITS { + let d = (u >> (i << crate::digit::BIT_SHIFT)) as u8; if d != 0 { out.digits[i] = d; } diff --git a/src/float/mod.rs b/src/float/mod.rs index fd48b55..1813b0f 100644 --- a/src/float/mod.rs +++ b/src/float/mod.rs @@ -102,8 +102,8 @@ impl BUintD8 { pub(crate) const fn cast_to_unsigned_float_exponent(self) -> UnsignedFloatExponent { let mut out = 0; let mut i = 0; - while i << crate::digit::u8::BIT_SHIFT < UnsignedFloatExponent::BITS as usize && i < W { - out |= (self.digits[i] as UnsignedFloatExponent) << (i << crate::digit::u8::BIT_SHIFT); + while i << crate::digit::BIT_SHIFT < UnsignedFloatExponent::BITS as usize && i < W { + out |= (self.digits[i] as UnsignedFloatExponent) << (i << crate::digit::BIT_SHIFT); i += 1; } out diff --git a/src/float/rounding copy.rs b/src/float/rounding copy.rs index 224be77..9f89005 100644 --- a/src/float/rounding copy.rs +++ b/src/float/rounding copy.rs @@ -21,7 +21,7 @@ impl BUintD8 { i = 1; } else { loop { - let shift = i << crate::digit::u8::BIT_SHIFT; // TODO: make sure to generalise when using general digits + let shift = i << crate::digit::BIT_SHIFT; // TODO: make sure to generalise when using general digits if i >= W || shift >= ExpType::BITS as usize { break; } @@ -44,8 +44,8 @@ impl BUintD8 { const fn from_exp_type(int: ExpType) -> Option { let mut out = Self::ZERO; let mut i = 0; - while i << crate::digit::u8::BIT_SHIFT < ExpType::BITS as usize { // TODO: make sure to generalise when using general digits - let d = (int >> (i << crate::digit::u8::BIT_SHIFT)) as Digit; // TODO: make sure to generalise when using general digits + while i << crate::digit::BIT_SHIFT < ExpType::BITS as usize { // TODO: make sure to generalise when using general digits + let d = (int >> (i << crate::digit::BIT_SHIFT)) as Digit; // TODO: make sure to generalise when using general digits if d != 0 { if i < W { out.digits[i] = d; diff --git a/src/float/rounding.rs b/src/float/rounding.rs index 9c8c211..7df6fbd 100644 --- a/src/float/rounding.rs +++ b/src/float/rounding.rs @@ -21,7 +21,7 @@ impl BUintD8 { i = 1; } else { loop { - let shift = i << crate::digit::u8::BIT_SHIFT; // TODO: make sure to generalise when using general digits + let shift = i << crate::digit::BIT_SHIFT; // TODO: make sure to generalise when using general digits if i >= W || shift >= ExpType::BITS as usize { break; } @@ -44,8 +44,8 @@ impl BUintD8 { pub(crate) const fn from_exp_type(int: ExpType) -> Option { let mut out = Self::ZERO; let mut i = 0; - while i << crate::digit::u8::BIT_SHIFT < ExpType::BITS as usize { // TODO: make sure to generalise when using general digits - let d = (int >> (i << crate::digit::u8::BIT_SHIFT)) as Digit; // TODO: make sure to generalise when using general digits + while i << crate::digit::BIT_SHIFT < ExpType::BITS as usize { // TODO: make sure to generalise when using general digits + let d = (int >> (i << crate::digit::BIT_SHIFT)) as Digit; // TODO: make sure to generalise when using general digits if d != 0 { if i < W { out.digits[i] = d; diff --git a/src/lib.rs b/src/lib.rs index e91d114..e671823 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,11 +41,11 @@ pub mod random; pub mod types; -// #[cfg(feature = "float")] -// mod float; +#[cfg(feature = "float")] +mod float; -// #[cfg(feature = "float")] -// pub use float::Float; +#[cfg(feature = "float")] +pub use float::Float; #[cfg(test)] mod test; diff --git a/src/test/convert.rs b/src/test/convert.rs index f77582d..b158c88 100644 --- a/src/test/convert.rs +++ b/src/test/convert.rs @@ -79,29 +79,29 @@ impl TestConvert for f32 { } } -// #[cfg(feature = "float")] -// impl TestConvert for crate::float::F64 { -// type Output = u64; +#[cfg(feature = "float")] +impl TestConvert for crate::float::F64 { + type Output = u64; -// #[inline] -// fn into(self) -> Self::Output { -// use crate::cast::As; + #[inline] + fn into(self) -> Self::Output { + use crate::cast::As; -// self.to_bits().as_() -// } -// } + self.to_bits().as_() + } +} -// #[cfg(feature = "float")] -// impl TestConvert for crate::float::F32 { -// type Output = u32; +#[cfg(feature = "float")] +impl TestConvert for crate::float::F32 { + type Output = u32; -// #[inline] -// fn into(self) -> Self::Output { -// use crate::cast::As; + #[inline] + fn into(self) -> Self::Output { + use crate::cast::As; -// self.to_bits().as_() -// } -// } + self.to_bits().as_() + } +} impl TestConvert for (T, U) { type Output = (::Output, ::Output); diff --git a/src/test/types.rs b/src/test/types.rs index 66f5955..f2dfa87 100644 --- a/src/test/types.rs +++ b/src/test/types.rs @@ -33,10 +33,10 @@ test_types!(64); pub use core::primitive::*; pub use types::*; -// #[cfg(feature = "float")] -// #[cfg(not(test_int_bits = "32"))] -// pub type FTEST = crate::float::Float<8, 52>; +#[cfg(feature = "float")] +#[cfg(not(test_int_bits = "32"))] +pub type FTEST = crate::float::Float<8, 52>; -// #[cfg(feature = "float")] -// #[cfg(test_int_bits = "32")] -// pub type FTEST = crate::float::Float<4, 23>; +#[cfg(feature = "float")] +#[cfg(test_int_bits = "32")] +pub type FTEST = crate::float::Float<4, 23>;