[flang] Accept polymorphic component element in storage_size
[llvm-project.git] / libc / test / src / math / log2f_test.cpp
blob08619efb5203fe3e0904c0ec6af0f305b2a1629f
1 //===-- Unittests for log2f -----------------------------------------------===//
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/math/log2f.h"
11 #include "test/UnitTest/FPMatcher.h"
12 #include "test/UnitTest/Test.h"
13 #include "utils/MPFRWrapper/MPFRUtils.h"
14 #include <math.h>
16 #include <errno.h>
17 #include <stdint.h>
19 namespace mpfr = __llvm_libc::testing::mpfr;
21 DECLARE_SPECIAL_CONSTANTS(float)
23 TEST(LlvmLibcLog2fTest, SpecialNumbers) {
24 EXPECT_FP_EQ(aNaN, __llvm_libc::log2f(aNaN));
25 EXPECT_FP_EQ(inf, __llvm_libc::log2f(inf));
26 EXPECT_FP_IS_NAN_WITH_EXCEPTION(__llvm_libc::log2f(neg_inf), FE_INVALID);
27 EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, __llvm_libc::log2f(0.0f), FE_DIVBYZERO);
28 EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, __llvm_libc::log2f(-0.0f), FE_DIVBYZERO);
29 EXPECT_FP_IS_NAN_WITH_EXCEPTION(__llvm_libc::log2f(-1.0f), FE_INVALID);
30 EXPECT_FP_EQ(zero, __llvm_libc::log2f(1.0f));
33 TEST(LlvmLibcLog2fTest, TrickyInputs) {
34 constexpr int N = 10;
35 constexpr uint32_t INPUTS[N] = {
36 0x3f7d57f5U, 0x3f7e3274U, 0x3f7ed848U, 0x3f7fd6ccU, 0x3f7fffffU,
37 0x3f80079bU, 0x3f81d0b5U, 0x3f82e602U, 0x3f83c98dU, 0x3f8cba39U};
39 for (int i = 0; i < N; ++i) {
40 float x = float(FPBits(INPUTS[i]));
41 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log2, x,
42 __llvm_libc::log2f(x), 0.5);
46 TEST(LlvmLibcLog2fTest, InFloatRange) {
47 constexpr uint32_t COUNT = 1000000;
48 constexpr uint32_t STEP = UINT32_MAX / COUNT;
49 for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
50 float x = float(FPBits(v));
51 if (isnan(x) || isinf(x))
52 continue;
53 errno = 0;
54 float result = __llvm_libc::log2f(x);
55 // If the computation resulted in an error or did not produce valid result
56 // in the single-precision floating point range, then ignore comparing with
57 // MPFR result as MPFR can still produce valid results because of its
58 // wider precision.
59 if (isnan(result) || isinf(result) || errno != 0)
60 continue;
61 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log2, x,
62 __llvm_libc::log2f(x), 0.5);