[libc-commits] [libc] [llvm] [libc] Add proxy header for float.h. (PR #93504)

via libc-commits libc-commits at lists.llvm.org
Tue May 28 12:10:58 PDT 2024


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

>From 6fe877dd459127cde6522a1c11ee4ecd291bddad Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Tue, 28 May 2024 05:20:37 +0000
Subject: [PATCH 1/5] [libc] Add proxy header for float.h.

Fixes #88433, #90496.

Co-authored-by: aniplcc <aniplccode at gmail.com>
Co-authored-by: Tue Ly <lntue at google.com>
---
 libc/hdr/CMakeLists.txt                       |  10 +
 libc/hdr/float_macros.h                       |  19 ++
 libc/include/llvm-libc-macros/float-macros.h  | 221 ++++++++++++++++--
 .../macros/properties/CMakeLists.txt          |   2 +-
 libc/src/__support/macros/properties/types.h  |   2 +-
 libc/src/math/generic/CMakeLists.txt          |   1 +
 libc/src/math/generic/scalbnf128.cpp          |  11 +-
 .../llvm-project-overlay/libc/BUILD.bazel     |   7 +-
 8 files changed, 244 insertions(+), 29 deletions(-)
 create mode 100644 libc/hdr/float_macros.h

diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index 91b8cb71552a7..66b82c84dac49 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -87,4 +87,14 @@ add_proxy_header_library(
     libc.include.llvm-libc-macros.time_macros
 )
 
+add_proxy_header_library(
+  float_macros
+  HDRS
+    float_macros.h
+  DEPENDS
+    libc.include.llvm-libc-macros.float_macros
+  FULL_BUILD_DEPENDS
+    libc.include.float
+)
+
 add_subdirectory(types)
diff --git a/libc/hdr/float_macros.h b/libc/hdr/float_macros.h
new file mode 100644
index 0000000000000..f541a886e1f2d
--- /dev/null
+++ b/libc/hdr/float_macros.h
@@ -0,0 +1,19 @@
+//===-- Definition of macros from math.h ----------------------------------===//
+//
+// 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_HDR_FLOAT_MACROS_H
+#define LLVM_LIBC_HDR_FLOAT_MACROS_H
+
+#ifndef LIBC_FULL_BUILD
+// Overlay mode
+#include <float.h>
+#endif // !LLVM_LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/float-macros.h"
+
+#endif // LLVM_LIBC_HDR_FLOAT_MACROS_H
diff --git a/libc/include/llvm-libc-macros/float-macros.h b/libc/include/llvm-libc-macros/float-macros.h
index 4fe8590c5f70c..a054448dd5b39 100644
--- a/libc/include/llvm-libc-macros/float-macros.h
+++ b/libc/include/llvm-libc-macros/float-macros.h
@@ -9,165 +9,346 @@
 #ifndef LLVM_LIBC_MACROS_FLOAT_MACROS_H
 #define LLVM_LIBC_MACROS_FLOAT_MACROS_H
 
-// Suppress `#include_next is a language extension` warnings.
-#ifdef __clang__
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wgnu-include-next"
-#pragma clang diagnostic ignored "-Winclude-next-absolute-path"
-#else // gcc
-#pragma GCC system_header
-#endif //__clang__
-
-#include_next <float.h>
-
-#ifdef __clang__
-#pragma clang diagnostic pop
-#endif //__clang__
+// Check long double.
+#if defined(__linux__) && defined(__x86_64__)
+#define LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80
+#elif defined(__linux__) && (defined(__aarch64__) || defined(__riscv))
+#define LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT128
+#else
+#define LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64
+#endif
 
 #ifndef FLT_RADIX
+#ifdef __FLT_RADIX__
 #define FLT_RADIX __FLT_RADIX__
+#else
+#define FLT_RADIX 2
+#endif // __FLT_RADIX__
 #endif // FLT_RADIX
 
 #ifndef FLT_EVAL_METHOD
+#ifdef __FLT_EVAL_METHOD__
 #define FLT_EVAL_METHOD __FLT_EVAL_METHOD__
+#else
+#define FLT_EVAL_METHOD 0
+#endif // __FLT_EVAL_METHOD__
 #endif // FLT_EVAL_METHOD
 
-#ifndef DECIMAL_DIG
-#define DECIMAL_DIG __DECIMAL_DIG__
-#endif // DECIMAL_DIG
+#ifndef FLT_ROUNDS
+#define FLT_ROUNDS 1
+#endif // FLT_ROUNDS
 
 #ifndef FLT_DECIMAL_DIG
+#ifdef __FLT_DECIMAL_DIG__
 #define FLT_DECIMAL_DIG __FLT_DECIMAL_DIG__
+#else
+#define FLT_DECIMAL_DIG 9
+#endif // __FLT_DECIMAL_DIG__
 #endif // FLT_DECIMAL_DIG
 
 #ifndef DBL_DECIMAL_DIG
+#ifdef __DBL_DECIMAL_DIG__
 #define DBL_DECIMAL_DIG __DBL_DECIMAL_DIG__
+#else
+#define DBL_DECIMAL_DIG 17
+#endif // __DBL_DECIMAL_DIG__
 #endif // DBL_DECIMAL_DIG
 
 #ifndef LDBL_DECIMAL_DIG
+#ifdef __LDBL_DECIMAL_DIG__
 #define LDBL_DECIMAL_DIG __LDBL_DECIMAL_DIG__
+#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80)
+#define LDBL_DECIMAL_DIG 21
+#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT128)
+#define LDBL_DECIMAL_DIG 36
+#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64
+#define LDBL_DECIMAL_DIG DBL_DECIMAL_DIG
+#endif // __LDBL_DECIMAL_DIG
 #endif // LDBL_DECIMAL_DIG
 
+#ifndef DECIMAL_DIG
+#ifdef __DECIMAL_DIG__
+#define DECIMAL_DIG __DECIMAL_DIG__
+#else
+#define DECIMAL_DIG LDBL_DECIMAL_DIG
+#endif // __DECIMAL_DIG
+#endif // DECIMAL_DIG
+
 #ifndef FLT_DIG
+#ifdef __FLT_DIG__
 #define FLT_DIG __FLT_DIG__
+#else
+#define FLT_DIG 6
+#endif // __FLT_DIG__
 #endif // FLT_DIG
 
 #ifndef DBL_DIG
+#ifdef __DBL_DIG__
 #define DBL_DIG __DBL_DIG__
+#else
+#define DBL_DIG 15
+#endif // __DBL_DIG__
 #endif // DBL_DIG
 
 #ifndef LDBL_DIG
+#ifdef __LDBL_DIG__
 #define LDBL_DIG __LDBL_DIG__
+#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80)
+#define LDBL_DIG 18
+#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT128)
+#define LDBL_DIG 33
+#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64
+#define LDBL_DIG DBL_DIG
+#endif // __LDBL_DIG__
 #endif // LDBL_DIG
 
 #ifndef FLT_MANT_DIG
+#ifdef __FLT_MANT_DIG__
 #define FLT_MANT_DIG __FLT_MANT_DIG__
+#else
+#define FLT_MANT_DIG 24
+#endif // __FLT_MANT_DIG__
 #endif // FLT_MANT_DIG
 
 #ifndef DBL_MANT_DIG
+#ifdef __DBL_MANT_DIG__
 #define DBL_MANT_DIG __DBL_MANT_DIG__
+#else
+#define DBL_MANT_DIG 53
+#endif // __DBL_MANT_DIG__
 #endif // DBL_MANT_DIG
 
 #ifndef LDBL_MANT_DIG
+#ifdef __LDBL_MANT_DIG__
 #define LDBL_MANT_DIG __LDBL_MANT_DIG__
+#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80)
+#define LDBL_MANT_DIG 64
+#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT128)
+#define LDBL_MANT_DIG 113
+#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64
+#define LDBL_MANT_DIG DBL_MANT_DIG
+#endif // __LDBL_MANT_DIG__
 #endif // LDBL_MANT_DIG
 
 #ifndef FLT_MIN
+#ifdef __FLT_MIN__
 #define FLT_MIN __FLT_MIN__
+#else
+#define FLT_MIN 0x1.0p-126f
+#endif // __FLT_MIN__
 #endif // FLT_MIN
 
 #ifndef DBL_MIN
+#ifdef __DBL_MIN__
 #define DBL_MIN __DBL_MIN__
+#else
+#define DBL_MIN 0x1.0p-1022
+#endif // __DBL_MIN__
 #endif // DBL_MIN
 
 #ifndef LDBL_MIN
+#ifdef __LDBL_MIN__
 #define LDBL_MIN __LDBL_MIN__
+#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64)
+#define LDBL_MIN 0x1.0p-1022L
+#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80 or FLOAT128
+#define LDBL_MIN 0x1.0p-16382L
+#endif // __LDBL_MIN__
 #endif // LDBL_MIN
 
 #ifndef FLT_MAX
+#ifdef __FLT_MAX__
 #define FLT_MAX __FLT_MAX__
+#else
+#define FLT_MAX 0x1.fffffep+127f
+#endif // __FLT_MAX__
 #endif // FLT_MAX
 
 #ifndef DBL_MAX
+#ifdef __DBL_MAX__
 #define DBL_MAX __DBL_MAX__
+#else
+#define DBL_MAX 0x1.ffff'ffff'ffff'fp+1023
+#endif // __DBL_MAX__
 #endif // DBL_MAX
 
 #ifndef LDBL_MAX
+#ifdef __LDBL_MAX__
 #define LDBL_MAX __LDBL_MAX__
+#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80)
+#define LDBL_MAX 0x1.ffff'ffff'ffff'fffep+16383L
+#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT128)
+#define LDBL_MAX 0x1.ffff'ffff'ffff'ffff'ffff'ffff'ffffp+16383L
+#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64
+#define LDBL_MAX 0x1.ffff'ffff'ffff'fp+1023L
+#endif // __LDBL_MAX__
 #endif // LDBL_MAX
 
 #ifndef FLT_TRUE_MIN
-#define FLT_TRUE_MIN __FLT_TRUE_MIN__
+#ifdef __FLT_DENORM_MIN__
+#define FLT_TRUE_MIN __FLT_DENORM_MIN__
+#else
+#define FLT_TRUE_MIN 0x1.0p-149f
+#endif // __FLT_DENORM_MIN__
 #endif // FLT_TRUE_MIN
 
 #ifndef DBL_TRUE_MIN
-#define DBL_TRUE_MIN __DBL_TRUE_MIN__
+#ifdef __DBL_DENORM_MIN__
+#define DBL_TRUE_MIN __DBL_DENORM_MIN__
+#else
+#define DBL_TRUE_MIN 0x1.0p-1074
+#endif // __DBL_DENORM_MIN__
 #endif // DBL_TRUE_MIN
 
 #ifndef LDBL_TRUE_MIN
-#define LDBL_TRUE_MIN __LDBL_TRUE_MIN__
+#ifdef __LDBL_DENORM_MIN__
+#define LDBL_TRUE_MIN __LDBL_DENORM_MIN__
+#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80)
+#define LDBL_TRUE_MIN 0x1.0p-16445L
+#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT128)
+#define LDBL_TRUE_MIN 0x1.0p-16494L
+#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64
+#define LDBL_TRUE_MIN 0x1.0p-1074L
+#endif // __LDBL_DENORM_MIN__
 #endif // LDBL_TRUE_MIN
 
 #ifndef FLT_EPSILON
+#ifdef __FLT_EPSILON__
 #define FLT_EPSILON __FLT_EPSILON__
+#else
+#define FLT_EPSILON 0x1.0p-23f
+#endif // __FLT_EPSILON__
 #endif // FLT_EPSILON
 
 #ifndef DBL_EPSILON
+#ifdef __DBL_EPSILON__
 #define DBL_EPSILON __DBL_EPSILON__
+#else
+#define DBL_EPSILON 0x1.0p-52
+#endif // __DBL_EPSILON__
 #endif // DBL_EPSILON
 
 #ifndef LDBL_EPSILON
+#ifdef __LDBL_EPSILON__
 #define LDBL_EPSILON __LDBL_EPSILON__
+#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80)
+#define LDBL_EPSILON 0x1.0p-63L
+#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT128)
+#define LDBL_EPSILON 0x1.0p-112L
+#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64
+#define LDBL_EPSILON 0x1.0p-52L
+#endif // __LDBL_EPSILON__
 #endif // LDBL_EPSILON
 
 #ifndef FLT_MIN_EXP
+#ifdef __FLT_MIN_EXP__
 #define FLT_MIN_EXP __FLT_MIN_EXP__
+#else
+#define FLT_MIN_EXP (-125)
+#endif // __FLT_MIN_EXP__
 #endif // FLT_MIN_EXP
 
 #ifndef DBL_MIN_EXP
+#ifdef __DBL_MIN_EXP__
 #define DBL_MIN_EXP __DBL_MIN_EXP__
+#else
+#define DBL_MIN_EXP (-1021)
+#endif // __DBL_MIN_EXP__
 #endif // DBL_MIN_EXP
 
 #ifndef LDBL_MIN_EXP
+#ifdef __LDBL_MIN_EXP__
 #define LDBL_MIN_EXP __LDBL_MIN_EXP__
+#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64)
+#define LDBL_MIN_EXP DBL_MIN_EXP
+#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80 or FLOAT128
+#define LDBL_MIN_EXP (-16381)
+#endif // __LDBL_MIN_EXP__
 #endif // LDBL_MIN_EXP
 
 #ifndef FLT_MIN_10_EXP
+#ifdef __FLT_MIN_10_EXP__
 #define FLT_MIN_10_EXP __FLT_MIN_10_EXP__
+#else
+#define FLT_MIN_10_EXP (-37)
+#endif // __FLT_MIN_10_EXP__
 #endif // FLT_MIN_10_EXP
 
 #ifndef DBL_MIN_10_EXP
+#ifdef __DBL_MIN_10_EXP__
 #define DBL_MIN_10_EXP __DBL_MIN_10_EXP__
+#else
+#define DBL_MIN_10_EXP (-307)
+#endif // __DBL_MIN_10_EXP__
 #endif // DBL_MIN_10_EXP
 
 #ifndef LDBL_MIN_10_EXP
+#ifdef __LDBL_MIN_10_EXP__
 #define LDBL_MIN_10_EXP __LDBL_MIN_10_EXP__
+#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64)
+#define LDBL_MIN_10_EXP DBL_MIN_10_EXP
+#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80 or FLOAT128
+#define LDBL_MIN_10_EXP (-4931)
+#endif // __LDBL_MIN_10_EXP__
 #endif // LDBL_MIN_10_EXP
 
 #ifndef FLT_MAX_EXP
+#ifdef __FLT_MAX_EXP__
 #define FLT_MAX_EXP __FLT_MAX_EXP__
+#else
+#define FLT_MAX_EXP 128
+#endif // __FLT_MAX_EXP__
 #endif // FLT_MAX_EXP
 
 #ifndef DBL_MAX_EXP
+#ifdef __DBL_MAX_EXP__
 #define DBL_MAX_EXP __DBL_MAX_EXP__
+#else
+#define DBL_MAX_EXP 1024
+#endif // __DBL_MAX_EXP__
 #endif // DBL_MAX_EXP
 
 #ifndef LDBL_MAX_EXP
+#ifdef __LDBL_MAX_EXP__
 #define LDBL_MAX_EXP __LDBL_MAX_EXP__
+#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64)
+#define LDBL_MAX_EXP DBL_MAX_EXP
+#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80 or FLOAT128
+#define LDBL_MAX_EXP 16384
+#endif // __LDBL_MAX_EXP__
 #endif // LDBL_MAX_EXP
 
 #ifndef FLT_MAX_10_EXP
+#ifdef __FLT_MAX_10_EXP__
 #define FLT_MAX_10_EXP __FLT_MAX_10_EXP__
+#else
+#define FLT_MAX_10_EXP 38
+#endif // __FLT_MAX_10_EXP__
 #endif // FLT_MAX_10_EXP
 
 #ifndef DBL_MAX_10_EXP
+#ifdef __DBL_MAX_10_EXP__
 #define DBL_MAX_10_EXP __DBL_MAX_10_EXP__
+#else
+#define DBL_MAX_10_EXP 308
+#endif // __DBL_MAX_10_EXP__
 #endif // DBL_MAX_10_EXP
 
 #ifndef LDBL_MAX_10_EXP
+#ifdef __LDBL_MAX_10_EXP__
 #define LDBL_MAX_10_EXP __LDBL_MAX_10_EXP__
+#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64)
+#define LDBL_MAX_10_EXP DBL_MAX_10_EXP
+#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80 or FLOAT128
+#define LDBL_MAX_10_EXP 4932
+#endif // __LDBL_MAX_10_EXP__
 #endif // LDBL_MAX_10_EXP
 
 // TODO: Add FLT16 and FLT128 constants.
 
+// Cleanup
+#undef LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64
+#undef LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80
+#undef LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT128
+
 #endif // LLVM_LIBC_MACROS_FLOAT_MACROS_H
diff --git a/libc/src/__support/macros/properties/CMakeLists.txt b/libc/src/__support/macros/properties/CMakeLists.txt
index bbc45650f3fca..7718aeaa3de5a 100644
--- a/libc/src/__support/macros/properties/CMakeLists.txt
+++ b/libc/src/__support/macros/properties/CMakeLists.txt
@@ -33,6 +33,6 @@ add_header_library(
     .compiler
     .cpu_features
     .os
-    libc.include.llvm-libc-macros.float_macros
+    libc.hdr.float_macros
     libc.include.llvm-libc-types.float128
 )
diff --git a/libc/src/__support/macros/properties/types.h b/libc/src/__support/macros/properties/types.h
index d43cf99e6859b..781cf1b7a2b62 100644
--- a/libc/src/__support/macros/properties/types.h
+++ b/libc/src/__support/macros/properties/types.h
@@ -10,7 +10,7 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_TYPES_H
 #define LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_TYPES_H
 
-#include "include/llvm-libc-macros/float-macros.h" // LDBL_MANT_DIG
+#include "hdr/float_macros.h"                      // LDBL_MANT_DIG
 #include "include/llvm-libc-types/float128.h"      // float128
 #include "src/__support/macros/properties/architectures.h"
 #include "src/__support/macros/properties/compiler.h"
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index daaf505008ca1..d37ecb375bf4b 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2969,6 +2969,7 @@ add_entrypoint_object(
   HDRS
     ../scalbnf128.h
   DEPENDS
+    libc.hdr.float_macros
     libc.src.__support.macros.properties.types
     libc.src.__support.FPUtil.manipulation_functions
   COMPILE_OPTIONS
diff --git a/libc/src/math/generic/scalbnf128.cpp b/libc/src/math/generic/scalbnf128.cpp
index be3d29ed27e98..4b2b00b298c5e 100644
--- a/libc/src/math/generic/scalbnf128.cpp
+++ b/libc/src/math/generic/scalbnf128.cpp
@@ -7,18 +7,17 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/scalbnf128.h"
+#include "hdr/float_macros.h"
 #include "src/__support/FPUtil/ManipulationFunctions.h"
 #include "src/__support/common.h"
 
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(float128, scalbnf128, (float128 x, int n)) {
-// TODO: should be switched to use `FLT_RADIX` in hdr/float_macros.h" instead
-// see: https://github.com/llvm/llvm-project/issues/90496
-#if !defined(__FLT_RADIX__)
-#error __FLT_RADIX__ undefined.
-#elif __FLT_RADIX__ != 2
-#error __FLT_RADIX__!=2, unimplemented.
+#if !defined(FLT_RADIX)
+#error FLT_RADIX undefined.
+#elif FLT_RADIX != 2
+#error FLT_RADIX!=2, unimplemented.
 #else
   return fputil::ldexp(x, n);
 #endif
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 446499cf15d7b..70ec3a48a5e2e 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -127,6 +127,11 @@ libc_support_library(
     hdrs = ["hdr/time_macros.h"],
 )
 
+libc_support_library(
+    name = "hdr_float_macros",
+    hdrs = ["hdr/float_macros.h"],
+)
+
 ############################ Type Proxy Header Files ###########################
 
 libc_support_library(
@@ -189,7 +194,7 @@ libc_support_library(
         ":__support_macros_properties_compiler",
         ":__support_macros_properties_cpu_features",
         ":__support_macros_properties_os",
-        ":llvm_libc_macros_float_macros",
+        ":hdr_float_macros",
         ":llvm_libc_types_float128",
     ],
 )

>From 14be43f6a743b1e05414efaa5e391d28fcf49713 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Tue, 28 May 2024 17:20:18 +0000
Subject: [PATCH 2/5] Address comments.

---
 libc/hdr/float_macros.h              | 12 +++++++++---
 libc/src/math/generic/scalbnf128.cpp |  4 +---
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/libc/hdr/float_macros.h b/libc/hdr/float_macros.h
index f541a886e1f2d..0643e53b42720 100644
--- a/libc/hdr/float_macros.h
+++ b/libc/hdr/float_macros.h
@@ -9,11 +9,17 @@
 #ifndef LLVM_LIBC_HDR_FLOAT_MACROS_H
 #define LLVM_LIBC_HDR_FLOAT_MACROS_H
 
-#ifndef LIBC_FULL_BUILD
-// Overlay mode
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/float-macros.h"
+
+#else // Overlay mode
+
 #include <float.h>
-#endif // !LLVM_LIBC_FULL_BUILD
 
+// Filling missing macros if any.
 #include "include/llvm-libc-macros/float-macros.h"
 
+#endif // LLVM_LIBC_FULL_BUILD
+
 #endif // LLVM_LIBC_HDR_FLOAT_MACROS_H
diff --git a/libc/src/math/generic/scalbnf128.cpp b/libc/src/math/generic/scalbnf128.cpp
index 4b2b00b298c5e..c010f0dd94d78 100644
--- a/libc/src/math/generic/scalbnf128.cpp
+++ b/libc/src/math/generic/scalbnf128.cpp
@@ -14,9 +14,7 @@
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(float128, scalbnf128, (float128 x, int n)) {
-#if !defined(FLT_RADIX)
-#error FLT_RADIX undefined.
-#elif FLT_RADIX != 2
+#if FLT_RADIX != 2
 #error FLT_RADIX!=2, unimplemented.
 #else
   return fputil::ldexp(x, n);

>From e6f10b65bf6c40c15bb61b8ec986ed5bc29b17f7 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Tue, 28 May 2024 17:35:45 +0000
Subject: [PATCH 3/5] Address comments.

---
 libc/hdr/float_macros.h              | 3 ---
 libc/src/math/generic/scalbnf128.cpp | 8 ++++----
 2 files changed, 4 insertions(+), 7 deletions(-)

diff --git a/libc/hdr/float_macros.h b/libc/hdr/float_macros.h
index 0643e53b42720..a0ef5e29b9868 100644
--- a/libc/hdr/float_macros.h
+++ b/libc/hdr/float_macros.h
@@ -17,9 +17,6 @@
 
 #include <float.h>
 
-// Filling missing macros if any.
-#include "include/llvm-libc-macros/float-macros.h"
-
 #endif // LLVM_LIBC_FULL_BUILD
 
 #endif // LLVM_LIBC_HDR_FLOAT_MACROS_H
diff --git a/libc/src/math/generic/scalbnf128.cpp b/libc/src/math/generic/scalbnf128.cpp
index c010f0dd94d78..75e4968caf827 100644
--- a/libc/src/math/generic/scalbnf128.cpp
+++ b/libc/src/math/generic/scalbnf128.cpp
@@ -11,14 +11,14 @@
 #include "src/__support/FPUtil/ManipulationFunctions.h"
 #include "src/__support/common.h"
 
+#if FLT_RADIX != 2
+#error FLT_RADIX!=2, unimplemented.
+#endif
+
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(float128, scalbnf128, (float128 x, int n)) {
-#if FLT_RADIX != 2
-#error FLT_RADIX!=2, unimplemented.
-#else
   return fputil::ldexp(x, n);
-#endif
 }
 
 } // namespace LIBC_NAMESPACE

>From 1b58749ea47d8e039a1abb3f546152387b059c6b Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Tue, 28 May 2024 18:43:50 +0000
Subject: [PATCH 4/5] Only use predefined macros.

---
 libc/include/llvm-libc-macros/float-macros.h | 192 -------------------
 libc/src/math/generic/CMakeLists.txt         |   3 +
 libc/src/math/generic/scalbn.cpp             |  11 +-
 libc/src/math/generic/scalbnf.cpp            |  11 +-
 libc/src/math/generic/scalbnf128.cpp         |   2 +-
 libc/src/math/generic/scalbnl.cpp            |  11 +-
 6 files changed, 19 insertions(+), 211 deletions(-)

diff --git a/libc/include/llvm-libc-macros/float-macros.h b/libc/include/llvm-libc-macros/float-macros.h
index a054448dd5b39..503872c2d2d88 100644
--- a/libc/include/llvm-libc-macros/float-macros.h
+++ b/libc/include/llvm-libc-macros/float-macros.h
@@ -9,29 +9,12 @@
 #ifndef LLVM_LIBC_MACROS_FLOAT_MACROS_H
 #define LLVM_LIBC_MACROS_FLOAT_MACROS_H
 
-// Check long double.
-#if defined(__linux__) && defined(__x86_64__)
-#define LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80
-#elif defined(__linux__) && (defined(__aarch64__) || defined(__riscv))
-#define LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT128
-#else
-#define LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64
-#endif
-
 #ifndef FLT_RADIX
-#ifdef __FLT_RADIX__
 #define FLT_RADIX __FLT_RADIX__
-#else
-#define FLT_RADIX 2
-#endif // __FLT_RADIX__
 #endif // FLT_RADIX
 
 #ifndef FLT_EVAL_METHOD
-#ifdef __FLT_EVAL_METHOD__
 #define FLT_EVAL_METHOD __FLT_EVAL_METHOD__
-#else
-#define FLT_EVAL_METHOD 0
-#endif // __FLT_EVAL_METHOD__
 #endif // FLT_EVAL_METHOD
 
 #ifndef FLT_ROUNDS
@@ -39,316 +22,141 @@
 #endif // FLT_ROUNDS
 
 #ifndef FLT_DECIMAL_DIG
-#ifdef __FLT_DECIMAL_DIG__
 #define FLT_DECIMAL_DIG __FLT_DECIMAL_DIG__
-#else
-#define FLT_DECIMAL_DIG 9
-#endif // __FLT_DECIMAL_DIG__
 #endif // FLT_DECIMAL_DIG
 
 #ifndef DBL_DECIMAL_DIG
-#ifdef __DBL_DECIMAL_DIG__
 #define DBL_DECIMAL_DIG __DBL_DECIMAL_DIG__
-#else
-#define DBL_DECIMAL_DIG 17
-#endif // __DBL_DECIMAL_DIG__
 #endif // DBL_DECIMAL_DIG
 
 #ifndef LDBL_DECIMAL_DIG
-#ifdef __LDBL_DECIMAL_DIG__
 #define LDBL_DECIMAL_DIG __LDBL_DECIMAL_DIG__
-#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80)
-#define LDBL_DECIMAL_DIG 21
-#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT128)
-#define LDBL_DECIMAL_DIG 36
-#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64
-#define LDBL_DECIMAL_DIG DBL_DECIMAL_DIG
-#endif // __LDBL_DECIMAL_DIG
 #endif // LDBL_DECIMAL_DIG
 
 #ifndef DECIMAL_DIG
-#ifdef __DECIMAL_DIG__
 #define DECIMAL_DIG __DECIMAL_DIG__
-#else
-#define DECIMAL_DIG LDBL_DECIMAL_DIG
-#endif // __DECIMAL_DIG
 #endif // DECIMAL_DIG
 
 #ifndef FLT_DIG
-#ifdef __FLT_DIG__
 #define FLT_DIG __FLT_DIG__
-#else
-#define FLT_DIG 6
-#endif // __FLT_DIG__
 #endif // FLT_DIG
 
 #ifndef DBL_DIG
-#ifdef __DBL_DIG__
 #define DBL_DIG __DBL_DIG__
-#else
-#define DBL_DIG 15
-#endif // __DBL_DIG__
 #endif // DBL_DIG
 
 #ifndef LDBL_DIG
-#ifdef __LDBL_DIG__
 #define LDBL_DIG __LDBL_DIG__
-#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80)
-#define LDBL_DIG 18
-#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT128)
-#define LDBL_DIG 33
-#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64
-#define LDBL_DIG DBL_DIG
-#endif // __LDBL_DIG__
 #endif // LDBL_DIG
 
 #ifndef FLT_MANT_DIG
-#ifdef __FLT_MANT_DIG__
 #define FLT_MANT_DIG __FLT_MANT_DIG__
-#else
-#define FLT_MANT_DIG 24
-#endif // __FLT_MANT_DIG__
 #endif // FLT_MANT_DIG
 
 #ifndef DBL_MANT_DIG
-#ifdef __DBL_MANT_DIG__
 #define DBL_MANT_DIG __DBL_MANT_DIG__
-#else
-#define DBL_MANT_DIG 53
-#endif // __DBL_MANT_DIG__
 #endif // DBL_MANT_DIG
 
 #ifndef LDBL_MANT_DIG
-#ifdef __LDBL_MANT_DIG__
 #define LDBL_MANT_DIG __LDBL_MANT_DIG__
-#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80)
-#define LDBL_MANT_DIG 64
-#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT128)
-#define LDBL_MANT_DIG 113
-#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64
-#define LDBL_MANT_DIG DBL_MANT_DIG
-#endif // __LDBL_MANT_DIG__
 #endif // LDBL_MANT_DIG
 
 #ifndef FLT_MIN
-#ifdef __FLT_MIN__
 #define FLT_MIN __FLT_MIN__
-#else
-#define FLT_MIN 0x1.0p-126f
-#endif // __FLT_MIN__
 #endif // FLT_MIN
 
 #ifndef DBL_MIN
-#ifdef __DBL_MIN__
 #define DBL_MIN __DBL_MIN__
-#else
-#define DBL_MIN 0x1.0p-1022
-#endif // __DBL_MIN__
 #endif // DBL_MIN
 
 #ifndef LDBL_MIN
-#ifdef __LDBL_MIN__
 #define LDBL_MIN __LDBL_MIN__
-#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64)
-#define LDBL_MIN 0x1.0p-1022L
-#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80 or FLOAT128
-#define LDBL_MIN 0x1.0p-16382L
-#endif // __LDBL_MIN__
 #endif // LDBL_MIN
 
 #ifndef FLT_MAX
-#ifdef __FLT_MAX__
 #define FLT_MAX __FLT_MAX__
-#else
-#define FLT_MAX 0x1.fffffep+127f
-#endif // __FLT_MAX__
 #endif // FLT_MAX
 
 #ifndef DBL_MAX
-#ifdef __DBL_MAX__
 #define DBL_MAX __DBL_MAX__
-#else
-#define DBL_MAX 0x1.ffff'ffff'ffff'fp+1023
-#endif // __DBL_MAX__
 #endif // DBL_MAX
 
 #ifndef LDBL_MAX
-#ifdef __LDBL_MAX__
 #define LDBL_MAX __LDBL_MAX__
-#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80)
-#define LDBL_MAX 0x1.ffff'ffff'ffff'fffep+16383L
-#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT128)
-#define LDBL_MAX 0x1.ffff'ffff'ffff'ffff'ffff'ffff'ffffp+16383L
-#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64
-#define LDBL_MAX 0x1.ffff'ffff'ffff'fp+1023L
-#endif // __LDBL_MAX__
 #endif // LDBL_MAX
 
 #ifndef FLT_TRUE_MIN
-#ifdef __FLT_DENORM_MIN__
 #define FLT_TRUE_MIN __FLT_DENORM_MIN__
-#else
-#define FLT_TRUE_MIN 0x1.0p-149f
-#endif // __FLT_DENORM_MIN__
 #endif // FLT_TRUE_MIN
 
 #ifndef DBL_TRUE_MIN
-#ifdef __DBL_DENORM_MIN__
 #define DBL_TRUE_MIN __DBL_DENORM_MIN__
-#else
-#define DBL_TRUE_MIN 0x1.0p-1074
-#endif // __DBL_DENORM_MIN__
 #endif // DBL_TRUE_MIN
 
 #ifndef LDBL_TRUE_MIN
-#ifdef __LDBL_DENORM_MIN__
 #define LDBL_TRUE_MIN __LDBL_DENORM_MIN__
-#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80)
-#define LDBL_TRUE_MIN 0x1.0p-16445L
-#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT128)
-#define LDBL_TRUE_MIN 0x1.0p-16494L
-#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64
-#define LDBL_TRUE_MIN 0x1.0p-1074L
-#endif // __LDBL_DENORM_MIN__
 #endif // LDBL_TRUE_MIN
 
 #ifndef FLT_EPSILON
-#ifdef __FLT_EPSILON__
 #define FLT_EPSILON __FLT_EPSILON__
-#else
-#define FLT_EPSILON 0x1.0p-23f
-#endif // __FLT_EPSILON__
 #endif // FLT_EPSILON
 
 #ifndef DBL_EPSILON
-#ifdef __DBL_EPSILON__
 #define DBL_EPSILON __DBL_EPSILON__
-#else
-#define DBL_EPSILON 0x1.0p-52
-#endif // __DBL_EPSILON__
 #endif // DBL_EPSILON
 
 #ifndef LDBL_EPSILON
-#ifdef __LDBL_EPSILON__
 #define LDBL_EPSILON __LDBL_EPSILON__
-#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80)
-#define LDBL_EPSILON 0x1.0p-63L
-#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT128)
-#define LDBL_EPSILON 0x1.0p-112L
-#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64
-#define LDBL_EPSILON 0x1.0p-52L
-#endif // __LDBL_EPSILON__
 #endif // LDBL_EPSILON
 
 #ifndef FLT_MIN_EXP
-#ifdef __FLT_MIN_EXP__
 #define FLT_MIN_EXP __FLT_MIN_EXP__
-#else
-#define FLT_MIN_EXP (-125)
-#endif // __FLT_MIN_EXP__
 #endif // FLT_MIN_EXP
 
 #ifndef DBL_MIN_EXP
-#ifdef __DBL_MIN_EXP__
 #define DBL_MIN_EXP __DBL_MIN_EXP__
-#else
-#define DBL_MIN_EXP (-1021)
-#endif // __DBL_MIN_EXP__
 #endif // DBL_MIN_EXP
 
 #ifndef LDBL_MIN_EXP
-#ifdef __LDBL_MIN_EXP__
 #define LDBL_MIN_EXP __LDBL_MIN_EXP__
-#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64)
-#define LDBL_MIN_EXP DBL_MIN_EXP
-#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80 or FLOAT128
-#define LDBL_MIN_EXP (-16381)
-#endif // __LDBL_MIN_EXP__
 #endif // LDBL_MIN_EXP
 
 #ifndef FLT_MIN_10_EXP
-#ifdef __FLT_MIN_10_EXP__
 #define FLT_MIN_10_EXP __FLT_MIN_10_EXP__
-#else
-#define FLT_MIN_10_EXP (-37)
-#endif // __FLT_MIN_10_EXP__
 #endif // FLT_MIN_10_EXP
 
 #ifndef DBL_MIN_10_EXP
-#ifdef __DBL_MIN_10_EXP__
 #define DBL_MIN_10_EXP __DBL_MIN_10_EXP__
-#else
-#define DBL_MIN_10_EXP (-307)
-#endif // __DBL_MIN_10_EXP__
 #endif // DBL_MIN_10_EXP
 
 #ifndef LDBL_MIN_10_EXP
-#ifdef __LDBL_MIN_10_EXP__
 #define LDBL_MIN_10_EXP __LDBL_MIN_10_EXP__
-#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64)
-#define LDBL_MIN_10_EXP DBL_MIN_10_EXP
-#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80 or FLOAT128
-#define LDBL_MIN_10_EXP (-4931)
-#endif // __LDBL_MIN_10_EXP__
 #endif // LDBL_MIN_10_EXP
 
 #ifndef FLT_MAX_EXP
-#ifdef __FLT_MAX_EXP__
 #define FLT_MAX_EXP __FLT_MAX_EXP__
-#else
-#define FLT_MAX_EXP 128
-#endif // __FLT_MAX_EXP__
 #endif // FLT_MAX_EXP
 
 #ifndef DBL_MAX_EXP
-#ifdef __DBL_MAX_EXP__
 #define DBL_MAX_EXP __DBL_MAX_EXP__
-#else
-#define DBL_MAX_EXP 1024
-#endif // __DBL_MAX_EXP__
 #endif // DBL_MAX_EXP
 
 #ifndef LDBL_MAX_EXP
-#ifdef __LDBL_MAX_EXP__
 #define LDBL_MAX_EXP __LDBL_MAX_EXP__
-#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64)
-#define LDBL_MAX_EXP DBL_MAX_EXP
-#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80 or FLOAT128
-#define LDBL_MAX_EXP 16384
-#endif // __LDBL_MAX_EXP__
 #endif // LDBL_MAX_EXP
 
 #ifndef FLT_MAX_10_EXP
-#ifdef __FLT_MAX_10_EXP__
 #define FLT_MAX_10_EXP __FLT_MAX_10_EXP__
-#else
-#define FLT_MAX_10_EXP 38
-#endif // __FLT_MAX_10_EXP__
 #endif // FLT_MAX_10_EXP
 
 #ifndef DBL_MAX_10_EXP
-#ifdef __DBL_MAX_10_EXP__
 #define DBL_MAX_10_EXP __DBL_MAX_10_EXP__
-#else
-#define DBL_MAX_10_EXP 308
-#endif // __DBL_MAX_10_EXP__
 #endif // DBL_MAX_10_EXP
 
 #ifndef LDBL_MAX_10_EXP
-#ifdef __LDBL_MAX_10_EXP__
 #define LDBL_MAX_10_EXP __LDBL_MAX_10_EXP__
-#elif defined(LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64)
-#define LDBL_MAX_10_EXP DBL_MAX_10_EXP
-#else // LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80 or FLOAT128
-#define LDBL_MAX_10_EXP 4932
-#endif // __LDBL_MAX_10_EXP__
 #endif // LDBL_MAX_10_EXP
 
 // TODO: Add FLT16 and FLT128 constants.
 
-// Cleanup
-#undef LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT64
-#undef LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT80
-#undef LLVM_LIBC_MACROS_LONG_DOUBLE_IS_FLOAT128
-
 #endif // LLVM_LIBC_MACROS_FLOAT_MACROS_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index d37ecb375bf4b..269bc6be5d834 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2933,6 +2933,7 @@ add_entrypoint_object(
   HDRS
     ../scalbn.h
   DEPENDS
+    libc.hdr.float_macros
     libc.src.__support.FPUtil.manipulation_functions
   COMPILE_OPTIONS
     -O3
@@ -2945,6 +2946,7 @@ add_entrypoint_object(
   HDRS
     ../scalbnf.h
   DEPENDS
+    libc.hdr.float_macros
     libc.src.__support.FPUtil.manipulation_functions
   COMPILE_OPTIONS
     -O3
@@ -2957,6 +2959,7 @@ add_entrypoint_object(
   HDRS
     ../scalbnl.h
   DEPENDS
+    libc.hdr.float_macros
     libc.src.__support.FPUtil.manipulation_functions
   COMPILE_OPTIONS
     -O3
diff --git a/libc/src/math/generic/scalbn.cpp b/libc/src/math/generic/scalbn.cpp
index 3908f5892f144..207cce1550bc0 100644
--- a/libc/src/math/generic/scalbn.cpp
+++ b/libc/src/math/generic/scalbn.cpp
@@ -7,19 +7,18 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/scalbn.h"
+#include "hdr/float_macros.h"
 #include "src/__support/FPUtil/ManipulationFunctions.h"
 #include "src/__support/common.h"
 
+#if FLT_RADIX != 2
+#error "FLT_RADIX != 2 is not supported."
+#endif
+
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(double, scalbn, (double x, int n)) {
-#if !defined(__FLT_RADIX__)
-#error __FLT_RADIX__ undefined.
-#elif __FLT_RADIX__ != 2
-#error __FLT_RADIX__!=2, unimplemented.
-#else
   return fputil::ldexp(x, n);
-#endif
 }
 
 } // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/generic/scalbnf.cpp b/libc/src/math/generic/scalbnf.cpp
index 4a4fa86dcfd89..e478088d3ce5a 100644
--- a/libc/src/math/generic/scalbnf.cpp
+++ b/libc/src/math/generic/scalbnf.cpp
@@ -7,19 +7,18 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/scalbnf.h"
+#include "hdr/float_macros.h"
 #include "src/__support/FPUtil/ManipulationFunctions.h"
 #include "src/__support/common.h"
 
+#if FLT_RADIX != 2
+#error "FLT_RADIX != 2 is not supported."
+#endif
+
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(float, scalbnf, (float x, int n)) {
-#if !defined(__FLT_RADIX__)
-#error __FLT_RADIX__ undefined.
-#elif __FLT_RADIX__ != 2
-#error __FLT_RADIX__!=2, unimplemented.
-#else
   return fputil::ldexp(x, n);
-#endif
 }
 
 } // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/generic/scalbnf128.cpp b/libc/src/math/generic/scalbnf128.cpp
index 75e4968caf827..5fd59611d53de 100644
--- a/libc/src/math/generic/scalbnf128.cpp
+++ b/libc/src/math/generic/scalbnf128.cpp
@@ -12,7 +12,7 @@
 #include "src/__support/common.h"
 
 #if FLT_RADIX != 2
-#error FLT_RADIX!=2, unimplemented.
+#error "FLT_RADIX != 2 is not supported."
 #endif
 
 namespace LIBC_NAMESPACE {
diff --git a/libc/src/math/generic/scalbnl.cpp b/libc/src/math/generic/scalbnl.cpp
index 681338ec01f07..1225a7ebaf572 100644
--- a/libc/src/math/generic/scalbnl.cpp
+++ b/libc/src/math/generic/scalbnl.cpp
@@ -7,19 +7,18 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/scalbnl.h"
+#include "hdr/float_macros.h"
 #include "src/__support/FPUtil/ManipulationFunctions.h"
 #include "src/__support/common.h"
 
+#if FLT_RADIX != 2
+#error "FLT_RADIX != 2 is not supported."
+#endif
+
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(long double, scalbnl, (long double x, int n)) {
-#if !defined(__FLT_RADIX__)
-#error __FLT_RADIX__ undefined.
-#elif __FLT_RADIX__ != 2
-#error __FLT_RADIX__!=2, unimplemented.
-#else
   return fputil::ldexp(x, n);
-#endif
 }
 
 } // namespace LIBC_NAMESPACE

>From 08634a888f0acc78521605bff083659dd2a9a5a3 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Tue, 28 May 2024 19:10:33 +0000
Subject: [PATCH 5/5] Use __builtin_flt_rounds() if available.

---
 libc/include/llvm-libc-macros/float-macros.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/libc/include/llvm-libc-macros/float-macros.h b/libc/include/llvm-libc-macros/float-macros.h
index 503872c2d2d88..81c1df868bf6c 100644
--- a/libc/include/llvm-libc-macros/float-macros.h
+++ b/libc/include/llvm-libc-macros/float-macros.h
@@ -18,7 +18,11 @@
 #endif // FLT_EVAL_METHOD
 
 #ifndef FLT_ROUNDS
+#if __has_builtin(__builtin_flt_rounds)
+#define FLT_ROUNDS __builtin_flt_rounds()
+#else
 #define FLT_ROUNDS 1
+#endif
 #endif // FLT_ROUNDS
 
 #ifndef FLT_DECIMAL_DIG



More information about the libc-commits mailing list