Skip to content

[libc][math][c23] Add f16sqrtf C23 math function #95251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/src/__support/FPUtil/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_header_library(
sqrt.h
sqrt_80_bit_long_double.h
DEPENDS
libc.hdr.fenv_macros
libc.src.__support.common
libc.src.__support.CPP.bit
libc.src.__support.CPP.type_traits
Expand Down
3 changes: 2 additions & 1 deletion libc/src/__support/FPUtil/generic/sqrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
#include "src/__support/FPUtil/rounding_mode.h"
#include "src/__support/common.h"
#include "src/__support/uint128.h"
#include <fenv.h>

#include "hdr/fenv_macros.h"

namespace LIBC_NAMESPACE {
namespace fputil {
Expand Down
4 changes: 1 addition & 3 deletions libc/src/__support/FPUtil/sqrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
#include "src/__support/macros/properties/cpu_features.h"

#if defined(LIBC_TARGET_ARCH_IS_X86_64) && defined(LIBC_TARGET_CPU_HAS_SSE2)
// #include "x86_64/sqrt.h"
// TODO
#include "generic/sqrt.h"
#include "x86_64/sqrt.h"
#elif defined(LIBC_TARGET_ARCH_IS_AARCH64)
#include "aarch64/sqrt.h"
#elif defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)
Expand Down
20 changes: 10 additions & 10 deletions libc/test/src/math/exhaustive/exhaustive_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ struct UnaryOpChecker : public virtual LIBC_NAMESPACE::testing::Test {
};

template <typename OutType, typename InType>
using UnaryNarrowerOp = OutType(InType);
using UnaryNarrowingOp = OutType(InType);

template <typename OutType, typename InType, mpfr::Operation Op,
UnaryNarrowerOp<OutType, InType> Func>
struct UnaryNarrowerOpChecker : public virtual LIBC_NAMESPACE::testing::Test {
UnaryNarrowingOp<OutType, InType> Func>
struct UnaryNarrowingOpChecker : public virtual LIBC_NAMESPACE::testing::Test {
using FloatType = InType;
using FPBits = LIBC_NAMESPACE::fputil::FPBits<FloatType>;
using StorageType = typename FPBits::StorageType;

static constexpr UnaryNarrowerOp<OutType, FloatType> *FUNC = Func;
static constexpr UnaryNarrowingOp<OutType, FloatType> *FUNC = Func;

// Check in a range, return the number of failures.
uint64_t check(StorageType start, StorageType stop,
Expand All @@ -95,9 +95,9 @@ struct UnaryNarrowerOpChecker : 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 @@ -207,6 +207,6 @@ using LlvmLibcUnaryOpExhaustiveMathTest =
LlvmLibcExhaustiveMathTest<UnaryOpChecker<FloatType, Op, Func>>;

template <typename OutType, typename InType, mpfr::Operation Op,
UnaryNarrowerOp<OutType, InType> Func>
using LlvmLibcUnaryNarrowerOpExhaustiveMathTest = LlvmLibcExhaustiveMathTest<
UnaryNarrowerOpChecker<OutType, InType, Op, Func>>;
UnaryNarrowingOp<OutType, InType> Func>
using LlvmLibcUnaryNarrowingOpExhaustiveMathTest = LlvmLibcExhaustiveMathTest<
UnaryNarrowingOpChecker<OutType, InType, Op, Func>>;
2 changes: 1 addition & 1 deletion libc/test/src/math/exhaustive/f16sqrtf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;

using LlvmLibcF16sqrtfExhaustiveTest =
LlvmLibcUnaryNarrowerOpExhaustiveMathTest<
LlvmLibcUnaryNarrowingOpExhaustiveMathTest<
float16, float, mpfr::Operation::Sqrt, LIBC_NAMESPACE::f16sqrtf>;

// Range: [0, Inf];
Expand Down
31 changes: 18 additions & 13 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2504,9 +2504,10 @@ add_fp_unittest(
libc-math-smoke-tests
SRCS
sqrtf_test.cpp
HDRS
SqrtTest.h
DEPENDS
libc.src.math.sqrtf
libc.src.__support.FPUtil.fp_bits
)

add_fp_unittest(
Expand All @@ -2515,9 +2516,10 @@ add_fp_unittest(
libc-math-smoke-tests
SRCS
sqrt_test.cpp
HDRS
SqrtTest.h
DEPENDS
libc.src.math.sqrt
libc.src.__support.FPUtil.fp_bits
)

add_fp_unittest(
Expand All @@ -2526,9 +2528,10 @@ add_fp_unittest(
libc-math-smoke-tests
SRCS
sqrtl_test.cpp
HDRS
SqrtTest.h
DEPENDS
libc.src.math.sqrtl
libc.src.__support.FPUtil.fp_bits
)

add_fp_unittest(
Expand All @@ -2537,9 +2540,10 @@ add_fp_unittest(
libc-math-smoke-tests
SRCS
sqrtf128_test.cpp
HDRS
SqrtTest.h
DEPENDS
libc.src.math.sqrtf128
libc.src.__support.FPUtil.fp_bits
)

add_fp_unittest(
Expand All @@ -2548,9 +2552,9 @@ add_fp_unittest(
libc-math-smoke-tests
SRCS
generic_sqrtf_test.cpp
HDRS
SqrtTest.h
DEPENDS
libc.src.math.sqrtf
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.generic.sqrt
COMPILE_OPTIONS
-O3
Expand All @@ -2562,9 +2566,9 @@ add_fp_unittest(
libc-math-smoke-tests
SRCS
generic_sqrt_test.cpp
HDRS
SqrtTest.h
DEPENDS
libc.src.math.sqrt
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.generic.sqrt
COMPILE_OPTIONS
-O3
Expand All @@ -2576,9 +2580,9 @@ add_fp_unittest(
libc-math-smoke-tests
SRCS
generic_sqrtl_test.cpp
HDRS
SqrtTest.h
DEPENDS
libc.src.math.sqrtl
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.generic.sqrt
COMPILE_OPTIONS
-O3
Expand All @@ -2590,9 +2594,9 @@ add_fp_unittest(
libc-math-smoke-tests
SRCS
generic_sqrtf128_test.cpp
HDRS
SqrtTest.h
DEPENDS
libc.src.math.sqrtf128
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.generic.sqrt
COMPILE_OPTIONS
-O3
Expand Down Expand Up @@ -3550,7 +3554,8 @@ add_fp_unittest(
libc-math-smoke-tests
SRCS
f16sqrtf_test.cpp
HDRS
SqrtTest.h
DEPENDS
libc.src.math.f16sqrtf
libc.src.__support.FPUtil.fp_bits
)
2 changes: 1 addition & 1 deletion libc/utils/MPFRWrapper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ if(LIBC_TESTS_CAN_USE_MPFR)
add_dependencies(
libcMPFRWrapper
libc.src.__support.CPP.array
libc.src.__support.CPP.string_view
libc.src.__support.CPP.stringstream
libc.src.__support.CPP.string_view
libc.src.__support.CPP.type_traits
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.fpbits_str
Expand Down
95 changes: 31 additions & 64 deletions libc/utils/MPFRWrapper/MPFRUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,26 +757,32 @@ ternary_operation_one_output(Operation op, InputType x, InputType y,
// to build the complete error messages before sending it to the outstream `OS`
// once at the end. This will stop the error messages from interleaving when
// the tests are running concurrently.
template <typename T>
void explain_unary_operation_single_output_error(Operation op, T input,
T matchValue,
template <typename InputType, typename OutputType>
void explain_unary_operation_single_output_error(Operation op, InputType input,
OutputType matchValue,
double ulp_tolerance,
RoundingMode rounding) {
unsigned int precision = get_precision<T>(ulp_tolerance);
unsigned int precision = get_precision<InputType>(ulp_tolerance);
MPFRNumber mpfrInput(input, precision);
MPFRNumber mpfr_result;
mpfr_result = unary_operation(op, input, precision, rounding);
Comment on lines 766 to 768
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not written by me, but the naming/case isn't consistent, and the assignment could be merged with the declaration.

MPFRNumber mpfrMatchValue(matchValue);
tlog << "Match value not within tolerance value of MPFR result:\n"
<< " Input decimal: " << mpfrInput.str() << '\n';
tlog << " Input bits: " << str(FPBits<T>(input)) << '\n';
tlog << '\n' << " Match decimal: " << mpfrMatchValue.str() << '\n';
tlog << " Match bits: " << str(FPBits<T>(matchValue)) << '\n';
tlog << '\n' << " MPFR result: " << mpfr_result.str() << '\n';
tlog << " MPFR rounded: " << str(FPBits<T>(mpfr_result.as<T>())) << '\n';
tlog << '\n';
tlog << " ULP error: "
<< mpfr_result.ulp_as_mpfr_number(matchValue).str() << '\n';
cpp::array<char, 1024> msg_buf;
cpp::StringStream msg(msg_buf);
msg << "Match value not within tolerance value of MPFR result:\n"
<< " Input decimal: " << mpfrInput.str() << '\n';
msg << " Input bits: " << str(FPBits<InputType>(input)) << '\n';
msg << '\n' << " Match decimal: " << mpfrMatchValue.str() << '\n';
msg << " Match bits: " << str(FPBits<OutputType>(matchValue)) << '\n';
msg << '\n' << " MPFR result: " << mpfr_result.str() << '\n';
msg << " MPFR rounded: "
<< str(FPBits<OutputType>(mpfr_result.as<OutputType>())) << '\n';
msg << '\n';
msg << " ULP error: " << mpfr_result.ulp_as_mpfr_number(matchValue).str()
<< '\n';
if (msg.overflow())
__builtin_unreachable();
Comment on lines +783 to +784
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The buffer size of 1024 should always be much more than enough.

tlog << msg.str();
}

template void explain_unary_operation_single_output_error<float>(Operation op,
Expand All @@ -790,37 +796,10 @@ template void explain_unary_operation_single_output_error<long double>(
#ifdef LIBC_TYPES_HAS_FLOAT16
template void explain_unary_operation_single_output_error<float16>(
Operation op, float16, float16, double, RoundingMode);
#endif

template <typename OutType, typename InType>
void explain_unary_narrower_operation_single_output_error(
Operation op, InType input, OutType matchValue, double ulp_tolerance,
RoundingMode rounding) {
unsigned int precision = get_precision<InType>(ulp_tolerance);
MPFRNumber mpfrInput(input, precision);
MPFRNumber mpfr_result;
mpfr_result = unary_operation(op, input, precision, rounding);
MPFRNumber mpfrMatchValue(matchValue);
cpp::array<char, 4096> msg_data;
cpp::StringStream msg(msg_data);
msg << "Match value not within tolerance value of MPFR result:\n"
<< " Input decimal: " << mpfrInput.str() << '\n';
msg << " Input bits: " << str(FPBits<InType>(input)) << '\n';
msg << '\n' << " Match decimal: " << mpfrMatchValue.str() << '\n';
msg << " Match bits: " << str(FPBits<OutType>(matchValue)) << '\n';
msg << '\n' << " MPFR result: " << mpfr_result.str() << '\n';
msg << " MPFR rounded: " << str(FPBits<OutType>(mpfr_result.as<OutType>()))
<< '\n';
msg << '\n';
msg << " ULP error: " << mpfr_result.ulp_as_mpfr_number(matchValue).str()
<< '\n';
tlog << msg.str();
}

#ifdef LIBC_TYPES_HAS_FLOAT16
template void
explain_unary_narrower_operation_single_output_error<float16, float>(
Operation op, float, float16, double, RoundingMode);
template void explain_unary_operation_single_output_error<float>(Operation op,
float, float16,
double,
RoundingMode);
#endif

template <typename T>
Expand Down Expand Up @@ -982,11 +961,12 @@ template void explain_ternary_operation_one_output_error<long double>(
Operation, const TernaryInput<long double> &, long double, double,
RoundingMode);

template <typename T>
bool compare_unary_operation_single_output(Operation op, T input, T libc_result,
template <typename InputType, typename OutputType>
bool compare_unary_operation_single_output(Operation op, InputType input,
OutputType libc_result,
double ulp_tolerance,
RoundingMode rounding) {
unsigned int precision = get_precision<T>(ulp_tolerance);
unsigned int precision = get_precision<InputType>(ulp_tolerance);
MPFRNumber mpfr_result;
mpfr_result = unary_operation(op, input, precision, rounding);
double ulp = mpfr_result.ulp(libc_result);
Expand All @@ -1005,22 +985,9 @@ template bool compare_unary_operation_single_output<long double>(
template bool compare_unary_operation_single_output<float16>(Operation, float16,
float16, double,
RoundingMode);
#endif

template <typename OutType, typename InType>
bool compare_unary_narrower_operation_single_output(Operation op, InType input,
OutType libc_result,
double ulp_tolerance,
RoundingMode rounding) {
unsigned int precision = get_precision<InType>(ulp_tolerance);
MPFRNumber mpfr_result;
mpfr_result = unary_operation(op, input, precision, rounding);
double ulp = mpfr_result.ulp(libc_result);
return (ulp <= ulp_tolerance);
}
#ifdef LIBC_TYPES_HAS_FLOAT16
template bool compare_unary_narrower_operation_single_output<float16, float>(
Operation, float, float16, double, RoundingMode);
template bool compare_unary_operation_single_output<float>(Operation, float,
float16, double,
RoundingMode);
#endif

template <typename T>
Expand Down
Loading
Loading