aboutsummaryrefslogtreecommitdiff
path: root/libc/src
diff options
context:
space:
mode:
authorOverMighty <its.overmighty@gmail.com>2024-07-01 01:20:39 +0200
committerGitHub <noreply@github.com>2024-06-30 19:20:39 -0400
commit6c1c451b867f250f1c2fab709f0c8657ffd21116 (patch)
tree7c9ce9a2267a2d228ca18c3ed9b9cbf40694f798 /libc/src
parent67957a45ee1ec42ae1671cdbfa0d73127346cc95 (diff)
downloadllvm-6c1c451b867f250f1c2fab709f0c8657ffd21116.zip
llvm-6c1c451b867f250f1c2fab709f0c8657ffd21116.tar.gz
llvm-6c1c451b867f250f1c2fab709f0c8657ffd21116.tar.bz2
[libc][math][c23] Add f16sqrt{,l,f128} C23 math functions (#96642)
Part of #95250.
Diffstat (limited to 'libc/src')
-rw-r--r--libc/src/__support/FPUtil/generic/CMakeLists.txt1
-rw-r--r--libc/src/__support/FPUtil/generic/sqrt.h103
-rw-r--r--libc/src/math/CMakeLists.txt3
-rw-r--r--libc/src/math/f16sqrt.h20
-rw-r--r--libc/src/math/f16sqrtf128.h20
-rw-r--r--libc/src/math/f16sqrtl.h20
-rw-r--r--libc/src/math/generic/CMakeLists.txt39
-rw-r--r--libc/src/math/generic/f16sqrt.cpp19
-rw-r--r--libc/src/math/generic/f16sqrtf128.cpp19
-rw-r--r--libc/src/math/generic/f16sqrtl.cpp19
10 files changed, 175 insertions, 88 deletions
diff --git a/libc/src/__support/FPUtil/generic/CMakeLists.txt b/libc/src/__support/FPUtil/generic/CMakeLists.txt
index bd8af98..fb49fd0 100644
--- a/libc/src/__support/FPUtil/generic/CMakeLists.txt
+++ b/libc/src/__support/FPUtil/generic/CMakeLists.txt
@@ -8,6 +8,7 @@ add_header_library(
libc.src.__support.common
libc.src.__support.CPP.bit
libc.src.__support.CPP.type_traits
+ libc.src.__support.FPUtil.dyadic_float
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.rounding_mode
diff --git a/libc/src/__support/FPUtil/generic/sqrt.h b/libc/src/__support/FPUtil/generic/sqrt.h
index d6e894f..1b71379 100644
--- a/libc/src/__support/FPUtil/generic/sqrt.h
+++ b/libc/src/__support/FPUtil/generic/sqrt.h
@@ -14,7 +14,7 @@
#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
-#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/FPUtil/dyadic_float.h"
#include "src/__support/common.h"
#include "src/__support/uint128.h"
@@ -78,16 +78,14 @@ sqrt(InType x) {
return x86::sqrt(x);
} else {
// IEEE floating points formats.
- using OutFPBits = typename fputil::FPBits<OutType>;
- using OutStorageType = typename OutFPBits::StorageType;
- using InFPBits = typename fputil::FPBits<InType>;
+ using OutFPBits = FPBits<OutType>;
+ using InFPBits = FPBits<InType>;
using InStorageType = typename InFPBits::StorageType;
+ using DyadicFloat =
+ DyadicFloat<cpp::bit_ceil(static_cast<size_t>(InFPBits::STORAGE_LEN))>;
+
constexpr InStorageType ONE = InStorageType(1) << InFPBits::FRACTION_LEN;
constexpr auto FLT_NAN = OutFPBits::quiet_nan().get_val();
- constexpr int EXTRA_FRACTION_LEN =
- InFPBits::FRACTION_LEN - OutFPBits::FRACTION_LEN;
- constexpr InStorageType EXTRA_FRACTION_MASK =
- (InStorageType(1) << EXTRA_FRACTION_LEN) - 1;
InFPBits bits(x);
@@ -135,6 +133,7 @@ sqrt(InType x) {
InStorageType y = ONE;
InStorageType r = x_mant - ONE;
+ // TODO: Reduce iteration count to OutFPBits::FRACTION_LEN + 2 or + 3.
for (InStorageType current_bit = ONE >> 1; current_bit;
current_bit >>= 1) {
r <<= 1;
@@ -146,91 +145,19 @@ sqrt(InType x) {
}
// We compute one more iteration in order to round correctly.
- bool lsb = (y & (InStorageType(1) << EXTRA_FRACTION_LEN)) !=
- 0; // Least significant bit
- bool rb = false; // Round bit
r <<= 2;
- InStorageType tmp = (y << 2) + 1;
+ y <<= 2;
+ InStorageType tmp = y + 1;
if (r >= tmp) {
r -= tmp;
- rb = true;
- }
-
- bool sticky = false;
-
- if constexpr (EXTRA_FRACTION_LEN > 0) {
- sticky = rb || (y & EXTRA_FRACTION_MASK) != 0;
- rb = (y & (InStorageType(1) << (EXTRA_FRACTION_LEN - 1))) != 0;
- }
-
- // Remove hidden bit and append the exponent field.
- x_exp = ((x_exp >> 1) + OutFPBits::EXP_BIAS);
-
- OutStorageType y_out = static_cast<OutStorageType>(
- ((y - ONE) >> EXTRA_FRACTION_LEN) |
- (static_cast<OutStorageType>(x_exp) << OutFPBits::FRACTION_LEN));
-
- if constexpr (EXTRA_FRACTION_LEN > 0) {
- if (x_exp >= OutFPBits::MAX_BIASED_EXPONENT) {
- switch (quick_get_round()) {
- case FE_TONEAREST:
- case FE_UPWARD:
- return OutFPBits::inf().get_val();
- default:
- return OutFPBits::max_normal().get_val();
- }
- }
-
- if (x_exp <
- -OutFPBits::EXP_BIAS - OutFPBits::SIG_LEN + EXTRA_FRACTION_LEN) {
- switch (quick_get_round()) {
- case FE_UPWARD:
- return OutFPBits::min_subnormal().get_val();
- default:
- return OutType(0.0);
- }
- }
-
- if (x_exp <= 0) {
- int underflow_extra_fraction_len = EXTRA_FRACTION_LEN - x_exp + 1;
- InStorageType underflow_extra_fraction_mask =
- (InStorageType(1) << underflow_extra_fraction_len) - 1;
-
- rb = (y & (InStorageType(1) << (underflow_extra_fraction_len - 1))) !=
- 0;
- OutStorageType subnormal_mant =
- static_cast<OutStorageType>(y >> underflow_extra_fraction_len);
- lsb = (subnormal_mant & 1) != 0;
- sticky = sticky || (y & underflow_extra_fraction_mask) != 0;
-
- switch (quick_get_round()) {
- case FE_TONEAREST:
- if (rb && (lsb || sticky))
- ++subnormal_mant;
- break;
- case FE_UPWARD:
- if (rb || sticky)
- ++subnormal_mant;
- break;
- }
-
- return cpp::bit_cast<OutType>(subnormal_mant);
- }
- }
-
- switch (quick_get_round()) {
- case FE_TONEAREST:
- // Round to nearest, ties to even
- if (rb && (lsb || (r != 0)))
- ++y_out;
- break;
- case FE_UPWARD:
- if (rb || (r != 0) || sticky)
- ++y_out;
- break;
+ // Rounding bit.
+ y |= 2;
}
+ // Sticky bit.
+ y |= static_cast<unsigned int>(r != 0);
- return cpp::bit_cast<OutType>(y_out);
+ DyadicFloat yd(Sign::POS, (x_exp >> 1) - 2 - InFPBits::FRACTION_LEN, y);
+ return yd.template as<OutType, /*ShouldSignalExceptions=*/true>();
}
}
}
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index fb0d971..4777f66 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -109,7 +109,10 @@ add_math_entrypoint_object(f16fmaf)
add_math_entrypoint_object(f16fmal)
add_math_entrypoint_object(f16fmaf128)
+add_math_entrypoint_object(f16sqrt)
add_math_entrypoint_object(f16sqrtf)
+add_math_entrypoint_object(f16sqrtl)
+add_math_entrypoint_object(f16sqrtf128)
add_math_entrypoint_object(fabs)
add_math_entrypoint_object(fabsf)
diff --git a/libc/src/math/f16sqrt.h b/libc/src/math/f16sqrt.h
new file mode 100644
index 0000000..f1134ac
--- /dev/null
+++ b/libc/src/math/f16sqrt.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for f16sqrt -----------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_F16SQRT_H
+#define LLVM_LIBC_SRC_MATH_F16SQRT_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 f16sqrt(double x);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_F16SQRT_H
diff --git a/libc/src/math/f16sqrtf128.h b/libc/src/math/f16sqrtf128.h
new file mode 100644
index 0000000..61a6ce9
--- /dev/null
+++ b/libc/src/math/f16sqrtf128.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for f16sqrtf128 -------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_F16SQRTF128_H
+#define LLVM_LIBC_SRC_MATH_F16SQRTF128_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 f16sqrtf128(float128 x);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_F16SQRTF128_H
diff --git a/libc/src/math/f16sqrtl.h b/libc/src/math/f16sqrtl.h
new file mode 100644
index 0000000..fd3c55f
--- /dev/null
+++ b/libc/src/math/f16sqrtl.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for f16sqrtl ----------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_F16SQRTL_H
+#define LLVM_LIBC_SRC_MATH_F16SQRTL_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 f16sqrtl(long double x);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_F16SQRTL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 0e4893c..7dd6c48 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3881,6 +3881,19 @@ add_entrypoint_object(
)
add_entrypoint_object(
+ f16sqrt
+ SRCS
+ f16sqrt.cpp
+ HDRS
+ ../f16sqrt.h
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.sqrt
+ COMPILE_OPTIONS
+ -O3
+)
+
+add_entrypoint_object(
f16sqrtf
SRCS
f16sqrtf.cpp
@@ -3892,3 +3905,29 @@ add_entrypoint_object(
COMPILE_OPTIONS
-O3
)
+
+add_entrypoint_object(
+ f16sqrtl
+ SRCS
+ f16sqrtl.cpp
+ HDRS
+ ../f16sqrtl.h
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.sqrt
+ COMPILE_OPTIONS
+ -O3
+)
+
+add_entrypoint_object(
+ f16sqrtf128
+ SRCS
+ f16sqrtf128.cpp
+ HDRS
+ ../f16sqrtf128.h
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.sqrt
+ COMPILE_OPTIONS
+ -O3
+)
diff --git a/libc/src/math/generic/f16sqrt.cpp b/libc/src/math/generic/f16sqrt.cpp
new file mode 100644
index 0000000..9d5f081
--- /dev/null
+++ b/libc/src/math/generic/f16sqrt.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of f16sqrt function --------------------------------===//
+//
+// 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/f16sqrt.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, f16sqrt, (double x)) {
+ return fputil::sqrt<float16>(x);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/generic/f16sqrtf128.cpp b/libc/src/math/generic/f16sqrtf128.cpp
new file mode 100644
index 0000000..11a1e82
--- /dev/null
+++ b/libc/src/math/generic/f16sqrtf128.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of f16sqrtf128 function ----------------------------===//
+//
+// 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/f16sqrtf128.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, f16sqrtf128, (float128 x)) {
+ return fputil::sqrt<float16>(x);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/generic/f16sqrtl.cpp b/libc/src/math/generic/f16sqrtl.cpp
new file mode 100644
index 0000000..2aaac9a
--- /dev/null
+++ b/libc/src/math/generic/f16sqrtl.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of f16sqrtl function -------------------------------===//
+//
+// 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/f16sqrtl.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, f16sqrtl, (long double x)) {
+ return fputil::sqrt<float16>(x);
+}
+
+} // namespace LIBC_NAMESPACE