40 changes: 40 additions & 0 deletions libc/test/src/math/exp10f16_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===-- Exhaustive test for exp10f16 --------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/math/exp10f16.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
#include "utils/MPFRWrapper/MPFRUtils.h"

using LlvmLibcExp10f16Test = LIBC_NAMESPACE::testing::FPTest<float16>;

namespace mpfr = LIBC_NAMESPACE::testing::mpfr;

// Range: [0, Inf];
static constexpr uint16_t POS_START = 0x0000U;
static constexpr uint16_t POS_STOP = 0x7c00U;

// Range: [-Inf, 0];
static constexpr uint16_t NEG_START = 0x8000U;
static constexpr uint16_t NEG_STOP = 0xfc00U;

TEST_F(LlvmLibcExp10f16Test, PositiveRange) {
for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
float16 x = FPBits(v).get_val();
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp10, x,
LIBC_NAMESPACE::exp10f16(x), 0.5);
}
}

TEST_F(LlvmLibcExp10f16Test, NegativeRange) {
for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
float16 x = FPBits(v).get_val();
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp10, x,
LIBC_NAMESPACE::exp10f16(x), 0.5);
}
}
11 changes: 11 additions & 0 deletions libc/test/src/math/performance_testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ add_perf_binary(
-fno-builtin
)

add_perf_binary(
exp10f16_perf
SRCS
exp10f16_perf.cpp
DEPENDS
.single_input_single_output_diff
libc.src.math.exp10f16
COMPILE_OPTIONS
-fno-builtin
)

add_perf_binary(
exp2f_perf
SRCS
Expand Down
22 changes: 22 additions & 0 deletions libc/test/src/math/performance_testing/exp10f16_perf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Performance test for exp10f16 -------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "SingleInputSingleOutputPerf.h"

#include "src/math/exp10f16.h"

// LLVM libc might be the only libc implementation with support for float16 math
// functions currently. We can't compare our float16 functions against the
// system libc, so we compare them against this placeholder function.
static float16 placeholderf16(float16 x) { return x; }

int main() {
SINGLE_INPUT_SINGLE_OUTPUT_PERF_EX(float16, LIBC_NAMESPACE::exp10f16,
::placeholderf16, 20'000,
"exp10f16_perf.log")
}
30 changes: 21 additions & 9 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,18 @@ add_fp_unittest(
libc.src.math.exp2m1f
)

add_fp_unittest(
exp10_test
SUITE
libc-math-smoke-tests
SRCS
exp10_test.cpp
DEPENDS
libc.src.errno.errno
libc.src.math.exp10
libc.src.__support.FPUtil.fp_bits
)

add_fp_unittest(
exp10f_test
SUITE
Expand All @@ -1089,15 +1101,15 @@ add_fp_unittest(
)

add_fp_unittest(
exp10_test
SUITE
libc-math-smoke-tests
SRCS
exp10_test.cpp
DEPENDS
libc.src.errno.errno
libc.src.math.exp10
libc.src.__support.FPUtil.fp_bits
exp10f16_test
SUITE
libc-math-smoke-tests
SRCS
exp10f16_test.cpp
DEPENDS
libc.hdr.fenv_macros
libc.src.errno.errno
libc.src.math.exp10f16
)

add_fp_unittest(
Expand Down
65 changes: 65 additions & 0 deletions libc/test/src/math/smoke/exp10f16_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//===-- Unittests for exp10f16 --------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "hdr/fenv_macros.h"
#include "src/errno/libc_errno.h"
#include "src/math/exp10f16.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"

using LlvmLibcExp10f16Test = LIBC_NAMESPACE::testing::FPTest<float16>;

TEST_F(LlvmLibcExp10f16Test, SpecialNumbers) {
LIBC_NAMESPACE::libc_errno = 0;

EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::exp10f16(aNaN));
EXPECT_MATH_ERRNO(0);

EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::exp10f16(sNaN), FE_INVALID);
EXPECT_MATH_ERRNO(0);

EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::exp10f16(inf));
EXPECT_MATH_ERRNO(0);

EXPECT_FP_EQ_ALL_ROUNDING(static_cast<float16>(zero),
LIBC_NAMESPACE::exp10f16(neg_inf));
EXPECT_MATH_ERRNO(0);

EXPECT_FP_EQ_ALL_ROUNDING(static_cast<float16>(1.0f),
LIBC_NAMESPACE::exp10f16(zero));
EXPECT_MATH_ERRNO(0);

EXPECT_FP_EQ_ALL_ROUNDING(static_cast<float16>(1.0f),
LIBC_NAMESPACE::exp10f16(neg_zero));
EXPECT_MATH_ERRNO(0);
}

TEST_F(LlvmLibcExp10f16Test, Overflow) {
LIBC_NAMESPACE::libc_errno = 0;

EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp10f16(max_normal),
FE_OVERFLOW);
EXPECT_MATH_ERRNO(ERANGE);

EXPECT_FP_EQ_WITH_EXCEPTION(
inf, LIBC_NAMESPACE::exp10f16(static_cast<float16>(5.0)), FE_OVERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
}

TEST_F(LlvmLibcExp10f16Test, Underflow) {
LIBC_NAMESPACE::libc_errno = 0;

EXPECT_FP_EQ_WITH_EXCEPTION(zero, LIBC_NAMESPACE::exp10f16(neg_max_normal),
FE_UNDERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(ERANGE);

EXPECT_FP_EQ_WITH_EXCEPTION(
zero, LIBC_NAMESPACE::exp10f16(static_cast<float16>(-8.0)),
FE_UNDERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(ERANGE);
}