Skip to content

Commit

Permalink
feat: Implement checked_rem
Browse files Browse the repository at this point in the history
  • Loading branch information
sorairolake committed Nov 13, 2024
1 parent 63390b1 commit 72892f2
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ All notable changes to this project will be documented in this file.
The format is based on https://keepachangelog.com/[Keep a Changelog], and this
project adheres to https://semver.org/[Semantic Versioning].

== {compare-url}/v0.1.0\...HEAD[Unreleased]

=== Added

* Implement `BitInt::checked_rem` and `BitUint::checked_rem`
({pull-request-url}/7[#7])

== {project-url}/releases/tag/v0.1.0[0.1.0] - 2024-11-12

=== Added
Expand Down
14 changes: 14 additions & 0 deletions benches/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,17 @@ fn checked_div_bit_uint(b: &mut Bencher) {

b.iter(|| n.checked_div(2));
}

#[bench]
fn checked_rem_bit_int(b: &mut Bencher) {
let n = BitI32::<31>::new(5).unwrap();

b.iter(|| n.checked_rem(2));
}

#[bench]
fn checked_rem_bit_uint(b: &mut Bencher) {
let n = BitU32::<31>::new(5).unwrap();

b.iter(|| n.checked_rem(2));
}
39 changes: 37 additions & 2 deletions src/bit_int/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ macro_rules! impl_ops {
///
/// assert_eq!(n.checked_div(2).map(BitInt::get), Some(21));
/// assert!(n.checked_div(0).is_none());
///
#[doc = concat!("assert!(BitInt::<", stringify!($T), ", 7>::MIN.checked_div(-1).is_none());")]
/// ```
#[must_use]
Expand All @@ -99,6 +98,29 @@ macro_rules! impl_ops {
None
}
}

/// Computes `self % rhs`, returning [`None`] if `rhs == 0` or the division
/// results in overflow.
///
/// # Examples
///
/// ```
/// # use bit_int::BitInt;
/// #
#[doc = concat!("let n = BitInt::<", stringify!($T), ", 4>::new(5).unwrap();")]
///
/// assert_eq!(n.checked_rem(2).map(BitInt::get), Some(1));
/// assert!(n.checked_rem(0).is_none());
#[doc = concat!("assert!(BitInt::<", stringify!($T), ", 4>::MIN.checked_rem(-1).is_none());")]
/// ```
#[must_use]
#[inline]
pub const fn checked_rem(self, rhs: $T) -> Option<Self> {
match self.get().checked_rem(rhs) {
Some(result) if self.checked_div(rhs).is_some() => Self::new(result),
_ => None,
}
}
}
};
}
Expand Down Expand Up @@ -158,12 +180,25 @@ mod tests {

assert_eq!(n.checked_div(2).map(BitI8::get), Some(21));
assert!(n.checked_div(0).is_none());

assert!(BitI8::<7>::MIN.checked_div(-1).is_none());
}

#[test]
const fn checked_div_is_const_fn() {
const _: Option<BitI8<7>> = BitI8::<7>::MAX.checked_div(0);
}

#[test]
fn checked_rem() {
let n = BitI8::<4>::new(5).unwrap();

assert_eq!(n.checked_rem(2).map(BitI8::get), Some(1));
assert!(n.checked_rem(0).is_none());
assert!(BitI8::<4>::MIN.checked_rem(-1).is_none());
}

#[test]
const fn checked_rem_is_const_fn() {
const _: Option<BitI8<4>> = BitI8::<4>::MAX.checked_rem(0);
}
}
35 changes: 35 additions & 0 deletions src/bit_uint/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,28 @@ macro_rules! impl_ops {
None
}
}

/// Computes `self % rhs`, returning [`None`] if `rhs == 0`.
///
/// # Examples
///
/// ```
/// # use bit_int::BitUint;
/// #
#[doc = concat!("let n = BitUint::<", stringify!($T), ", 3>::new(5).unwrap();")]
///
/// assert_eq!(n.checked_rem(2).map(BitUint::get), Some(1));
/// assert!(n.checked_rem(0).is_none());
/// ```
#[must_use]
#[inline]
pub const fn checked_rem(self, rhs: $T) -> Option<Self> {
if let Some(result) = self.get().checked_rem(rhs) {
Self::new(result)
} else {
None
}
}
}
};
}
Expand Down Expand Up @@ -161,4 +183,17 @@ mod tests {
const fn checked_div_is_const_fn() {
const _: Option<BitU8<6>> = BitU8::<6>::MAX.checked_div(0);
}

#[test]
fn checked_rem() {
let n = BitU8::<3>::new(5).unwrap();

assert_eq!(n.checked_rem(2).map(BitU8::get), Some(1));
assert!(n.checked_rem(0).is_none());
}

#[test]
const fn checked_rem_is_const_fn() {
const _: Option<BitU8<3>> = BitU8::<3>::MAX.checked_rem(0);
}
}

0 comments on commit 72892f2

Please sign in to comment.