Skip to content

Commit 3f906f5

Browse files
authored
[libc][math] Add initial support for C23 float128 math functions, starting with copysignf128. (llvm#71731)
1 parent 77d75dc commit 3f906f5

File tree

19 files changed

+310
-15
lines changed

19 files changed

+310
-15
lines changed

libc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ option(LIBC_INCLUDE_DOCS "Build the libc documentation." ${LLVM_INCLUDE_DOCS})
245245

246246
include(CMakeParseArguments)
247247
include(LLVMLibCCheckCpuFeatures)
248+
include(CheckCompilerFeatures)
248249
include(LLVMLibCRules)
249250

250251
if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# ------------------------------------------------------------------------------
2+
# Compiler features definition and flags
3+
# ------------------------------------------------------------------------------
4+
5+
# Initialize ALL_COMPILER_FEATURES as empty list.
6+
set(ALL_COMPILER_FEATURES "float128")
7+
8+
# Making sure ALL_COMPILER_FEATURES is sorted.
9+
list(SORT ALL_COMPILER_FEATURES)
10+
11+
# Function to check whether the compiler supports the provided set of features.
12+
# Usage:
13+
# compiler_supports(
14+
# <output variable>
15+
# <list of cpu features>
16+
# )
17+
function(compiler_supports output_var features)
18+
_intersection(var "${LIBC_CPU_FEATURES}" "${features}")
19+
if("${var}" STREQUAL "${features}")
20+
set(${output_var} TRUE PARENT_SCOPE)
21+
else()
22+
unset(${output_var} PARENT_SCOPE)
23+
endif()
24+
endfunction()
25+
26+
# ------------------------------------------------------------------------------
27+
# Internal helpers and utilities.
28+
# ------------------------------------------------------------------------------
29+
30+
# Computes the intersection between two lists.
31+
function(_intersection output_var list1 list2)
32+
foreach(element IN LISTS list1)
33+
if("${list2}" MATCHES "(^|;)${element}(;|$)")
34+
list(APPEND tmp "${element}")
35+
endif()
36+
endforeach()
37+
set(${output_var} ${tmp} PARENT_SCOPE)
38+
endfunction()
39+
40+
set(AVAILABLE_COMPILER_FEATURES "")
41+
42+
# Try compile a C file to check if flag is supported.
43+
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
44+
foreach(feature IN LISTS ALL_COMPILER_FEATURES)
45+
try_compile(
46+
has_feature
47+
${CMAKE_CURRENT_BINARY_DIR}/compiler_features
48+
SOURCES ${LIBC_SOURCE_DIR}/cmake/modules/compiler_features/check_${feature}.cpp
49+
COMPILE_DEFINITIONS -I${LIBC_SOURCE_DIR} ${LIBC_COMPILE_OPTIONS_NATIVE}
50+
)
51+
if(has_feature)
52+
list(APPEND AVAILABLE_COMPILER_FEATURES ${feature})
53+
if(${feature} STREQUAL "float128")
54+
set(LIBC_COMPILER_HAS_FLOAT128 TRUE)
55+
endif()
56+
endif()
57+
endforeach()
58+
59+
message(STATUS "Compiler features available: ${AVAILABLE_COMPILER_FEATURES}")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "src/__support/macros/properties/compiler.h"
2+
3+
#ifndef LIBC_COMPILER_HAS_FLOAT128
4+
#error unsupported
5+
#endif

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,13 @@ set(TARGET_LIBM_ENTRYPOINTS
360360
libc.src.math.truncl
361361
)
362362

363+
if(LIBC_COMPILER_HAS_FLOAT128)
364+
list(APPEND TARGET_LIBM_ENTRYPOINTS
365+
# math.h C23 _Float128 entrypoints
366+
libc.src.math.copysignf128
367+
)
368+
endif()
369+
363370
if(LLVM_LIBC_FULL_BUILD)
364371
list(APPEND TARGET_LIBC_ENTRYPOINTS
365372
# assert.h entrypoints

libc/docs/math/index.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ Implementation Status
9696

9797
* To check math functions enabled for embedded system:
9898

99-
- `barebone-aarch32 <https://github.com/llvm/llvm-project/tree/main/libc/config/baremetal/arm/entrypoints.txt>`_
99+
- `baremetal-aarch32 <https://github.com/llvm/llvm-project/tree/main/libc/config/baremetal/arm/entrypoints.txt>`_
100100

101-
- barebone-riscv32 - to be added
101+
- baremetal-riscv32 - to be added
102102

103103
Basic Operations
104104
----------------
@@ -120,6 +120,8 @@ Basic Operations
120120
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
121121
| copysignl | |check| | |check| | | |check| | |check| | | | |check| | | | | |
122122
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
123+
| copysignf128 | |check| | |check| | | | | | | | | | | |
124+
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
123125
| fabs | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | | | |
124126
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
125127
| fabsf | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | | | |

libc/spec/spec.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ def DoubleType : NamedType<"double">;
5050
def LongDoubleType : NamedType<"long double">;
5151
def CharType : NamedType<"char">;
5252

53+
// TODO: Add compatibility layer to use C23 type _Float128 if possible.
54+
def Float128Type : NamedType<"__float128">
55+
5356
// Common types
5457
def VoidPtr : PtrType<VoidType>;
5558
def VoidPtrPtr : PtrType<VoidPtr>;

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ def StdC : StandardSpec<"stdc"> {
358358
FunctionSpec<"copysign", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
359359
FunctionSpec<"copysignf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
360360
FunctionSpec<"copysignl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
361+
FunctionSpec<"copysignf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>]>,
361362

362363
FunctionSpec<"ceil", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
363364
FunctionSpec<"ceilf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,

libc/src/__support/CPP/type_traits/is_floating_point.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "src/__support/CPP/type_traits/is_same.h"
1212
#include "src/__support/CPP/type_traits/remove_cv.h"
1313
#include "src/__support/macros/attributes.h"
14+
#include "src/__support/macros/properties/compiler.h"
1415

1516
namespace LIBC_NAMESPACE::cpp {
1617

@@ -23,8 +24,13 @@ template <typename T> struct is_floating_point {
2324
}
2425

2526
public:
27+
#if defined(LIBC_COMPILER_HAS_FLOAT128)
28+
LIBC_INLINE_VAR static constexpr bool value =
29+
__is_unqualified_any_of<T, float, double, long double, float128>();
30+
#else
2631
LIBC_INLINE_VAR static constexpr bool value =
2732
__is_unqualified_any_of<T, float, double, long double>();
33+
#endif // LIBC_COMPILER_HAS_FLOAT128
2834
};
2935
template <typename T>
3036
LIBC_INLINE_VAR constexpr bool is_floating_point_v =

libc/src/__support/FPUtil/FloatProperties.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,38 @@ template <> struct FloatProperties<long double> {
175175
};
176176
#endif
177177

178+
#if (defined(LIBC_COMPILER_HAS_FLOAT128) && \
179+
!defined(LIBC_FLOAT128_IS_LONG_DOUBLE))
180+
// Properties for numbers represented in 128 bits long double on non x86
181+
// platform.
182+
template <> struct FloatProperties<float128> {
183+
typedef UInt128 BitsType;
184+
static_assert(sizeof(BitsType) == sizeof(float128),
185+
"Unexpected size of 'float128' type.");
186+
187+
static constexpr uint32_t BIT_WIDTH = sizeof(BitsType) << 3;
188+
189+
static constexpr uint32_t MANTISSA_WIDTH = 112;
190+
static constexpr uint32_t MANTISSA_PRECISION = MANTISSA_WIDTH + 1;
191+
static constexpr uint32_t EXPONENT_WIDTH = 15;
192+
static constexpr BitsType MANTISSA_MASK = (BitsType(1) << MANTISSA_WIDTH) - 1;
193+
static constexpr BitsType SIGN_MASK = BitsType(1)
194+
<< (EXPONENT_WIDTH + MANTISSA_WIDTH);
195+
static constexpr BitsType EXPONENT_MASK = ~(SIGN_MASK | MANTISSA_MASK);
196+
static constexpr uint32_t EXPONENT_BIAS = 16383;
197+
198+
static constexpr BitsType EXP_MANT_MASK = MANTISSA_MASK | EXPONENT_MASK;
199+
static_assert(EXP_MANT_MASK == ~SIGN_MASK,
200+
"Exponent and mantissa masks are not as expected.");
201+
202+
// If a number x is a NAN, then it is a quiet NAN if:
203+
// QuietNaNMask & bits(x) != 0
204+
// Else, it is a signalling NAN.
205+
static constexpr BitsType QUIET_NAN_MASK = BitsType(1)
206+
<< (MANTISSA_WIDTH - 1);
207+
};
208+
#endif // LIBC_COMPILER_HAS_FLOAT128
209+
178210
// Define the float type corresponding to the BitsType.
179211
template <typename BitsType> struct FloatType;
180212

libc/src/__support/macros/properties/compiler.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,25 @@
2121
#define LIBC_COMPILER_IS_MSC
2222
#endif
2323

24+
// Check compiler features
25+
#if defined(FLT128_MANT_DIG)
26+
// C23 _Float128 type is available.
27+
#define LIBC_COMPILER_HAS_FLOAT128
28+
#define LIBC_FLOAT128_IS_C23
29+
using float128 = _Float128;
30+
31+
#elif defined(__SIZEOF_FLOAT128__)
32+
// Builtin __float128 is available.
33+
#define LIBC_COMPILER_HAS_FLOAT128
34+
#define LIBC_FLOAT128_IS_BUILTIN
35+
using float128 = __float128;
36+
37+
#elif (defined(__linux__) && defined(__aarch64__))
38+
// long double on Linux aarch64 is 128-bit floating point.
39+
#define LIBC_COMPILER_HAS_FLOAT128
40+
#define LIBC_FLOAT128_IS_LONG_DOUBLE
41+
using float128 = long double;
42+
43+
#endif
44+
2445
#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_COMPILER_H

0 commit comments

Comments
 (0)