Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / math / exp10f_test.cpp
blobe3151dafa942938fdb67c9eec6b63f5bad0e9cfc
1 //===-- Unittests for exp10f ----------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "src/__support/FPUtil/FPBits.h"
10 #include "src/errno/libc_errno.h"
11 #include "src/math/exp10f.h"
12 #include "test/UnitTest/FPMatcher.h"
13 #include "test/UnitTest/Test.h"
14 #include "utils/MPFRWrapper/MPFRUtils.h"
15 #include <math.h>
17 #include <stdint.h>
19 using LlvmLibcExp10fTest = LIBC_NAMESPACE::testing::FPTest<float>;
21 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
23 TEST_F(LlvmLibcExp10fTest, SpecialNumbers) {
24 libc_errno = 0;
26 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::exp10f(aNaN));
27 EXPECT_MATH_ERRNO(0);
29 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::exp10f(inf));
30 EXPECT_MATH_ERRNO(0);
32 EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::exp10f(neg_inf));
33 EXPECT_MATH_ERRNO(0);
35 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::exp10f(0.0f));
36 EXPECT_MATH_ERRNO(0);
38 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::exp10f(-0.0f));
39 EXPECT_MATH_ERRNO(0);
42 TEST_F(LlvmLibcExp10fTest, Overflow) {
43 libc_errno = 0;
44 EXPECT_FP_EQ_WITH_EXCEPTION(
45 inf, LIBC_NAMESPACE::exp10f(float(FPBits(0x7f7fffffU))), FE_OVERFLOW);
46 EXPECT_MATH_ERRNO(ERANGE);
48 EXPECT_FP_EQ_WITH_EXCEPTION(
49 inf, LIBC_NAMESPACE::exp10f(float(FPBits(0x43000000U))), FE_OVERFLOW);
50 EXPECT_MATH_ERRNO(ERANGE);
52 EXPECT_FP_EQ_WITH_EXCEPTION(
53 inf, LIBC_NAMESPACE::exp10f(float(FPBits(0x43000001U))), FE_OVERFLOW);
54 EXPECT_MATH_ERRNO(ERANGE);
57 TEST_F(LlvmLibcExp10fTest, Underflow) {
58 libc_errno = 0;
59 EXPECT_FP_EQ_WITH_EXCEPTION(
60 0.0f, LIBC_NAMESPACE::exp10f(float(FPBits(0xff7fffffU))), FE_UNDERFLOW);
61 EXPECT_MATH_ERRNO(ERANGE);
63 float x = float(FPBits(0xc2cffff8U));
64 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp10, x,
65 LIBC_NAMESPACE::exp10f(x), 0.5);
66 EXPECT_MATH_ERRNO(ERANGE);
68 x = float(FPBits(0xc2d00008U));
69 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp10, x,
70 LIBC_NAMESPACE::exp10f(x), 0.5);
71 EXPECT_MATH_ERRNO(ERANGE);
74 TEST_F(LlvmLibcExp10fTest, TrickyInputs) {
75 constexpr int N = 20;
76 constexpr uint32_t INPUTS[N] = {
77 0x325e5bd8, // x = 0x1.bcb7bp-27f
78 0x325e5bd9, // x = 0x1.bcb7b2p-27f
79 0x325e5bda, // x = 0x1.bcb7b4p-27f
80 0x3d14d956, // x = 0x1.29b2acp-5f
81 0x4116498a, // x = 0x1.2c9314p3f
82 0x4126f431, // x = 0x1.4de862p3f
83 0x4187d13c, // x = 0x1.0fa278p4f
84 0x4203e9da, // x = 0x1.07d3b4p5f
85 0x420b5f5d, // x = 0x1.16bebap5f
86 0x42349e35, // x = 0x1.693c6ap5f
87 0x3f800000, // x = 1.0f
88 0x40000000, // x = 2.0f
89 0x40400000, // x = 3.0f
90 0x40800000, // x = 4.0f
91 0x40a00000, // x = 5.0f
92 0x40c00000, // x = 6.0f
93 0x40e00000, // x = 7.0f
94 0x41000000, // x = 8.0f
95 0x41100000, // x = 9.0f
96 0x41200000, // x = 10.0f
98 for (int i = 0; i < N; ++i) {
99 libc_errno = 0;
100 float x = float(FPBits(INPUTS[i]));
101 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp10, x,
102 LIBC_NAMESPACE::exp10f(x), 0.5);
103 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp10, -x,
104 LIBC_NAMESPACE::exp10f(-x), 0.5);
108 TEST_F(LlvmLibcExp10fTest, InFloatRange) {
109 constexpr uint32_t COUNT = 100'000;
110 constexpr uint32_t STEP = UINT32_MAX / COUNT;
111 for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
112 float x = float(FPBits(v));
113 if (isnan(x) || isinf(x))
114 continue;
115 libc_errno = 0;
116 float result = LIBC_NAMESPACE::exp10f(x);
118 // If the computation resulted in an error or did not produce valid result
119 // in the single-precision floating point range, then ignore comparing with
120 // MPFR result as MPFR can still produce valid results because of its
121 // wider precision.
122 if (isnan(result) || isinf(result) || libc_errno != 0)
123 continue;
124 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp10, x,
125 LIBC_NAMESPACE::exp10f(x), 0.5);