Skip to content

Commit

Permalink
Merge pull request #24 from isaacholt100/arbitrary
Browse files Browse the repository at this point in the history
Bump version to 0.7.0
  • Loading branch information
isaacholt100 authored May 28, 2023
2 parents d3b5e5e + d28f8e4 commit 9f4a08f
Show file tree
Hide file tree
Showing 72 changed files with 6,387 additions and 6,398 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bnum"
version = "0.6.0"
version = "0.7.0"
authors = ["isaac-holt <isaac_holt@icloud.com>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -26,6 +26,7 @@ num-traits = { version = "0.2.15", optional = true, default-features = false }
serde = { version = "1.0.152", features = ["derive"], optional = true, default-features = false }
serde-big-array = { version = "0.4.1", optional = true, default-features = false }
rand = { version = "0.8.5", features = ["min_const_gen", "small_rng", "std_rng"], optional = true, default-features = false }
arbitrary = { version = "1.3.0", features = ["derive"], optional = true }

[dev-dependencies]
quickcheck = "1.0.3"
Expand All @@ -36,4 +37,4 @@ lto = true # enable link-time optimisation for faster runtime, but slower compil
opt-level = 3 # maximum optimisation level for faster runtime, but slower compile time

[package.metadata.docs.rs]
features = ["nightly", "serde", "numtraits", "rand"]
features = ["nightly", "serde", "numtraits", "rand", "arbitrary"]
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ In version `0.1.0`, the `from_be` and `to_be` methods on all integers were imple
To install and use `bnum`, simply add the following line to your `Cargo.toml` file in the `[dependencies]` section:

```toml
bnum = "0.6.0"
bnum = "0.7.0"
```

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

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

## Example Usage
Expand Down Expand Up @@ -92,6 +92,10 @@ assert_eq!(neg_one.count_ones(), 80); // signed integers are stored in two's com

## Features

### Fuzzing

The `arbitrary` feature derives the [`Arbitrary`](https://docs.rs/arbitrary/latest/arbitrary/trait.Arbitrary.html) trait from the [`arbitrary`](https://docs.rs/arbitrary/latest/arbitrary/) crate. **Note: currently, this feature cannot be used with `no_std`.**

### Random Number Generation

The `rand` feature allows creation of random bnum integers via the [`rand`](https://docs.rs/rand/latest/rand/) crate.
Expand Down
4 changes: 2 additions & 2 deletions changes/v0.7.0
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Removal of `const` impls of standard library traits, addition of equivalent methods defined on the integer types themselves.
`abs_diff`, `BUint::{overflowing_neg, wrapping_neg}` now `const`.
(TODO): implementation of `Arbitrary`.
Following methods are now `const`: `abs_diff`, `BUint::{overflowing_neg, wrapping_neg}`, `wrapping_{shr, shl}`, `overflowing_{shr, shl}`, `checked_{shr, shl}`, `rotate_left`, `rotate_right`, `from_be_slice`, `from_le_slice`.
Implementation of `Arbitrary`.
224 changes: 112 additions & 112 deletions src/bint/cast.rs
Original file line number Diff line number Diff line change
@@ -1,134 +1,134 @@
macro_rules! bint_as {
($BInt: ident, $Digit: ident; $($int: ty), *) => {
$(
impl_const! {
impl<const N: usize> const CastFrom<$BInt<N>> for $int {
#[inline]
fn cast_from(from: $BInt<N>) -> Self {
if from.is_negative() {
let digits = from.bits.digits;
let mut out = !0;
let mut i = 0;
while i << digit::$Digit::BIT_SHIFT < <$int>::BITS as usize && i < N {
out &= !((!digits[i]) as $int << (i << digit::$Digit::BIT_SHIFT));
i += 1;
}
out
} else {
<$int>::cast_from(from.bits)
}
}
}
}
)*
};
($BInt: ident, $Digit: ident; $($int: ty), *) => {
$(
impl_const! {
impl<const N: usize> const CastFrom<$BInt<N>> for $int {
#[inline]
fn cast_from(from: $BInt<N>) -> Self {
if from.is_negative() {
let digits = from.bits.digits;
let mut out = !0;
let mut i = 0;
while i << digit::$Digit::BIT_SHIFT < <$int>::BITS as usize && i < N {
out &= !((!digits[i]) as $int << (i << digit::$Digit::BIT_SHIFT));
i += 1;
}
out
} else {
<$int>::cast_from(from.bits)
}
}
}
}
)*
};
}

macro_rules! as_bint {
($BInt: ident, $BUint: ident; $($ty: ty), *) => {
$(impl_const! {
impl<const N: usize> const CastFrom<$ty> for $BInt<N> {
#[inline]
fn cast_from(from: $ty) -> Self {
Self::from_bits($BUint::cast_from(from))
}
}
})*
}
($BInt: ident, $BUint: ident; $($ty: ty), *) => {
$(impl_const! {
impl<const N: usize> const CastFrom<$ty> for $BInt<N> {
#[inline]
fn cast_from(from: $ty) -> Self {
Self::from_bits($BUint::cast_from(from))
}
}
})*
}
}

use crate::cast::CastFrom;
use crate::nightly::impl_const;
use crate::digit;
use crate::nightly::impl_const;

macro_rules! cast {
($BUint: ident, $BInt: ident, $Digit: ident) => {
bint_as!($BInt, $Digit; u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
($BUint: ident, $BInt: ident, $Digit: ident) => {
bint_as!($BInt, $Digit; u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);

impl<const N: usize> CastFrom<$BInt<N>> for f32 {
#[inline]
fn cast_from(from: $BInt<N>) -> Self {
let f = f32::cast_from(from.unsigned_abs());
if from.is_negative() {
-f
} else {
f
}
}
}
impl<const N: usize> CastFrom<$BInt<N>> for f32 {
#[inline]
fn cast_from(from: $BInt<N>) -> Self {
let f = f32::cast_from(from.unsigned_abs());
if from.is_negative() {
-f
} else {
f
}
}
}

impl<const N: usize> CastFrom<$BInt<N>> for f64 {
#[inline]
fn cast_from(from: $BInt<N>) -> Self {
let f = f64::cast_from(from.unsigned_abs());
if from.is_negative() {
-f
} else {
f
}
}
}
impl<const N: usize> CastFrom<$BInt<N>> for f64 {
#[inline]
fn cast_from(from: $BInt<N>) -> Self {
let f = f64::cast_from(from.unsigned_abs());
if from.is_negative() {
-f
} else {
f
}
}
}

as_bint!($BInt, $BUint; u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, bool, char);
as_bint!($BInt, $BUint; u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, bool, char);

impl_const! {
impl<const N: usize, const M: usize> const CastFrom<$BUint<M>> for $BInt<N> {
#[inline]
fn cast_from(from: $BUint<M>) -> Self {
Self::from_bits($BUint::cast_from(from))
}
}
}
impl_const! {
impl<const N: usize, const M: usize> const CastFrom<$BUint<M>> for $BInt<N> {
#[inline]
fn cast_from(from: $BUint<M>) -> Self {
Self::from_bits($BUint::cast_from(from))
}
}
}

impl_const! {
impl<const N: usize, const M: usize> const CastFrom<$BInt<M>> for $BInt<N> {
#[inline]
fn cast_from(from: $BInt<M>) -> Self {
Self::from_bits($BUint::cast_from(from))
}
}
}
impl_const! {
impl<const N: usize, const M: usize> const CastFrom<$BInt<M>> for $BInt<N> {
#[inline]
fn cast_from(from: $BInt<M>) -> Self {
Self::from_bits($BUint::cast_from(from))
}
}
}

macro_rules! cast_from_float {
($f: ty) => {
#[inline]
fn cast_from(from: $f) -> Self {
if from.is_sign_negative() {
let u = $BUint::<N>::cast_from(-from);
if u >= Self::MIN.to_bits() {
Self::MIN
} else {
-Self::from_bits(u)
}
} else {
let u = $BUint::<N>::cast_from(from);
let i = Self::from_bits(u);
if i.is_negative() {
Self::MAX
} else {
i
}
}
}
};
}
macro_rules! cast_from_float {
($f: ty) => {
#[inline]
fn cast_from(from: $f) -> Self {
if from.is_sign_negative() {
let u = $BUint::<N>::cast_from(-from);
if u >= Self::MIN.to_bits() {
Self::MIN
} else {
-Self::from_bits(u)
}
} else {
let u = $BUint::<N>::cast_from(from);
let i = Self::from_bits(u);
if i.is_negative() {
Self::MAX
} else {
i
}
}
}
};
}

impl<const N: usize> CastFrom<f32> for $BInt<N> {
cast_from_float!(f32);
}
impl<const N: usize> CastFrom<f32> for $BInt<N> {
cast_from_float!(f32);
}

impl<const N: usize> CastFrom<f64> for $BInt<N> {
cast_from_float!(f64);
}
impl<const N: usize> CastFrom<f64> for $BInt<N> {
cast_from_float!(f64);
}

#[cfg(test)]
paste::paste! {
mod [<$Digit _digit_tests>] {
use crate::test::types::big_types::$Digit::*;
crate::int::cast::tests!(itest);
}
}
};
#[cfg(test)]
paste::paste! {
mod [<$Digit _digit_tests>] {
use crate::test::types::big_types::$Digit::*;
crate::int::cast::tests!(itest);
}
}
};
}

crate::macro_impl!(cast);
Loading

0 comments on commit 9f4a08f

Please sign in to comment.