libm/
lib.rs

1//! libm in pure Rust
2#![deny(warnings)]
3#![no_std]
4#![cfg_attr(all(feature = "unstable"), feature(core_intrinsics))]
5#![allow(clippy::unreadable_literal)]
6#![allow(clippy::many_single_char_names)]
7#![allow(clippy::needless_return)]
8#![allow(clippy::int_plus_one)]
9#![allow(clippy::deprecated_cfg_attr)]
10#![allow(clippy::mixed_case_hex_literals)]
11#![allow(clippy::float_cmp)]
12#![allow(clippy::eq_op)]
13#![allow(clippy::assign_op_pattern)]
14
15mod math;
16
17use core::{f32, f64};
18
19pub use self::math::*;
20
21/// Approximate equality with 1 ULP of tolerance
22#[doc(hidden)]
23#[inline]
24pub fn _eqf(a: f32, b: f32) -> Result<(), u32> {
25    if a.is_nan() && b.is_nan() {
26        Ok(())
27    } else {
28        let err = (a.to_bits() as i32).wrapping_sub(b.to_bits() as i32).abs();
29
30        if err <= 1 {
31            Ok(())
32        } else {
33            Err(err as u32)
34        }
35    }
36}
37
38#[doc(hidden)]
39#[inline]
40pub fn _eq(a: f64, b: f64) -> Result<(), u64> {
41    if a.is_nan() && b.is_nan() {
42        Ok(())
43    } else {
44        let err = (a.to_bits() as i64).wrapping_sub(b.to_bits() as i64).abs();
45
46        if err <= 1 {
47            Ok(())
48        } else {
49            Err(err as u64)
50        }
51    }
52}
53
54// PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520
55#[cfg(not(target_arch = "powerpc64"))]
56#[cfg(all(test, feature = "musl-reference-tests"))]
57include!(concat!(env!("OUT_DIR"), "/musl-tests.rs"));