1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/// Square root helper.
pub trait Sqrt: Sized {
/// Returns the square root of `self`.
///
/// Implementations should document domain requirements (e.g., non-negative
/// inputs) and how they signal invalid inputs.
fn sqrt(self) -> Self;
}
#[cfg(any(feature = "std", feature = "libm"))]
mod float_impls {
use super::Sqrt;
#[inline]
fn sqrt_f32(x: f32) -> f32 {
#[cfg(feature = "libm")]
{
libm::sqrtf(x)
}
#[cfg(not(feature = "libm"))]
{
f32::sqrt(x)
}
}
#[inline]
fn sqrt_f64(x: f64) -> f64 {
#[cfg(feature = "libm")]
{
libm::sqrt(x)
}
#[cfg(not(feature = "libm"))]
{
f64::sqrt(x)
}
}
macro_rules! impl_sqrt_for_float {
($ty:ty, $fn:ident) => {
impl Sqrt for $ty {
fn sqrt(self) -> Self {
$fn(self)
}
}
};
}
impl_sqrt_for_float!(f32, sqrt_f32);
impl_sqrt_for_float!(f64, sqrt_f64);
}