[libc-commits] [libc] af0f58c - [libc][math][c23] Add entrypoints and tests for fsqrt{, l, f128} (#99669)

via libc-commits libc-commits at lists.llvm.org
Sun Jul 21 08:17:45 PDT 2024


Author: Job Henandez Lara
Date: 2024-07-21T11:17:41-04:00
New Revision: af0f58cf14329f5e0fe56fed7c9eb4cd3107a1ce

URL: https://github.com/llvm/llvm-project/commit/af0f58cf14329f5e0fe56fed7c9eb4cd3107a1ce
DIFF: https://github.com/llvm/llvm-project/commit/af0f58cf14329f5e0fe56fed7c9eb4cd3107a1ce.diff

LOG: [libc][math][c23] Add entrypoints and tests for fsqrt{,l,f128} (#99669)

Added: 
    libc/src/math/fsqrt.h
    libc/src/math/fsqrtf128.h
    libc/src/math/fsqrtl.h
    libc/src/math/generic/fsqrt.cpp
    libc/src/math/generic/fsqrtf128.cpp
    libc/src/math/generic/fsqrtl.cpp
    libc/test/src/math/fsqrt_test.cpp
    libc/test/src/math/fsqrtl_test.cpp
    libc/test/src/math/smoke/fsqrt_test.cpp
    libc/test/src/math/smoke/fsqrtf128_test.cpp
    libc/test/src/math/smoke/fsqrtl_test.cpp

Modified: 
    libc/config/linux/x86_64/entrypoints.txt
    libc/docs/math/index.rst
    libc/spec/llvm_libc_ext.td
    libc/spec/stdc.td
    libc/src/math/CMakeLists.txt
    libc/src/math/generic/CMakeLists.txt
    libc/test/src/math/CMakeLists.txt
    libc/test/src/math/smoke/CMakeLists.txt
    libc/utils/MPFRWrapper/MPFRUtils.cpp

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index d4e246efb3f8a..736908809d96c 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -448,6 +448,8 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.fromfpx
     libc.src.math.fromfpxf
     libc.src.math.fromfpxl
+    libc.src.math.fsqrt
+    libc.src.math.fsqrtl
     libc.src.math.hypot
     libc.src.math.hypotf
     libc.src.math.ilogb
@@ -659,6 +661,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
     libc.src.math.frexpf128
     libc.src.math.fromfpf128
     libc.src.math.fromfpxf128
+    libc.src.math.fsqrtf128
     libc.src.math.ilogbf128
     libc.src.math.ldexpf128
     libc.src.math.llogbf128

diff  --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index cff7f69deb7f8..5cf9d8a42929e 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -300,7 +300,7 @@ Higher Math Functions
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | f16sqrt   | |check|\*        | |check|\*       | |check|\*              | N/A                  | |check|                | 7.12.14.6              | F.10.11                    |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| fsqrt     | N/A              |                 |                        | N/A                  |                        | 7.12.14.6              | F.10.11                    |
+| fsqrt     | N/A              | |check|         |  |check|               | N/A                  | |check|\*              | 7.12.14.6              | F.10.11                    |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | hypot     | |check|          | |check|         |                        |                      |                        | 7.12.7.4               | F.10.4.4                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

diff  --git a/libc/spec/llvm_libc_ext.td b/libc/spec/llvm_libc_ext.td
index 55b354cae74e0..6291a9ceec60d 100644
--- a/libc/spec/llvm_libc_ext.td
+++ b/libc/spec/llvm_libc_ext.td
@@ -85,6 +85,8 @@ def LLVMLibcExt : StandardSpec<"llvm_libc_ext"> {
           GuardedFunctionSpec<"f16sqrtf", RetValSpec<Float16Type>, [ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
           GuardedFunctionSpec<"f16sqrtl", RetValSpec<Float16Type>, [ArgSpec<LongDoubleType>], "LIBC_TYPES_HAS_FLOAT16">,
 
+          GuardedFunctionSpec<"fsqrtf128", RetValSpec<FloatType>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
+
           FunctionSpec<"powi", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>]>,
           FunctionSpec<"powif", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntType>]>,
       ]

diff  --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 285166ef0be78..437d6d04faf96 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -739,6 +739,9 @@ def StdC : StandardSpec<"stdc"> {
 
           GuardedFunctionSpec<"f16mulf128", RetValSpec<Float16Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">,
 
+          FunctionSpec<"fsqrt", RetValSpec<FloatType>, [ArgSpec<DoubleType>]>,
+          FunctionSpec<"fsqrtl", RetValSpec<FloatType>, [ArgSpec<LongDoubleType>]>,
+	  
           GuardedFunctionSpec<"f16divf128", RetValSpec<Float16Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">,
 
           GuardedFunctionSpec<"f16sqrtf128", RetValSpec<Float16Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">,

diff  --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 050bf0f1ebf22..82dd23186187b 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -131,6 +131,10 @@ add_math_entrypoint_object(f16sqrtf)
 add_math_entrypoint_object(f16sqrtl)
 add_math_entrypoint_object(f16sqrtf128)
 
+add_math_entrypoint_object(fsqrt)
+add_math_entrypoint_object(fsqrtl)
+add_math_entrypoint_object(fsqrtf128)
+
 add_math_entrypoint_object(f16sub)
 add_math_entrypoint_object(f16subf)
 add_math_entrypoint_object(f16subl)

diff  --git a/libc/src/math/fsqrt.h b/libc/src/math/fsqrt.h
new file mode 100644
index 0000000000000..e6a8c3179f811
--- /dev/null
+++ b/libc/src/math/fsqrt.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for fsqrt -------------------------*- 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_FSQRT_H
+#define LLVM_LIBC_SRC_MATH_FSQRT_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float fsqrt(double x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FSQRT_H

diff  --git a/libc/src/math/fsqrtf128.h b/libc/src/math/fsqrtf128.h
new file mode 100644
index 0000000000000..5286cbc33116d
--- /dev/null
+++ b/libc/src/math/fsqrtf128.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for fsqrtf128 ---------------------*- 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_FSQRTF128_H
+#define LLVM_LIBC_SRC_MATH_FSQRTF128_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float fsqrtf128(float128 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FSQRTF128_H

diff  --git a/libc/src/math/fsqrtl.h b/libc/src/math/fsqrtl.h
new file mode 100644
index 0000000000000..a50eea3335458
--- /dev/null
+++ b/libc/src/math/fsqrtl.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for fsqrtl ------------------------*- 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_FSQRTL_H
+#define LLVM_LIBC_SRC_MATH_FSQRTL_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float fsqrtl(long double x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FSQRTL_H

diff  --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 8e8ffb01adf60..7712a6d1ae527 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4201,6 +4201,43 @@ add_entrypoint_object(
     -O3
 )
 
+add_entrypoint_object(
+  fsqrt
+  SRCS
+    fsqrt.cpp
+  HDRS
+    ../fsqrt.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.sqrt
+  COMPILE_OPTIONS
+    -O3
+)
+
+add_entrypoint_object(
+  fsqrtl
+  SRCS
+    fsqrtl.cpp
+  HDRS
+    ../fsqrtl.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.sqrt
+  COMPILE_OPTIONS
+    -O3
+)
+
+add_entrypoint_object(
+  fsqrtf128
+  SRCS
+    fsqrtf128.cpp
+  HDRS
+    ../fsqrtf128.h
+  DEPENDS
+    libc.src.__support.macros.properties.types
+    libc.src.__support.FPUtil.generic.sqrt
+  COMPILE_OPTIONS
+    -O3
+)
+
 add_entrypoint_object(
   cbrtf
   SRCS

diff  --git a/libc/src/math/generic/fsqrt.cpp b/libc/src/math/generic/fsqrt.cpp
new file mode 100644
index 0000000000000..d54471fd067bf
--- /dev/null
+++ b/libc/src/math/generic/fsqrt.cpp
@@ -0,0 +1,18 @@
+//===-- Implementation of fsqrt 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/fsqrt.h"
+#include "src/__support/FPUtil/generic/sqrt.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, fsqrt, (double x)) { return fputil::sqrt<float>(x); }
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/math/generic/fsqrtf128.cpp b/libc/src/math/generic/fsqrtf128.cpp
new file mode 100644
index 0000000000000..f2c049529d6cd
--- /dev/null
+++ b/libc/src/math/generic/fsqrtf128.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of fsqrt128 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/fsqrtf128.h"
+#include "src/__support/FPUtil/generic/sqrt.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, fsqrtf128, (float128 x)) {
+  return fputil::sqrt<float>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/math/generic/fsqrtl.cpp b/libc/src/math/generic/fsqrtl.cpp
new file mode 100644
index 0000000000000..b896a84aeded8
--- /dev/null
+++ b/libc/src/math/generic/fsqrtl.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of fsqrtl 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/fsqrtl.h"
+#include "src/__support/FPUtil/generic/sqrt.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, fsqrtl, (long double x)) {
+  return fputil::sqrt<float>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index f69b0d9b1b666..586338b2213de 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2245,6 +2245,32 @@ add_fp_unittest(
     libc.src.math.f16sqrtl
 )
 
+add_fp_unittest(
+  fsqrt_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    fsqrt_test.cpp
+  HDRS
+    SqrtTest.h
+  DEPENDS
+    libc.src.math.fsqrt
+)
+
+add_fp_unittest(
+  fsqrtl_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    fsqrtl_test.cpp
+  HDRS
+    SqrtTest.h
+  DEPENDS
+    libc.src.math.fsqrtl
+)
+
 add_fp_unittest(
   cbrtf_test
   NEED_MPFR

diff  --git a/libc/test/src/math/fsqrt_test.cpp b/libc/test/src/math/fsqrt_test.cpp
new file mode 100644
index 0000000000000..8471e3c9365e9
--- /dev/null
+++ b/libc/test/src/math/fsqrt_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for fsqrt -----------------------------------------------===//
+//
+// 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/fsqrt.h"
+
+LIST_NARROWING_SQRT_TESTS(float, double, LIBC_NAMESPACE::fsqrt)

diff  --git a/libc/test/src/math/fsqrtl_test.cpp b/libc/test/src/math/fsqrtl_test.cpp
new file mode 100644
index 0000000000000..1082a33466857
--- /dev/null
+++ b/libc/test/src/math/fsqrtl_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for fsqrtl ----------------------------------------------===//
+//
+// 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/fsqrtl.h"
+
+LIST_NARROWING_SQRT_TESTS(float, long double, LIBC_NAMESPACE::fsqrtl)

diff  --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 69f53c5d27f15..f7caa03d11646 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3959,6 +3959,43 @@ add_fp_unittest(
     libc.src.math.f16sqrtf128
 )
 
+add_fp_unittest(
+  fsqrt_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    fsqrt_test.cpp
+  HDRS
+    SqrtTest.h
+  DEPENDS
+    libc.src.math.fsqrt
+)
+
+
+add_fp_unittest(
+  fsqrtl_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    fsqrtl_test.cpp
+  HDRS
+    SqrtTest.h
+  DEPENDS
+    libc.src.math.fsqrtl
+)
+
+add_fp_unittest(
+  fsqrtf128_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    fsqrtf128_test.cpp
+  HDRS
+    SqrtTest.h
+  DEPENDS
+    libc.src.math.fsqrtf128
+)
+
 add_fp_unittest(
   sin_test
   SUITE

diff  --git a/libc/test/src/math/smoke/fsqrt_test.cpp b/libc/test/src/math/smoke/fsqrt_test.cpp
new file mode 100644
index 0000000000000..8471e3c9365e9
--- /dev/null
+++ b/libc/test/src/math/smoke/fsqrt_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for fsqrt -----------------------------------------------===//
+//
+// 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/fsqrt.h"
+
+LIST_NARROWING_SQRT_TESTS(float, double, LIBC_NAMESPACE::fsqrt)

diff  --git a/libc/test/src/math/smoke/fsqrtf128_test.cpp b/libc/test/src/math/smoke/fsqrtf128_test.cpp
new file mode 100644
index 0000000000000..b125a35b09b9e
--- /dev/null
+++ b/libc/test/src/math/smoke/fsqrtf128_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for fsqrtf128 -------------------------------------------===//
+//
+// 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/fsqrtf128.h"
+
+LIST_NARROWING_SQRT_TESTS(float, float128, LIBC_NAMESPACE::fsqrtf128)

diff  --git a/libc/test/src/math/smoke/fsqrtl_test.cpp b/libc/test/src/math/smoke/fsqrtl_test.cpp
new file mode 100644
index 0000000000000..1082a33466857
--- /dev/null
+++ b/libc/test/src/math/smoke/fsqrtl_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for fsqrtl ----------------------------------------------===//
+//
+// 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/fsqrtl.h"
+
+LIST_NARROWING_SQRT_TESTS(float, long double, LIBC_NAMESPACE::fsqrtl)

diff  --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index bb63e0b9f4de0..d2c28c19ab176 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -887,6 +887,13 @@ template void explain_unary_operation_single_output_error(Operation op,
                                                           long double,
                                                           long double, double,
                                                           RoundingMode);
+template void explain_unary_operation_single_output_error(Operation op, double,
+                                                          float, double,
+                                                          RoundingMode);
+template void explain_unary_operation_single_output_error(Operation op,
+                                                          long double, float,
+                                                          double, RoundingMode);
+
 #ifdef LIBC_TYPES_HAS_FLOAT16
 template void explain_unary_operation_single_output_error(Operation op, float16,
                                                           float16, double,
@@ -1106,6 +1113,11 @@ template bool compare_unary_operation_single_output(Operation, double, double,
 template bool compare_unary_operation_single_output(Operation, long double,
                                                     long double, double,
                                                     RoundingMode);
+template bool compare_unary_operation_single_output(Operation, double, float,
+                                                    double, RoundingMode);
+template bool compare_unary_operation_single_output(Operation, long double,
+                                                    float, double,
+                                                    RoundingMode);
 #ifdef LIBC_TYPES_HAS_FLOAT16
 template bool compare_unary_operation_single_output(Operation, float16, float16,
                                                     double, RoundingMode);


        


More information about the libc-commits mailing list