Skip to content

Commit 89a92c0

Browse files
committed
add test in test/src/math
1 parent 78eb980 commit 89a92c0

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

libc/test/src/math/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,21 @@ add_fp_unittest(
201201
libc.src.math.tanf16
202202
)
203203

204+
add_fp_unittest(
205+
tanpif_test
206+
NEED_MPFR
207+
SUITE
208+
libc-math-unittests
209+
SRCS
210+
tanpif_test.cpp
211+
HDRS
212+
sdcomp26094.h
213+
DEPENDS
214+
libc.src.math.tanpif
215+
libc.src.__support.CPP.array
216+
libc.src.__support.FPUtil.fp_bits
217+
)
218+
204219
add_fp_unittest(
205220
tanpif16_test
206221
NEED_MPFR

libc/test/src/math/tanpif_test.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//===-- Unittests for tanpif ----------------------------------------------===//
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/math/tanpif.h"
10+
#include "test/UnitTest/FPMatcher.h"
11+
#include "test/src/math/sdcomp26094.h"
12+
#include "utils/MPFRWrapper/MPFRUtils.h"
13+
14+
using LlvmLibcTanpifTest = LIBC_NAMESPACE::testing::FPTest<float>;
15+
16+
using LIBC_NAMESPACE::testing::SDCOMP26094_VALUES;
17+
18+
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
19+
20+
TEST_F(LlvmLibcTanpifTest, SpecificBitPatterns) {
21+
constexpr int N = 38;
22+
constexpr uint32_t INPUTS[N] = {
23+
0x3f00'0000U, // x = 0.5
24+
0x461d'd600U, // x = 10101.5
25+
0x3f06'0a92U, // x = pi/6
26+
0x3f3a'dc51U, // x = 0x1.75b8a2p-1f
27+
0x3f49'0fdbU, // x = pi/4
28+
0x3f86'0a92U, // x = pi/3
29+
0x3fa7'832aU, // x = 0x1.4f0654p+0f
30+
0x3fc9'0fdbU, // x = pi/2
31+
0x4017'1973U, // x = 0x1.2e32e6p+1f
32+
0x4049'0fdbU, // x = pi
33+
0x4096'cbe4U, // x = 0x1.2d97c8p+2f
34+
0x40c9'0fdbU, // x = 2*pi
35+
0x433b'7490U, // x = 0x1.76e92p+7f
36+
0x437c'e5f1U, // x = 0x1.f9cbe2p+7f
37+
0x4619'9998U, // x = 0x1.33333p+13f
38+
0x474d'246fU, // x = 0x1.9a48dep+15f
39+
0x4afd'ece4U, // x = 0x1.fbd9c8p+22f
40+
0x4c23'32e9U, // x = 0x1.4665d2p+25f
41+
0x50a3'e87fU, // x = 0x1.47d0fep+34f
42+
0x5239'47f6U, // x = 0x1.728fecp+37f
43+
0x53b1'46a6U, // x = 0x1.628d4cp+40f
44+
0x55ca'fb2aU, // x = 0x1.95f654p+44f
45+
0x588e'f060U, // x = 0x1.1de0cp+50f
46+
0x5c07'bcd0U, // x = 0x1.0f79ap+57f
47+
0x5ebc'fddeU, // x = 0x1.79fbbcp+62f
48+
0x5fa6'eba7U, // x = 0x1.4dd74ep+64f
49+
0x61a4'0b40U, // x = 0x1.48168p+68f
50+
0x6386'134eU, // x = 0x1.0c269cp+72f
51+
0x6589'8498U, // x = 0x1.13093p+76f
52+
0x6600'0001U, // x = 0x1.000002p+77f
53+
0x664e'46e4U, // x = 0x1.9c8dc8p+77f
54+
0x66b0'14aaU, // x = 0x1.602954p+78f
55+
0x67a9'242bU, // x = 0x1.524856p+80f
56+
0x6a19'76f1U, // x = 0x1.32ede2p+85f
57+
0x6c55'da58U, // x = 0x1.abb4bp+89f
58+
0x6f79'be45U, // x = 0x1.f37c8ap+95f
59+
0x7276'69d4U, // x = 0x1.ecd3a8p+101f
60+
0x7758'4625U, // x = 0x1.b08c4ap+111f
61+
};
62+
63+
for (int i = 0; i < N; ++i) {
64+
float x = FPBits(INPUTS[i]).get_val();
65+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanpi, x,
66+
LIBC_NAMESPACE::tanpif(x), 0.5);
67+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanpi, -x,
68+
LIBC_NAMESPACE::tanpif(-x), 0.5);
69+
}
70+
}
71+
72+
// For small values, tanpi(x) is pi * x.
73+
TEST_F(LlvmLibcTanpifTest, SmallValues) {
74+
float x = FPBits(0x1780'0000U).get_val();
75+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanpi, x,
76+
LIBC_NAMESPACE::tanpif(x), 0.5);
77+
78+
x = FPBits(0x0040'0000U).get_val();
79+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanpi, x,
80+
LIBC_NAMESPACE::tanpif(x), 0.5);
81+
}
82+
83+
// SDCOMP-26094: check tanpif in the cases for which the range reducer
84+
// returns values furthest beyond its nominal upper bound of pi/4.
85+
TEST_F(LlvmLibcTanpifTest, SDCOMP_26094) {
86+
for (uint32_t v : SDCOMP26094_VALUES) {
87+
float x = FPBits((v)).get_val();
88+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanpi, x,
89+
LIBC_NAMESPACE::tanpif(x), 0.5);
90+
}
91+
}

0 commit comments

Comments
 (0)