1 | //===-- Unittests for logf-----------------------------------------------===// |
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 "hdr/math_macros.h" |
10 | #include "src/__support/FPUtil/FPBits.h" |
11 | #include "src/math/logf.h" |
12 | #include "test/UnitTest/FPMatcher.h" |
13 | #include "test/UnitTest/Test.h" |
14 | #include "utils/MPFRWrapper/MPFRUtils.h" |
15 | |
16 | #include <stdint.h> |
17 | |
18 | using LlvmLibcLogfTest = LIBC_NAMESPACE::testing::FPTest<float>; |
19 | |
20 | namespace mpfr = LIBC_NAMESPACE::testing::mpfr; |
21 | |
22 | TEST_F(LlvmLibcLogfTest, SpecialNumbers) { |
23 | EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::logf(aNaN)); |
24 | EXPECT_FP_EQ(inf, LIBC_NAMESPACE::logf(inf)); |
25 | EXPECT_FP_IS_NAN_WITH_EXCEPTION(LIBC_NAMESPACE::logf(neg_inf), FE_INVALID); |
26 | EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::logf(0.0f), |
27 | FE_DIVBYZERO); |
28 | EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::logf(-0.0f), |
29 | FE_DIVBYZERO); |
30 | EXPECT_FP_IS_NAN_WITH_EXCEPTION(LIBC_NAMESPACE::logf(-1.0f), FE_INVALID); |
31 | EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::logf(1.0f)); |
32 | } |
33 | |
34 | TEST_F(LlvmLibcLogfTest, TrickyInputs) { |
35 | constexpr int N = 35; |
36 | constexpr uint32_t INPUTS[N] = { |
37 | 0x1b7679ffU, /*0x1.ecf3fep-73f*/ |
38 | 0x1e88452dU, /*0x1.108a5ap-66f*/ |
39 | 0x3f800001U, /*0x1.000002p+0f*/ |
40 | 0x3509dcf6U, /*0x1.13b9ecp-21f*/ |
41 | 0x3bf86ef0U, /*0x1.f0ddep-8f*/ |
42 | 0x3c413d3aU, /*0x1.827a74p-7f*/ |
43 | 0x3ca1c99fU, /*0x1.43933ep-6f*/ |
44 | 0x3d13e105U, /*0x1.27c20ap-5f*/ |
45 | 0x3f7ff1f2U, /*0x1.ffe3e4p-1f*/ |
46 | 0x3f7fffffU, /*0x1.fffffep-1f*/ |
47 | 0x3f800001U, /*0x1.000002p+0f*/ |
48 | 0x3f800006U, /*0x1.00000cp+0f*/ |
49 | 0x3f800014U, /*0x1.000028p+0f*/ |
50 | 0x3f80001cU, /*0x1.000038p+0f*/ |
51 | 0x3f80c777U, /*0x1.018eeep+0f*/ |
52 | 0x3f80ce72U, /*0x1.019ce4p+0f*/ |
53 | 0x3f80d19fU, /*0x1.01a33ep+0f*/ |
54 | 0x3f80f7bfU, /*0x1.01ef7ep+0f*/ |
55 | 0x3f80fcfeU, /*0x1.01f9fcp+0f*/ |
56 | 0x3f81feb4U, /*0x1.03fd68p+0f*/ |
57 | 0x3f83d731U, /*0x1.07ae62p+0f*/ |
58 | 0x3f90cb1dU, /*0x1.21963ap+0f*/ |
59 | 0x3fc55379U, /*0x1.8aa6f2p+0f*/ |
60 | 0x3fd364d7U, /*0x1.a6c9aep+0f*/ |
61 | 0x41178febU, /*0x1.2f1fd6p+3f*/ |
62 | 0x4c5d65a5U, /*0x1.bacb4ap+25f*/ |
63 | 0x4e85f412U, /*0x1.0be824p+30f*/ |
64 | 0x500ffb03U, /*0x1.1ff606p+33f*/ |
65 | 0x5cd69e88U, /*0x1.ad3d1p+58f*/ |
66 | 0x5ee8984eU, /*0x1.d1309cp+62f*/ |
67 | 0x65d890d3U, /*0x1.b121a6p+76f*/ |
68 | 0x665e7ca6U, /*0x1.bcf94cp+77f*/ |
69 | 0x6f31a8ecU, /*0x1.6351d8p+95f*/ |
70 | 0x79e7ec37U, /*0x1.cfd86ep+116f*/ |
71 | 0x7a17f30aU, /*0x1.2fe614p+117f*/ |
72 | }; |
73 | for (int i = 0; i < N; ++i) { |
74 | float x = FPBits(INPUTS[i]).get_val(); |
75 | EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log, x, |
76 | LIBC_NAMESPACE::logf(x), 0.5); |
77 | } |
78 | } |
79 | |
80 | TEST_F(LlvmLibcLogfTest, InFloatRange) { |
81 | constexpr uint32_t COUNT = 100'000; |
82 | constexpr uint32_t STEP = UINT32_MAX / COUNT; |
83 | for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) { |
84 | float x = FPBits(v).get_val(); |
85 | if (isnan(x: x) || isinf(x: x)) |
86 | continue; |
87 | ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log, x, |
88 | LIBC_NAMESPACE::logf(x), 0.5); |
89 | } |
90 | } |
91 | |