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

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Fri Feb 2 15:07:47 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
----------------
nickdesaulniers wrote:

Argh, sorry for causing confusion here, but I do think we'll benefit in the long run by documenting the state of the world.

So some issues we've found:
- gcc did not support `_Float128` in C mode until gcc-7, and C++ mode until gcc-13.
- gcc defines `__STDC_IEC_60559_BFP__` to `201404L` so we cannot compare `__STDC_IEC_60559_BFP__` to `202311L` (at least doing so won't work for any compiler TODAY). Just having this one check would be ideal, but alas.
- clang doesn't support `_Float128`.
- glibc will define `__STDC_IEC_60559_BFP__` to `201404L`, so we can't use it to detect compiler support in overlay mode (or not without great pain).

So perhaps we can have:
```c
// GCC did not support _Float128 in C mode until gcc-7, or C++ mode until g++-13.
#if defined(__GNUC__) && !defined(clang) && \
    (defined(__cpluscplus) && __GNUC__ >= 13) || \
    (!defined(__cplusplus) && __GNUC__ >= 7)
#define LIBC_COMPILER_HAS_C23_FLOAT128
#endif

// TODO: replace when clang has actual _Float128 support.
// https://github.com/llvm/llvm-project/issues/80195
#ifdef __clang__
#define LIBC_COMPILER_HAS_FLOAT128_EXTENSION
#endif

#ifdef LIBC_COMPILER_HAS_C23_FLOAT128
typedef _Float128 float128;
#elif LIBC_COMPILER_HAS_FLOAT128_EXTENSION
typedef __float128 float128;
#elif LDBL_MANT_DIG == 113
typedef long double float128;
#else
#error "no f128 support"
#endif
```

I was hopefully naive that we could just have:
```c
#if __STDC_IEC_60559_BFP__ < 202311L
#error "no _Float128 support"
#endif
```

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


More information about the libc-commits mailing list