[libc-commits] [libc] [libc][math] Add getpayloadl function. (PR #102214)

Job Henandez Lara via libc-commits libc-commits at lists.llvm.org
Tue Aug 6 20:46:58 PDT 2024


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

>From ece34b7838800c321dcc114a0d484221535642e3 Mon Sep 17 00:00:00 2001
From: Job Hernandez <hj93 at protonmail.com>
Date: Tue, 6 Aug 2024 13:19:41 -0700
Subject: [PATCH 1/4] [libc][math] debug getpayloadl

---
 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/spec/stdc.td                             |  2 +-
 libc/src/math/CMakeLists.txt                  |  1 +
 libc/src/math/generic/CMakeLists.txt          | 12 +++++++++++
 libc/src/math/generic/getpayloadl.cpp         | 20 +++++++++++++++++++
 libc/src/math/getpayloadl.h                   | 20 +++++++++++++++++++
 libc/test/src/math/smoke/CMakeLists.txt       | 12 +++++++++++
 libc/test/src/math/smoke/GetPayloadTest.h     |  4 ++--
 libc/test/src/math/smoke/getpayloadl_test.cpp | 13 ++++++++++++
 13 files changed, 86 insertions(+), 4 deletions(-)
 create mode 100644 libc/src/math/generic/getpayloadl.cpp
 create mode 100644 libc/src/math/getpayloadl.h
 create mode 100644 libc/test/src/math/smoke/getpayloadl_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index e211e9a3d557c..d78fdf8a8901b 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -463,6 +463,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.fsqrtl
     libc.src.math.getpayload
     libc.src.math.getpayloadf
+    libc.src.math.getpayloadl
     libc.src.math.hypot
     libc.src.math.hypotf
     libc.src.math.ilogb
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 8a214109f4abc..36e940af4fb6b 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -299,6 +299,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.fromfpxl
     libc.src.math.getpayload
     libc.src.math.getpayloadf
+    libc.src.math.getpayloadl
     libc.src.math.hypot
     libc.src.math.hypotf
     libc.src.math.ilogb
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 8e2eb36f1b89a..f117c5fc608fa 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -466,6 +466,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.fsqrtl
     libc.src.math.getpayload
     libc.src.math.getpayloadf
+    libc.src.math.getpayloadl
     libc.src.math.hypot
     libc.src.math.hypotf
     libc.src.math.ilogb
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 2b76487218b13..c2bde60c0285d 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -466,6 +466,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.fsqrtl
     libc.src.math.getpayload
     libc.src.math.getpayloadf
+    libc.src.math.getpayloadl
     libc.src.math.hypot
     libc.src.math.hypotf
     libc.src.math.ilogb
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index 92e5fb700d536..315192027d914 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -178,7 +178,7 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | fsub             | N/A              |                 |                        | N/A                  |                        | 7.12.14.2              | F.10.11                    |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| getpayload       | |check|          | |check|         |                        | |check|              | |check|                | F.10.13.1              | N/A                        |
+| getpayload       | |check|          | |check|         | |check|                | |check|              | |check|                | F.10.13.1              | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | ilogb            | |check|          | |check|         | |check|                | |check|              | |check|                | 7.12.6.8               | F.10.3.8                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index ca7bc5c9b5744..123f182e8d614 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -746,7 +746,7 @@ def StdC : StandardSpec<"stdc"> {
 	  GuardedFunctionSpec<"totalordermagf128", RetValSpec<IntType>, [ArgSpec<Float128Ptr>, ArgSpec<Float128Ptr>], "LIBC_TYPES_HAS_FLOAT128">,
 
           FunctionSpec<"getpayload", RetValSpec<DoubleType>, [ArgSpec<DoublePtr>]>,
-          FunctionSpec<"getpayloadf", RetValSpec<FloatType>, [ArgSpec<FloatPtr>]>,
+	  FunctionSpec<"getpayloadf", RetValSpec<FloatType>, [ArgSpec<FloatPtr>]>,	          FunctionSpec<"getpayloadl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoublePtr>]>,
           GuardedFunctionSpec<"getpayloadf16", RetValSpec<Float16Type>, [ArgSpec<Float16Ptr>], "LIBC_TYPES_HAS_FLOAT16">,
           GuardedFunctionSpec<"getpayloadf128", RetValSpec<Float128Type>, [ArgSpec<Float128Ptr>], "LIBC_TYPES_HAS_FLOAT128">,
 	  
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 6accddb7e5e84..a1afed59a7b13 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -274,6 +274,7 @@ add_math_entrypoint_object(fromfpxf128)
 
 add_math_entrypoint_object(getpayload)
 add_math_entrypoint_object(getpayloadf)
+add_math_entrypoint_object(getpayloadl)
 add_math_entrypoint_object(getpayloadf16)
 add_math_entrypoint_object(getpayloadf128)
 
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 445c070243999..fecbcf41678e8 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4336,6 +4336,18 @@ add_entrypoint_object(
     -O3
 )
 
+add_entrypoint_object(
+  getpayloadl
+  SRCS
+    getpayloadl.cpp
+  HDRS
+    ../getpayloadl.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+  COMPILE_OPTIONS
+    -O3
+)
+
 add_entrypoint_object(
   getpayloadf16
   SRCS
diff --git a/libc/src/math/generic/getpayloadl.cpp b/libc/src/math/generic/getpayloadl.cpp
new file mode 100644
index 0000000000000..028dc1e2611c0
--- /dev/null
+++ b/libc/src/math/generic/getpayloadl.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of getpayloadl 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/getpayloadl.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(long double, getpayloadl, (const long double *x)) {
+  return fputil::getpayload(*x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/getpayloadl.h b/libc/src/math/getpayloadl.h
new file mode 100644
index 0000000000000..1ae9f86c19e00
--- /dev/null
+++ b/libc/src/math/getpayloadl.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for getpayloadl -------------------*- 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_GETPAYLOADL_H
+#define LLVM_LIBC_SRC_MATH_GETPAYLOADL_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+long double getpayloadl(const long double *x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_GETPAYLOADL_H
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index e1c95b6196a8d..007c3346e0568 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3861,6 +3861,18 @@ add_fp_unittest(
     libc.src.math.getpayloadf
 )
 
+add_fp_unittest(
+  getpayloadl_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    getpayloadl_test.cpp
+  HDRS
+    GetPayloadTest.h
+  DEPENDS
+    libc.src.math.getpayloadl
+)
+
 add_fp_unittest(
   getpayloadf16_test
   SUITE
diff --git a/libc/test/src/math/smoke/GetPayloadTest.h b/libc/test/src/math/smoke/GetPayloadTest.h
index 922a2f04f6fb3..d904571269e13 100644
--- a/libc/test/src/math/smoke/GetPayloadTest.h
+++ b/libc/test/src/math/smoke/GetPayloadTest.h
@@ -39,10 +39,10 @@ class GetPayloadTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(T(0.0), funcWrapper(func, neg_aNaN));
 
     // Essentially this:
-    //   T default_snan_payload = StorageType(1) << (FPBits::SIG_LEN - 2);
+    //   T default_snan_payload = StorageType(1) << (FPBits::FRACTION_LEN - 2);
     // but supports StorageType being a BigInt.
     FPBits default_snan_payload_bits = FPBits::one();
-    default_snan_payload_bits.set_biased_exponent(FPBits::SIG_LEN - 2 +
+    default_snan_payload_bits.set_biased_exponent(FPBits::FRACTION_LEN - 2 +
                                                   FPBits::EXP_BIAS);
     T default_snan_payload = default_snan_payload_bits.get_val();
 
diff --git a/libc/test/src/math/smoke/getpayloadl_test.cpp b/libc/test/src/math/smoke/getpayloadl_test.cpp
new file mode 100644
index 0000000000000..d783548c67032
--- /dev/null
+++ b/libc/test/src/math/smoke/getpayloadl_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for getpayloadl -----------------------------------------===//
+//
+// 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 "GetPayloadTest.h"
+
+#include "src/math/getpayloadl.h"
+
+LIST_GETPAYLOAD_TESTS(long double, LIBC_NAMESPACE::getpayloadl)

>From 4bb8c6d3fc1a8b3fd9c263d193354520bb5a1488 Mon Sep 17 00:00:00 2001
From: Job Hernandez <hj93 at protonmail.com>
Date: Tue, 6 Aug 2024 18:29:42 -0700
Subject: [PATCH 2/4] address review

---
 libc/spec/stdc.td | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 123f182e8d614..e11f0db507b16 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -746,7 +746,8 @@ def StdC : StandardSpec<"stdc"> {
 	  GuardedFunctionSpec<"totalordermagf128", RetValSpec<IntType>, [ArgSpec<Float128Ptr>, ArgSpec<Float128Ptr>], "LIBC_TYPES_HAS_FLOAT128">,
 
           FunctionSpec<"getpayload", RetValSpec<DoubleType>, [ArgSpec<DoublePtr>]>,
-	  FunctionSpec<"getpayloadf", RetValSpec<FloatType>, [ArgSpec<FloatPtr>]>,	          FunctionSpec<"getpayloadl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoublePtr>]>,
+	  FunctionSpec<"getpayloadf", RetValSpec<FloatType>, [ArgSpec<FloatPtr>]>,
+          FunctionSpec<"getpayloadl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoublePtr>]>,
           GuardedFunctionSpec<"getpayloadf16", RetValSpec<Float16Type>, [ArgSpec<Float16Ptr>], "LIBC_TYPES_HAS_FLOAT16">,
           GuardedFunctionSpec<"getpayloadf128", RetValSpec<Float128Type>, [ArgSpec<Float128Ptr>], "LIBC_TYPES_HAS_FLOAT128">,
 	  

>From 2432533eebf8b0420772419ca21ca3c1cd8570e8 Mon Sep 17 00:00:00 2001
From: Job Hernandez <hj93 at protonmail.com>
Date: Tue, 6 Aug 2024 20:26:24 -0700
Subject: [PATCH 3/4] address review

---
 libc/newhdrgen/yaml/math.yaml | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/libc/newhdrgen/yaml/math.yaml b/libc/newhdrgen/yaml/math.yaml
index 79d0e9863e9ae..a76d6cce19211 100644
--- a/libc/newhdrgen/yaml/math.yaml
+++ b/libc/newhdrgen/yaml/math.yaml
@@ -1540,6 +1540,24 @@ functions:
       - type: int
       - type: unsigned int
     guard: LIBC_TYPES_HAS_FLOAT16
+  - name: getpayload
+    standards:
+      - stdc
+    return_type: double
+    arguments:
+      - double *
+  - name: getpayloadl
+    standards:
+      - stdc
+    return_type: long double
+    arguments:
+      - long double *
+  - name: getpayloadf
+    standards:
+      - stdc
+    return_type: float
+    arguments:
+      - float *     
   - name: getpayloadf16
     standards:
       - stdc
@@ -1547,6 +1565,13 @@ functions:
     arguments:
       - type: _Float16 *
     guard: LIBC_TYPES_HAS_FLOAT16
+  - name: getpayloadf128
+    standards:
+      - stdc
+    return_type: float128
+    arguments:
+      - type: float128 *
+    guard: LIBC_TYPES_HAS_FLOAT128
   - name: ilogbf16
     standards:
       - stdc

>From b63271c172d1e0ec5c867ddf63f778dd0723103e Mon Sep 17 00:00:00 2001
From: Job Hernandez <hj93 at protonmail.com>
Date: Tue, 6 Aug 2024 20:46:42 -0700
Subject: [PATCH 4/4] address review

---
 libc/newhdrgen/yaml/math.yaml | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libc/newhdrgen/yaml/math.yaml b/libc/newhdrgen/yaml/math.yaml
index a76d6cce19211..62cde78fcc158 100644
--- a/libc/newhdrgen/yaml/math.yaml
+++ b/libc/newhdrgen/yaml/math.yaml
@@ -1545,19 +1545,19 @@ functions:
       - stdc
     return_type: double
     arguments:
-      - double *
+      - type: double *
   - name: getpayloadl
     standards:
       - stdc
     return_type: long double
     arguments:
-      - long double *
+      - type: long double *
   - name: getpayloadf
     standards:
       - stdc
     return_type: float
     arguments:
-      - float *     
+      - type: float *     
   - name: getpayloadf16
     standards:
       - stdc



More information about the libc-commits mailing list