1 | //===-- Unittests for exp2f -----------------------------------------------===// |
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/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA |
12 | #include "src/errno/libc_errno.h" |
13 | #include "src/math/exp2f.h" |
14 | #include "test/UnitTest/FPMatcher.h" |
15 | #include "test/UnitTest/Test.h" |
16 | |
17 | #include <stdint.h> |
18 | |
19 | using LlvmLibcExp2fTest = LIBC_NAMESPACE::testing::FPTest<float>; |
20 | |
21 | TEST_F(LlvmLibcExp2fTest, SpecialNumbers) { |
22 | LIBC_NAMESPACE::libc_errno = 0; |
23 | |
24 | EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::exp2f(aNaN)); |
25 | EXPECT_MATH_ERRNO(0); |
26 | |
27 | EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::exp2f(inf)); |
28 | EXPECT_MATH_ERRNO(0); |
29 | |
30 | EXPECT_FP_EQ_ALL_ROUNDING(0.0f, LIBC_NAMESPACE::exp2f(neg_inf)); |
31 | EXPECT_MATH_ERRNO(0); |
32 | |
33 | EXPECT_FP_EQ_ALL_ROUNDING(1.0f, LIBC_NAMESPACE::exp2f(0.0f)); |
34 | EXPECT_MATH_ERRNO(0); |
35 | |
36 | EXPECT_FP_EQ_ALL_ROUNDING(1.0f, LIBC_NAMESPACE::exp2f(-0.0f)); |
37 | EXPECT_MATH_ERRNO(0); |
38 | |
39 | EXPECT_FP_EQ_ALL_ROUNDING(2.0f, LIBC_NAMESPACE::exp2f(1.0f)); |
40 | EXPECT_FP_EQ_ALL_ROUNDING(0.5f, LIBC_NAMESPACE::exp2f(-1.0f)); |
41 | EXPECT_FP_EQ_ALL_ROUNDING(4.0f, LIBC_NAMESPACE::exp2f(2.0f)); |
42 | EXPECT_FP_EQ_ALL_ROUNDING(0.25f, LIBC_NAMESPACE::exp2f(-2.0f)); |
43 | } |
44 | |
45 | TEST_F(LlvmLibcExp2fTest, Overflow) { |
46 | LIBC_NAMESPACE::libc_errno = 0; |
47 | EXPECT_FP_EQ_WITH_EXCEPTION( |
48 | inf, LIBC_NAMESPACE::exp2f(FPBits(0x7f7fffffU).get_val()), FE_OVERFLOW); |
49 | EXPECT_MATH_ERRNO(ERANGE); |
50 | |
51 | EXPECT_FP_EQ_WITH_EXCEPTION( |
52 | inf, LIBC_NAMESPACE::exp2f(FPBits(0x43000000U).get_val()), FE_OVERFLOW); |
53 | EXPECT_MATH_ERRNO(ERANGE); |
54 | |
55 | EXPECT_FP_EQ_WITH_EXCEPTION( |
56 | inf, LIBC_NAMESPACE::exp2f(FPBits(0x43000001U).get_val()), FE_OVERFLOW); |
57 | EXPECT_MATH_ERRNO(ERANGE); |
58 | } |
59 | |