1 //===-- Unittests 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 "src/__support/CPP/Array.h" 10 #include "src/__support/FPUtil/FPBits.h" 11 #include "src/math/sincosf.h" 12 #include "test/src/math/sdcomp26094.h" 13 #include "utils/MPFRWrapper/MPFRUtils.h" 14 #include "utils/UnitTest/FPMatcher.h" 15 #include "utils/UnitTest/Test.h" 16 #include <math.h> 17 18 #include <errno.h> 19 #include <stdint.h> 20 21 using __llvm_libc::testing::SDCOMP26094_VALUES; 22 using FPBits = __llvm_libc::fputil::FPBits<float>; 23 24 namespace mpfr = __llvm_libc::testing::mpfr; 25 26 DECLARE_SPECIAL_CONSTANTS(float) 27 28 TEST(LlvmLibcSinCosfTest, SpecialNumbers) { 29 errno = 0; 30 float sin, cos; 31 32 __llvm_libc::sincosf(aNaN, &sin, &cos); 33 EXPECT_FP_EQ(aNaN, cos); 34 EXPECT_FP_EQ(aNaN, sin); 35 EXPECT_MATH_ERRNO(0); 36 37 __llvm_libc::sincosf(0.0f, &sin, &cos); 38 EXPECT_FP_EQ(1.0f, cos); 39 EXPECT_FP_EQ(0.0f, sin); 40 EXPECT_MATH_ERRNO(0); 41 42 __llvm_libc::sincosf(-0.0f, &sin, &cos); 43 EXPECT_FP_EQ(1.0f, cos); 44 EXPECT_FP_EQ(-0.0f, sin); 45 EXPECT_MATH_ERRNO(0); 46 47 __llvm_libc::sincosf(inf, &sin, &cos); 48 EXPECT_FP_EQ(aNaN, cos); 49 EXPECT_FP_EQ(aNaN, sin); 50 EXPECT_MATH_ERRNO(EDOM); 51 52 __llvm_libc::sincosf(neg_inf, &sin, &cos); 53 EXPECT_FP_EQ(aNaN, cos); 54 EXPECT_FP_EQ(aNaN, sin); 55 EXPECT_MATH_ERRNO(EDOM); 56 } 57 58 TEST(LlvmLibcSinCosfTest, InFloatRange) { 59 constexpr uint32_t COUNT = 1000000; 60 constexpr uint32_t STEP = UINT32_MAX / COUNT; 61 for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) { 62 float x = float(FPBits((v))); 63 if (isnan(x) || isinf(x)) 64 continue; 65 66 float sin, cos; 67 __llvm_libc::sincosf(x, &sin, &cos); 68 ASSERT_MPFR_MATCH(mpfr::Operation::Cos, x, cos, 1.0); 69 ASSERT_MPFR_MATCH(mpfr::Operation::Sin, x, sin, 1.0); 70 } 71 } 72 73 // For small values, cos(x) is 1 and sin(x) is x. 74 TEST(LlvmLibcSinCosfTest, SmallValues) { 75 uint32_t bits = 0x17800000; 76 float x = float(FPBits((bits))); 77 float result_cos, result_sin; 78 __llvm_libc::sincosf(x, &result_sin, &result_cos); 79 EXPECT_MPFR_MATCH(mpfr::Operation::Cos, x, result_cos, 1.0); 80 EXPECT_MPFR_MATCH(mpfr::Operation::Sin, x, result_sin, 1.0); 81 EXPECT_FP_EQ(1.0f, result_cos); 82 EXPECT_FP_EQ(x, result_sin); 83 84 bits = 0x00400000; 85 x = float(FPBits((bits))); 86 __llvm_libc::sincosf(x, &result_sin, &result_cos); 87 EXPECT_MPFR_MATCH(mpfr::Operation::Cos, x, result_cos, 1.0); 88 EXPECT_MPFR_MATCH(mpfr::Operation::Sin, x, result_sin, 1.0); 89 EXPECT_FP_EQ(1.0f, result_cos); 90 EXPECT_FP_EQ(x, result_sin); 91 } 92 93 // SDCOMP-26094: check sinf in the cases for which the range reducer 94 // returns values furthest beyond its nominal upper bound of pi/4. 95 TEST(LlvmLibcSinCosfTest, SDCOMP_26094) { 96 for (uint32_t v : SDCOMP26094_VALUES) { 97 float x = float(FPBits((v))); 98 float sin, cos; 99 __llvm_libc::sincosf(x, &sin, &cos); 100 EXPECT_MPFR_MATCH(mpfr::Operation::Cos, x, cos, 1.0); 101 EXPECT_MPFR_MATCH(mpfr::Operation::Sin, x, sin, 1.0); 102 } 103 } 104