A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.rs/num-traits/0.2.19/x86_64-unknown-linux-gnu/src/num_traits/pow.rs.html below:

pow.rs - source

1use crate::{CheckedMul, One};
2use core::num::Wrapping;
3use core::ops::Mul;
4
5pub trait Pow<RHS> {
7    type Output;
9
10    fn pow(self, rhs: RHS) -> Self::Output;
19}
20
21macro_rules! pow_impl {
22    ($t:ty) => {
23        pow_impl!($t, u8);
24        pow_impl!($t, usize);
25
26        };
31    ($t:ty, $rhs:ty) => {
32        pow_impl!($t, $rhs, usize, pow);
33    };
34    ($t:ty, $rhs:ty, $desired_rhs:ty, $method:expr) => {
35        impl Pow<$rhs> for $t {
36            type Output = $t;
37            #[inline]
38            fn pow(self, rhs: $rhs) -> $t {
39                ($method)(self, <$desired_rhs>::from(rhs))
40            }
41        }
42
43        impl<'a> Pow<&'a $rhs> for $t {
44            type Output = $t;
45            #[inline]
46            fn pow(self, rhs: &'a $rhs) -> $t {
47                ($method)(self, <$desired_rhs>::from(*rhs))
48            }
49        }
50
51        impl<'a> Pow<$rhs> for &'a $t {
52            type Output = $t;
53            #[inline]
54            fn pow(self, rhs: $rhs) -> $t {
55                ($method)(*self, <$desired_rhs>::from(rhs))
56            }
57        }
58
59        impl<'a, 'b> Pow<&'a $rhs> for &'b $t {
60            type Output = $t;
61            #[inline]
62            fn pow(self, rhs: &'a $rhs) -> $t {
63                ($method)(*self, <$desired_rhs>::from(*rhs))
64            }
65        }
66    };
67}
68
69pow_impl!(u8, u8, u32, u8::pow);
70pow_impl!(u8, u16, u32, u8::pow);
71pow_impl!(u8, u32, u32, u8::pow);
72pow_impl!(u8, usize);
73pow_impl!(i8, u8, u32, i8::pow);
74pow_impl!(i8, u16, u32, i8::pow);
75pow_impl!(i8, u32, u32, i8::pow);
76pow_impl!(i8, usize);
77pow_impl!(u16, u8, u32, u16::pow);
78pow_impl!(u16, u16, u32, u16::pow);
79pow_impl!(u16, u32, u32, u16::pow);
80pow_impl!(u16, usize);
81pow_impl!(i16, u8, u32, i16::pow);
82pow_impl!(i16, u16, u32, i16::pow);
83pow_impl!(i16, u32, u32, i16::pow);
84pow_impl!(i16, usize);
85pow_impl!(u32, u8, u32, u32::pow);
86pow_impl!(u32, u16, u32, u32::pow);
87pow_impl!(u32, u32, u32, u32::pow);
88pow_impl!(u32, usize);
89pow_impl!(i32, u8, u32, i32::pow);
90pow_impl!(i32, u16, u32, i32::pow);
91pow_impl!(i32, u32, u32, i32::pow);
92pow_impl!(i32, usize);
93pow_impl!(u64, u8, u32, u64::pow);
94pow_impl!(u64, u16, u32, u64::pow);
95pow_impl!(u64, u32, u32, u64::pow);
96pow_impl!(u64, usize);
97pow_impl!(i64, u8, u32, i64::pow);
98pow_impl!(i64, u16, u32, i64::pow);
99pow_impl!(i64, u32, u32, i64::pow);
100pow_impl!(i64, usize);
101
102pow_impl!(u128, u8, u32, u128::pow);
103pow_impl!(u128, u16, u32, u128::pow);
104pow_impl!(u128, u32, u32, u128::pow);
105pow_impl!(u128, usize);
106
107pow_impl!(i128, u8, u32, i128::pow);
108pow_impl!(i128, u16, u32, i128::pow);
109pow_impl!(i128, u32, u32, i128::pow);
110pow_impl!(i128, usize);
111
112pow_impl!(usize, u8, u32, usize::pow);
113pow_impl!(usize, u16, u32, usize::pow);
114pow_impl!(usize, u32, u32, usize::pow);
115pow_impl!(usize, usize);
116pow_impl!(isize, u8, u32, isize::pow);
117pow_impl!(isize, u16, u32, isize::pow);
118pow_impl!(isize, u32, u32, isize::pow);
119pow_impl!(isize, usize);
120pow_impl!(Wrapping<u8>);
121pow_impl!(Wrapping<i8>);
122pow_impl!(Wrapping<u16>);
123pow_impl!(Wrapping<i16>);
124pow_impl!(Wrapping<u32>);
125pow_impl!(Wrapping<i32>);
126pow_impl!(Wrapping<u64>);
127pow_impl!(Wrapping<i64>);
128pow_impl!(Wrapping<u128>);
129pow_impl!(Wrapping<i128>);
130pow_impl!(Wrapping<usize>);
131pow_impl!(Wrapping<isize>);
132
133#[cfg(any(feature = "std", feature = "libm"))]
146mod float_impls {
147    use super::Pow;
148    use crate::Float;
149
150    pow_impl!(f32, i8, i32, <f32 as Float>::powi);
151    pow_impl!(f32, u8, i32, <f32 as Float>::powi);
152    pow_impl!(f32, i16, i32, <f32 as Float>::powi);
153    pow_impl!(f32, u16, i32, <f32 as Float>::powi);
154    pow_impl!(f32, i32, i32, <f32 as Float>::powi);
155    pow_impl!(f64, i8, i32, <f64 as Float>::powi);
156    pow_impl!(f64, u8, i32, <f64 as Float>::powi);
157    pow_impl!(f64, i16, i32, <f64 as Float>::powi);
158    pow_impl!(f64, u16, i32, <f64 as Float>::powi);
159    pow_impl!(f64, i32, i32, <f64 as Float>::powi);
160    pow_impl!(f32, f32, f32, <f32 as Float>::powf);
161    pow_impl!(f64, f32, f64, <f64 as Float>::powf);
162    pow_impl!(f64, f64, f64, <f64 as Float>::powf);
163}
164
165#[inline]
179pub fn pow<T: Clone + One + Mul<T, Output = T>>(mut base: T, mut exp: usize) -> T {
180    if exp == 0 {
181        return T::one();
182    }
183
184    while exp & 1 == 0 {
185        base = base.clone() * base;
186        exp >>= 1;
187    }
188    if exp == 1 {
189        return base;
190    }
191
192    let mut acc = base.clone();
193    while exp > 1 {
194        exp >>= 1;
195        base = base.clone() * base;
196        if exp & 1 == 1 {
197            acc = acc * base.clone();
198        }
199    }
200    acc
201}
202
203#[inline]
220pub fn checked_pow<T: Clone + One + CheckedMul>(mut base: T, mut exp: usize) -> Option<T> {
221    if exp == 0 {
222        return Some(T::one());
223    }
224
225    while exp & 1 == 0 {
226        base = base.checked_mul(&base)?;
227        exp >>= 1;
228    }
229    if exp == 1 {
230        return Some(base);
231    }
232
233    let mut acc = base.clone();
234    while exp > 1 {
235        exp >>= 1;
236        base = base.checked_mul(&base)?;
237        if exp & 1 == 1 {
238            acc = acc.checked_mul(&base)?;
239        }
240    }
241    Some(acc)
242}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4