use crate::int::types::Int;
#[inline]
#[allow(dead_code)]
pub(crate) fn rem_native_direct<const N: usize>(a: Int<N>, b: Int<N>) -> Int<N> {
assert!(
!b.is_zero(),
"attempt to calculate the remainder with a divisor of zero"
);
let ai = a.to_i128();
let bi = b.to_i128();
let r = if bi == -1 { 0 } else { ai % bi };
Int::<N>::from_i128(r)
}
#[cfg(test)]
mod tests {
use super::rem_native_direct;
use crate::int::algos::rem::rem_native::rem_native;
use crate::int::types::Int;
#[test]
fn n1_matches_shipped_magnitude_path() {
let cases: &[(i64, i64)] = &[
(10, 3),
(-10, 3),
(10, -3),
(-10, -3),
(0, 7),
(7, 7),
(i64::MAX, 2),
(i64::MIN + 1, 2),
(i64::MIN, 7),
(i64::MIN, -1),
];
for &(a, b) in cases {
let ia = Int::<1>::from_i64(a);
let ib = Int::<1>::from_i64(b);
assert_eq!(
rem_native_direct::<1>(ia, ib),
rem_native::<1>(ia, ib),
"n1 ({a} % {b})"
);
}
}
#[test]
fn n2_matches_shipped_magnitude_path() {
let cases: &[(i128, i128)] = &[
(100, 7),
(-100, 7),
(100, -7),
(-100, -7),
(i128::MAX, 3),
(i128::MIN + 1, 3),
(i128::MIN, 7),
(i128::MIN, -1),
];
for &(a, b) in cases {
let ia = Int::<2>::from_i128(a);
let ib = Int::<2>::from_i128(b);
assert_eq!(
rem_native_direct::<2>(ia, ib),
rem_native::<2>(ia, ib),
"n2 ({a} % {b})"
);
}
}
}