Skip to content

Commit d4ef4b8

Browse files
authored
[libc] Fix generated float128 header for aarch64 target. (#78017)
1 parent 6ba9d29 commit d4ef4b8

File tree

10 files changed

+56
-28
lines changed

10 files changed

+56
-28
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,7 @@ set(TARGET_LIBM_ENTRYPOINTS
379379
if(LIBC_COMPILER_HAS_FLOAT128)
380380
list(APPEND TARGET_LIBM_ENTRYPOINTS
381381
# math.h C23 _Float128 entrypoints
382-
# Temporarily disabled since float128 isn't working on the aarch64 buildbot
383-
# libc.src.math.fabsf128
382+
libc.src.math.fabsf128
384383
)
385384
endif()
386385

libc/config/linux/api.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def IntTypesAPI : PublicAPI<"inttypes.h"> {
5555
}
5656

5757
def MathAPI : PublicAPI<"math.h"> {
58-
let Types = ["double_t", "float_t"];
58+
let Types = ["double_t", "float_t", "float128"];
5959
}
6060

6161
def FenvAPI: PublicAPI<"fenv.h"> {

libc/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ add_gen_header(
100100
.llvm-libc-macros.math_macros
101101
.llvm-libc-types.double_t
102102
.llvm-libc-types.float_t
103+
.llvm-libc-types.float128
103104
)
104105

105106
# TODO: This should be conditional on POSIX networking being included.

libc/include/llvm-libc-types/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,10 @@ add_header(ENTRY HDR ENTRY.h)
9898
add_header(struct_hsearch_data HDR struct_hsearch_data.h)
9999
add_header(struct_epoll_event HDR struct_epoll_event.h)
100100
add_header(struct_epoll_data HDR struct_epoll_data.h)
101+
add_header(
102+
float128
103+
HDR
104+
float128.h
105+
DEPENDS
106+
libc.include.llvm-libc-macros.float_macros
107+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===-- Definition of float128 type ---------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __LLVM_LIBC_TYPES_FLOAT128_H__
10+
#define __LLVM_LIBC_TYPES_FLOAT128_H__
11+
12+
#include <include/llvm-libc-macros/float-macros.h> // LDBL_MANT_DIG
13+
14+
// Currently, C23 `_Float128` type is only defined as a built-in type in GCC 7
15+
// or later, and only for C. For C++, or for clang, `__float128` is defined
16+
// instead, and only on x86-64 targets.
17+
//
18+
// TODO: Update C23 `_Float128` type detection again when clang supports it.
19+
// https://github.com/llvm/llvm-project/issues/80195
20+
#if defined(__STDC_IEC_60559_BFP__) && !defined(__clang__) && \
21+
!defined(__cplusplus)
22+
// Use _Float128 C23 type.
23+
#define LIBC_COMPILER_HAS_C23_FLOAT128
24+
typedef _Float128 float128;
25+
#elif defined(__FLOAT128__) || defined(__SIZEOF_FLOAT128__)
26+
// Use __float128 type. gcc and clang sometime use __SIZEOF_FLOAT128__ to
27+
// notify the availability of __float128.
28+
// clang also uses __FLOAT128__ macro to notify the availability of __float128
29+
// type: https://reviews.llvm.org/D15120
30+
#define LIBC_COMPILER_HAS_FLOAT128_EXTENSION
31+
typedef __float128 float128;
32+
#elif (LDBL_MANT_DIG == 113)
33+
// Use long double.
34+
typedef long double float128;
35+
#endif
36+
37+
#endif // __LLVM_LIBC_TYPES_FLOAT128_H__

libc/spec/spec.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def UnsignedCharType : NamedType<"unsigned char">;
5353
def UnsignedShortType : NamedType<"unsigned short">;
5454

5555
// TODO: Add compatibility layer to use C23 type _Float128 if possible.
56-
def Float128Type : NamedType<"__float128">;
56+
def Float128Type : NamedType<"float128">;
5757

5858
// Common types
5959
def VoidPtr : PtrType<VoidType>;

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ def StdC : StandardSpec<"stdc"> {
352352
[
353353
NamedType<"float_t">,
354354
NamedType<"double_t">,
355+
NamedType<"float128">,
355356
],
356357
[], // Enumerations
357358
[

libc/src/__support/FPUtil/generic/sqrt.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ template <> struct SpecialLongDouble<long double> {
3636
template <typename T>
3737
LIBC_INLINE void normalize(int &exponent,
3838
typename FPBits<T>::StorageType &mantissa) {
39-
const int shift = cpp::countl_zero(mantissa) -
40-
(8 * sizeof(mantissa) - 1 - FPBits<T>::FRACTION_LEN);
39+
const int shift =
40+
cpp::countl_zero(mantissa) -
41+
(8 * static_cast<int>(sizeof(mantissa)) - 1 - FPBits<T>::FRACTION_LEN);
4142
exponent -= shift;
4243
mantissa <<= shift;
4344
}

libc/src/__support/macros/properties/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,6 @@ add_header_library(
3333
.compiler
3434
.cpu_features
3535
.os
36+
libc.include.llvm-libc-macros.float_macros
37+
libc.include.llvm-libc-types.float128
3638
)

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
#ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_FLOAT_H
1212
#define LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_FLOAT_H
1313

14+
#include "include/llvm-libc-macros/float-macros.h" // LDBL_MANT_DIG
15+
#include "include/llvm-libc-types/float128.h" // float128
1416
#include "src/__support/macros/properties/architectures.h"
1517
#include "src/__support/macros/properties/compiler.h"
1618
#include "src/__support/macros/properties/cpu_features.h"
1719
#include "src/__support/macros/properties/os.h"
1820

19-
#include <float.h> // LDBL_MANT_DIG
20-
2121
// 'long double' properties.
2222
#if (LDBL_MANT_DIG == 53)
2323
#define LIBC_LONG_DOUBLE_IS_FLOAT64
@@ -53,26 +53,6 @@ using float16 = _Float16;
5353
#endif
5454

5555
// float128 support.
56-
#if (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301)) && \
57-
(defined(LIBC_TARGET_ARCH_IS_AARCH64) || \
58-
defined(LIBC_TARGET_ARCH_IS_ANY_RISCV) || \
59-
defined(LIBC_TARGET_ARCH_IS_X86_64))
60-
#define LIBC_COMPILER_HAS_C23_FLOAT128
61-
#endif
62-
#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 600)) && \
63-
(defined(LIBC_TARGET_ARCH_IS_X86_64) && \
64-
defined(LIBC_TARGET_OS_IS_LINUX) && !defined(LIBC_TARGET_OS_IS_FUCHSIA))
65-
#define LIBC_COMPILER_HAS_FLOAT128_EXTENSION
66-
#endif
67-
68-
#if defined(LIBC_COMPILER_HAS_C23_FLOAT128)
69-
using float128 = _Float128;
70-
#elif defined(LIBC_COMPILER_HAS_FLOAT128_EXTENSION)
71-
using float128 = __float128;
72-
#elif defined(LIBC_LONG_DOUBLE_IS_FLOAT128)
73-
using float128 = long double;
74-
#endif
75-
7656
#if defined(LIBC_COMPILER_HAS_C23_FLOAT128) || \
7757
defined(LIBC_COMPILER_HAS_FLOAT128_EXTENSION) || \
7858
defined(LIBC_LONG_DOUBLE_IS_FLOAT128)

0 commit comments

Comments
 (0)