[libc-commits] [libc] [libc] Add sqrtf128 implementation for Linux x86_64. (PR #79195)

via libc-commits libc-commits at lists.llvm.org
Tue Jan 23 11:43:44 PST 2024


https://github.com/felixh5678 updated https://github.com/llvm/llvm-project/pull/79195

>From 61b6ccd775756c4ea94253c4f02bf6400803f7ad Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Tue, 23 Jan 2024 14:07:01 -0500
Subject: [PATCH 1/2] [libc] Add sqrtf128 implementation for Linux x86_64.

---
 libc/config/linux/aarch64/entrypoints.txt     |  2 ++
 libc/config/linux/x86_64/entrypoints.txt      |  1 +
 libc/docs/math/index.rst                      | 12 ++++----
 libc/spec/stdc.td                             |  1 +
 libc/src/math/CMakeLists.txt                  |  1 +
 libc/src/math/generic/CMakeLists.txt          | 12 ++++++++
 libc/src/math/generic/sqrtf128.cpp            | 17 +++++++++++
 libc/src/math/sqrtf128.h                      | 20 +++++++++++++
 libc/test/src/math/smoke/CMakeLists.txt       | 30 +++++++++++++++++++
 .../src/math/smoke/generic_sqrtf128_test.cpp  | 13 ++++++++
 libc/test/src/math/smoke/sqrtf128_test.cpp    | 13 ++++++++
 11 files changed, 117 insertions(+), 5 deletions(-)
 create mode 100644 libc/src/math/generic/sqrtf128.cpp
 create mode 100644 libc/src/math/sqrtf128.h
 create mode 100644 libc/test/src/math/smoke/generic_sqrtf128_test.cpp
 create mode 100644 libc/test/src/math/smoke/sqrtf128_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 3f66a582f5e3ee..751dd9cbbdc843 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -350,6 +350,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.sqrt
     libc.src.math.sqrtf
     libc.src.math.sqrtl
+    libc.src.math.sqrtf128
     libc.src.math.tanf
     libc.src.math.tanhf
     libc.src.math.trunc
@@ -362,6 +363,7 @@ if(LIBC_COMPILER_HAS_FLOAT128)
     # math.h C23 _Float128 entrypoints
     # Temporarily disabled since float128 isn't working on the aarch64 buildbot
     # libc.src.math.fabsf128
+    #libc.src.math.sqrtf128
   )
 endif()
 
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index d5ab891674a2d8..a1486ac689812b 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -374,6 +374,7 @@ if(LIBC_COMPILER_HAS_FLOAT128)
     # math.h C23 _Float128 entrypoints
     libc.src.math.copysignf128
     libc.src.math.fabsf128
+    libc.src.math.sqrtf128
   )
 endif()
 
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index 49593dfe01c810..e28b8e87988441 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -452,6 +452,8 @@ Higher Math Functions
 +------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
 | sqrtl      | |check| | |check| | |check| | |check| | |check| |         |         | |check| | |check| | |check| |         |         |
 +------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
+| sqrtf128   | |check| |         |         |         |         |         |         |         |         |         |         |         |
++------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
 | tan        | |check| |         |         |         |         |         |         |         |         |         |         |         |
 +------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
 | tanf       | |check| | |check| | |check| | |check| | |check| |         |         | |check| | |check| | |check| |         |         |
@@ -475,9 +477,9 @@ Higher Math Functions
 Accuracy of Higher Math Functions
 =================================
 
-============== ================ =============== ======================
-<Func>         <Func_f> (float) <Func> (double) <Func_l> (long double)
-============== ================ =============== ======================
+============== ================ =============== ====================== ======================
+<Func>         <Func_f> (float) <Func> (double) <Func_l> (long double) <Func_f128> (float128)
+============== ================ =============== ====================== ======================
 acos           |check|
 acosh          |check|
 asin           |check|
@@ -501,10 +503,10 @@ pow            |check|
 sin            |check|          large
 sincos         |check|          large
 sinh           |check|
-sqrt           |check|          |check|         |check|
+sqrt           |check|          |check|         |check|                |check|
 tan            |check|
 tanh           |check|
-============== ================ =============== ======================
+============== ================ =============== ====================== ======================
 
 
 Legends:
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 99e9b3ca65ca59..8c2feba23adbf3 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -480,6 +480,7 @@ def StdC : StandardSpec<"stdc"> {
           FunctionSpec<"sqrt", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
           FunctionSpec<"sqrtf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
           FunctionSpec<"sqrtl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
+          FunctionSpec<"sqrtf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>]>, 
 
           FunctionSpec<"trunc", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
           FunctionSpec<"truncf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index a8d3fbd475f07f..3097c4b3a82cc4 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -231,6 +231,7 @@ add_math_entrypoint_object(sinhf)
 add_math_entrypoint_object(sqrt)
 add_math_entrypoint_object(sqrtf)
 add_math_entrypoint_object(sqrtl)
+add_math_entrypoint_object(sqrtf128)
 
 add_math_entrypoint_object(tan)
 add_math_entrypoint_object(tanf)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 887a8e6038a49d..8b32a3296bb87f 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1315,6 +1315,18 @@ add_entrypoint_object(
     -O3
 )
 
+add_entrypoint_object(
+  sqrtf128
+  SRCS
+    sqrtf128.cpp
+  HDRS
+    ../sqrtf128.h
+  DEPENDS
+    libc.src.__support.FPUtil.sqrt 
+  COMPILE_OPTIONS
+    -O3
+  )
+
 add_entrypoint_object(
   remquof
   SRCS
diff --git a/libc/src/math/generic/sqrtf128.cpp b/libc/src/math/generic/sqrtf128.cpp
new file mode 100644
index 00000000000000..0196c3e0a96ae5
--- /dev/null
+++ b/libc/src/math/generic/sqrtf128.cpp
@@ -0,0 +1,17 @@
+//===-- Implementation of sqrtf128 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/sqrtf128.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float128, sqrtf128, (float128 x)) { return fputil::sqrt(x); }
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/sqrtf128.h b/libc/src/math/sqrtf128.h
new file mode 100644
index 00000000000000..bccb6bbb6332da
--- /dev/null
+++ b/libc/src/math/sqrtf128.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for sqrtf128 ----------------------*- 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_SQRTF128_H
+#define LLVM_LIBC_SRC_MATH_SQRTF128_H
+
+#include "src/__support/macros/properties/float.h"
+
+namespace LIBC_NAMESPACE {
+
+float128 sqrtf128(float128 x);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_SQRTF128_H
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 163fa924b243ac..03526e019c3e9f 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -1102,6 +1102,20 @@ add_fp_unittest(
   UNIT_TEST_ONLY
 )
 
+add_fp_unittest(
+  sqrtf128_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    sqrtf128_test.cpp
+  DEPENDS
+    libc.include.math
+    libc.src.math.sqrtf128
+    libc.src.__support.FPUtil.fp_bits
+  # FIXME: Currently fails on the GPU build.
+  UNIT_TEST_ONLY
+)
+
 add_fp_unittest(
   generic_sqrtf_test
   SUITE
@@ -1150,6 +1164,22 @@ add_fp_unittest(
   UNIT_TEST_ONLY
 )
 
+add_fp_unittest(
+  generic_sqrtf128_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    generic_sqrtf128_test.cpp
+  DEPENDS
+    libc.src.math.sqrtf128
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.generic.sqrt
+  COMPILE_OPTIONS
+    -O3
+  # FIXME: Currently fails on the GPU build.
+  UNIT_TEST_ONLY
+)
+
 add_fp_unittest(
   remquof_test
   SUITE
diff --git a/libc/test/src/math/smoke/generic_sqrtf128_test.cpp b/libc/test/src/math/smoke/generic_sqrtf128_test.cpp
new file mode 100644
index 00000000000000..edba114adf06cf
--- /dev/null
+++ b/libc/test/src/math/smoke/generic_sqrtf128_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for generic implementation of sqrtf128-------------------===//
+//
+// 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 "SqrtTest.h"
+
+#include "src/__support/FPUtil/generic/sqrt.h"
+
+LIST_SQRT_TESTS(float128, LIBC_NAMESPACE::fputil::sqrt<float128>)
diff --git a/libc/test/src/math/smoke/sqrtf128_test.cpp b/libc/test/src/math/smoke/sqrtf128_test.cpp
new file mode 100644
index 00000000000000..23397b0623ce53
--- /dev/null
+++ b/libc/test/src/math/smoke/sqrtf128_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for sqrtf128---------------------------------------------===//
+//
+// 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 "SqrtTest.h"
+
+#include "src/math/sqrtf128.h"
+
+LIST_SQRT_TESTS(float128, LIBC_NAMESPACE::sqrtf128)

>From 31ffc84d2fffc4af65925e4691944e3c73d52efb Mon Sep 17 00:00:00 2001
From: Felix <felix at Dirks-MacBook-Pro.local>
Date: Tue, 23 Jan 2024 14:41:31 -0500
Subject: [PATCH 2/2] remove aarch64 entrypoints

---
 libc/config/linux/aarch64/entrypoints.txt | 2 --
 1 file changed, 2 deletions(-)

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 751dd9cbbdc843..3f66a582f5e3ee 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -350,7 +350,6 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.sqrt
     libc.src.math.sqrtf
     libc.src.math.sqrtl
-    libc.src.math.sqrtf128
     libc.src.math.tanf
     libc.src.math.tanhf
     libc.src.math.trunc
@@ -363,7 +362,6 @@ if(LIBC_COMPILER_HAS_FLOAT128)
     # math.h C23 _Float128 entrypoints
     # Temporarily disabled since float128 isn't working on the aarch64 buildbot
     # libc.src.math.fabsf128
-    #libc.src.math.sqrtf128
   )
 endif()
 



More information about the libc-commits mailing list