-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generic exp for pow and checked_pow #300
Open
mtshr
wants to merge
3
commits into
rust-num:master
Choose a base branch
from
mtshr:generic-exp-for-pow
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+61
−97
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
use crate::{CheckedMul, One}; | ||
use crate::{CheckedMul, One, PrimInt, Unsigned}; | ||
use core::num::Wrapping; | ||
use core::ops::Mul; | ||
|
||
/// Binary operator for raising a value to a power. | ||
pub trait Pow<RHS> { | ||
|
@@ -19,17 +18,24 @@ pub trait Pow<RHS> { | |
} | ||
|
||
macro_rules! pow_impl { | ||
(prim_int $t:ty) => { | ||
pow_impl!($t, u8); | ||
pow_impl!($t, u16); | ||
pow_impl!($t, u32, u32, <$t>::pow); | ||
pow_impl!($t, u64); | ||
pow_impl!($t, u128); | ||
pow_impl!($t, usize); | ||
}; | ||
($t:ty) => { | ||
pow_impl!($t, u8); | ||
pow_impl!($t, u16); | ||
pow_impl!($t, u32); | ||
pow_impl!($t, u64); | ||
pow_impl!($t, u128); | ||
pow_impl!($t, usize); | ||
|
||
// FIXME: these should be possible | ||
// pow_impl!($t, u16); | ||
// pow_impl!($t, u32); | ||
// pow_impl!($t, u64); | ||
}; | ||
($t:ty, $rhs:ty) => { | ||
pow_impl!($t, $rhs, usize, pow); | ||
pow_impl!($t, $rhs, $rhs, pow); | ||
}; | ||
($t:ty, $rhs:ty, $desired_rhs:ty, $method:expr) => { | ||
impl Pow<$rhs> for $t { | ||
|
@@ -66,57 +72,19 @@ macro_rules! pow_impl { | |
}; | ||
} | ||
|
||
pow_impl!(u8, u8, u32, u8::pow); | ||
pow_impl!(u8, u16, u32, u8::pow); | ||
pow_impl!(u8, u32, u32, u8::pow); | ||
pow_impl!(u8, usize); | ||
pow_impl!(i8, u8, u32, i8::pow); | ||
pow_impl!(i8, u16, u32, i8::pow); | ||
pow_impl!(i8, u32, u32, i8::pow); | ||
pow_impl!(i8, usize); | ||
pow_impl!(u16, u8, u32, u16::pow); | ||
pow_impl!(u16, u16, u32, u16::pow); | ||
pow_impl!(u16, u32, u32, u16::pow); | ||
pow_impl!(u16, usize); | ||
pow_impl!(i16, u8, u32, i16::pow); | ||
pow_impl!(i16, u16, u32, i16::pow); | ||
pow_impl!(i16, u32, u32, i16::pow); | ||
pow_impl!(i16, usize); | ||
pow_impl!(u32, u8, u32, u32::pow); | ||
pow_impl!(u32, u16, u32, u32::pow); | ||
pow_impl!(u32, u32, u32, u32::pow); | ||
pow_impl!(u32, usize); | ||
pow_impl!(i32, u8, u32, i32::pow); | ||
pow_impl!(i32, u16, u32, i32::pow); | ||
pow_impl!(i32, u32, u32, i32::pow); | ||
pow_impl!(i32, usize); | ||
pow_impl!(u64, u8, u32, u64::pow); | ||
pow_impl!(u64, u16, u32, u64::pow); | ||
pow_impl!(u64, u32, u32, u64::pow); | ||
pow_impl!(u64, usize); | ||
pow_impl!(i64, u8, u32, i64::pow); | ||
pow_impl!(i64, u16, u32, i64::pow); | ||
pow_impl!(i64, u32, u32, i64::pow); | ||
pow_impl!(i64, usize); | ||
|
||
pow_impl!(u128, u8, u32, u128::pow); | ||
pow_impl!(u128, u16, u32, u128::pow); | ||
pow_impl!(u128, u32, u32, u128::pow); | ||
pow_impl!(u128, usize); | ||
|
||
pow_impl!(i128, u8, u32, i128::pow); | ||
pow_impl!(i128, u16, u32, i128::pow); | ||
pow_impl!(i128, u32, u32, i128::pow); | ||
pow_impl!(i128, usize); | ||
|
||
pow_impl!(usize, u8, u32, usize::pow); | ||
pow_impl!(usize, u16, u32, usize::pow); | ||
pow_impl!(usize, u32, u32, usize::pow); | ||
pow_impl!(usize, usize); | ||
pow_impl!(isize, u8, u32, isize::pow); | ||
pow_impl!(isize, u16, u32, isize::pow); | ||
pow_impl!(isize, u32, u32, isize::pow); | ||
pow_impl!(isize, usize); | ||
pow_impl!(prim_int u8); | ||
pow_impl!(prim_int u16); | ||
pow_impl!(prim_int u32); | ||
pow_impl!(prim_int u64); | ||
pow_impl!(prim_int u128); | ||
pow_impl!(prim_int i8); | ||
pow_impl!(prim_int i16); | ||
pow_impl!(prim_int i32); | ||
pow_impl!(prim_int i64); | ||
pow_impl!(prim_int i128); | ||
pow_impl!(prim_int usize); | ||
pow_impl!(prim_int isize); | ||
|
||
pow_impl!(Wrapping<u8>); | ||
pow_impl!(Wrapping<i8>); | ||
pow_impl!(Wrapping<u16>); | ||
|
@@ -130,18 +98,6 @@ pow_impl!(Wrapping<i128>); | |
pow_impl!(Wrapping<usize>); | ||
pow_impl!(Wrapping<isize>); | ||
|
||
// FIXME: these should be possible | ||
// pow_impl!(u8, u64); | ||
// pow_impl!(i16, u64); | ||
// pow_impl!(i8, u64); | ||
// pow_impl!(u16, u64); | ||
// pow_impl!(u32, u64); | ||
// pow_impl!(i32, u64); | ||
// pow_impl!(u64, u64); | ||
// pow_impl!(i64, u64); | ||
// pow_impl!(usize, u64); | ||
// pow_impl!(isize, u64); | ||
|
||
#[cfg(any(feature = "std", feature = "libm"))] | ||
mod float_impls { | ||
use super::Pow; | ||
|
@@ -171,29 +127,33 @@ mod float_impls { | |
/// ```rust | ||
/// use num_traits::pow; | ||
/// | ||
/// assert_eq!(pow(2i8, 4), 16); | ||
/// assert_eq!(pow(6u8, 3), 216); | ||
/// assert_eq!(pow(0u8, 0), 1); // Be aware if this case affects you | ||
/// assert_eq!(pow(2i8, 4u32), 16); | ||
/// assert_eq!(pow(6u8, 3u32), 216); | ||
/// assert_eq!(pow(0u8, 0u32), 1); // Be aware if this case affects you | ||
/// ``` | ||
#[inline] | ||
pub fn pow<T: Clone + One + Mul<T, Output = T>>(mut base: T, mut exp: usize) -> T { | ||
if exp == 0 { | ||
pub fn pow<T, U>(mut base: T, mut exp: U) -> T | ||
where | ||
T: Clone + One, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed |
||
U: PrimInt + Unsigned, | ||
{ | ||
if exp == U::zero() { | ||
return T::one(); | ||
} | ||
|
||
while exp & 1 == 0 { | ||
while exp & U::one() == U::zero() { | ||
base = base.clone() * base; | ||
exp >>= 1; | ||
exp = exp >> 1; | ||
} | ||
if exp == 1 { | ||
if exp == U::one() { | ||
return base; | ||
} | ||
|
||
let mut acc = base.clone(); | ||
while exp > 1 { | ||
exp >>= 1; | ||
while exp > U::one() { | ||
exp = exp >> 1; | ||
base = base.clone() * base; | ||
if exp & 1 == 1 { | ||
if exp & U::one() == U::one() { | ||
acc = acc * base.clone(); | ||
} | ||
} | ||
|
@@ -211,30 +171,34 @@ pub fn pow<T: Clone + One + Mul<T, Output = T>>(mut base: T, mut exp: usize) -> | |
/// ```rust | ||
/// use num_traits::checked_pow; | ||
/// | ||
/// assert_eq!(checked_pow(2i8, 4), Some(16)); | ||
/// assert_eq!(checked_pow(7i8, 8), None); | ||
/// assert_eq!(checked_pow(7u32, 8), Some(5_764_801)); | ||
/// assert_eq!(checked_pow(0u32, 0), Some(1)); // Be aware if this case affect you | ||
/// assert_eq!(checked_pow(2i8, 4u32), Some(16)); | ||
/// assert_eq!(checked_pow(7i8, 8u32), None); | ||
/// assert_eq!(checked_pow(7u32, 8u32), Some(5_764_801)); | ||
/// assert_eq!(checked_pow(0u32, 0u32), Some(1)); // Be aware if this case affect you | ||
/// ``` | ||
#[inline] | ||
pub fn checked_pow<T: Clone + One + CheckedMul>(mut base: T, mut exp: usize) -> Option<T> { | ||
if exp == 0 { | ||
pub fn checked_pow<T, U>(mut base: T, mut exp: U) -> Option<T> | ||
where | ||
T: Clone + One + CheckedMul, | ||
U: PrimInt + Unsigned, | ||
{ | ||
if exp == U::zero() { | ||
return Some(T::one()); | ||
} | ||
|
||
while exp & 1 == 0 { | ||
while exp & U::one() == U::zero() { | ||
base = base.checked_mul(&base)?; | ||
exp >>= 1; | ||
exp = exp >> 1; | ||
} | ||
if exp == 1 { | ||
if exp == U::one() { | ||
return Some(base); | ||
} | ||
|
||
let mut acc = base.clone(); | ||
while exp > 1 { | ||
exp >>= 1; | ||
while exp > U::one() { | ||
exp = exp >> 1; | ||
base = base.checked_mul(&base)?; | ||
if exp & 1 == 1 { | ||
if exp & U::one() == U::one() { | ||
acc = acc.checked_mul(&base)?; | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Utilizes the default implementation of pow for exp with u32