1 //===-- Unittests for log1pf ----------------------------------------------===// 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/errno/libc_errno.h" 12 #include "src/math/log1pf.h" 13 #include "test/UnitTest/FPMatcher.h" 14 #include "test/UnitTest/Test.h" 15 #include "utils/MPFRWrapper/MPFRUtils.h" 16 17 #include <stdint.h> 18 19 using LlvmLibcLog1pfTest = LIBC_NAMESPACE::testing::FPTest<float>; 20 21 namespace mpfr = LIBC_NAMESPACE::testing::mpfr; 22 23 TEST_F(LlvmLibcLog1pfTest, SpecialNumbers) { 24 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::log1pf(aNaN)); 25 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::log1pf(inf)); 26 EXPECT_FP_IS_NAN_WITH_EXCEPTION(LIBC_NAMESPACE::log1pf(neg_inf), FE_INVALID); 27 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::log1pf(0.0f)); 28 EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::log1pf(-0.0f)); 29 EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::log1pf(-1.0f), 30 FE_DIVBYZERO); 31 } 32 33 TEST_F(LlvmLibcLog1pfTest, TrickyInputs) { 34 constexpr int N = 27; 35 constexpr uint32_t INPUTS[N] = { 36 0x35c00006U, /*0x1.80000cp-20f*/ 37 0x35400003U, /*0x1.800006p-21f*/ 38 0x3640000cU, /*0x1.800018p-19f*/ 39 0x36c00018U, /*0x1.80003p-18f*/ 40 0x3710001bU, /*0x1.200036p-17f*/ 41 0x37400030U, /*0x1.80006p-17f*/ 42 0x3770004bU, /*0x1.e00096p-17f*/ 43 0x3b9315c8U, /*0x1.262b9p-8f*/ 44 0x3c6eb7afU, /*0x1.dd6f5ep-7f*/ 45 0x3ddbfec3U, /*0x1.b7fd86p-4f*/ 46 0x3efd81adU, /*0x1.fb035ap-2f*/ 47 0x41078febU, /*0x1.0f1fd6p+3f*/ 48 0x4cc1c80bU, /*0x1.839016p+26f*/ 49 0x5cd69e88U, /*0x1.ad3d1p+58f*/ 50 0x5ee8984eU, /*0x1.d1309cp+62f*/ 51 0x65d890d3U, /*0x1.b121a6p+76f*/ 52 0x665e7ca6U, /*0x1.bcf94cp+77f*/ 53 0x6f31a8ecU, /*0x1.6351d8p+95f*/ 54 0x79e7ec37U, /*0x1.cfd86ep+116f*/ 55 0x7a17f30aU, /*0x1.2fe614p+117f*/ 56 0xb53ffffdU, /*-0x1.7ffffap-21f*/ 57 0xb70fffe5U, /*-0x1.1fffcap-17f*/ 58 0xbb0ec8c4U, /*-0x1.1d9188p-9f*/ 59 0xbc4d092cU, /*-0x1.9a1258p-7f*/ 60 0xbc657728U, /*-0x1.caee5p-7f*/ 61 0xbd1d20afU, /*-0x1.3a415ep-5f*/ 62 0xbf800000U, /*-1.0f*/ 63 }; 64 for (int i = 0; i < N; ++i) { 65 float x = FPBits(INPUTS[i]).get_val(); 66 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log1p, x, 67 LIBC_NAMESPACE::log1pf(x), 0.5); 68 } 69 } 70 71 TEST_F(LlvmLibcLog1pfTest, InFloatRange) { 72 constexpr uint32_t COUNT = 100'000; 73 constexpr uint32_t STEP = UINT32_MAX / COUNT; 74 for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) { 75 float x = FPBits(v).get_val(); 76 if (FPBits(v).is_nan() || FPBits(v).is_inf()) 77 continue; 78 LIBC_NAMESPACE::libc_errno = 0; 79 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log1p, x, 80 LIBC_NAMESPACE::log1pf(x), 0.5); 81 } 82 } 83