about summary refs log tree commit diff
path: root/library/compiler-builtins/libm-test/tests/compare_built_musl.rs
blob: 86f3b8b711ea7bd5238469c6c8f8a372e047dde1 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//! Compare our implementations with the result of musl functions, as provided by `musl-math-sys`.
//!
//! Currently this only tests randomized inputs. In the future this may be improved to test edge
//! cases or run exhaustive tests.
//!
//! Note that musl functions do not always provide 0.5ULP rounding, so our functions can do better
//! than these results.

// There are some targets we can't build musl for
#![cfg(feature = "build-musl")]

use libm_test::generate::{case_list, edge_cases, random, spaced};
use libm_test::{CheckBasis, CheckCtx, CheckOutput, GeneratorKind, MathOp, TupleCall};

const BASIS: CheckBasis = CheckBasis::Musl;

fn musl_runner<Op: MathOp>(
    ctx: &CheckCtx,
    cases: impl Iterator<Item = Op::RustArgs>,
    musl_fn: Op::CFn,
) {
    for input in cases {
        let musl_res = input.call(musl_fn);
        let crate_res = input.call_intercept_panics(Op::ROUTINE);

        crate_res.validate(musl_res, input, ctx).unwrap();
    }
}

/// Test against musl with generators from a domain.
macro_rules! musl_tests {
    (
        fn_name: $fn_name:ident,
        attrs: [$($attr:meta),*],
    ) => {
        paste::paste! {
            #[test]
            $(#[$attr])*
            fn [< musl_case_list_ $fn_name >]() {
                type Op = libm_test::op::$fn_name::Routine;
                let ctx = CheckCtx::new(Op::IDENTIFIER, BASIS, GeneratorKind::List);
                let cases = case_list::get_test_cases_basis::<Op>(&ctx).0;
                musl_runner::<Op>(&ctx, cases, musl_math_sys::$fn_name);
            }

            #[test]
            $(#[$attr])*
            fn [< musl_random_ $fn_name >]() {
                type Op = libm_test::op::$fn_name::Routine;
                let ctx = CheckCtx::new(Op::IDENTIFIER, BASIS, GeneratorKind::Random);
                let cases = random::get_test_cases::<<Op as MathOp>::RustArgs>(&ctx).0;
                musl_runner::<Op>(&ctx, cases, musl_math_sys::$fn_name);
            }

            #[test]
            $(#[$attr])*
            fn [< musl_edge_case_ $fn_name >]() {
                type Op = libm_test::op::$fn_name::Routine;
                let ctx = CheckCtx::new(Op::IDENTIFIER, BASIS, GeneratorKind::EdgeCases);
                let cases = edge_cases::get_test_cases::<Op>(&ctx).0;
                musl_runner::<Op>(&ctx, cases, musl_math_sys::$fn_name);
            }

            #[test]
            $(#[$attr])*
            fn [< musl_quickspace_ $fn_name >]() {
                type Op = libm_test::op::$fn_name::Routine;
                let ctx = CheckCtx::new(Op::IDENTIFIER, BASIS, GeneratorKind::Spaced);
                let cases = spaced::get_test_cases::<Op>(&ctx).0;
                musl_runner::<Op>(&ctx, cases, musl_math_sys::$fn_name);
            }
        }
    };
}

libm_macros::for_each_function! {
    callback: musl_tests,
    attributes: [],
    // Not provided by musl
    skip_f16_f128: true,
    skip: [
        // TODO integer inputs
        jn,
        jnf,
        ldexp,
        ldexpf,
        scalbn,
        scalbnf,
        yn,
        ynf,

        // Not provided by musl
        // verify-sorted-start
        fmaximum,
        fmaximum_num,
        fmaximum_numf,
        fmaximumf,
        fminimum,
        fminimum_num,
        fminimum_numf,
        fminimumf,
        roundeven,
        roundevenf,
        // // verify-sorted-end
    ],
}