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

via libc-commits libc-commits at lists.llvm.org
Thu Mar 27 08:11:25 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 01/11] [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 02/11] 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

>From ed922b7df9113bd06c2f8cb37e98c3ca81ba6ec0 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Mon, 24 Mar 2025 16:58:45 +0000
Subject: [PATCH 03/11] Fix and create common aliasing macro.

---
 .../modules/LLVMLibCCompileOptionRules.cmake  |  4 +-
 libc/src/__support/common.h                   | 44 +++++++++++++++----
 libc/src/math/generic/copysign.cpp            |  5 +--
 libc/src/math/generic/copysignf128.cpp        |  5 +--
 libc/src/math/generic/copysignl.cpp           |  7 ---
 5 files changed, 40 insertions(+), 25 deletions(-)

diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index b6fec2e687495..d20bcc2fdb2cb 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -118,7 +118,9 @@ 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(LIBC_TARGET_LONG_DOUBLE_IS_FLOAT128 OR LIBC_TARGET_LONG_DOUBLE_IS_FLOAT64)
+      list(APPEND compile_options "-DLIBC_ALIAS_LONG_DOUBLE")
+    endif()
 
     if(LLVM_LIBC_FULL_BUILD)
       # Only add -ffreestanding flag in non-GPU full build mode.
diff --git a/libc/src/__support/common.h b/libc/src/__support/common.h
index 42e8a79187fac..af2409bdaf605 100644
--- a/libc/src/__support/common.h
+++ b/libc/src/__support/common.h
@@ -37,21 +37,47 @@
 
 #define LLVM_LIBC_ATTR(name) EXPAND_THEN_SECOND(LLVM_LIBC_FUNCTION_ATTR_##name)
 
-// MacOS needs to be excluded because it does not support aliasing.
-#if defined(LIBC_COPT_PUBLIC_PACKAGING) && (!defined(__APPLE__))
-#define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist)                           \
+// MacOS needs to be excluded because it does not support [[gnu::aliasing]].
+#ifndef __APPLE__
+
+#if defined(LIBC_COPT_PUBLIC_PACKAGING)
+#define LLVM_LIBC_FUNCTION(type, name, arglist)                                \
   LLVM_LIBC_ATTR(name)                                                         \
   LLVM_LIBC_FUNCTION_ATTR decltype(LIBC_NAMESPACE::name)                       \
-      __##name##_impl__ __asm__(#name);                                        \
+      __##name##_impl__ asm(#name);                                            \
   decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#name)]];                   \
   type __##name##_impl__ arglist
-#else
-#define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist) type name arglist
-#endif
 
-// This extra layer of macro allows `name` to be a macro to rename a function.
+#define LLVM_LIBC_ALIASING_FUNCTION(name, alias)                               \
+  namespace LIBC_NAMESPACE_DECL {                                              \
+  decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#alias)]];                  \
+  asm(#name " = " #alias)                                                      \
+  }                                                                            \
+  static_assert(true, "Require semicolon")
+#else
 #define LLVM_LIBC_FUNCTION(type, name, arglist)                                \
-  LLVM_LIBC_FUNCTION_IMPL(type, name, arglist)
+  LLVM_LIBC_ATTR(name)                                                         \
+  LLVM_LIBC_FUNCTION_ATTR decltype(LIBC_NAMESPACE::name)                       \
+      __##name##_impl__ asm("__" #name "_impl__");                             \
+  decltype(LIBC_NAMESPACE::name) name [[gnu::alias("__" #name "_impl__")]];    \
+  type __##name##_impl__ arglist
+
+#define LLVM_LIBC_ALIASING_FUNCTION(name, alias)                               \
+  namespace LIBC_NAMESPACE_DECL {                                              \
+  decltype(LIBC_NAMESPACE::name) name [[gnu::alias("__" #alias "_impl__")]];   \
+  asm(#name " = __" #alias "_impl__")                                          \
+  }                                                                            \
+  static_assert(true, "Require semicolon")
+#endif // LIBC_COPT_PUBLIC_PACKAGING
+
+#else
+
+#define LLVM_LIBC_FUNCTION(type, name, arglist) type name arglist
+
+#define LLVM_LIBC_ALIASING_FUNCTION(name, alias)                               \
+  static_assert(true, "Require semicolon")
+
+#endif // !__APPLE__
 
 namespace LIBC_NAMESPACE_DECL {
 namespace internal {
diff --git a/libc/src/math/generic/copysign.cpp b/libc/src/math/generic/copysign.cpp
index cf46e72390442..fef1d1f9ad332 100644
--- a/libc/src/math/generic/copysign.cpp
+++ b/libc/src/math/generic/copysign.cpp
@@ -28,9 +28,6 @@ LLVM_LIBC_FUNCTION(double, copysign, (double x, double y)) {
     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
+LLVM_LIBC_ALIASING_FUNCTION(copysignl, copysign);
 
 #endif // LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64
diff --git a/libc/src/math/generic/copysignf128.cpp b/libc/src/math/generic/copysignf128.cpp
index 013cc95cb71b7..7130d55b97f34 100644
--- a/libc/src/math/generic/copysignf128.cpp
+++ b/libc/src/math/generic/copysignf128.cpp
@@ -23,9 +23,6 @@ LLVM_LIBC_FUNCTION(float128, copysignf128, (float128 x, float128 y)) {
     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
+LLVM_LIBC_ALIASING_FUNCTION(copysignl, copysignf128);
 
 #endif // LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128
diff --git a/libc/src/math/generic/copysignl.cpp b/libc/src/math/generic/copysignl.cpp
index d32680687a5d3..73c47bb04ad4a 100644
--- a/libc/src/math/generic/copysignl.cpp
+++ b/libc/src/math/generic/copysignl.cpp
@@ -10,11 +10,6 @@
 #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 {
 
@@ -23,5 +18,3 @@ LLVM_LIBC_FUNCTION(long double, copysignl, (long double x, long double y)) {
 }
 
 } // namespace LIBC_NAMESPACE_DECL
-
-#endif

>From 8e1abc1a334a57b13e65318c6a74c805a227347b Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Mon, 24 Mar 2025 17:01:26 +0000
Subject: [PATCH 04/11] Remove debugging message.

---
 libc/src/math/CMakeLists.txt | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 42d028aa3d6cd..c86b98c65a6c8 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -10,7 +10,6 @@ function(add_math_entrypoint_object name)
   if(NOT ARGN)
     set(alias_entrypoint ${name})
   else()
-    message(STATUS "Banananana ${name} ${ARGN}")
     set(alias_entrypoint ${ARGN})
   endif()
 

>From 268dd921857f3edb85e68792f377395cfba6c3b8 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Mon, 24 Mar 2025 17:07:56 +0000
Subject: [PATCH 05/11] Add missing semicolons.

---
 libc/src/__support/common.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/src/__support/common.h b/libc/src/__support/common.h
index af2409bdaf605..4582a43adf74f 100644
--- a/libc/src/__support/common.h
+++ b/libc/src/__support/common.h
@@ -51,7 +51,7 @@
 #define LLVM_LIBC_ALIASING_FUNCTION(name, alias)                               \
   namespace LIBC_NAMESPACE_DECL {                                              \
   decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#alias)]];                  \
-  asm(#name " = " #alias)                                                      \
+  asm(#name " = " #alias);                                                     \
   }                                                                            \
   static_assert(true, "Require semicolon")
 #else
@@ -65,7 +65,7 @@
 #define LLVM_LIBC_ALIASING_FUNCTION(name, alias)                               \
   namespace LIBC_NAMESPACE_DECL {                                              \
   decltype(LIBC_NAMESPACE::name) name [[gnu::alias("__" #alias "_impl__")]];   \
-  asm(#name " = __" #alias "_impl__")                                          \
+  asm(#name " = __" #alias "_impl__");                                         \
   }                                                                            \
   static_assert(true, "Require semicolon")
 #endif // LIBC_COPT_PUBLIC_PACKAGING

>From f4d28564bc36cc866ad7dc9058d475379cf36658 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Mon, 24 Mar 2025 17:21:29 +0000
Subject: [PATCH 06/11] Fix token clashing.

---
 libc/src/__support/common.h | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/libc/src/__support/common.h b/libc/src/__support/common.h
index 4582a43adf74f..2ae72edd655e6 100644
--- a/libc/src/__support/common.h
+++ b/libc/src/__support/common.h
@@ -48,10 +48,10 @@
   decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#name)]];                   \
   type __##name##_impl__ arglist
 
-#define LLVM_LIBC_ALIASING_FUNCTION(name, alias)                               \
+#define LLVM_LIBC_ALIASING_FUNCTION(name, func)                                \
   namespace LIBC_NAMESPACE_DECL {                                              \
-  decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#alias)]];                  \
-  asm(#name " = " #alias);                                                     \
+  decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#func)]];                   \
+  asm(#name " = " #func);                                                      \
   }                                                                            \
   static_assert(true, "Require semicolon")
 #else
@@ -62,10 +62,10 @@
   decltype(LIBC_NAMESPACE::name) name [[gnu::alias("__" #name "_impl__")]];    \
   type __##name##_impl__ arglist
 
-#define LLVM_LIBC_ALIASING_FUNCTION(name, alias)                               \
+#define LLVM_LIBC_ALIASING_FUNCTION(name, func)                                \
   namespace LIBC_NAMESPACE_DECL {                                              \
-  decltype(LIBC_NAMESPACE::name) name [[gnu::alias("__" #alias "_impl__")]];   \
-  asm(#name " = __" #alias "_impl__");                                         \
+  decltype(LIBC_NAMESPACE::name) name [[gnu::alias("__" #func "_impl__")]];    \
+  asm(#name " = __" #func "_impl__");                                          \
   }                                                                            \
   static_assert(true, "Require semicolon")
 #endif // LIBC_COPT_PUBLIC_PACKAGING
@@ -74,7 +74,7 @@
 
 #define LLVM_LIBC_FUNCTION(type, name, arglist) type name arglist
 
-#define LLVM_LIBC_ALIASING_FUNCTION(name, alias)                               \
+#define LLVM_LIBC_ALIASING_FUNCTION(name, func)                                \
   static_assert(true, "Require semicolon")
 
 #endif // !__APPLE__

>From 0d04f79d526ef9beb0c35354659bb2287c8e6a30 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Mon, 24 Mar 2025 21:59:21 +0000
Subject: [PATCH 07/11] Remove namespace block from the aliasing macro.

---
 libc/src/__support/common.h | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/libc/src/__support/common.h b/libc/src/__support/common.h
index 2ae72edd655e6..fc4c4ea3fcc44 100644
--- a/libc/src/__support/common.h
+++ b/libc/src/__support/common.h
@@ -49,10 +49,8 @@
   type __##name##_impl__ arglist
 
 #define LLVM_LIBC_ALIASING_FUNCTION(name, func)                                \
-  namespace LIBC_NAMESPACE_DECL {                                              \
-  decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#func)]];                   \
+  decltype(LIBC_NAMESPACE::name) LIBC_NAMESPACE::name [[gnu::alias(#func)]];   \
   asm(#name " = " #func);                                                      \
-  }                                                                            \
   static_assert(true, "Require semicolon")
 #else
 #define LLVM_LIBC_FUNCTION(type, name, arglist)                                \
@@ -63,10 +61,9 @@
   type __##name##_impl__ arglist
 
 #define LLVM_LIBC_ALIASING_FUNCTION(name, func)                                \
-  namespace LIBC_NAMESPACE_DECL {                                              \
-  decltype(LIBC_NAMESPACE::name) name [[gnu::alias("__" #func "_impl__")]];    \
+  decltype(LIBC_NAMESPACE::name) LIBC_NAMESPACE::name                          \
+      [[gnu::alias("__" #func "_impl__")]];                                    \
   asm(#name " = __" #func "_impl__");                                          \
-  }                                                                            \
   static_assert(true, "Require semicolon")
 #endif // LIBC_COPT_PUBLIC_PACKAGING
 

>From e2b6c90366f5cc4f813b0be72ae80923f0b7ca30 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Tue, 25 Mar 2025 13:43:12 +0000
Subject: [PATCH 08/11] Change macro name to LLVM_LIBC_ALIAS.

---
 libc/src/__support/common.h            | 7 +++----
 libc/src/math/generic/copysign.cpp     | 2 +-
 libc/src/math/generic/copysignf128.cpp | 2 +-
 3 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/libc/src/__support/common.h b/libc/src/__support/common.h
index fc4c4ea3fcc44..60b76e0a813d6 100644
--- a/libc/src/__support/common.h
+++ b/libc/src/__support/common.h
@@ -48,7 +48,7 @@
   decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#name)]];                   \
   type __##name##_impl__ arglist
 
-#define LLVM_LIBC_ALIASING_FUNCTION(name, func)                                \
+#define LLVM_LIBC_ALIAS(name, func)                                            \
   decltype(LIBC_NAMESPACE::name) LIBC_NAMESPACE::name [[gnu::alias(#func)]];   \
   asm(#name " = " #func);                                                      \
   static_assert(true, "Require semicolon")
@@ -60,7 +60,7 @@
   decltype(LIBC_NAMESPACE::name) name [[gnu::alias("__" #name "_impl__")]];    \
   type __##name##_impl__ arglist
 
-#define LLVM_LIBC_ALIASING_FUNCTION(name, func)                                \
+#define LLVM_LIBC_ALIAS(name, func)                                            \
   decltype(LIBC_NAMESPACE::name) LIBC_NAMESPACE::name                          \
       [[gnu::alias("__" #func "_impl__")]];                                    \
   asm(#name " = __" #func "_impl__");                                          \
@@ -71,8 +71,7 @@
 
 #define LLVM_LIBC_FUNCTION(type, name, arglist) type name arglist
 
-#define LLVM_LIBC_ALIASING_FUNCTION(name, func)                                \
-  static_assert(true, "Require semicolon")
+#define LLVM_LIBC_ALIAS(name, func) static_assert(true, "Require semicolon")
 
 #endif // !__APPLE__
 
diff --git a/libc/src/math/generic/copysign.cpp b/libc/src/math/generic/copysign.cpp
index fef1d1f9ad332..15b3bc6f76e3a 100644
--- a/libc/src/math/generic/copysign.cpp
+++ b/libc/src/math/generic/copysign.cpp
@@ -28,6 +28,6 @@ LLVM_LIBC_FUNCTION(double, copysign, (double x, double y)) {
     defined(LIBC_ALIAS_LONG_DOUBLE)
 #include "src/math/copysignl.h"
 
-LLVM_LIBC_ALIASING_FUNCTION(copysignl, copysign);
+LLVM_LIBC_ALIAS(copysignl, copysign);
 
 #endif // LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64
diff --git a/libc/src/math/generic/copysignf128.cpp b/libc/src/math/generic/copysignf128.cpp
index 7130d55b97f34..92b4f7d4f29a3 100644
--- a/libc/src/math/generic/copysignf128.cpp
+++ b/libc/src/math/generic/copysignf128.cpp
@@ -23,6 +23,6 @@ LLVM_LIBC_FUNCTION(float128, copysignf128, (float128 x, float128 y)) {
     defined(LIBC_ALIAS_LONG_DOUBLE)
 #include "src/math/copysignl.h"
 
-LLVM_LIBC_ALIASING_FUNCTION(copysignl, copysignf128);
+LLVM_LIBC_ALIAS(copysignl, copysignf128);
 
 #endif // LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128

>From 0be1f7037f30b44d81c94c2ba2f0a72aaa78ffd7 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Tue, 25 Mar 2025 14:02:55 +0000
Subject: [PATCH 09/11] Remove extra asm statement for non public packaging.

---
 libc/src/__support/common.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libc/src/__support/common.h b/libc/src/__support/common.h
index 60b76e0a813d6..c2cef04a17d66 100644
--- a/libc/src/__support/common.h
+++ b/libc/src/__support/common.h
@@ -63,7 +63,6 @@
 #define LLVM_LIBC_ALIAS(name, func)                                            \
   decltype(LIBC_NAMESPACE::name) LIBC_NAMESPACE::name                          \
       [[gnu::alias("__" #func "_impl__")]];                                    \
-  asm(#name " = __" #func "_impl__");                                          \
   static_assert(true, "Require semicolon")
 #endif // LIBC_COPT_PUBLIC_PACKAGING
 

>From 95f920aaca47dab00fa3fb4cfc1da2e78ca04be2 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Thu, 27 Mar 2025 14:33:11 +0000
Subject: [PATCH 10/11] Change from asm to gnu::alias attribute to also export
 the symbol.

---
 libc/src/__support/common.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/__support/common.h b/libc/src/__support/common.h
index c2cef04a17d66..484436bf76359 100644
--- a/libc/src/__support/common.h
+++ b/libc/src/__support/common.h
@@ -50,7 +50,7 @@
 
 #define LLVM_LIBC_ALIAS(name, func)                                            \
   decltype(LIBC_NAMESPACE::name) LIBC_NAMESPACE::name [[gnu::alias(#func)]];   \
-  asm(#name " = " #func);                                                      \
+  extern "C" decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#func)]];        \
   static_assert(true, "Require semicolon")
 #else
 #define LLVM_LIBC_FUNCTION(type, name, arglist)                                \

>From 08627d430448ed4def5268b396b8622c2875fa4d Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Thu, 27 Mar 2025 15:10:12 +0000
Subject: [PATCH 11/11] Add build guards for copysignl.

---
 libc/src/__support/macros/properties/types.h | 9 +++++++++
 libc/src/math/generic/copysign.cpp           | 5 ++---
 libc/src/math/generic/copysignf128.cpp       | 3 +--
 libc/src/math/generic/copysignl.cpp          | 7 +++++++
 4 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/libc/src/__support/macros/properties/types.h b/libc/src/__support/macros/properties/types.h
index 6293b9d4d292a..e34d487baf095 100644
--- a/libc/src/__support/macros/properties/types.h
+++ b/libc/src/__support/macros/properties/types.h
@@ -58,4 +58,13 @@ using float16 = _Float16;
 // LIBC_TYPES_HAS_FLOAT128 and 'float128' type are provided by
 // "include/llvm-libc-types/float128.h"
 
+// Alias long double functions if requested.
+#ifdef LIBC_ALIAS_LONG_DOUBLE
+#if defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
+#define LIBC_ALIAS_LONG_DOUBLE_TO_DOUBLE
+#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
+#define LIBC_ALIAS_LONG_DOUBLE_TO_FLOAT128
+#endif
+#endif // LIBC_ALIAS_LONG_DOUBLE
+
 #endif // LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_TYPES_H
diff --git a/libc/src/math/generic/copysign.cpp b/libc/src/math/generic/copysign.cpp
index 15b3bc6f76e3a..3970c46d3d50c 100644
--- a/libc/src/math/generic/copysign.cpp
+++ b/libc/src/math/generic/copysign.cpp
@@ -24,10 +24,9 @@ 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)
+#if defined(LIBC_ALIAS_LONG_DOUBLE_TO_DOUBLE)
 #include "src/math/copysignl.h"
 
 LLVM_LIBC_ALIAS(copysignl, copysign);
 
-#endif // LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64
+#endif // LIBC_ALIAS_LONG_DOUBLE_TO_DOUBLE
diff --git a/libc/src/math/generic/copysignf128.cpp b/libc/src/math/generic/copysignf128.cpp
index 92b4f7d4f29a3..1e0eb62e7d7ff 100644
--- a/libc/src/math/generic/copysignf128.cpp
+++ b/libc/src/math/generic/copysignf128.cpp
@@ -19,8 +19,7 @@ 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)
+#if defined(LIBC_ALIAS_LONG_DOUBLE_TO_FLOAT128)
 #include "src/math/copysignl.h"
 
 LLVM_LIBC_ALIAS(copysignl, copysignf128);
diff --git a/libc/src/math/generic/copysignl.cpp b/libc/src/math/generic/copysignl.cpp
index 73c47bb04ad4a..9bbcafdebfd0a 100644
--- a/libc/src/math/generic/copysignl.cpp
+++ b/libc/src/math/generic/copysignl.cpp
@@ -13,8 +13,15 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
+// TODO: Change this implementation to copysignf80 and add long double alias
+// similar to copysign and copysignf128.
+#if !defined(LIBC_ALIAS_LONG_DOUBLE_TO_DOUBLE) &&                              \
+    !defined(LIBC_ALIAS_LONG_DOUBLE_TO_FLOAT128)
+
 LLVM_LIBC_FUNCTION(long double, copysignl, (long double x, long double y)) {
   return fputil::copysign(x, y);
 }
 
+#endif
+
 } // namespace LIBC_NAMESPACE_DECL



More information about the libc-commits mailing list