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

via libc-commits libc-commits at lists.llvm.org
Mon Jan 22 19:48:04 PST 2024


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

>From d9c77d02a9de54ff53a30b99133c73447d69671e Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Fri, 12 Jan 2024 21:25:34 -0500
Subject: [PATCH 1/3] [libc] Fix generated float128 header for aarch64 target.

---
 libc/config/linux/api.td                     |  2 +-
 libc/include/CMakeLists.txt                  |  1 +
 libc/include/llvm-libc-types/CMakeLists.txt  |  1 +
 libc/include/llvm-libc-types/float128.h      | 30 ++++++++++++++++++++
 libc/spec/spec.td                            |  2 +-
 libc/spec/stdc.td                            |  1 +
 libc/src/__support/macros/properties/float.h |  9 ++++--
 7 files changed, 41 insertions(+), 5 deletions(-)
 create mode 100644 libc/include/llvm-libc-types/float128.h

diff --git a/libc/config/linux/api.td b/libc/config/linux/api.td
index 85f6b59264eb06..8d39069a96e48d 100644
--- a/libc/config/linux/api.td
+++ b/libc/config/linux/api.td
@@ -55,7 +55,7 @@ def IntTypesAPI : PublicAPI<"inttypes.h"> {
 }
 
 def MathAPI : PublicAPI<"math.h"> {
-  let Types = ["double_t", "float_t"];
+  let Types = ["double_t", "float_t", "float128"];
 }
 
 def FenvAPI: PublicAPI<"fenv.h"> {
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index cec8b86516b51f..54dc413663e37a 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -92,6 +92,7 @@ add_gen_header(
     .llvm-libc-macros.math_macros
     .llvm-libc-types.double_t
     .llvm-libc-types.float_t
+    .llvm-libc-types.float128
 )
 
 # TODO: This should be conditional on POSIX networking being included.
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 500900ffa0bbb0..7603eaac811417 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -96,3 +96,4 @@ add_header(rpc_opcodes_t HDR rpc_opcodes_t.h)
 add_header(ACTION HDR ACTION.h)
 add_header(ENTRY HDR ENTRY.h)
 add_header(struct_hsearch_data HDR struct_hsearch_data.h)
+add_header(float128 HDR float128.h)
diff --git a/libc/include/llvm-libc-types/float128.h b/libc/include/llvm-libc-types/float128.h
new file mode 100644
index 00000000000000..7030384e7d314a
--- /dev/null
+++ b/libc/include/llvm-libc-types/float128.h
@@ -0,0 +1,30 @@
+//===-- 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__
+
+#if defined(__clang__)
+#define LIBC_COMPILER_IS_CLANG
+#define LIBC_COMPILER_CLANG_VER (__clang_major__ * 100 + __clang_minor__)
+#elif defined(__GNUC__)
+#define LIBC_COMPILER_IS_GCC
+#define LIBC_COMPILER_GCC_VER (__GNUC__ * 100 + __GNUC_MINOR__)
+#endif
+
+#if (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301)) &&     \
+    (defined(__aarch64__) || defined(__riscv) || defined(__x86_64__))
+typedef _Float128 float128;
+#elif (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 600)) &&\
+    (defined(__x86_64__) && !defined(__Fuchsia__))
+typedef __float128 float128;
+#elif (LDBL_MANT_DIG == 113) || (__LDBL_MANT_DIG__ == 113)
+typedef long double float128;
+#endif
+
+#endif // __LLVM_LIBC_TYPES_FLOAT128_H__
diff --git a/libc/spec/spec.td b/libc/spec/spec.td
index 9b689b5eb502a9..5b281a496432fc 100644
--- a/libc/spec/spec.td
+++ b/libc/spec/spec.td
@@ -51,7 +51,7 @@ def LongDoubleType : NamedType<"long double">;
 def CharType : NamedType<"char">;
 
 // TODO: Add compatibility layer to use C23 type _Float128 if possible.
-def Float128Type : NamedType<"__float128">;
+def Float128Type : NamedType<"float128">;
 
 // Common types
 def VoidPtr : PtrType<VoidType>;
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 99e9b3ca65ca59..7ea69f00faa682 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -352,6 +352,7 @@ def StdC : StandardSpec<"stdc"> {
       [
           NamedType<"float_t">,
           NamedType<"double_t">,
+          NamedType<"float128">,
       ],
       [], // Enumerations
       [
diff --git a/libc/src/__support/macros/properties/float.h b/libc/src/__support/macros/properties/float.h
index 98ca2a5d4bc46f..bf2bcc7ffd9d63 100644
--- a/libc/src/__support/macros/properties/float.h
+++ b/libc/src/__support/macros/properties/float.h
@@ -19,11 +19,11 @@
 #include <float.h> // LDBL_MANT_DIG
 
 // 'long double' properties.
-#if (LDBL_MANT_DIG == 53)
+#if (LDBL_MANT_DIG == 53) || (__LDBL_MANT_DIG__ == 53)
 #define LIBC_LONG_DOUBLE_IS_FLOAT64
-#elif (LDBL_MANT_DIG == 64)
+#elif (LDBL_MANT_DIG == 64) || (__LDBL_MANT_DIG__ == 64)
 #define LIBC_LONG_DOUBLE_IS_X86_FLOAT80
-#elif (LDBL_MANT_DIG == 113)
+#elif (LDBL_MANT_DIG == 113) || (__LDBL_MANT_DIG__ == 113)
 #define LIBC_LONG_DOUBLE_IS_FLOAT128
 #endif
 
@@ -53,6 +53,9 @@ using float16 = _Float16;
 #endif
 
 // float128 support.
+// If the definition of float128 here is updated, also update
+//   include/llvm-libc-types/float128.h
+// so that the type definition in generated header `math.h` matched.
 #if (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301)) &&     \
     (defined(LIBC_TARGET_ARCH_IS_AARCH64) ||                                   \
      defined(LIBC_TARGET_ARCH_IS_ANY_RISCV) ||                                 \

>From befb8b9331df6a20c5335f9e4f3c10993f28e38a Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Fri, 12 Jan 2024 22:30:22 -0500
Subject: [PATCH 2/3] Fix formatting.

---
 libc/include/llvm-libc-types/float128.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/include/llvm-libc-types/float128.h b/libc/include/llvm-libc-types/float128.h
index 7030384e7d314a..ce3a69ec803455 100644
--- a/libc/include/llvm-libc-types/float128.h
+++ b/libc/include/llvm-libc-types/float128.h
@@ -20,7 +20,8 @@
 #if (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301)) &&     \
     (defined(__aarch64__) || defined(__riscv) || defined(__x86_64__))
 typedef _Float128 float128;
-#elif (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 600)) &&\
+#elif (defined(LIBC_COMPILER_CLANG_VER) &&                                     \
+       (LIBC_COMPILER_CLANG_VER >= 600)) &&                                    \
     (defined(__x86_64__) && !defined(__Fuchsia__))
 typedef __float128 float128;
 #elif (LDBL_MANT_DIG == 113) || (__LDBL_MANT_DIG__ == 113)

>From 3b377aa1840a03ec16ea0c3c6bd61f3b947b6650 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Mon, 22 Jan 2024 22:46:34 -0500
Subject: [PATCH 3/3] Sync and consolidate float128 type logic in one place.

---
 libc/config/linux/aarch64/entrypoints.txt     |  3 +-
 libc/include/llvm-libc-types/CMakeLists.txt   |  8 ++++-
 libc/include/llvm-libc-types/float128.h       | 32 +++++++++++++-----
 .../macros/properties/CMakeLists.txt          |  1 +
 libc/src/__support/macros/properties/float.h  | 33 +++----------------
 5 files changed, 37 insertions(+), 40 deletions(-)

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 3f66a582f5e3ee..37197a26e16d69 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -360,8 +360,7 @@ set(TARGET_LIBM_ENTRYPOINTS
 if(LIBC_COMPILER_HAS_FLOAT128)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
     # math.h C23 _Float128 entrypoints
-    # Temporarily disabled since float128 isn't working on the aarch64 buildbot
-    # libc.src.math.fabsf128
+    libc.src.math.fabsf128
   )
 endif()
 
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 7603eaac811417..75c4685bc53def 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -96,4 +96,10 @@ add_header(rpc_opcodes_t HDR rpc_opcodes_t.h)
 add_header(ACTION HDR ACTION.h)
 add_header(ENTRY HDR ENTRY.h)
 add_header(struct_hsearch_data HDR struct_hsearch_data.h)
-add_header(float128 HDR float128.h)
+add_header(
+  float128
+  HDR
+    float128.h
+  DEPENDS
+    libc.include.llvm-libc-macros.float_macros
+)
diff --git a/libc/include/llvm-libc-types/float128.h b/libc/include/llvm-libc-types/float128.h
index ce3a69ec803455..61dfb647469188 100644
--- a/libc/include/llvm-libc-types/float128.h
+++ b/libc/include/llvm-libc-types/float128.h
@@ -9,23 +9,37 @@
 #ifndef __LLVM_LIBC_TYPES_FLOAT128_H__
 #define __LLVM_LIBC_TYPES_FLOAT128_H__
 
+#include <llvm-libc-macros/float-macros.h> // LDBL_MANT_DIG
+
+// Define temporary compiler and its version
 #if defined(__clang__)
-#define LIBC_COMPILER_IS_CLANG
-#define LIBC_COMPILER_CLANG_VER (__clang_major__ * 100 + __clang_minor__)
+#define __LIBC_COMPILER_IS_CLANG
+#define __LIBC_COMPILER_CLANG_VER (__clang_major__ * 100 + __clang_minor__)
 #elif defined(__GNUC__)
-#define LIBC_COMPILER_IS_GCC
-#define LIBC_COMPILER_GCC_VER (__GNUC__ * 100 + __GNUC_MINOR__)
-#endif
+#define __LIBC_COMPILER_IS_GCC
+#define __LIBC_COMPILER_GCC_VER (__GNUC__ * 100 + __GNUC_MINOR__)
+#endif // __clang__, __GNUC__
 
-#if (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301)) &&     \
+#if (defined(__LIBC_COMPILER_GCC_VER) && (__LIBC_COMPILER_GCC_VER >= 1301)) && \
     (defined(__aarch64__) || defined(__riscv) || defined(__x86_64__))
+#define LIBC_COMPILER_HAS_C23_FLOAT128
 typedef _Float128 float128;
-#elif (defined(LIBC_COMPILER_CLANG_VER) &&                                     \
-       (LIBC_COMPILER_CLANG_VER >= 600)) &&                                    \
+#elif (defined(__LIBC_COMPILER_CLANG_VER) &&                                   \
+       (__LIBC_COMPILER_CLANG_VER >= 600)) &&                                  \
     (defined(__x86_64__) && !defined(__Fuchsia__))
+#define LIBC_COMPILER_HAS_FLOAT128_EXTENSION
 typedef __float128 float128;
-#elif (LDBL_MANT_DIG == 113) || (__LDBL_MANT_DIG__ == 113)
+#elif (LDBL_MANT_DIG == 113)
 typedef long double float128;
 #endif
 
+// Clean up temporary macros
+#ifdef __LIBC_COMPILER_IS_CLANG
+#undef __LIBC_COMPILER_IS_CLANG
+#undef __LIBC_COMPILER_CLANG_VER
+#elif defined(__LIBC_COMPILER_IS_GCC)
+#undef __LIBC_COMPILER_IS_GCC
+#undef __LIBC_COMPILER_GCC_VER
+#endif // __LIBC_COMPILER_IS_CLANG, __LIBC_COMPILER_IS_GCC
+
 #endif // __LLVM_LIBC_TYPES_FLOAT128_H__
diff --git a/libc/src/__support/macros/properties/CMakeLists.txt b/libc/src/__support/macros/properties/CMakeLists.txt
index ee87ce68c9da37..b309c052a59077 100644
--- a/libc/src/__support/macros/properties/CMakeLists.txt
+++ b/libc/src/__support/macros/properties/CMakeLists.txt
@@ -33,4 +33,5 @@ add_header_library(
     .compiler
     .cpu_features
     .os
+    libc.include.llvm-libc-types.float128
 )
diff --git a/libc/src/__support/macros/properties/float.h b/libc/src/__support/macros/properties/float.h
index bf2bcc7ffd9d63..510f3923749358 100644
--- a/libc/src/__support/macros/properties/float.h
+++ b/libc/src/__support/macros/properties/float.h
@@ -11,19 +11,19 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_FLOAT_H
 #define LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_FLOAT_H
 
+#include "llvm-libc-macros/float-macros.h" // LDBL_MANT_DIG
+#include "llvm-libc-types/float128.h"      // float128
 #include "src/__support/macros/properties/architectures.h"
 #include "src/__support/macros/properties/compiler.h"
 #include "src/__support/macros/properties/cpu_features.h"
 #include "src/__support/macros/properties/os.h"
 
-#include <float.h> // LDBL_MANT_DIG
-
 // 'long double' properties.
-#if (LDBL_MANT_DIG == 53) || (__LDBL_MANT_DIG__ == 53)
+#if (LDBL_MANT_DIG == 53)
 #define LIBC_LONG_DOUBLE_IS_FLOAT64
-#elif (LDBL_MANT_DIG == 64) || (__LDBL_MANT_DIG__ == 64)
+#elif (LDBL_MANT_DIG == 64)
 #define LIBC_LONG_DOUBLE_IS_X86_FLOAT80
-#elif (LDBL_MANT_DIG == 113) || (__LDBL_MANT_DIG__ == 113)
+#elif (LDBL_MANT_DIG == 113)
 #define LIBC_LONG_DOUBLE_IS_FLOAT128
 #endif
 
@@ -53,29 +53,6 @@ using float16 = _Float16;
 #endif
 
 // float128 support.
-// If the definition of float128 here is updated, also update
-//   include/llvm-libc-types/float128.h
-// so that the type definition in generated header `math.h` matched.
-#if (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301)) &&     \
-    (defined(LIBC_TARGET_ARCH_IS_AARCH64) ||                                   \
-     defined(LIBC_TARGET_ARCH_IS_ANY_RISCV) ||                                 \
-     defined(LIBC_TARGET_ARCH_IS_X86_64))
-#define LIBC_COMPILER_HAS_C23_FLOAT128
-#endif
-#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 600)) &&  \
-    (defined(LIBC_TARGET_ARCH_IS_X86_64) &&                                    \
-     defined(LIBC_TARGET_OS_IS_LINUX) && !defined(LIBC_TARGET_OS_IS_FUCHSIA))
-#define LIBC_COMPILER_HAS_FLOAT128_EXTENSION
-#endif
-
-#if defined(LIBC_COMPILER_HAS_C23_FLOAT128)
-using float128 = _Float128;
-#elif defined(LIBC_COMPILER_HAS_FLOAT128_EXTENSION)
-using float128 = __float128;
-#elif defined(LIBC_LONG_DOUBLE_IS_FLOAT128)
-using float128 = long double;
-#endif
-
 #if defined(LIBC_COMPILER_HAS_C23_FLOAT128) ||                                 \
     defined(LIBC_COMPILER_HAS_FLOAT128_EXTENSION) ||                           \
     defined(LIBC_LONG_DOUBLE_IS_FLOAT128)



More information about the libc-commits mailing list