Skip to content

Commit

Permalink
add float feature
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacholt100 committed Jan 7, 2025
1 parent b321029 commit 5748947
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 115 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
2 changes: 1 addition & 1 deletion bench/benches/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
38 changes: 19 additions & 19 deletions src/buint/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,28 +92,28 @@ use crate::cast::CastFrom;
use crate::doc;
use crate::ExpType;

// #[cfg(feature = "float")]
// impl<const N: usize> FloatMantissa for BUintD8<N> {
// const ZERO: Self = Self::ZERO;
// const ONE: Self = Self::ONE;
// const TWO: Self = Self::TWO;
// const MAX: Self = Self::MAX;
#[cfg(feature = "float")]
impl<const N: usize> FloatMantissa for BUintD8<N> {
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> {
// Self::checked_shr(self, n)
// }
#[inline]
fn checked_shr(self, n: ExpType) -> Option<Self> {
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<const N: usize> CastUintFromFloatHelper for BUintD8<N> {
const MAX: Self = Self::MAX;
Expand Down
16 changes: 8 additions & 8 deletions src/buint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,14 @@ impl<const N: usize> BUintD8<N> {
}

#[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 {
Expand Down
34 changes: 17 additions & 17 deletions src/doc/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
16 changes: 8 additions & 8 deletions src/doc/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
38 changes: 14 additions & 24 deletions src/float/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -20,7 +20,7 @@ macro_rules! uint_as_float {
};
}

uint_as_float!(u8, u16, u32, u64, u128, usize, BUintD8<N>, BUintD16<N>, BUintD32<N>, BUint<N>);
uint_as_float!(u8, u16, u32, u64, u128, usize, BUintD8<N>);

macro_rules! int_as_float {
($($int: ty), *) => {
Expand All @@ -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<const W: usize, const MB: usize, const N: usize> CastFrom<BIntD8<N>> for Float<W, MB> {
fn cast_from(from: BIntD8<N>) -> Self {
let pos_cast = Self::cast_from(from.unsigned_abs());
if from.is_negative() {
-pos_cast
} else {
pos_cast
}
}
}
)*
};
impl<const W: usize, const MB: usize, const N: usize> CastFrom<BIntD8<N>> for Float<W, MB> {
fn cast_from(from: BIntD8<N>) -> 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<const W: usize, const MB: usize, const N: usize> CastFrom<Float<W, MB>> for BIntD8<N> {
crate::bint::cast::bint_cast_from_float!(Float<W, MB>, BUintD8<N>);
}
};
impl<const W: usize, const MB: usize, const N: usize> CastFrom<Float<W, MB>> for BIntD8<N> {
crate::bint::cast::bint_cast_from_float!(Float<W, MB>);
}

macro_rules! float_as_int {
Expand Down Expand Up @@ -111,7 +101,7 @@ macro_rules! float_as_uint {
};
}

float_as_uint!(BUintD8<N>, BUintD16<N>, BUintD32<N>, BUint<N>, u8, u16, u32, u64, u128, usize);
float_as_uint!(BUintD8<N>, u8, u16, u32, u64, u128, usize);

impl<const W: usize, const MB: usize> FloatCastHelper for Float<W, MB> {
const BITS: ExpType = Self::BITS;
Expand Down
4 changes: 2 additions & 2 deletions src/float/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const fn buint_from_usize<const N: usize>(u: usize) -> BUintD8<N> {
const UINT_BITS: usize = <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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/float/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ impl<const W: usize> BUintD8<W> {
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
Expand Down
6 changes: 3 additions & 3 deletions src/float/rounding copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<const W: usize> BUintD8<W> {
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;
}
Expand All @@ -44,8 +44,8 @@ impl<const W: usize> BUintD8<W> {
const fn from_exp_type(int: ExpType) -> Option<Self> {
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;
Expand Down
6 changes: 3 additions & 3 deletions src/float/rounding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<const W: usize> BUintD8<W> {
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;
}
Expand All @@ -44,8 +44,8 @@ impl<const W: usize> BUintD8<W> {
pub(crate) const fn from_exp_type(int: ExpType) -> Option<Self> {
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;
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 18 additions & 18 deletions src/test/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: TestConvert, U: TestConvert> TestConvert for (T, U) {
type Output = (<T as TestConvert>::Output, <U as TestConvert>::Output);
Expand Down
12 changes: 6 additions & 6 deletions src/test/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

0 comments on commit 5748947

Please sign in to comment.