macro_rules! check_finite {
($val:expr) => {
if !$val.is_finite() {
return Err(if $val.is_nan() {
crate::DataError::NotANumber
} else {
crate::DataError::Infinite
});
}
};
}
#[cfg(any(feature = "std", feature = "libm"))]
#[inline]
pub(crate) fn sqrt(x: f64) -> f64 {
#[cfg(feature = "std")]
{
x.sqrt()
}
#[cfg(all(not(feature = "std"), feature = "libm"))]
{
libm::sqrt(x)
}
}
#[cfg(any(feature = "std", feature = "libm"))]
#[inline]
pub(crate) fn exp(x: f64) -> f64 {
#[cfg(feature = "std")]
{
x.exp()
}
#[cfg(all(not(feature = "std"), feature = "libm"))]
{
libm::exp(x)
}
}
#[cfg(any(feature = "std", feature = "libm"))]
#[inline]
pub(crate) fn ln(x: f64) -> f64 {
#[cfg(feature = "std")]
{
x.ln()
}
#[cfg(all(not(feature = "std"), feature = "libm"))]
{
libm::log(x)
}
}
pub(crate) trait MulAdd {
fn fma(self, b: Self, c: Self) -> Self;
}
impl MulAdd for f64 {
#[inline]
fn fma(self, b: f64, c: f64) -> f64 {
#[cfg(feature = "std")]
{
self.mul_add(b, c)
}
#[cfg(all(not(feature = "std"), feature = "libm"))]
{
libm::fma(self, b, c)
}
#[cfg(not(any(feature = "std", feature = "libm")))]
{
self * b + c
}
}
}
impl MulAdd for f32 {
#[inline]
fn fma(self, b: f32, c: f32) -> f32 {
#[cfg(feature = "std")]
{
self.mul_add(b, c)
}
#[cfg(all(not(feature = "std"), feature = "libm"))]
{
libm::fmaf(self, b, c)
}
#[cfg(not(any(feature = "std", feature = "libm")))]
{
self * b + c
}
}
}