Skip to content

Commit

Permalink
Merge pull request #40 from isaacholt100/latest
Browse files Browse the repository at this point in the history
Latest
  • Loading branch information
isaacholt100 authored Mar 6, 2024
2 parents f1520e5 + bc00364 commit d715a3e
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bnum"
version = "0.10.0"
version = "0.11.0"
authors = ["isaac-holt <isaac_holt@icloud.com>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ This crate uses Rust's const generics to allow creation of integers of arbitrary
To install and use `bnum`, simply add the following line to your `Cargo.toml` file in the `[dependencies]` section:

```toml
bnum = "0.10.0"
bnum = "0.11.0"
```

Or, to enable various `bnum` features as well, add for example this line instead:

```toml
bnum = { version = "0.10.0", features = ["rand"] } # enables the "rand" feature
bnum = { version = "0.11.0", features = ["rand"] } # enables the "rand" feature
```

## Example Usage
Expand Down
2 changes: 2 additions & 0 deletions changes/v0.11.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Make integer ilog methods panic for invalid inputs in release mode as well as debug mode.
- Match ilog panic message with that of Rust's primitives.
4 changes: 2 additions & 2 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ coverage:
status:
project:
default:
threshold: 1% # needed because due to property based testing, coverage isn't exactly the same each time
threshold: 2% # needed because due to property based testing, coverage isn't exactly the same each time
patch:
default:
threshold: 1% # needed because due to property based testing, coverage isn't exactly the same each time
threshold: 2% # needed because due to property based testing, coverage isn't exactly the same each time
4 changes: 2 additions & 2 deletions src/bint/endian.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::digit;
use crate::doc;
use core::mem::MaybeUninit;
// use core::mem::MaybeUninit;

macro_rules! endian {
($BUint: ident, $BInt: ident, $Digit: ident) => {
Expand Down Expand Up @@ -131,7 +131,7 @@ macro_rules! endian {
$Digit::MIN
};
let mut out_digits = [sign_bits; N];
let slice_ptr = slice.as_ptr();
// let slice_ptr = slice.as_ptr();
let mut i = 0;
let exact = len >> digit::$Digit::BYTE_SHIFT;
while i < exact {
Expand Down
22 changes: 12 additions & 10 deletions src/bint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ macro_rules! ilog {
#[must_use = doc::must_use_op!()]
#[inline]
pub const fn $method(self, $($base : $ty),*) -> ExpType {
$(
if $base.le(&<$ty>::ONE) {
panic!(errors::err_msg!(errors::invalid_log_base!()))
}
), *
if self.is_negative() {
#[cfg(debug_assertions)]
panic!(errors::err_msg!("attempt to calculate ilog of negative number"));
#[cfg(not(debug_assertions))]
0
panic!(errors::err_msg!(errors::non_positive_log_message!()))
} else {
self.bits.$method($($base.bits)?)
}
Expand Down Expand Up @@ -463,16 +465,16 @@ macro_rules! mod_impl {

#[test]
fn sum() {
let v = vec![&UTEST::ZERO, &UTEST::ONE, &UTEST::TWO, &UTEST::THREE, &UTEST::FOUR];
assert_eq!(UTEST::TEN, v.iter().copied().sum());
assert_eq!(UTEST::TEN, v.into_iter().sum());
let v = vec![&ITEST::ZERO, &ITEST::ONE, &ITEST::TWO, &ITEST::THREE, &ITEST::FOUR];
assert_eq!(ITEST::TEN, v.iter().copied().sum());
assert_eq!(ITEST::TEN, v.into_iter().sum());
}

#[test]
fn product() {
let v = vec![&UTEST::ONE, &UTEST::TWO, &UTEST::THREE];
assert_eq!(UTEST::SIX, v.iter().copied().sum());
assert_eq!(UTEST::SIX, v.into_iter().sum());
let v = vec![&ITEST::ONE, &ITEST::TWO, &ITEST::THREE];
assert_eq!(ITEST::SIX, v.iter().copied().sum());
assert_eq!(ITEST::SIX, v.into_iter().sum());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/buint/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ macro_rules! as_buint {
use crate::cast::CastFrom;
use crate::doc;
use crate::nightly::impl_const;
use core::mem::MaybeUninit;
// use core::mem::MaybeUninit;

macro_rules! cast {
($BUint: ident, $BInt: ident, $Digit: ident) => {
Expand Down
6 changes: 3 additions & 3 deletions src/buint/endian.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::digit;
use crate::doc;
use core::mem::MaybeUninit;
// use core::mem::MaybeUninit;

macro_rules! endian {
($BUint: ident, $BInt: ident, $Digit: ident) => {
Expand Down Expand Up @@ -66,7 +66,7 @@ macro_rules! endian {
pub const fn from_be_slice(slice: &[u8]) -> Option<Self> {
let len = slice.len();
let mut out = Self::ZERO;
let slice_ptr = slice.as_ptr();
// let slice_ptr = slice.as_ptr();
let mut i = 0;
let exact = len >> digit::$Digit::BYTE_SHIFT;
while i < exact {
Expand Down Expand Up @@ -131,7 +131,7 @@ macro_rules! endian {
pub const fn from_le_slice(slice: &[u8]) -> Option<Self> {
let len = slice.len();
let mut out = Self::ZERO;
let slice_ptr = slice.as_ptr();
// let slice_ptr = slice.as_ptr();
let mut i = 0;
let exact = len >> digit::$Digit::BYTE_SHIFT;
while i < exact {
Expand Down
39 changes: 14 additions & 25 deletions src/buint/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#[cfg(debug_assertions)]
use crate::errors::{self, option_expect};

use crate::digit;
use crate::doc;
use crate::ExpType;
use core::mem::MaybeUninit;
// use core::mem::MaybeUninit;

#[cfg(feature = "serde")]
use ::{
Expand Down Expand Up @@ -296,42 +295,32 @@ macro_rules! mod_impl {
#[must_use = doc::must_use_op!()]
#[inline]
pub const fn ilog2(self) -> ExpType {
#[cfg(debug_assertions)]
return option_expect!(
option_expect!(
self.checked_ilog2(),
errors::err_msg!("attempt to calculate ilog2 of zero")
);
#[cfg(not(debug_assertions))]
match self.checked_ilog2() {
Some(n) => n,
None => 0,
}
errors::err_msg!(errors::non_positive_log_message!())
)
}

#[doc = doc::ilog10!(U)]
#[must_use = doc::must_use_op!()]
#[inline]
pub const fn ilog10(self) -> ExpType {
#[cfg(debug_assertions)]
return option_expect!(self.checked_ilog10(), errors::err_msg!("attempt to calculate ilog10 of zero"));
#[cfg(not(debug_assertions))]
match self.checked_ilog10() {
Some(n) => n,
None => 0,
}
option_expect!(
self.checked_ilog10(),
errors::err_msg!(errors::non_positive_log_message!())
)
}

#[doc = doc::ilog!(U)]
#[must_use = doc::must_use_op!()]
#[inline]
pub const fn ilog(self, base: Self) -> ExpType {
#[cfg(debug_assertions)]
return option_expect!(self.checked_ilog(base), errors::err_msg!("attempt to calculate ilog of zero or ilog with base < 2"));
#[cfg(not(debug_assertions))]
match self.checked_ilog(base) {
Some(n) => n,
None => 0,
if base.le(&Self::ONE) {
panic!("{}", errors::err_msg!(errors::invalid_log_base!()));
}
option_expect!(
self.checked_ilog(base), errors::err_msg!(errors::non_positive_log_message!())
)
}

#[doc = doc::abs_diff!(U)]
Expand Down Expand Up @@ -384,7 +373,7 @@ macro_rules! mod_impl {
let digit_shift = (rhs >> digit::$Digit::BIT_SHIFT) as usize;
let bit_shift = rhs & digit::$Digit::BITS_MINUS_1;

let num_copies = N.saturating_sub(digit_shift); // TODO: use unchecked_ methods from primitives when these are stablised and constified
// let num_copies = N.saturating_sub(digit_shift); // TODO: use unchecked_ methods from primitives when these are stablised and constified

if bit_shift != 0 {
let carry_shift = digit::$Digit::BITS - bit_shift;
Expand Down
16 changes: 16 additions & 0 deletions src/errors/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ macro_rules! rem_by_zero_message {

pub(crate) use rem_by_zero_message;

macro_rules! non_positive_log_message {
() => {
"argument of integer logarithm must be positive"
}
}

pub(crate) use non_positive_log_message;

macro_rules! invalid_log_base {
() => {
"base of integer logarithm must be at least 2"
}
}

pub(crate) use invalid_log_base;

macro_rules! rem_zero {
() => {
panic!(crate::errors::err_msg!(crate::errors::rem_by_zero_message!()))
Expand Down
8 changes: 4 additions & 4 deletions src/int/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,22 @@ macro_rules! tests {
}
test_bignum! {
function: <$int>::ilog(a: $int, base: $int),
skip: crate::test::debug_skip!(a <= 0 || base <= 1)
skip: a <= 0 || base <= 1
}
test_bignum! {
function: <$int>::ilog2(a: $int),
skip: crate::test::debug_skip!(a <= 0)
skip: a <= 0
}
test_bignum! {
function: <$int>::ilog10(a: $int),
skip: crate::test::debug_skip!(a <= 0)
skip: a <= 0
}
test_bignum! {
function: <$int>::checked_next_multiple_of(a: $int, b: $int)
}
test_bignum! {
function: <$int>::next_multiple_of(a: $int, b: $int),
skip: crate::test::debug_skip!(a.checked_next_multiple_of(b).is_none())
skip: crate::test::debug_skip!(a.checked_next_multiple_of(b).is_none()) || b == 0
}
test_bignum! {
function: <$int>::div_floor(a: $int, b: $int),
Expand Down

0 comments on commit d715a3e

Please sign in to comment.