Skip to content

Commit a0d196c

Browse files
committed
chore: add smoke and general tests for expbf16 higher math function
Signed-off-by: Krishna Pandey <[email protected]>
1 parent df2bd8a commit a0d196c

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

libc/test/src/math/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,18 @@ add_fp_unittest(
11381138
libc.src.math.expf16
11391139
)
11401140

1141+
add_fp_unittest(
1142+
expbf16_test
1143+
NEED_MPFR
1144+
SUITE
1145+
libc-math-unittests
1146+
SRCS
1147+
expbf16_test.cpp
1148+
DEPENDS
1149+
libc.src.math.expbf16
1150+
libc.src.__support.FPUtil.bfloat16
1151+
)
1152+
11411153
add_fp_unittest(
11421154
exp2_test
11431155
NEED_MPFR
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//===-- Exhaustive test for expbf16 ---------------------------------------===//
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 "src/__support/FPUtil/bfloat16.h"
10+
#include "src/math/expbf16.h"
11+
#include "test/UnitTest/FPMatcher.h"
12+
#include "test/UnitTest/Test.h"
13+
#include "utils/MPFRWrapper/MPFRUtils.h"
14+
15+
using LlvmLibcExpBf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
16+
17+
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
18+
19+
// range: [0, inf]
20+
static constexpr uint16_t POS_START = 0x0000U;
21+
static constexpr uint16_t POS_STOP = 0x7f80U;
22+
// static constexpr uint16_t POS_STOP = 0x3e00U;
23+
24+
// range: [-0, -inf]
25+
static constexpr uint16_t NEG_START = 0x8000U;
26+
static constexpr uint16_t NEG_STOP = 0xff80U;
27+
28+
TEST_F(LlvmLibcExpBf16Test, PositiveRange) {
29+
for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
30+
bfloat16 x = FPBits(v).get_val();
31+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp, x,
32+
LIBC_NAMESPACE::expbf16(x), 0.5);
33+
}
34+
35+
auto test_value = [&](uint16_t v) {
36+
bfloat16 x = FPBits(v).get_val();
37+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp, x,
38+
LIBC_NAMESPACE::expbf16(x), 0.5);
39+
};
40+
41+
auto test_value_f = [&](float v) {
42+
bfloat16 x(v);
43+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp, x,
44+
LIBC_NAMESPACE::expbf16(x), 0.5);
45+
};
46+
47+
test_value(0xc2c8);
48+
test_value(0xc2ba);
49+
50+
test_value_f(17.45f);
51+
}
52+
53+
TEST_F(LlvmLibcExpBf16Test, NegativeRange) {
54+
for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
55+
bfloat16 x = FPBits(v).get_val();
56+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp, x,
57+
LIBC_NAMESPACE::expbf16(x), 0.5);
58+
}
59+
}

libc/test/src/math/smoke/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,20 @@ add_fp_unittest(
13951395
libc.src.__support.FPUtil.cast
13961396
)
13971397

1398+
add_fp_unittest(
1399+
expbf16_test
1400+
SUITE
1401+
libc-math-smoke-tests
1402+
SRCS
1403+
expbf16_test.cpp
1404+
DEPENDS
1405+
libc.hdr.errno_macros
1406+
libc.hdr.fenv_macros
1407+
libc.src.math.expbf16
1408+
libc.src.__support.FPUtil.bfloat16
1409+
libc.src.__support.FPUtil.cast
1410+
)
1411+
13981412
add_fp_unittest(
13991413
exp2_test
14001414
SUITE
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===-- Unittests for expbf16 ---------------------------------------------===//
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/errno_macros.h"
10+
#include "hdr/fenv_macros.h"
11+
#include "src/__support/FPUtil/bfloat16.h"
12+
#include "src/__support/FPUtil/cast.h"
13+
#include "src/math/expbf16.h"
14+
#include "test/UnitTest/FPMatcher.h"
15+
#include "test/UnitTest/Test.h"
16+
17+
using LlvmLibcExpf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
18+
19+
TEST_F(LlvmLibcExpf16Test, SpecialNumbers) {
20+
EXPECT_FP_IS_NAN(LIBC_NAMESPACE::expbf16(aNaN));
21+
EXPECT_MATH_ERRNO(0);
22+
23+
EXPECT_FP_IS_NAN_WITH_EXCEPTION(LIBC_NAMESPACE::expbf16(sNaN), FE_INVALID);
24+
EXPECT_MATH_ERRNO(0);
25+
26+
EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::expbf16(inf));
27+
EXPECT_MATH_ERRNO(0);
28+
29+
EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::expbf16(neg_inf));
30+
EXPECT_MATH_ERRNO(0);
31+
32+
EXPECT_FP_EQ_ALL_ROUNDING(LIBC_NAMESPACE::fputil::cast<bfloat16>(1.0f),
33+
LIBC_NAMESPACE::expbf16(zero));
34+
EXPECT_MATH_ERRNO(0);
35+
36+
EXPECT_FP_EQ_ALL_ROUNDING(LIBC_NAMESPACE::fputil::cast<bfloat16>(1.0f),
37+
LIBC_NAMESPACE::expbf16(neg_zero));
38+
EXPECT_MATH_ERRNO(0);
39+
}
40+
41+
TEST_F(LlvmLibcExpf16Test, Overflow) {
42+
EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::expbf16(max_normal),
43+
FE_OVERFLOW);
44+
EXPECT_MATH_ERRNO(ERANGE);
45+
46+
EXPECT_FP_EQ_WITH_EXCEPTION(
47+
inf,
48+
LIBC_NAMESPACE::expbf16(LIBC_NAMESPACE::fputil::cast<bfloat16>(89.0f)),
49+
FE_OVERFLOW);
50+
EXPECT_MATH_ERRNO(ERANGE);
51+
}
52+
53+
TEST_F(LlvmLibcExpf16Test, Underflow) {
54+
EXPECT_FP_EQ_WITH_EXCEPTION(zero, LIBC_NAMESPACE::expbf16(neg_max_normal),
55+
FE_UNDERFLOW | FE_INEXACT);
56+
EXPECT_MATH_ERRNO(ERANGE);
57+
58+
EXPECT_FP_EQ_WITH_EXCEPTION(
59+
zero,
60+
LIBC_NAMESPACE::expbf16(LIBC_NAMESPACE::fputil::cast<bfloat16>(-92.5f)),
61+
FE_UNDERFLOW | FE_INEXACT);
62+
EXPECT_MATH_ERRNO(ERANGE);
63+
}

0 commit comments

Comments
 (0)