[libc-commits] [libc] [libc] Fix generated float128 header for aarch64 target. (PR #78017)

via libc-commits libc-commits at lists.llvm.org
Fri Feb 2 21:18:58 PST 2024


================
@@ -0,0 +1,31 @@
+//===-- Definition of float128 type ---------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __LLVM_LIBC_TYPES_FLOAT128_H__
+#define __LLVM_LIBC_TYPES_FLOAT128_H__
+
+#include <include/llvm-libc-macros/float-macros.h> // LDBL_MANT_DIG
+
+// TODO: https://github.com/llvm/llvm-project/issues/80195
+//   Check _Float128 C23 type detection again when clang supports it.
+#if defined(__STDC_IEC_60559_BFP__) && !defined(__clang__)
+// Use _Float128 C23 type.
+#define LIBC_COMPILER_HAS_C23_FLOAT128
+typedef _Float128 float128;
+#elif defined(__FLOAT128__)
+// Use __float128 type.
+// clang uses __FLOAT128__ macro to notify the availability of __float128 type:
+//   https://reviews.llvm.org/D15120
+#define LIBC_COMPILER_HAS_FLOAT128_EXTENSION
+typedef __float128 float128;
+#elif (LDBL_MANT_DIG == 113)
+// Use long double.
+typedef long double float128;
+#endif
----------------
lntue wrote:

Let me summarize the current support in the following table:
```
                    |     clang      |   gcc pre-13   |     gcc-13     |   g++ pre-13   |     g++-13     |
----------------------------------------------------------------------------------------------------------
                    | x86-64 | arm64 | x86-64 | arm64 | x86-64 | arm64 | x86-64 | arm64 | x86-64 | arm64 |
----------------------------------------------------------------------------------------------------------
_Float128           |        |       |    X   |   X   |    X   |   X   |        |       |    X   |   X   |
----------------------------------------------------------------------------------------------------------
__float128          |    X   |       |    X   |       |    X   |       |    X   |       |    X   |       |
----------------------------------------------------------------------------------------------------------
long double as f128 |        |   X   |        |   X   |        |   X   |        |   X   |        |   X   |
```

- gcc arm64: https://godbolt.org/z/n5vsczs76
- g++ x86-64: https://godbolt.org/z/fzqMTenzo
- g++ arm64: https://godbolt.org/z/3hjqGsez5

So far the cleanest check I found without relying on compiler versioning is follow:
```
#if defined(__STDC_IEC_60559_BFP__) && !defined(__clang__) && !defined(__cplusplus)
#define LIBC_COMPILER_HAS_C23_FLOAT128
#elif defined(__FLOAT128__) || defined(__SIZEOF_FLOAT128__)
#define LIBC_COMPILER_HAS_FLOAT128_EXTENSION
#elif (LDBL_MANT_DIG == 113)
// Use long double.
#define LIBC_LONG_DOUBLE_IS_FLOAT128
#endif

#ifdef LIBC_COMPILER_HAS_C23_FLOAT128
typedef _Float128 float128;
#elif defined(LIBC_COMPILER_HAS_FLOAT128_EXTENSION)
typedef __float128 float128;
#elif defined(LIBC_LONG_DOUBLE_IS_FLOAT128)
typedef long double float128;
#endif
```
with comments / TODO to revisit the conditions in the future when clang has support for `_Float128`.
WDYT?

https://github.com/llvm/llvm-project/pull/78017


More information about the libc-commits mailing list