xref: /aosp_15_r20/external/llvm-libc/test/src/math/exhaustive/sincosf_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Exhaustive test for sincosf ---------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "exhaustive_test.h"
10 #include "src/math/sincosf.h"
11 #include "utils/MPFRWrapper/MPFRUtils.h"
12 
13 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
14 
15 struct SincosfChecker : public virtual LIBC_NAMESPACE::testing::Test {
16   using FloatType = float;
17   using FPBits = LIBC_NAMESPACE::fputil::FPBits<float>;
18   using StorageType = uint32_t;
19 
checkSincosfChecker20   uint64_t check(StorageType start, StorageType stop,
21                  mpfr::RoundingMode rounding) {
22     mpfr::ForceRoundingMode r(rounding);
23     if (!r.success)
24       return (stop > start);
25     StorageType bits = start;
26     uint64_t failed = 0;
27     do {
28       FPBits xbits(bits);
29       FloatType x = xbits.get_val();
30       FloatType sinx, cosx;
31       LIBC_NAMESPACE::sincosf(x, &sinx, &cosx);
32 
33       bool correct = TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Sin, x,
34                                                        sinx, 0.5, rounding);
35       correct = correct && TEST_MPFR_MATCH_ROUNDING_SILENTLY(
36                                mpfr::Operation::Cos, x, cosx, 0.5, rounding);
37       failed += (!correct);
38       // Uncomment to print out failed values.
39       // if (!correct) {
40       //   TEST_MPFR_MATCH(mpfr::Operation::Sin, x, sinx, 0.5, rounding);
41       //   TEST_MPFR_MATCH(mpfr::Operation::Cos, x, cosx, 0.5, rounding);
42       // }
43     } while (bits++ < stop);
44     return failed;
45   }
46 };
47 
48 using LlvmLibcSincosfExhaustiveTest =
49     LlvmLibcExhaustiveMathTest<SincosfChecker>;
50 
51 // Range: [0, Inf];
52 static constexpr uint32_t POS_START = 0x0000'0000U;
53 static constexpr uint32_t POS_STOP = 0x7f80'0000U;
54 
TEST_F(LlvmLibcSincosfExhaustiveTest,PostiveRange)55 TEST_F(LlvmLibcSincosfExhaustiveTest, PostiveRange) {
56   test_full_range_all_roundings(POS_START, POS_STOP);
57 }
58 
59 // Range: [-1, 0];
60 static constexpr uint32_t NEG_START = 0xb000'0000U;
61 static constexpr uint32_t NEG_STOP = 0xbf7f'ffffU;
62 
TEST_F(LlvmLibcSincosfExhaustiveTest,NegativeRange)63 TEST_F(LlvmLibcSincosfExhaustiveTest, NegativeRange) {
64   test_full_range_all_roundings(NEG_START, NEG_STOP);
65 }
66