Skip to content

Commit

Permalink
Tidy up, update codecov bade
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacholt100 committed Sep 28, 2023
1 parent 78183ee commit 3cfa029
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 149 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Crates.io](https://img.shields.io/crates/d/bnum?logo=rust)
](https://crates.io/crates/bnum)
[![dependency status](https://deps.rs/repo/github/isaacholt100/bnum/status.svg)](https://deps.rs/repo/github/isaacholt100/bnum)
[![codecov](https://codecov.io/gh/isaacholt100/bnum/branch/v0.9.0/graph/badge.svg)](https://codecov.io/gh/isaacholt100/bnum)
[![codecov](https://codecov.io/gh/isaacholt100/bnum/branch/master/graph/badge.svg)](https://codecov.io/gh/isaacholt100/bnum)
[![license](https://img.shields.io/crates/l/bnum)](https://github.com/isaacholt100/bnum)

Arbitrary precision, fixed-size signed and unsigned integer types for Rust.
Expand Down
144 changes: 144 additions & 0 deletions src/float/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,147 @@ mod tests {
function: <ftest>::next_down(f: ftest)
}
}


// TODO: create round-to-nearest ties-to-even function, it could take a uint and a target bit width, and return the correctly rounded result in the target precision, as well as the overflow, and whether a round up occurred
// #[allow(unused)]
// fn f64_as_f32(f: f64) -> f32 {
// if f.is_infinite() {
// return if f.is_sign_negative() {
// f32::NEG_INFINITY
// } else {
// f32::INFINITY
// };
// }
// if f == 0.0 && f.is_sign_positive() {
// return 0.0;
// }
// if f == 0.0 && f.is_sign_negative() {
// return -0.0;
// }
// let bits = f.to_bits();
// let mut mant = bits & 0xfffffffffffff;
// let mut exp = ((bits & (i64::MAX as u64)) >> 52) as i32;
// if exp != 0 {
// mant |= 0x10000000000000;

// } else {
// exp = 1;
// }
// exp -= 1023;
// //println!("exp: {}", exp);
// let mut mantissa_shift = 52 - 23;
// /*if mant.leading_zeros() != 64 - (52 + 1) {
// exp
// }*/
// if exp >= f32::MAX_EXP {
// return if f.is_sign_negative() {
// f32::NEG_INFINITY
// } else {
// f32::INFINITY
// };
// }
// if exp < f32::MIN_EXP - 1 {
// let diff = (f32::MIN_EXP - 1) - exp;
// mantissa_shift += diff;
// exp = -(f32::MAX_EXP - 1);
// }
// let new_mant = mant.checked_shr(mantissa_shift as u32).unwrap_or(0);
// //println!("{:025b}", new_mant);

// let shifted_back = new_mant.checked_shl(mantissa_shift as u32).unwrap_or(0);
// let overflow = mant ^ shifted_back;
// /*println!("overflow: {:029b}", overflow);
// println!("mant: {:053b}", mant);
// println!("shbk: {:053b}", shifted_back);
// println!("lz: {}", overflow.leading_zeros());*/
// if overflow.leading_zeros() as i32 == 64 - mantissa_shift { // this means there is a one at the overflow bit
// if overflow.count_ones() == 1 { // this means the overflowing is 100...00 so apply ties-to-even rounding
// if new_mant & 1 == 1 { // if the last bit is 1, then we round up
// mant = new_mant + 1;
// //println!("updated mant: {:025b}", mant);
// } else { // otherwise we round down
// mant = new_mant;
// }
// } else {
// mant = new_mant + 1; // round up
// }
// } else {
// mant = new_mant;
// }
// //1111111111111111111111111
// //111111111111111111111111
// if mant.leading_zeros() < 64 - (23 + 1) {
// // println!("mant overflow");
// mant >>= 1;
// exp += 1;
// }
// if exp > f32::MAX_EXP {
// return if f.is_sign_negative() {
// f32::NEG_INFINITY
// } else {
// f32::INFINITY
// };
// }
// mant ^= 0x800000;
// let sign = (bits >> 63) as u32;
// let exp = (exp + (f32::MAX_EXP - 1)) as u32;
// let mant = mant as u32;
// let bits = (sign << 31) | (exp << 23) | mant;
// f32::from_bits(bits)
// }

// #[cfg(test)]
// quickcheck::quickcheck! {
// fn qc_test_f64_as_f32(f: f64) -> quickcheck::TestResult {
// if !f.is_finite() {
// return quickcheck::TestResult::discard();
// }
// let f2 = f64_as_f32(f);
// let f3 = f as f32;
// quickcheck::TestResult::from_bool(f2 == f3)
// }
// }

// type U32 = BUintD32::<1>;
// fn parse(s: &str) -> (types::U128, U32) {
// let mut radix = 10;
// let mut custom_radix = false;
// let mut src = s;
// let bytes = s.as_bytes();
// let len = bytes.len();
// let mut first_char_zero = false;
// let mut bit_width = U32::power_of_two(7);
// let mut i = 0;
// while i < len {
// let byte = bytes[i];
// if i == 0 && byte == b'0' {
// first_char_zero = true;
// } else if i == 1 && first_char_zero && (byte == b'b' || byte == b'o' || byte == b'x') {
// let ptr = unsafe { src.as_ptr().add(2) };
// let new = core::ptr::slice_from_raw_parts(ptr, src.len() - 2);
// src = unsafe { &*(new as *const str) };
// radix = match byte {
// b'b' => 2,
// b'o' => 8,
// b'x' => 16,
// _ => unreachable!(),
// };
// custom_radix = true;
// }
// if i != 0 && i != len - 1 && byte == b'u' {
// let old_len = src.len();
// let ptr = src.as_ptr();

// let new_len = if custom_radix { i - 2 } else { i };
// let bit_width_ptr = core::ptr::slice_from_raw_parts(unsafe { ptr.add(new_len + 1) }, old_len - new_len - 1);
// let new = core::ptr::slice_from_raw_parts(ptr, new_len);
// src = unsafe { &*(new as *const str) };
// let bit_width_str = unsafe { &*(bit_width_ptr as *const str) };
// bit_width = U32::parse_str_radix(bit_width_str, 10);
// break;
// }
// i += 1;
// }
// (types::U128::parse_str_radix(src, radix), bit_width)
// }
150 changes: 2 additions & 148 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ mod test;
#[cfg(test)]
use test::types::*;

/*#[cfg(feature = "usize_exptype")]
type ExpType = usize;
#[cfg(not(feature = "usize_exptype"))]*/
type ExpType = u32;

macro_rules! macro_impl {
Expand Down Expand Up @@ -101,152 +98,9 @@ pub use bigints::*;

/// Trait for fallible conversions between `bnum` integer types.
///
/// Unfortunately, [`TryFrom`] cannot currently be used for conversions between `bnum` integers, since `TryFrom<T> for T` is already implemented by the standard library (and so it is not possible to implement `TryFrom<BUint<M>> for BUint<N>`). When the `generic_const_exprs` feature becomes stabilised, it may be possible to use `TryFrom` instead of `BTryFrom`. `BTryFrom` is designed to have the same behaviour as `TryFrom` for conversions between two primitive types, and conversions between a primitive type and a bnum type. `BTryFrom` is a workaround for the issue described above, and so you should not implement it yourself. It is only meant for conversions between `bnum` integers.
/// Unfortunately, [`TryFrom`] cannot currently be used for conversions between `bnum` integers, since [`TryFrom<T> for T`](https://doc.rust-lang.org/std/convert/trait.TryFrom.html#impl-TryFrom%3CU%3E-for-T) is already implemented by the standard library (and so it is not possible to implement `TryFrom<BUint<M>> for BUint<N>`). When the [`generic_const_exprs`](https://github.com/rust-lang/rust/issues/76560) feature becomes stabilised, it may be possible to use [`TryFrom`] instead of `BTryFrom`. `BTryFrom` is designed to have the same behaviour as [`TryFrom`] for conversions between two primitive types, and conversions between a primitive type and a bnum type. `BTryFrom` is a workaround for the issue described above, and so you should not implement it yourself. It should only be used for conversions between `bnum` integers.
pub trait BTryFrom<T>: Sized {
type Error;

fn try_from(from: T) -> Result<Self, Self::Error>;
}

// TODO: create round-to-nearest ties-to-even function, it could take a uint and a target bit width, and return the correctly rounded result in the target precision, as well as the overflow, and whether a round up occurred
// #[allow(unused)]
// fn f64_as_f32(f: f64) -> f32 {
// if f.is_infinite() {
// return if f.is_sign_negative() {
// f32::NEG_INFINITY
// } else {
// f32::INFINITY
// };
// }
// if f == 0.0 && f.is_sign_positive() {
// return 0.0;
// }
// if f == 0.0 && f.is_sign_negative() {
// return -0.0;
// }
// let bits = f.to_bits();
// let mut mant = bits & 0xfffffffffffff;
// let mut exp = ((bits & (i64::MAX as u64)) >> 52) as i32;
// if exp != 0 {
// mant |= 0x10000000000000;

// } else {
// exp = 1;
// }
// exp -= 1023;
// //println!("exp: {}", exp);
// let mut mantissa_shift = 52 - 23;
// /*if mant.leading_zeros() != 64 - (52 + 1) {
// exp
// }*/
// if exp >= f32::MAX_EXP {
// return if f.is_sign_negative() {
// f32::NEG_INFINITY
// } else {
// f32::INFINITY
// };
// }
// if exp < f32::MIN_EXP - 1 {
// let diff = (f32::MIN_EXP - 1) - exp;
// mantissa_shift += diff;
// exp = -(f32::MAX_EXP - 1);
// }
// let new_mant = mant.checked_shr(mantissa_shift as u32).unwrap_or(0);
// //println!("{:025b}", new_mant);

// let shifted_back = new_mant.checked_shl(mantissa_shift as u32).unwrap_or(0);
// let overflow = mant ^ shifted_back;
// /*println!("overflow: {:029b}", overflow);
// println!("mant: {:053b}", mant);
// println!("shbk: {:053b}", shifted_back);
// println!("lz: {}", overflow.leading_zeros());*/
// if overflow.leading_zeros() as i32 == 64 - mantissa_shift { // this means there is a one at the overflow bit
// if overflow.count_ones() == 1 { // this means the overflowing is 100...00 so apply ties-to-even rounding
// if new_mant & 1 == 1 { // if the last bit is 1, then we round up
// mant = new_mant + 1;
// //println!("updated mant: {:025b}", mant);
// } else { // otherwise we round down
// mant = new_mant;
// }
// } else {
// mant = new_mant + 1; // round up
// }
// } else {
// mant = new_mant;
// }
// //1111111111111111111111111
// //111111111111111111111111
// if mant.leading_zeros() < 64 - (23 + 1) {
// // println!("mant overflow");
// mant >>= 1;
// exp += 1;
// }
// if exp > f32::MAX_EXP {
// return if f.is_sign_negative() {
// f32::NEG_INFINITY
// } else {
// f32::INFINITY
// };
// }
// mant ^= 0x800000;
// let sign = (bits >> 63) as u32;
// let exp = (exp + (f32::MAX_EXP - 1)) as u32;
// let mant = mant as u32;
// let bits = (sign << 31) | (exp << 23) | mant;
// f32::from_bits(bits)
// }

// #[cfg(test)]
// quickcheck::quickcheck! {
// fn qc_test_f64_as_f32(f: f64) -> quickcheck::TestResult {
// if !f.is_finite() {
// return quickcheck::TestResult::discard();
// }
// let f2 = f64_as_f32(f);
// let f3 = f as f32;
// quickcheck::TestResult::from_bool(f2 == f3)
// }
// }

// type U32 = BUintD32::<1>;
// fn parse(s: &str) -> (types::U128, U32) {
// let mut radix = 10;
// let mut custom_radix = false;
// let mut src = s;
// let bytes = s.as_bytes();
// let len = bytes.len();
// let mut first_char_zero = false;
// let mut bit_width = U32::power_of_two(7);
// let mut i = 0;
// while i < len {
// let byte = bytes[i];
// if i == 0 && byte == b'0' {
// first_char_zero = true;
// } else if i == 1 && first_char_zero && (byte == b'b' || byte == b'o' || byte == b'x') {
// let ptr = unsafe { src.as_ptr().add(2) };
// let new = core::ptr::slice_from_raw_parts(ptr, src.len() - 2);
// src = unsafe { &*(new as *const str) };
// radix = match byte {
// b'b' => 2,
// b'o' => 8,
// b'x' => 16,
// _ => unreachable!(),
// };
// custom_radix = true;
// }
// if i != 0 && i != len - 1 && byte == b'u' {
// let old_len = src.len();
// let ptr = src.as_ptr();

// let new_len = if custom_radix { i - 2 } else { i };
// let bit_width_ptr = core::ptr::slice_from_raw_parts(unsafe { ptr.add(new_len + 1) }, old_len - new_len - 1);
// let new = core::ptr::slice_from_raw_parts(ptr, new_len);
// src = unsafe { &*(new as *const str) };
// let bit_width_str = unsafe { &*(bit_width_ptr as *const str) };
// bit_width = U32::parse_str_radix(bit_width_str, 10);
// break;
// }
// i += 1;
// }
// (types::U128::parse_str_radix(src, radix), bit_width)
// }
}

0 comments on commit 3cfa029

Please sign in to comment.