[libc-commits] [libc] [libc][math][c23] Add totalorderl function. (PR #102564)

Job Henandez Lara via libc-commits libc-commits at lists.llvm.org
Thu Aug 8 22:30:31 PDT 2024


https://github.com/Jobhdez updated https://github.com/llvm/llvm-project/pull/102564

>From 0cb9d9ed212627b7f80e6a50fd33a801d4a05b43 Mon Sep 17 00:00:00 2001
From: Job Hernandez <hj93 at protonmail.com>
Date: Thu, 8 Aug 2024 19:50:27 -0700
Subject: [PATCH 1/5] [libc][math][c23] Add totalorderl function.

---
 libc/config/linux/aarch64/entrypoints.txt     |  1 +
 libc/config/linux/arm/entrypoints.txt         |  1 +
 libc/config/linux/riscv/entrypoints.txt       |  1 +
 libc/config/linux/x86_64/entrypoints.txt      |  1 +
 libc/docs/math/index.rst                      |  2 +-
 libc/newhdrgen/yaml/math.yaml                 | 29 +++++++++++++++++++
 libc/spec/stdc.td                             |  1 +
 libc/src/math/CMakeLists.txt                  |  1 +
 libc/src/math/generic/CMakeLists.txt          | 12 ++++++++
 libc/src/math/generic/totalorderl.cpp         | 20 +++++++++++++
 libc/src/math/totalorderl.h                   | 20 +++++++++++++
 libc/test/src/math/smoke/CMakeLists.txt       | 12 ++++++++
 libc/test/src/math/smoke/totalorderl_test.cpp | 13 +++++++++
 13 files changed, 113 insertions(+), 1 deletion(-)
 create mode 100644 libc/src/math/generic/totalorderl.cpp
 create mode 100644 libc/src/math/totalorderl.h
 create mode 100644 libc/test/src/math/smoke/totalorderl_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index bb0ebca29e6ae2..75e010e63c92df 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -568,6 +568,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.tanhf
     libc.src.math.totalorder
     libc.src.math.totalorderf
+    libc.src.math.totalorderl
     libc.src.math.totalordermag
     libc.src.math.totalordermagf
     libc.src.math.totalordermagl
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 7fd60799bbcc72..02265aef6fdfb4 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -396,6 +396,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.tanhf
     libc.src.math.totalorder
     libc.src.math.totalorderf
+    libc.src.math.totalorderl
     libc.src.math.totalordermag
     libc.src.math.totalordermagf
     libc.src.math.totalordermagl
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 0d48b55a965483..f80e741115e793 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -571,6 +571,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.tanhf
     libc.src.math.totalorder
     libc.src.math.totalorderf
+    libc.src.math.totalorderl
     libc.src.math.totalordermag
     libc.src.math.totalordermagf
     libc.src.math.totalordermagl
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index b9134c8496c30a..683ac8b1c71e37 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -571,6 +571,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.tanhf
     libc.src.math.totalorder
     libc.src.math.totalorderf
+    libc.src.math.totalorderl
     libc.src.math.totalordermag
     libc.src.math.totalordermagf
     libc.src.math.totalordermagl
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index 9185a95192963e..b617289d1364fd 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -228,7 +228,7 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | setpayloadsig    | |check|          | |check|         | |check|                | |check|              | |check|                | F.10.13.3              | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| totalorder       | |check|          | |check|         |                        | |check|              | |check|                | F.10.12.1              | N/A                        |
+| totalorder       | |check|          | |check|         | |check|                | |check|              | |check|                | F.10.12.1              | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | totalordermag    | |check|          | |check|         | |check|                | |check|              | |check|                | F.10.12.2              | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/newhdrgen/yaml/math.yaml b/libc/newhdrgen/yaml/math.yaml
index 2f1203e581f9a7..f9e1834091f4ec 100644
--- a/libc/newhdrgen/yaml/math.yaml
+++ b/libc/newhdrgen/yaml/math.yaml
@@ -1839,6 +1839,27 @@ functions:
       - type: float128 *
       - type: float128
     guard: LIBC_TYPES_HAS_FLOAT128
+  - name: totalorder
+    standards:
+      - stdc
+    return_type: int
+    arguments:
+      - type: double *
+      - type: double *
+  - name: totalorderf
+    standards:
+      - stdc
+    return_type: int
+    arguments:
+      - type: float *
+      - type: float *
+  - name: totalorderl
+    standards:
+      - stdc
+    return_type: int
+    arguments:
+      - type: long double *
+      - type: long double *
   - name: totalorderf16
     standards:
       - stdc
@@ -1847,6 +1868,14 @@ functions:
       - type: _Float16 *
       - type: _Float16 *
     guard: LIBC_TYPES_HAS_FLOAT16
+  - name: totalorderf16
+    standards:
+      - stdc
+    return_type: int
+    arguments:
+      - type: float128 *
+      - type: float128 *
+    guard: LIBC_TYPES_HAS_FLOAT16
   - name: totalordermagf16
     standards:
       - stdc
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 72bfe0cf71aa2b..94fc5cb0ec1028 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -745,6 +745,7 @@ def StdC : StandardSpec<"stdc"> {
 
           FunctionSpec<"totalorder", RetValSpec<IntType>, [ArgSpec<DoublePtr>, ArgSpec<DoublePtr>]>,
 	  FunctionSpec<"totalorderf", RetValSpec<IntType>, [ArgSpec<FloatPtr>, ArgSpec<FloatPtr>]>,
+	  FunctionSpec<"totalorderf", RetValSpec<IntType>, [ArgSpec<LongDoublePtr>, ArgSpec<LongDoublePtr>]>,
 	  GuardedFunctionSpec<"totalorderf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Ptr>], "LIBC_TYPES_HAS_FLOAT16">,
 	  GuardedFunctionSpec<"totalorderf128", RetValSpec<IntType>, [ArgSpec<Float128Ptr>, ArgSpec<Float128Ptr>], "LIBC_TYPES_HAS_FLOAT128">,
 
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index e2ebf2ddd4bfa4..7f1800ab3480d7 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -479,6 +479,7 @@ add_math_entrypoint_object(lgamma_r)
 
 add_math_entrypoint_object(totalorder)
 add_math_entrypoint_object(totalorderf)
+add_math_entrypoint_object(totalorderl)
 add_math_entrypoint_object(totalorderf16)
 add_math_entrypoint_object(totalorderf128)
 
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index c80c7ca7f7af11..534036aa364560 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4368,6 +4368,18 @@ add_entrypoint_object(
     -O3
 )
 
+add_entrypoint_object(
+  totalorderl
+  SRCS
+    totalorderl.cpp
+  HDRS
+    ../totalorderl.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+  COMPILE_OPTIONS
+    -O3
+)
+
 add_entrypoint_object(
   totalorderf16
   SRCS
diff --git a/libc/src/math/generic/totalorderl.cpp b/libc/src/math/generic/totalorderl.cpp
new file mode 100644
index 00000000000000..f3fa2b2c01f6f4
--- /dev/null
+++ b/libc/src/math/generic/totalorderl.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of totalorderl 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/totalorderl.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, totalorderl, (const long double *x, const long double *y)) {
+  return static_cast<int>(fputil::totalorder(*x, *y));
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/totalorderl.h b/libc/src/math/totalorderl.h
new file mode 100644
index 00000000000000..064b2a514fa8e9
--- /dev/null
+++ b/libc/src/math/totalorderl.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for totalorderl -------------------*- 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_TOTALORDERL_H
+#define LLVM_LIBC_SRC_MATH_TOTALORDERL_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int totalorderl(const long double *x, const long double *y);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_TOTALORDERL_H
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 609ecef42f7459..9fe61672f7258d 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3837,6 +3837,18 @@ add_fp_unittest(
     libc.src.math.totalorderf
 )
 
+add_fp_unittest(
+  totalorderl_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    totalorderl_test.cpp
+  HDRS
+    TotalOrderTest.h
+  DEPENDS
+    libc.src.math.totalorderl
+)
+
 add_fp_unittest(
   totalorderf16_test
   SUITE
diff --git a/libc/test/src/math/smoke/totalorderl_test.cpp b/libc/test/src/math/smoke/totalorderl_test.cpp
new file mode 100644
index 00000000000000..fc2353fce3859a
--- /dev/null
+++ b/libc/test/src/math/smoke/totalorderl_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for totalorderl -----------------------------------------===//
+//
+// 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 "TotalOrderTest.h"
+
+#include "src/math/totalorderl.h"
+
+LIST_TOTALORDER_TESTS(long double, LIBC_NAMESPACE::totalorderl)

>From c78a55e43b42c6659b45096b1609a2ec58323bf7 Mon Sep 17 00:00:00 2001
From: Job Hernandez <hj93 at protonmail.com>
Date: Thu, 8 Aug 2024 20:05:24 -0700
Subject: [PATCH 2/5] address review

---
 libc/newhdrgen/yaml/math.yaml | 4 ++--
 libc/spec/stdc.td             | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/libc/newhdrgen/yaml/math.yaml b/libc/newhdrgen/yaml/math.yaml
index f9e1834091f4ec..147a62cb749990 100644
--- a/libc/newhdrgen/yaml/math.yaml
+++ b/libc/newhdrgen/yaml/math.yaml
@@ -1868,14 +1868,14 @@ functions:
       - type: _Float16 *
       - type: _Float16 *
     guard: LIBC_TYPES_HAS_FLOAT16
-  - name: totalorderf16
+  - name: totalorderf128
     standards:
       - stdc
     return_type: int
     arguments:
       - type: float128 *
       - type: float128 *
-    guard: LIBC_TYPES_HAS_FLOAT16
+    guard: LIBC_TYPES_HAS_FLOAT128
   - name: totalordermagf16
     standards:
       - stdc
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 94fc5cb0ec1028..0ebfa9ac896ebc 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -745,7 +745,7 @@ def StdC : StandardSpec<"stdc"> {
 
           FunctionSpec<"totalorder", RetValSpec<IntType>, [ArgSpec<DoublePtr>, ArgSpec<DoublePtr>]>,
 	  FunctionSpec<"totalorderf", RetValSpec<IntType>, [ArgSpec<FloatPtr>, ArgSpec<FloatPtr>]>,
-	  FunctionSpec<"totalorderf", RetValSpec<IntType>, [ArgSpec<LongDoublePtr>, ArgSpec<LongDoublePtr>]>,
+	  FunctionSpec<"totalorderl", RetValSpec<IntType>, [ArgSpec<LongDoublePtr>, ArgSpec<LongDoublePtr>]>,
 	  GuardedFunctionSpec<"totalorderf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Ptr>], "LIBC_TYPES_HAS_FLOAT16">,
 	  GuardedFunctionSpec<"totalorderf128", RetValSpec<IntType>, [ArgSpec<Float128Ptr>, ArgSpec<Float128Ptr>], "LIBC_TYPES_HAS_FLOAT128">,
 

>From 72a3206f3d5306d7117fd5afa47db0f584a9d904 Mon Sep 17 00:00:00 2001
From: Job Hernandez <hj93 at protonmail.com>
Date: Thu, 8 Aug 2024 21:25:57 -0700
Subject: [PATCH 3/5] fix bug

---
 libc/src/__support/FPUtil/BasicOperations.h | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index 8131c9b663cfe4..824c9bfb3947f3 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -321,12 +321,8 @@ totalorder(T x, T y) {
   StorageType x_u = x_bits.uintval();
   StorageType y_u = y_bits.uintval();
 
-  using signed_t = make_integral_or_big_int_signed_t<StorageType>;
-  signed_t x_signed = static_cast<signed_t>(x_u);
-  signed_t y_signed = static_cast<signed_t>(y_u);
-
-  bool both_neg = (x_u & y_u & FPBits::SIGN_MASK) != 0;
-  return x_signed == y_signed || ((x_signed <= y_signed) != both_neg);
+  bool has_neg = ((x_u | y_u) & FPBits::SIGN_MASK) != 0;
+  return x_u == y_u || ((x_u < y_u) != has_neg);
 }
 
 template <typename T>

>From 88464be8c3d815d5d7e521f1d1d823ca45451e10 Mon Sep 17 00:00:00 2001
From: Job Hernandez <hj93 at protonmail.com>
Date: Thu, 8 Aug 2024 21:32:50 -0700
Subject: [PATCH 4/5] fix formatter

---
 libc/src/math/generic/totalorderl.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/src/math/generic/totalorderl.cpp b/libc/src/math/generic/totalorderl.cpp
index f3fa2b2c01f6f4..1aae1c6cd025a4 100644
--- a/libc/src/math/generic/totalorderl.cpp
+++ b/libc/src/math/generic/totalorderl.cpp
@@ -13,7 +13,8 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(int, totalorderl, (const long double *x, const long double *y)) {
+LLVM_LIBC_FUNCTION(int, totalorderl,
+                   (const long double *x, const long double *y)) {
   return static_cast<int>(fputil::totalorder(*x, *y));
 }
 

>From c75ef975bcf1da882e7398028e5fe92f692490da Mon Sep 17 00:00:00 2001
From: Job Hernandez <hj93 at protonmail.com>
Date: Thu, 8 Aug 2024 22:30:14 -0700
Subject: [PATCH 5/5] address review

---
 libc/newhdrgen/yaml/math.yaml | 20 ++++++++++----------
 libc/spec/spec.td             |  6 ++++++
 libc/spec/stdc.td             | 10 +++++-----
 3 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/libc/newhdrgen/yaml/math.yaml b/libc/newhdrgen/yaml/math.yaml
index 147a62cb749990..025f5fa48bf60b 100644
--- a/libc/newhdrgen/yaml/math.yaml
+++ b/libc/newhdrgen/yaml/math.yaml
@@ -1844,37 +1844,37 @@ functions:
       - stdc
     return_type: int
     arguments:
-      - type: double *
-      - type: double *
+      - type: const double *
+      - type: const double *
   - name: totalorderf
     standards:
       - stdc
     return_type: int
     arguments:
-      - type: float *
-      - type: float *
+      - type: const float *
+      - type: const float *
   - name: totalorderl
     standards:
       - stdc
     return_type: int
     arguments:
-      - type: long double *
-      - type: long double *
+      - type: const long double *
+      - type: const long double *
   - name: totalorderf16
     standards:
       - stdc
     return_type: int
     arguments:
-      - type: _Float16 *
-      - type: _Float16 *
+      - type: const _Float16 *
+      - type: const _Float16 *
     guard: LIBC_TYPES_HAS_FLOAT16
   - name: totalorderf128
     standards:
       - stdc
     return_type: int
     arguments:
-      - type: float128 *
-      - type: float128 *
+      - type: const float128 *
+      - type: const float128 *
     guard: LIBC_TYPES_HAS_FLOAT128
   - name: totalordermagf16
     standards:
diff --git a/libc/spec/spec.td b/libc/spec/spec.td
index a3a5db7465b39c..ad9ca76846c280 100644
--- a/libc/spec/spec.td
+++ b/libc/spec/spec.td
@@ -119,6 +119,12 @@ def Float16Ptr : PtrType<Float16Type>;
 def Float128Ptr : PtrType<Float128Type>;
 def UnsignedCharPtr : PtrType<UnsignedCharType>;
 
+def ConstDoublePtr : ConstType<DoublePtr>;
+def ConstFloatPtr : ConstType<FloatPtr>;
+def ConstLongDoublePtr : ConstType<LongDoublePtr>;
+def ConstFloat16Ptr : ConstType<Float16Ptr>;
+def ConstFloat128Ptr : ConstType<Float128Ptr>;
+
 def SigHandlerT : NamedType<"__sighandler_t">;
 
 def TimeTType : NamedType<"time_t">;
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 0ebfa9ac896ebc..b3b087ab3b58ad 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -743,11 +743,11 @@ def StdC : StandardSpec<"stdc"> {
 
           FunctionSpec<"dsqrtl", RetValSpec<DoubleType>, [ArgSpec<LongDoubleType>]>,
 
-          FunctionSpec<"totalorder", RetValSpec<IntType>, [ArgSpec<DoublePtr>, ArgSpec<DoublePtr>]>,
-	  FunctionSpec<"totalorderf", RetValSpec<IntType>, [ArgSpec<FloatPtr>, ArgSpec<FloatPtr>]>,
-	  FunctionSpec<"totalorderl", RetValSpec<IntType>, [ArgSpec<LongDoublePtr>, ArgSpec<LongDoublePtr>]>,
-	  GuardedFunctionSpec<"totalorderf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Ptr>], "LIBC_TYPES_HAS_FLOAT16">,
-	  GuardedFunctionSpec<"totalorderf128", RetValSpec<IntType>, [ArgSpec<Float128Ptr>, ArgSpec<Float128Ptr>], "LIBC_TYPES_HAS_FLOAT128">,
+          FunctionSpec<"totalorder", RetValSpec<IntType>, [ArgSpec<ConstDoublePtr>, ArgSpec<ConstDoublePtr>]>,
+	  FunctionSpec<"totalorderf", RetValSpec<IntType>, [ArgSpec<ConstFloatPtr>, ArgSpec<ConstFloatPtr>]>,
+	  FunctionSpec<"totalorderl", RetValSpec<IntType>, [ArgSpec<ConstLongDoublePtr>, ArgSpec<ConstLongDoublePtr>]>,
+	  GuardedFunctionSpec<"totalorderf16", RetValSpec<IntType>, [ArgSpec<ConstFloat16Ptr>, ArgSpec<ConstFloat16Ptr>], "LIBC_TYPES_HAS_FLOAT16">,
+	  GuardedFunctionSpec<"totalorderf128", RetValSpec<IntType>, [ArgSpec<ConstFloat128Ptr>, ArgSpec<ConstFloat128Ptr>], "LIBC_TYPES_HAS_FLOAT128">,
 
           FunctionSpec<"totalordermag", RetValSpec<IntType>, [ArgSpec<DoublePtr>, ArgSpec<DoublePtr>]>,
 	  FunctionSpec<"totalordermagf", RetValSpec<IntType>, [ArgSpec<FloatPtr>, ArgSpec<FloatPtr>]>,



More information about the libc-commits mailing list