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::sdcomp26094Values; 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_EQ(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_EQ(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_EQ(errno, 0); 46 47 errno = 0; 48 __llvm_libc::sincosf(inf, &sin, &cos); 49 EXPECT_FP_EQ(aNaN, cos); 50 EXPECT_FP_EQ(aNaN, sin); 51 EXPECT_EQ(errno, EDOM); 52 53 errno = 0; 54 __llvm_libc::sincosf(negInf, &sin, &cos); 55 EXPECT_FP_EQ(aNaN, cos); 56 EXPECT_FP_EQ(aNaN, sin); 57 EXPECT_EQ(errno, EDOM); 58 } 59 60 TEST(LlvmLibcSinCosfTest, InFloatRange) { 61 constexpr uint32_t count = 1000000; 62 constexpr uint32_t step = UINT32_MAX / count; 63 for (uint32_t i = 0, v = 0; i <= count; ++i, v += step) { 64 float x = float(FPBits((v))); 65 if (isnan(x) || isinf(x)) 66 continue; 67 68 float sin, cos; 69 __llvm_libc::sincosf(x, &sin, &cos); 70 ASSERT_MPFR_MATCH(mpfr::Operation::Cos, x, cos, 1.0); 71 ASSERT_MPFR_MATCH(mpfr::Operation::Sin, x, sin, 1.0); 72 } 73 } 74 75 // For small values, cos(x) is 1 and sin(x) is x. 76 TEST(LlvmLibcSinCosfTest, SmallValues) { 77 uint32_t bits = 0x17800000; 78 float x = float(FPBits((bits))); 79 float result_cos, result_sin; 80 __llvm_libc::sincosf(x, &result_sin, &result_cos); 81 EXPECT_MPFR_MATCH(mpfr::Operation::Cos, x, result_cos, 1.0); 82 EXPECT_MPFR_MATCH(mpfr::Operation::Sin, x, result_sin, 1.0); 83 EXPECT_FP_EQ(1.0f, result_cos); 84 EXPECT_FP_EQ(x, result_sin); 85 86 bits = 0x00400000; 87 x = float(FPBits((bits))); 88 __llvm_libc::sincosf(x, &result_sin, &result_cos); 89 EXPECT_MPFR_MATCH(mpfr::Operation::Cos, x, result_cos, 1.0); 90 EXPECT_MPFR_MATCH(mpfr::Operation::Sin, x, result_sin, 1.0); 91 EXPECT_FP_EQ(1.0f, result_cos); 92 EXPECT_FP_EQ(x, result_sin); 93 } 94 95 // SDCOMP-26094: check sinf in the cases for which the range reducer 96 // returns values furthest beyond its nominal upper bound of pi/4. 97 TEST(LlvmLibcSinCosfTest, SDCOMP_26094) { 98 for (uint32_t v : sdcomp26094Values) { 99 float x = float(FPBits((v))); 100 float sin, cos; 101 __llvm_libc::sincosf(x, &sin, &cos); 102 EXPECT_MPFR_MATCH(mpfr::Operation::Cos, x, cos, 1.0); 103 EXPECT_MPFR_MATCH(mpfr::Operation::Sin, x, sin, 1.0); 104 } 105 } 106