33 changes: 33 additions & 0 deletions libc/test/src/math/exhaustive/cbrtf_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===-- Exhaustive test for cbrtf -----------------------------------------===//
//
// 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 "exhaustive_test.h"
#include "src/math/cbrtf.h"
#include "utils/MPFRWrapper/MPFRUtils.h"

namespace mpfr = LIBC_NAMESPACE::testing::mpfr;

using LlvmLibcCbrtfExhaustiveTest =
LlvmLibcUnaryOpExhaustiveMathTest<float, mpfr::Operation::Cbrt,
LIBC_NAMESPACE::cbrtf>;

// Range: [0, Inf];
static constexpr uint32_t POS_START = 0x0000'0000U;
static constexpr uint32_t POS_STOP = 0x7f80'0000U;

TEST_F(LlvmLibcCbrtfExhaustiveTest, PostiveRange) {
test_full_range_all_roundings(POS_START, POS_STOP);
}

// Range: [-Inf, 0];
static constexpr uint32_t NEG_START = 0x8000'0000U;
static constexpr uint32_t NEG_STOP = 0xff80'0000U;

TEST_F(LlvmLibcCbrtfExhaustiveTest, NegativeRange) {
test_full_range_all_roundings(NEG_START, NEG_STOP);
}
15 changes: 8 additions & 7 deletions libc/test/src/math/exhaustive/exhaustive_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ struct UnaryOpChecker : public virtual LIBC_NAMESPACE::testing::Test {
TEST_MPFR_MATCH_ROUNDING_SILENTLY(Op, x, Func(x), 0.5, rounding);
failed += (!correct);
// Uncomment to print out failed values.
// if (!correct) {
// EXPECT_MPFR_MATCH_ROUNDING(Op, x, Func(x), 0.5, rounding);
// }
if (!correct) {
EXPECT_MPFR_MATCH_ROUNDING(Op, x, Func(x), 0.5, rounding);
}
} while (bits++ < stop);
return failed;
}
Expand Down Expand Up @@ -97,9 +97,9 @@ struct BinaryOpChecker : public virtual LIBC_NAMESPACE::testing::Test {
0.5, rounding);
failed += (!correct);
// Uncomment to print out failed values.
// if (!correct) {
// EXPECT_MPFR_MATCH_ROUNDING(Op, input, Func(x, y), 0.5, rounding);
// }
if (!correct) {
EXPECT_MPFR_MATCH_ROUNDING(Op, input, Func(x, y), 0.5, rounding);
}
} while (ybits++ < y_stop);
} while (xbits++ < x_stop);
return failed;
Expand Down Expand Up @@ -187,7 +187,8 @@ struct LlvmLibcExhaustiveMathTest
std::stringstream msg;
msg << "Test failed for " << std::dec << failed_in_range
<< " inputs in range: ";
explain_failed_range(msg, start, stop, extra_range_bounds...);
explain_failed_range(msg, range_begin, range_end,
extra_range_bounds...);
msg << "\n";
std::cerr << msg.str() << std::flush;

Expand Down
10 changes: 10 additions & 0 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3961,3 +3961,13 @@ add_fp_unittest(
DEPENDS
libc.src.math.tan
)

add_fp_unittest(
cbrtf_test
SUITE
libc-math-smoke-tests
SRCS
cbrtf_test.cpp
DEPENDS
libc.src.math.cbrtf
)
33 changes: 33 additions & 0 deletions libc/test/src/math/smoke/cbrtf_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===-- Unittests for cbrtf -----------------------------------------------===//
//
// 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/cbrtf.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"

using LlvmLibcCbrtfTest = LIBC_NAMESPACE::testing::FPTest<float>;

using LIBC_NAMESPACE::testing::tlog;

TEST_F(LlvmLibcCbrtfTest, SpecialNumbers) {
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::cbrtf(aNaN));
EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::cbrtf(inf));
EXPECT_FP_EQ_ALL_ROUNDING(neg_inf, LIBC_NAMESPACE::cbrtf(neg_inf));
EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::cbrtf(zero));
EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::cbrtf(neg_zero));
EXPECT_FP_EQ_ALL_ROUNDING(1.0f, LIBC_NAMESPACE::cbrtf(1.0f));
EXPECT_FP_EQ_ALL_ROUNDING(-1.0f, LIBC_NAMESPACE::cbrtf(-1.0f));
EXPECT_FP_EQ_ALL_ROUNDING(2.0f, LIBC_NAMESPACE::cbrtf(8.0f));
EXPECT_FP_EQ_ALL_ROUNDING(-2.0f, LIBC_NAMESPACE::cbrtf(-8.0f));
EXPECT_FP_EQ_ALL_ROUNDING(3.0f, LIBC_NAMESPACE::cbrtf(27.0f));
EXPECT_FP_EQ_ALL_ROUNDING(-3.0f, LIBC_NAMESPACE::cbrtf(-27.0f));
EXPECT_FP_EQ_ALL_ROUNDING(5.0f, LIBC_NAMESPACE::cbrtf(125.0f));
EXPECT_FP_EQ_ALL_ROUNDING(-5.0f, LIBC_NAMESPACE::cbrtf(-125.0f));
EXPECT_FP_EQ_ALL_ROUNDING(0x1.0p42f, LIBC_NAMESPACE::cbrtf(0x1.0p126f));
EXPECT_FP_EQ_ALL_ROUNDING(-0x1.0p42f, LIBC_NAMESPACE::cbrtf(-0x1.0p126f));
}
8 changes: 8 additions & 0 deletions libc/utils/MPFRWrapper/MPFRUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ class MPFRNumber {
return result;
}

MPFRNumber cbrt() const {
MPFRNumber result(*this);
mpfr_cbrt(result.value, value, mpfr_rounding);
return result;
}

MPFRNumber ceil() const {
MPFRNumber result(*this);
mpfr_ceil(result.value, value);
Expand Down Expand Up @@ -702,6 +708,8 @@ unary_operation(Operation op, InputType input, unsigned int precision,
return mpfrInput.atan();
case Operation::Atanh:
return mpfrInput.atanh();
case Operation::Cbrt:
return mpfrInput.cbrt();
case Operation::Ceil:
return mpfrInput.ceil();
case Operation::Cos:
Expand Down
1 change: 1 addition & 0 deletions libc/utils/MPFRWrapper/MPFRUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ enum class Operation : int {
Asinh,
Atan,
Atanh,
Cbrt,
Ceil,
Cos,
Cosh,
Expand Down