127 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "hdr/errno_macros.h"10#include "hdr/math_macros.h"11#include "hdr/stdint_proxy.h"12#include "src/__support/FPUtil/FPBits.h"13#include "src/__support/macros/optimization.h"14#include "src/math/cosf.h"15#include "test/UnitTest/FPMatcher.h"16#include "test/UnitTest/Test.h"17#include "test/src/math/sdcomp26094.h"18#include "utils/MPFRWrapper/MPFRUtils.h"19 20#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS21#define TOLERANCE 322#define FP_ASSERT ASSERT_MPFR_MATCH23#else24#define TOLERANCE 025#define FP_ASSERT ASSERT_MPFR_MATCH_ALL_ROUNDING26#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS27 28using LIBC_NAMESPACE::testing::SDCOMP26094_VALUES;29using LlvmLibcCosfTest = LIBC_NAMESPACE::testing::FPTest<float>;30 31namespace mpfr = LIBC_NAMESPACE::testing::mpfr;32 33TEST_F(LlvmLibcCosfTest, SpecialNumbers) {34 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::cosf(aNaN));35 EXPECT_MATH_ERRNO(0);36 37 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::cosf(0.0f));38 EXPECT_MATH_ERRNO(0);39 40 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::cosf(-0.0f));41 EXPECT_MATH_ERRNO(0);42 43 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::cosf(inf));44 EXPECT_MATH_ERRNO(EDOM);45 46 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::cosf(neg_inf));47 EXPECT_MATH_ERRNO(EDOM);48}49 50TEST_F(LlvmLibcCosfTest, InFloatRange) {51 constexpr uint32_t COUNT = 100'000;52 constexpr uint32_t STEP = UINT32_MAX / COUNT;53 for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {54 float x = FPBits(v).get_val();55 if (FPBits(v).is_nan() || FPBits(v).is_inf())56 continue;57 FP_ASSERT(mpfr::Operation::Cos, x, LIBC_NAMESPACE::cosf(x),58 TOLERANCE + 0.5);59 }60}61 62TEST_F(LlvmLibcCosfTest, SpecificBitPatterns) {63 constexpr int N = 42;64 constexpr uint32_t INPUTS[N] = {65 0x3f06'0a92U, // x = pi/666 0x3f3a'dc51U, // x = 0x1.75b8a2p-1f67 0x3f49'0fdbU, // x = pi/468 0x3f86'0a92U, // x = pi/369 0x3fa7'832aU, // x = 0x1.4f0654p+0f70 0x3fc9'0fdbU, // x = pi/271 0x4017'1973U, // x = 0x1.2e32e6p+1f72 0x4049'0fdbU, // x = pi73 0x4096'cbe4U, // x = 0x1.2d97c8p+2f74 0x40c9'0fdbU, // x = 2*pi75 0x433b'7490U, // x = 0x1.76e92p+7f76 0x437c'e5f1U, // x = 0x1.f9cbe2p+7f77 0x4619'9998U, // x = 0x1.33333p+13f78 0x474d'246fU, // x = 0x1.9a48dep+15f79 0x4afd'ece4U, // x = 0x1.fbd9c8p+22f80 0x4c23'32e9U, // x = 0x1.4665d2p+25f81 0x50a3'e87fU, // x = 0x1.47d0fep+34f82 0x5239'47f6U, // x = 0x1.728fecp+37f83 0x53b1'46a6U, // x = 0x1.628d4cp+40f84 0x5532'5019U, // x = 0x1.64a032p+43f85 0x55ca'fb2aU, // x = 0x1.95f654p+44f86 0x588e'f060U, // x = 0x1.1de0cp+50f87 0x5922'aa80U, // x = 0x1.4555p+51f88 0x5aa4'542cU, // x = 0x1.48a858p+54f89 0x5c07'bcd0U, // x = 0x1.0f79ap+57f90 0x5ebc'fddeU, // x = 0x1.79fbbcp+62f91 0x5f18'b878U, // x = 0x1.3170fp+63f92 0x5fa6'eba7U, // x = 0x1.4dd74ep+64f93 0x6115'cb11U, // x = 0x1.2b9622p+67f94 0x61a4'0b40U, // x = 0x1.48168p+68f95 0x6386'134eU, // x = 0x1.0c269cp+72f96 0x6589'8498U, // x = 0x1.13093p+76f97 0x6600'0001U, // x = 0x1.000002p+77f98 0x664e'46e4U, // x = 0x1.9c8dc8p+77f99 0x66b0'14aaU, // x = 0x1.602954p+78f100 0x67a9'242bU, // x = 0x1.524856p+80f101 0x6a19'76f1U, // x = 0x1.32ede2p+85f102 0x6c55'da58U, // x = 0x1.abb4bp+89f103 0x6f79'be45U, // x = 0x1.f37c8ap+95f104 0x7276'69d4U, // x = 0x1.ecd3a8p+101f105 0x7758'4625U, // x = 0x1.b08c4ap+111f106 0x7bee'f5efU, // x = 0x1.ddebdep+120f107 };108 109 for (int i = 0; i < N; ++i) {110 float x = FPBits(INPUTS[i]).get_val();111 FP_ASSERT(mpfr::Operation::Cos, x, LIBC_NAMESPACE::cosf(x),112 TOLERANCE + 0.5);113 FP_ASSERT(mpfr::Operation::Cos, -x, LIBC_NAMESPACE::cosf(-x),114 TOLERANCE + 0.5);115 }116}117 118// SDCOMP-26094: check cosf in the cases for which the range reducer119// returns values furthest beyond its nominal upper bound of pi/4.120TEST_F(LlvmLibcCosfTest, SDCOMP_26094) {121 for (uint32_t v : SDCOMP26094_VALUES) {122 float x = FPBits(v).get_val();123 FP_ASSERT(mpfr::Operation::Cos, x, LIBC_NAMESPACE::cosf(x),124 TOLERANCE + 0.5);125 }126}127