[llvm-branch-commits] [libc] [libc] make sqrtf128 use emulated float128 (PR #207739)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Mon Jul 6 07:15:56 PDT 2026
https://github.com/Sukumarsawant created https://github.com/llvm/llvm-project/pull/207739
Testing emulated float128 type with sqrtf128 function
>From c654b5d3c49df5f3c7d0f8f117c572f37123ce46 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Mon, 6 Jul 2026 19:42:15 +0530
Subject: [PATCH] make sqrtf128 use emulated float128
---
libc/shared/math/sqrtf128.h | 9 +++-----
libc/src/__support/math/CMakeLists.txt | 2 +-
libc/src/__support/math/sqrtf128.h | 15 ++++++-------
libc/src/math/generic/CMakeLists.txt | 1 +
libc/src/math/generic/sqrtf128.cpp | 5 ++++-
libc/src/math/sqrtf128.h | 5 ++++-
libc/test/shared/shared_math_test.cpp | 2 +-
libc/test/src/math/CMakeLists.txt | 1 +
libc/test/src/math/smoke/CMakeLists.txt | 1 +
libc/test/src/math/smoke/sqrtf128_test.cpp | 25 ++++++++++++----------
libc/test/src/math/sqrtf128_test.cpp | 9 ++++----
11 files changed, 41 insertions(+), 34 deletions(-)
diff --git a/libc/shared/math/sqrtf128.h b/libc/shared/math/sqrtf128.h
index 609b8921ab36b..ed0c1c5c63906 100644
--- a/libc/shared/math/sqrtf128.h
+++ b/libc/shared/math/sqrtf128.h
@@ -9,13 +9,12 @@
#ifndef LLVM_LIBC_SHARED_MATH_SQRTF128_H
#define LLVM_LIBC_SHARED_MATH_SQRTF128_H
-#include "include/llvm-libc-types/float128.h"
-
-#ifdef LIBC_TYPES_HAS_FLOAT128
-
#include "shared/libc_common.h"
+#include "src/__support/FPUtil/float128.h"
#include "src/__support/math/sqrtf128.h"
+using LIBC_NAMESPACE::fputil::Float128;
+
namespace LIBC_NAMESPACE_DECL {
namespace shared {
@@ -24,6 +23,4 @@ using math::sqrtf128;
} // namespace shared
} // namespace LIBC_NAMESPACE_DECL
-#endif // LIBC_TYPES_HAS_FLOAT128
-
#endif // LLVM_LIBC_SHARED_MATH_SQRTF128_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index a2699355a18e8..78b4f55cca47e 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -5394,7 +5394,7 @@ add_header_library(
libc.src.__support.common
libc.src.__support.macros.optimization
libc.src.__support.uint128
- libc.include.llvm-libc-types.float128
+ libc.src.__support.FPUtil.float128
)
add_header_library(
diff --git a/libc/src/__support/math/sqrtf128.h b/libc/src/__support/math/sqrtf128.h
index 14dcbec1acbb6..9710ffe92eef0 100644
--- a/libc/src/__support/math/sqrtf128.h
+++ b/libc/src/__support/math/sqrtf128.h
@@ -9,18 +9,17 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SQRTF128_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_SQRTF128_H
-#include "include/llvm-libc-types/float128.h"
-
-#ifdef LIBC_TYPES_HAS_FLOAT128
-
#include "src/__support/CPP/bit.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/float128.h"
#include "src/__support/FPUtil/rounding_mode.h"
#include "src/__support/common.h"
#include "src/__support/macros/optimization.h"
#include "src/__support/uint128.h"
+using LIBC_NAMESPACE::fputil::Float128;
+
// Compute sqrtf128 with correct rounding for all rounding modes using integer
// arithmetic by Alexei Sibidanov (sibid at uvic.ca):
// https://github.com/sibidanov/llvm-project/tree/as_sqrt_v2
@@ -280,9 +279,9 @@ LIBC_INLINE constexpr uint64_t rsqrt_approx(uint64_t m) {
} // namespace sqrtf128_internal
-LIBC_INLINE float128 sqrtf128(float128 x) {
+LIBC_INLINE Float128 sqrtf128(Float128 x) {
using namespace sqrtf128_internal;
- using FPBits = fputil::FPBits<float128>;
+ using FPBits = fputil::FPBits<Float128>;
// Get rounding mode.
uint32_t rm = fputil::get_round();
@@ -441,12 +440,10 @@ LIBC_INLINE float128 sqrtf128(float128 x) {
// if(frac) fputil::raise_except_if_required(FE_INEXACT);
v += static_cast<UInt128>(e2) << FPBits::FRACTION_LEN; // place exponent
- return cpp::bit_cast<float128>(v);
+ return cpp::bit_cast<Float128>(v);
}
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
-#endif // LIBC_TYPES_HAS_FLOAT128
-
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SQRTF128_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 6769f4fdd4dcb..2a24329b93a9c 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2685,6 +2685,7 @@ add_entrypoint_object(
../sqrtf128.h
DEPENDS
libc.src.__support.math.sqrtf128
+ libc.src.__support.FPUtil.float128
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/sqrtf128.cpp b/libc/src/math/generic/sqrtf128.cpp
index 47e4da97581ba..9134324ace67b 100644
--- a/libc/src/math/generic/sqrtf128.cpp
+++ b/libc/src/math/generic/sqrtf128.cpp
@@ -7,11 +7,14 @@
//===----------------------------------------------------------------------===//
#include "src/math/sqrtf128.h"
+#include "src/__support/FPUtil/float128.h"
#include "src/__support/math/sqrtf128.h"
+using LIBC_NAMESPACE::fputil::Float128;
+
namespace LIBC_NAMESPACE_DECL {
-LLVM_LIBC_FUNCTION(float128, sqrtf128, (float128 x)) {
+LLVM_LIBC_FUNCTION(Float128, sqrtf128, (Float128 x)) {
return math::sqrtf128(x);
}
diff --git a/libc/src/math/sqrtf128.h b/libc/src/math/sqrtf128.h
index 81f4c12eff90d..80604826c087c 100644
--- a/libc/src/math/sqrtf128.h
+++ b/libc/src/math/sqrtf128.h
@@ -9,12 +9,15 @@
#ifndef LLVM_LIBC_SRC_MATH_SQRTF128_H
#define LLVM_LIBC_SRC_MATH_SQRTF128_H
+#include "src/__support/FPUtil/float128.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/types.h"
+using LIBC_NAMESPACE::fputil::Float128;
+
namespace LIBC_NAMESPACE_DECL {
-float128 sqrtf128(float128 x);
+Float128 sqrtf128(Float128 x);
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index c71e44f71b23a..ac8fd3f511d50 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -583,6 +583,7 @@ using LIBC_NAMESPACE::fputil::Float128;
TEST(LlvmLibcSharedMathTest, AllEmuFloat128) {
EXPECT_FP_EQ(Float128(0.0), LIBC_NAMESPACE::shared::ceilf128(Float128(0.0)));
+ EXPECT_FP_EQ(Float128(1.0), LIBC_NAMESPACE::shared::sqrtf128(Float128(1.0)));
}
#ifdef LIBC_TYPES_HAS_FLOAT128
@@ -608,7 +609,6 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
EXPECT_FP_EQ(float128(0.0), LIBC_NAMESPACE::shared::logbf128(float128(1.0)));
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::dfmaf128(
float128(0.0), float128(0.0), float128(0.0)));
- EXPECT_FP_EQ(float128(1.0), LIBC_NAMESPACE::shared::sqrtf128(float128(1.0)));
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::dsqrtf128(float128(0.0)));
EXPECT_EQ(0L, LIBC_NAMESPACE::shared::llogbf128(float128(1.0)));
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index c4944081bb2a2..a07ba41bafb82 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1790,6 +1790,7 @@ add_fp_unittest(
SqrtTest.h
DEPENDS
libc.src.math.sqrtf128
+ libc.src.__support.FPUtil.float128
)
add_fp_unittest(
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 7876270493e11..e5b49cb0bbe39 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3600,6 +3600,7 @@ add_fp_unittest(
SqrtTest.h
DEPENDS
libc.src.math.sqrtf128
+ libc.src.__support.FPUtil.float128
)
add_fp_unittest(
diff --git a/libc/test/src/math/smoke/sqrtf128_test.cpp b/libc/test/src/math/smoke/sqrtf128_test.cpp
index 2fca74842d313..5dbc063b79946 100644
--- a/libc/test/src/math/smoke/sqrtf128_test.cpp
+++ b/libc/test/src/math/smoke/sqrtf128_test.cpp
@@ -8,14 +8,17 @@
#include "SqrtTest.h"
+#include "src/__support/FPUtil/float128.h"
#include "src/__support/uint128.h"
#include "src/math/sqrtf128.h"
-LIST_SQRT_TESTS(float128, LIBC_NAMESPACE::sqrtf128);
+using LIBC_NAMESPACE::fputil::Float128;
+
+LIST_SQRT_TESTS(Float128, LIBC_NAMESPACE::sqrtf128);
TEST_F(LlvmLibcSqrtTest, HardToRound) {
using LIBC_NAMESPACE::fputil::testing::RoundingMode;
- using FPBits = LIBC_NAMESPACE::fputil::FPBits<float128>;
+ using FPBits = LIBC_NAMESPACE::fputil::FPBits<Float128>;
// Since there is no exact half cases for square root I encode the
// round direction in the sign of the result. E.g. if the number is
@@ -23,7 +26,7 @@ TEST_F(LlvmLibcSqrtTest, HardToRound) {
// (the absolute value). Thus I can test not only hard to round
// cases for the round to nearest mode but also the directional
// modes.
- float128 HARD_TO_ROUND[][2] = {
+ Float128 HARD_TO_ROUND[][2] = {
{0x0.000000dee2f5b6a26c8f07f05442p-16382q,
-0x1.ddbd8763a617cff753e2a31083p-8204q},
{0x0.000000c86d174c5ad8ae54a548e7p-16382q,
@@ -85,9 +88,9 @@ TEST_F(LlvmLibcSqrtTest, HardToRound) {
0x1.ffffffffffffffffffffffffffffp+0q},
};
- auto rnd = [](float128 x, RoundingMode rm) -> float128 {
+ auto rnd = [](Float128 x, RoundingMode rm) -> Float128 {
bool is_neg = x < 0;
- float128 y = is_neg ? -x : x;
+ Float128 y = is_neg ? -x : x;
FPBits ybits(y);
if (is_neg &&
@@ -107,7 +110,7 @@ TEST_F(LlvmLibcSqrtTest, HardToRound) {
}
// Exact results for subnormal arguments
- float128 EXACT_SUBNORMAL[][2] = {
+ Float128 EXACT_SUBNORMAL[][2] = {
{0x0.0000000000000000000000000001p-16382q, 0x1p-8247q},
{0x0.0000000000000000000000000004p-16382q, 0x1p-8246q},
{0x0.0000000000001000000000000000p-16382q, 0x1p-8217q},
@@ -121,17 +124,17 @@ TEST_F(LlvmLibcSqrtTest, HardToRound) {
// Check exact cases starting from small numbers
for (unsigned k = 1; k < 100 * 100; ++k) {
unsigned k2 = k * k;
- float128 x = static_cast<float128>(k2);
- float128 y = static_cast<float128>(k);
+ Float128 x = static_cast<Float128>(k2);
+ Float128 y = static_cast<Float128>(k);
EXPECT_FP_EQ_ALL_ROUNDING(y, LIBC_NAMESPACE::sqrtf128(x));
};
// Then from the largest number.
uint64_t k0 = 101904826760412362ULL;
for (uint64_t k = k0; k > k0 - 10000; --k) {
- float128 k_f128 = static_cast<float128>(k);
- float128 x = k_f128 * k_f128;
- float128 y = static_cast<float128>(k);
+ Float128 k_f128 = static_cast<Float128>(k);
+ Float128 x = k_f128 * k_f128;
+ Float128 y = static_cast<Float128>(k);
EXPECT_FP_EQ_ALL_ROUNDING(y, LIBC_NAMESPACE::sqrtf128(x));
}
}
diff --git a/libc/test/src/math/sqrtf128_test.cpp b/libc/test/src/math/sqrtf128_test.cpp
index 25229f834d33c..4f9ea74d1e204 100644
--- a/libc/test/src/math/sqrtf128_test.cpp
+++ b/libc/test/src/math/sqrtf128_test.cpp
@@ -7,15 +7,16 @@
//===----------------------------------------------------------------------===//
#include "SqrtTest.h"
-
+#include "src/__support/FPUtil/float128.h"
+#include "src/__support/integer_literals.h"
#include "src/math/sqrtf128.h"
-#include "src/__support/integer_literals.h"
+using LIBC_NAMESPACE::fputil::Float128;
-LIST_SQRT_TESTS(float128, LIBC_NAMESPACE::sqrtf128)
+LIST_SQRT_TESTS(Float128, LIBC_NAMESPACE::sqrtf128)
TEST_F(LlvmLibcSqrtTest, SpecialInputs) {
- constexpr float128 INPUTS[] = {
+ constexpr Float128 INPUTS[] = {
0x0.000000dee2f5b6a26c8f07f05442p-16382q,
0x0.000000c86d174c5ad8ae54a548e7p-16382q,
0x0.000020ab15cfe0b8e488e128f535p-16382q,
More information about the llvm-branch-commits
mailing list