pub trait Pow<Exp = Self> {
type Output;
fn pow(self, exponent: Exp) -> Self::Output;
}
#[cfg(any(feature = "std", feature = "libm"))]
mod float_impls {
use super::Pow;
#[inline]
fn powf_f32(base: f32, exp: f32) -> f32 {
#[cfg(feature = "libm")]
{
libm::powf(base, exp)
}
#[cfg(not(feature = "libm"))]
{
f32::powf(base, exp)
}
}
#[inline]
fn powf_f64(base: f64, exp: f64) -> f64 {
#[cfg(feature = "libm")]
{
libm::pow(base, exp)
}
#[cfg(not(feature = "libm"))]
{
f64::powf(base, exp)
}
}
#[inline]
fn powi_f32(base: f32, exp: i32) -> f32 {
#[cfg(feature = "libm")]
{
powf_f32(base, exp as f32)
}
#[cfg(not(feature = "libm"))]
{
f32::powi(base, exp)
}
}
#[inline]
fn powi_f64(base: f64, exp: i32) -> f64 {
#[cfg(feature = "libm")]
{
powf_f64(base, exp as f64)
}
#[cfg(not(feature = "libm"))]
{
f64::powi(base, exp)
}
}
macro_rules! impl_pow_float_self {
($ty:ty, $fn:ident) => {
impl Pow<$ty> for $ty {
type Output = $ty;
#[inline]
fn pow(self, exponent: $ty) -> Self::Output {
$fn(self, exponent)
}
}
};
}
macro_rules! impl_pow_float_int {
($ty:ty, $fn:ident) => {
impl Pow<i32> for $ty {
type Output = $ty;
#[inline]
fn pow(self, exponent: i32) -> Self::Output {
$fn(self, exponent)
}
}
};
}
impl_pow_float_self!(f32, powf_f32);
impl_pow_float_self!(f64, powf_f64);
impl_pow_float_int!(f32, powi_f32);
impl_pow_float_int!(f64, powi_f64);
}
macro_rules! impl_pow_int {
($($ty:ty),+ $(,)?) => {
$(impl Pow<u32> for $ty {
type Output = $ty;
#[inline]
fn pow(self, exponent: u32) -> Self::Output {
<$ty>::pow(self, exponent)
}
})+
};
}
impl_pow_int!(i8, i16, i32, i64, i128, isize);
impl_pow_int!(u8, u16, u32, u64, u128, usize);