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 "include/errno.h" 10 #include "include/math.h" 11 #include "src/errno/llvmlibc_errno.h" 12 #include "src/math/cosf.h" 13 #include "test/src/math/sdcomp26094.h" 14 #include "utils/CPP/Array.h" 15 #include "utils/FPUtil/BitPatterns.h" 16 #include "utils/FPUtil/ClassificationFunctions.h" 17 #include "utils/FPUtil/FloatOperations.h" 18 #include "utils/FPUtil/FloatProperties.h" 19 #include "utils/MPFRWrapper/MPFRUtils.h" 20 #include "utils/UnitTest/Test.h" 21 22 #include <stdint.h> 23 24 using __llvm_libc::fputil::isNegativeQuietNaN; 25 using __llvm_libc::fputil::isQuietNaN; 26 using __llvm_libc::fputil::valueAsBits; 27 using __llvm_libc::fputil::valueFromBits; 28 29 using BitPatterns = __llvm_libc::fputil::BitPatterns<float>; 30 31 using __llvm_libc::testing::sdcomp26094Values; 32 33 namespace mpfr = __llvm_libc::testing::mpfr; 34 35 // 12 additional bits of precision over the base precision of a |float| 36 // value. 37 static constexpr mpfr::Tolerance tolerance{mpfr::Tolerance::floatPrecision, 12, 38 3 * 0x1000 / 4}; 39 40 TEST(CosfTest, SpecialNumbers) { 41 llvmlibc_errno = 0; 42 43 EXPECT_TRUE( 44 isQuietNaN(__llvm_libc::cosf(valueFromBits(BitPatterns::aQuietNaN)))); 45 EXPECT_EQ(llvmlibc_errno, 0); 46 47 EXPECT_TRUE(isNegativeQuietNaN( 48 __llvm_libc::cosf(valueFromBits(BitPatterns::aNegativeQuietNaN)))); 49 EXPECT_EQ(llvmlibc_errno, 0); 50 51 EXPECT_TRUE(isQuietNaN( 52 __llvm_libc::cosf(valueFromBits(BitPatterns::aSignallingNaN)))); 53 EXPECT_EQ(llvmlibc_errno, 0); 54 55 EXPECT_TRUE(isNegativeQuietNaN( 56 __llvm_libc::cosf(valueFromBits(BitPatterns::aNegativeSignallingNaN)))); 57 EXPECT_EQ(llvmlibc_errno, 0); 58 59 EXPECT_EQ(BitPatterns::one, 60 valueAsBits(__llvm_libc::cosf(valueFromBits(BitPatterns::zero)))); 61 EXPECT_EQ(llvmlibc_errno, 0); 62 63 EXPECT_EQ(BitPatterns::one, valueAsBits(__llvm_libc::cosf( 64 valueFromBits(BitPatterns::negZero)))); 65 EXPECT_EQ(llvmlibc_errno, 0); 66 67 llvmlibc_errno = 0; 68 EXPECT_TRUE(isQuietNaN(__llvm_libc::cosf(valueFromBits(BitPatterns::inf)))); 69 EXPECT_EQ(llvmlibc_errno, EDOM); 70 71 llvmlibc_errno = 0; 72 EXPECT_TRUE( 73 isQuietNaN(__llvm_libc::cosf(valueFromBits(BitPatterns::negInf)))); 74 EXPECT_EQ(llvmlibc_errno, EDOM); 75 } 76 77 TEST(CosfTest, InFloatRange) { 78 constexpr uint32_t count = 1000000; 79 constexpr uint32_t step = UINT32_MAX / count; 80 for (uint32_t i = 0, v = 0; i <= count; ++i, v += step) { 81 float x = valueFromBits(v); 82 if (isnan(x) || isinf(x)) 83 continue; 84 ASSERT_MPFR_MATCH(mpfr::Operation::Cos, x, __llvm_libc::cosf(x), tolerance); 85 } 86 } 87 88 // For small values, cos(x) is 1. 89 TEST(CosfTest, SmallValues) { 90 float x = valueFromBits(0x17800000U); 91 float result = __llvm_libc::cosf(x); 92 EXPECT_MPFR_MATCH(mpfr::Operation::Cos, x, result, tolerance); 93 EXPECT_EQ(BitPatterns::one, valueAsBits(result)); 94 95 x = valueFromBits(0x0040000U); 96 result = __llvm_libc::cosf(x); 97 EXPECT_MPFR_MATCH(mpfr::Operation::Cos, x, result, tolerance); 98 EXPECT_EQ(BitPatterns::one, valueAsBits(result)); 99 } 100 101 // SDCOMP-26094: check cosf in the cases for which the range reducer 102 // returns values furthest beyond its nominal upper bound of pi/4. 103 TEST(CosfTest, SDCOMP_26094) { 104 for (uint32_t v : sdcomp26094Values) { 105 float x = valueFromBits(v); 106 ASSERT_MPFR_MATCH(mpfr::Operation::Cos, x, __llvm_libc::cosf(x), tolerance); 107 } 108 } 109