1 //===-- Unittests for cosf ------------------------------------------------===// 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/cosf.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(LlvmLibcCosfTest, SpecialNumbers) { 29 errno = 0; 30 31 EXPECT_FP_EQ(aNaN, __llvm_libc::cosf(aNaN)); 32 EXPECT_MATH_ERRNO(0); 33 34 EXPECT_FP_EQ(1.0f, __llvm_libc::cosf(0.0f)); 35 EXPECT_MATH_ERRNO(0); 36 37 EXPECT_FP_EQ(1.0f, __llvm_libc::cosf(-0.0f)); 38 EXPECT_MATH_ERRNO(0); 39 40 EXPECT_FP_EQ(aNaN, __llvm_libc::cosf(inf)); 41 EXPECT_MATH_ERRNO(EDOM); 42 43 EXPECT_FP_EQ(aNaN, __llvm_libc::cosf(neg_inf)); 44 EXPECT_MATH_ERRNO(EDOM); 45 } 46 47 TEST(LlvmLibcCosfTest, InFloatRange) { 48 constexpr uint32_t COUNT = 1000000; 49 constexpr uint32_t STEP = UINT32_MAX / COUNT; 50 for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) { 51 float x = float(FPBits(v)); 52 if (isnan(x) || isinf(x)) 53 continue; 54 ASSERT_MPFR_MATCH(mpfr::Operation::Cos, x, __llvm_libc::cosf(x), 1.0); 55 } 56 } 57 58 // For small values, cos(x) is 1. 59 TEST(LlvmLibcCosfTest, SmallValues) { 60 float x = float(FPBits(0x17800000U)); 61 float result = __llvm_libc::cosf(x); 62 EXPECT_MPFR_MATCH(mpfr::Operation::Cos, x, result, 1.0); 63 EXPECT_FP_EQ(1.0f, result); 64 65 x = float(FPBits(0x0040000U)); 66 result = __llvm_libc::cosf(x); 67 EXPECT_MPFR_MATCH(mpfr::Operation::Cos, x, result, 1.0); 68 EXPECT_FP_EQ(1.0f, result); 69 } 70 71 // SDCOMP-26094: check cosf in the cases for which the range reducer 72 // returns values furthest beyond its nominal upper bound of pi/4. 73 TEST(LlvmLibcCosfTest, SDCOMP_26094) { 74 for (uint32_t v : SDCOMP26094_VALUES) { 75 float x = float(FPBits(v)); 76 ASSERT_MPFR_MATCH(mpfr::Operation::Cos, x, __llvm_libc::cosf(x), 1.0); 77 } 78 } 79