Skip to content

Commit d53238e

Browse files
committed
[libc] Proof of concept of aliasing long double math functions.
1 parent 0be69e5 commit d53238e

File tree

6 files changed

+59
-5
lines changed

6 files changed

+59
-5
lines changed

libc/cmake/modules/LLVMLibCArchitectures.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ if(LIBC_TARGET_ARCHITECTURE STREQUAL "arm")
150150
set(LIBC_TARGET_ARCHITECTURE_IS_ARM TRUE)
151151
elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "aarch64")
152152
set(LIBC_TARGET_ARCHITECTURE_IS_AARCH64 TRUE)
153+
set(LIBC_TARGET_LONG_DOUBLE_IS_FLOAT128 TRUE)
153154
elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "x86_64")
154155
set(LIBC_TARGET_ARCHITECTURE_IS_X86_64 TRUE)
155156
elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "i386")

libc/cmake/modules/LLVMLibCCompileOptionRules.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ function(_get_common_compile_options output_var flags)
118118
if(LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE)
119119
list(APPEND compile_options "-fpie")
120120

121+
list(APPEND compile_options "-DLIBC_ALIAS_LONG_DOUBLE")
122+
121123
if(LLVM_LIBC_FULL_BUILD)
122124
# Only add -ffreestanding flag in non-GPU full build mode.
123125
if(NOT LIBC_TARGET_OS_IS_GPU)

libc/src/math/CMakeLists.txt

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,31 @@ function(add_math_entrypoint_object name)
77
# We prefer machine specific implementation if available. Hence we check
88
# that first and return early if we are able to add an alias target for the
99
# machine specific implementation.
10-
get_fq_target_name("${LIBC_TARGET_ARCHITECTURE}.${name}" fq_machine_specific_target_name)
10+
if(NOT ARGN)
11+
set(alias_entrypoint ${name})
12+
else()
13+
message(STATUS "Banananana ${name} ${ARGN}")
14+
set(alias_entrypoint ${ARGN})
15+
endif()
16+
17+
get_fq_target_name("${LIBC_TARGET_ARCHITECTURE}.${alias_entrypoint}" fq_machine_specific_target_name)
1118
if(TARGET ${fq_machine_specific_target_name})
1219
add_entrypoint_object(
1320
${name}
1421
ALIAS
1522
DEPENDS
16-
.${LIBC_TARGET_ARCHITECTURE}.${name}
23+
.${LIBC_TARGET_ARCHITECTURE}.${alias_entrypoint}
1724
)
1825
return()
1926
endif()
2027

21-
get_fq_target_name("generic.${name}" fq_generic_target_name)
28+
get_fq_target_name("generic.${alias_entrypoint}" fq_generic_target_name)
2229
if(TARGET ${fq_generic_target_name})
2330
add_entrypoint_object(
2431
${name}
2532
ALIAS
2633
DEPENDS
27-
.generic.${name}
34+
.generic.${alias_entrypoint}
2835
)
2936
return()
3037
endif()
@@ -40,6 +47,20 @@ function(add_math_entrypoint_object name)
4047
)
4148
endfunction()
4249

50+
function(add_long_double_math_entrypoint_object name)
51+
get_fq_target_name(${name} fq_double_math_target)
52+
get_fq_target_name("${name}l" fq_long_double_math_target)
53+
get_fq_target_name("${name}f128" fq_float128_math_target)
54+
55+
if(LIBC_TARGET_LONG_DOUBLE_IS_DOUBLE)
56+
add_math_entrypoint_object("${name}l" "${name}")
57+
elseif(LIBC_TARGET_LONG_DOUBLE_IS_FLOAT128)
58+
add_math_entrypoint_object("${name}l" "${name}f128")
59+
else()
60+
add_math_entrypoint_object("${name}l")
61+
endif()
62+
endfunction()
63+
4364
add_math_entrypoint_object(acos)
4465
add_math_entrypoint_object(acosf)
4566
add_math_entrypoint_object(acosf16)
@@ -88,9 +109,9 @@ add_math_entrypoint_object(ceilf128)
88109

89110
add_math_entrypoint_object(copysign)
90111
add_math_entrypoint_object(copysignf)
91-
add_math_entrypoint_object(copysignl)
92112
add_math_entrypoint_object(copysignf16)
93113
add_math_entrypoint_object(copysignf128)
114+
add_long_double_math_entrypoint_object(copysign)
94115

95116
add_math_entrypoint_object(cos)
96117
add_math_entrypoint_object(cosf)

libc/src/math/generic/copysign.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "src/__support/FPUtil/ManipulationFunctions.h"
1111
#include "src/__support/common.h"
1212
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/properties/types.h"
1314

1415
namespace LIBC_NAMESPACE_DECL {
1516

@@ -22,3 +23,14 @@ LLVM_LIBC_FUNCTION(double, copysign, (double x, double y)) {
2223
}
2324

2425
} // namespace LIBC_NAMESPACE_DECL
26+
27+
#if defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64) && \
28+
defined(LIBC_ALIAS_LONG_DOUBLE)
29+
#include "src/math/copysignl.h"
30+
31+
namespace LIBC_NAMESPACE_DECL {
32+
decltype(LIBC_NAMESPACE::copysignl) copysignl [[gnu::alias("copysignl")]];
33+
asm("copysignl = copysign");
34+
} // namespace LIBC_NAMESPACE_DECL
35+
36+
#endif // LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64

libc/src/math/generic/copysignf128.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,14 @@ LLVM_LIBC_FUNCTION(float128, copysignf128, (float128 x, float128 y)) {
1818
}
1919

2020
} // namespace LIBC_NAMESPACE_DECL
21+
22+
#if defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128) && \
23+
defined(LIBC_ALIAS_LONG_DOUBLE)
24+
#include "src/math/copysignl.h"
25+
26+
namespace LIBC_NAMESPACE_DECL {
27+
decltype(LIBC_NAMESPACE::copysignl) copysignl [[gnu::alias("copysignl")]];
28+
asm("copysignl = copysignf128");
29+
} // namespace LIBC_NAMESPACE_DECL
30+
31+
#endif // LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128

libc/src/math/generic/copysignl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
#include "src/__support/FPUtil/ManipulationFunctions.h"
1111
#include "src/__support/common.h"
1212
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/properties/types.h"
14+
15+
#if !(defined(LIBC_ALIAS_LONG_DOUBLE) && \
16+
(defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64) || \
17+
defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)))
1318

1419
namespace LIBC_NAMESPACE_DECL {
1520

@@ -18,3 +23,5 @@ LLVM_LIBC_FUNCTION(long double, copysignl, (long double x, long double y)) {
1823
}
1924

2025
} // namespace LIBC_NAMESPACE_DECL
26+
27+
#endif

0 commit comments

Comments
 (0)