[libc-commits] [libc] [libc] Proof of concept of aliasing long double math functions. (PR #132627)

via libc-commits libc-commits at lists.llvm.org
Sun Mar 23 11:24:12 PDT 2025


https://github.com/lntue updated https://github.com/llvm/llvm-project/pull/132627

>From d53238e6e2d6b14b882ed000a5cf81480d4366ad Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Sun, 23 Mar 2025 17:57:21 +0000
Subject: [PATCH 1/2] [libc] Proof of concept of aliasing long double math
 functions.

---
 .../cmake/modules/LLVMLibCArchitectures.cmake |  1 +
 .../modules/LLVMLibCCompileOptionRules.cmake  |  2 ++
 libc/src/math/CMakeLists.txt                  | 31 ++++++++++++++++---
 libc/src/math/generic/copysign.cpp            | 12 +++++++
 libc/src/math/generic/copysignf128.cpp        | 11 +++++++
 libc/src/math/generic/copysignl.cpp           |  7 +++++
 6 files changed, 59 insertions(+), 5 deletions(-)

diff --git a/libc/cmake/modules/LLVMLibCArchitectures.cmake b/libc/cmake/modules/LLVMLibCArchitectures.cmake
index 62f3a2e3bdb59..5cdce54accdf3 100644
--- a/libc/cmake/modules/LLVMLibCArchitectures.cmake
+++ b/libc/cmake/modules/LLVMLibCArchitectures.cmake
@@ -150,6 +150,7 @@ if(LIBC_TARGET_ARCHITECTURE STREQUAL "arm")
   set(LIBC_TARGET_ARCHITECTURE_IS_ARM TRUE)
 elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "aarch64")
   set(LIBC_TARGET_ARCHITECTURE_IS_AARCH64 TRUE)
+  set(LIBC_TARGET_LONG_DOUBLE_IS_FLOAT128 TRUE)
 elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "x86_64")
   set(LIBC_TARGET_ARCHITECTURE_IS_X86_64 TRUE)
 elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "i386")
diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index 0facb0b9be0c1..b6fec2e687495 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -118,6 +118,8 @@ function(_get_common_compile_options output_var flags)
   if(LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE)
     list(APPEND compile_options "-fpie")
 
+    list(APPEND compile_options "-DLIBC_ALIAS_LONG_DOUBLE")
+
     if(LLVM_LIBC_FULL_BUILD)
       # Only add -ffreestanding flag in non-GPU full build mode.
       if(NOT LIBC_TARGET_OS_IS_GPU)
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 92c80a1053c9e..42d028aa3d6cd 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -7,24 +7,31 @@ function(add_math_entrypoint_object name)
   # We prefer machine specific implementation if available. Hence we check
   # that first and return early if we are able to add an alias target for the
   # machine specific implementation.
-  get_fq_target_name("${LIBC_TARGET_ARCHITECTURE}.${name}" fq_machine_specific_target_name)
+  if(NOT ARGN)
+    set(alias_entrypoint ${name})
+  else()
+    message(STATUS "Banananana ${name} ${ARGN}")
+    set(alias_entrypoint ${ARGN})
+  endif()
+
+  get_fq_target_name("${LIBC_TARGET_ARCHITECTURE}.${alias_entrypoint}" fq_machine_specific_target_name)
   if(TARGET ${fq_machine_specific_target_name})
     add_entrypoint_object(
       ${name}
       ALIAS
       DEPENDS
-        .${LIBC_TARGET_ARCHITECTURE}.${name}
+        .${LIBC_TARGET_ARCHITECTURE}.${alias_entrypoint}
     )
     return()
   endif()
 
-  get_fq_target_name("generic.${name}" fq_generic_target_name)
+  get_fq_target_name("generic.${alias_entrypoint}" fq_generic_target_name)
   if(TARGET ${fq_generic_target_name})
     add_entrypoint_object(
       ${name}
       ALIAS
       DEPENDS
-        .generic.${name}
+        .generic.${alias_entrypoint}
     )
     return()
   endif()
@@ -40,6 +47,20 @@ function(add_math_entrypoint_object name)
   )
 endfunction()
 
+function(add_long_double_math_entrypoint_object name)
+  get_fq_target_name(${name} fq_double_math_target)
+  get_fq_target_name("${name}l" fq_long_double_math_target)
+  get_fq_target_name("${name}f128" fq_float128_math_target)
+
+  if(LIBC_TARGET_LONG_DOUBLE_IS_DOUBLE)
+    add_math_entrypoint_object("${name}l" "${name}")
+  elseif(LIBC_TARGET_LONG_DOUBLE_IS_FLOAT128)
+    add_math_entrypoint_object("${name}l" "${name}f128")
+  else()
+    add_math_entrypoint_object("${name}l")
+  endif()
+endfunction()
+
 add_math_entrypoint_object(acos)
 add_math_entrypoint_object(acosf)
 add_math_entrypoint_object(acosf16)
@@ -88,9 +109,9 @@ add_math_entrypoint_object(ceilf128)
 
 add_math_entrypoint_object(copysign)
 add_math_entrypoint_object(copysignf)
-add_math_entrypoint_object(copysignl)
 add_math_entrypoint_object(copysignf16)
 add_math_entrypoint_object(copysignf128)
+add_long_double_math_entrypoint_object(copysign)
 
 add_math_entrypoint_object(cos)
 add_math_entrypoint_object(cosf)
diff --git a/libc/src/math/generic/copysign.cpp b/libc/src/math/generic/copysign.cpp
index 186bb2c5983f4..6eed8b08044bf 100644
--- a/libc/src/math/generic/copysign.cpp
+++ b/libc/src/math/generic/copysign.cpp
@@ -10,6 +10,7 @@
 #include "src/__support/FPUtil/ManipulationFunctions.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
@@ -22,3 +23,14 @@ LLVM_LIBC_FUNCTION(double, copysign, (double x, double y)) {
 }
 
 } // namespace LIBC_NAMESPACE_DECL
+
+#if defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64) &&                              \
+    defined(LIBC_ALIAS_LONG_DOUBLE)
+#include "src/math/copysignl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+decltype(LIBC_NAMESPACE::copysignl) copysignl [[gnu::alias("copysignl")]];
+asm("copysignl = copysign");
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64
\ No newline at end of file
diff --git a/libc/src/math/generic/copysignf128.cpp b/libc/src/math/generic/copysignf128.cpp
index 9a51c8d5eb8df..4101fd6764160 100644
--- a/libc/src/math/generic/copysignf128.cpp
+++ b/libc/src/math/generic/copysignf128.cpp
@@ -18,3 +18,14 @@ LLVM_LIBC_FUNCTION(float128, copysignf128, (float128 x, float128 y)) {
 }
 
 } // namespace LIBC_NAMESPACE_DECL
+
+#if defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128) &&                             \
+    defined(LIBC_ALIAS_LONG_DOUBLE)
+#include "src/math/copysignl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+decltype(LIBC_NAMESPACE::copysignl) copysignl [[gnu::alias("copysignl")]];
+asm("copysignl = copysignf128");
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128
\ No newline at end of file
diff --git a/libc/src/math/generic/copysignl.cpp b/libc/src/math/generic/copysignl.cpp
index 73c47bb04ad4a..d32680687a5d3 100644
--- a/libc/src/math/generic/copysignl.cpp
+++ b/libc/src/math/generic/copysignl.cpp
@@ -10,6 +10,11 @@
 #include "src/__support/FPUtil/ManipulationFunctions.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+#if !(defined(LIBC_ALIAS_LONG_DOUBLE) &&                                       \
+      (defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64) ||                           \
+       defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)))
 
 namespace LIBC_NAMESPACE_DECL {
 
@@ -18,3 +23,5 @@ LLVM_LIBC_FUNCTION(long double, copysignl, (long double x, long double y)) {
 }
 
 } // namespace LIBC_NAMESPACE_DECL
+
+#endif

>From 124367173425eb37c1eac32a4d8f3b50fa946127 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Sun, 23 Mar 2025 18:23:35 +0000
Subject: [PATCH 2/2] Add new lines at the EOFs.

---
 libc/src/math/generic/copysign.cpp     | 2 +-
 libc/src/math/generic/copysignf128.cpp | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/src/math/generic/copysign.cpp b/libc/src/math/generic/copysign.cpp
index 6eed8b08044bf..cf46e72390442 100644
--- a/libc/src/math/generic/copysign.cpp
+++ b/libc/src/math/generic/copysign.cpp
@@ -33,4 +33,4 @@ decltype(LIBC_NAMESPACE::copysignl) copysignl [[gnu::alias("copysignl")]];
 asm("copysignl = copysign");
 } // namespace LIBC_NAMESPACE_DECL
 
-#endif // LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64
\ No newline at end of file
+#endif // LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64
diff --git a/libc/src/math/generic/copysignf128.cpp b/libc/src/math/generic/copysignf128.cpp
index 4101fd6764160..013cc95cb71b7 100644
--- a/libc/src/math/generic/copysignf128.cpp
+++ b/libc/src/math/generic/copysignf128.cpp
@@ -28,4 +28,4 @@ decltype(LIBC_NAMESPACE::copysignl) copysignl [[gnu::alias("copysignl")]];
 asm("copysignl = copysignf128");
 } // namespace LIBC_NAMESPACE_DECL
 
-#endif // LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128
\ No newline at end of file
+#endif // LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128



More information about the libc-commits mailing list