Skip to content

Commit 4486fcb

Browse files
lntueaniplcc
andauthored
[libc] Add proxy header for float.h. (#93504)
This is the continuation of #88674. Fixes #88433, #90496. --------- Co-authored-by: aniplcc <[email protected]>
1 parent c179d50 commit 4486fcb

File tree

11 files changed

+78
-50
lines changed

11 files changed

+78
-50
lines changed

libc/hdr/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,14 @@ add_proxy_header_library(
8787
libc.include.llvm-libc-macros.time_macros
8888
)
8989

90+
add_proxy_header_library(
91+
float_macros
92+
HDRS
93+
float_macros.h
94+
DEPENDS
95+
libc.include.llvm-libc-macros.float_macros
96+
FULL_BUILD_DEPENDS
97+
libc.include.float
98+
)
99+
90100
add_subdirectory(types)

libc/hdr/float_macros.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of macros from math.h ----------------------------------===//
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_HDR_FLOAT_MACROS_H
10+
#define LLVM_LIBC_HDR_FLOAT_MACROS_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-macros/float-macros.h"
15+
16+
#else // Overlay mode
17+
18+
#include <float.h>
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_FLOAT_MACROS_H

libc/include/llvm-libc-macros/float-macros.h

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,6 @@
99
#ifndef LLVM_LIBC_MACROS_FLOAT_MACROS_H
1010
#define LLVM_LIBC_MACROS_FLOAT_MACROS_H
1111

12-
// Suppress `#include_next is a language extension` warnings.
13-
#ifdef __clang__
14-
#pragma clang diagnostic push
15-
#pragma clang diagnostic ignored "-Wgnu-include-next"
16-
#pragma clang diagnostic ignored "-Winclude-next-absolute-path"
17-
#else // gcc
18-
#pragma GCC system_header
19-
#endif //__clang__
20-
21-
#include_next <float.h>
22-
23-
#ifdef __clang__
24-
#pragma clang diagnostic pop
25-
#endif //__clang__
26-
2712
#ifndef FLT_RADIX
2813
#define FLT_RADIX __FLT_RADIX__
2914
#endif // FLT_RADIX
@@ -32,9 +17,13 @@
3217
#define FLT_EVAL_METHOD __FLT_EVAL_METHOD__
3318
#endif // FLT_EVAL_METHOD
3419

35-
#ifndef DECIMAL_DIG
36-
#define DECIMAL_DIG __DECIMAL_DIG__
37-
#endif // DECIMAL_DIG
20+
#ifndef FLT_ROUNDS
21+
#if __has_builtin(__builtin_flt_rounds)
22+
#define FLT_ROUNDS __builtin_flt_rounds()
23+
#else
24+
#define FLT_ROUNDS 1
25+
#endif
26+
#endif // FLT_ROUNDS
3827

3928
#ifndef FLT_DECIMAL_DIG
4029
#define FLT_DECIMAL_DIG __FLT_DECIMAL_DIG__
@@ -48,6 +37,10 @@
4837
#define LDBL_DECIMAL_DIG __LDBL_DECIMAL_DIG__
4938
#endif // LDBL_DECIMAL_DIG
5039

40+
#ifndef DECIMAL_DIG
41+
#define DECIMAL_DIG __DECIMAL_DIG__
42+
#endif // DECIMAL_DIG
43+
5144
#ifndef FLT_DIG
5245
#define FLT_DIG __FLT_DIG__
5346
#endif // FLT_DIG
@@ -97,15 +90,15 @@
9790
#endif // LDBL_MAX
9891

9992
#ifndef FLT_TRUE_MIN
100-
#define FLT_TRUE_MIN __FLT_TRUE_MIN__
93+
#define FLT_TRUE_MIN __FLT_DENORM_MIN__
10194
#endif // FLT_TRUE_MIN
10295

10396
#ifndef DBL_TRUE_MIN
104-
#define DBL_TRUE_MIN __DBL_TRUE_MIN__
97+
#define DBL_TRUE_MIN __DBL_DENORM_MIN__
10598
#endif // DBL_TRUE_MIN
10699

107100
#ifndef LDBL_TRUE_MIN
108-
#define LDBL_TRUE_MIN __LDBL_TRUE_MIN__
101+
#define LDBL_TRUE_MIN __LDBL_DENORM_MIN__
109102
#endif // LDBL_TRUE_MIN
110103

111104
#ifndef FLT_EPSILON

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ add_header_library(
3333
.compiler
3434
.cpu_features
3535
.os
36-
libc.include.llvm-libc-macros.float_macros
36+
libc.hdr.float_macros
3737
libc.include.llvm-libc-types.float128
3838
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_TYPES_H
1111
#define LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_TYPES_H
1212

13-
#include "include/llvm-libc-macros/float-macros.h" // LDBL_MANT_DIG
13+
#include "hdr/float_macros.h" // LDBL_MANT_DIG
1414
#include "include/llvm-libc-types/float128.h" // float128
1515
#include "src/__support/macros/properties/architectures.h"
1616
#include "src/__support/macros/properties/compiler.h"

libc/src/math/generic/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2933,6 +2933,7 @@ add_entrypoint_object(
29332933
HDRS
29342934
../scalbn.h
29352935
DEPENDS
2936+
libc.hdr.float_macros
29362937
libc.src.__support.FPUtil.manipulation_functions
29372938
COMPILE_OPTIONS
29382939
-O3
@@ -2945,6 +2946,7 @@ add_entrypoint_object(
29452946
HDRS
29462947
../scalbnf.h
29472948
DEPENDS
2949+
libc.hdr.float_macros
29482950
libc.src.__support.FPUtil.manipulation_functions
29492951
COMPILE_OPTIONS
29502952
-O3
@@ -2957,6 +2959,7 @@ add_entrypoint_object(
29572959
HDRS
29582960
../scalbnl.h
29592961
DEPENDS
2962+
libc.hdr.float_macros
29602963
libc.src.__support.FPUtil.manipulation_functions
29612964
COMPILE_OPTIONS
29622965
-O3
@@ -2969,6 +2972,7 @@ add_entrypoint_object(
29692972
HDRS
29702973
../scalbnf128.h
29712974
DEPENDS
2975+
libc.hdr.float_macros
29722976
libc.src.__support.macros.properties.types
29732977
libc.src.__support.FPUtil.manipulation_functions
29742978
COMPILE_OPTIONS

libc/src/math/generic/scalbn.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/math/scalbn.h"
10+
#include "hdr/float_macros.h"
1011
#include "src/__support/FPUtil/ManipulationFunctions.h"
1112
#include "src/__support/common.h"
1213

14+
#if FLT_RADIX != 2
15+
#error "FLT_RADIX != 2 is not supported."
16+
#endif
17+
1318
namespace LIBC_NAMESPACE {
1419

1520
LLVM_LIBC_FUNCTION(double, scalbn, (double x, int n)) {
16-
#if !defined(__FLT_RADIX__)
17-
#error __FLT_RADIX__ undefined.
18-
#elif __FLT_RADIX__ != 2
19-
#error __FLT_RADIX__!=2, unimplemented.
20-
#else
2121
return fputil::ldexp(x, n);
22-
#endif
2322
}
2423

2524
} // namespace LIBC_NAMESPACE

libc/src/math/generic/scalbnf.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/math/scalbnf.h"
10+
#include "hdr/float_macros.h"
1011
#include "src/__support/FPUtil/ManipulationFunctions.h"
1112
#include "src/__support/common.h"
1213

14+
#if FLT_RADIX != 2
15+
#error "FLT_RADIX != 2 is not supported."
16+
#endif
17+
1318
namespace LIBC_NAMESPACE {
1419

1520
LLVM_LIBC_FUNCTION(float, scalbnf, (float x, int n)) {
16-
#if !defined(__FLT_RADIX__)
17-
#error __FLT_RADIX__ undefined.
18-
#elif __FLT_RADIX__ != 2
19-
#error __FLT_RADIX__!=2, unimplemented.
20-
#else
2121
return fputil::ldexp(x, n);
22-
#endif
2322
}
2423

2524
} // namespace LIBC_NAMESPACE

libc/src/math/generic/scalbnf128.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,18 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/math/scalbnf128.h"
10+
#include "hdr/float_macros.h"
1011
#include "src/__support/FPUtil/ManipulationFunctions.h"
1112
#include "src/__support/common.h"
1213

14+
#if FLT_RADIX != 2
15+
#error "FLT_RADIX != 2 is not supported."
16+
#endif
17+
1318
namespace LIBC_NAMESPACE {
1419

1520
LLVM_LIBC_FUNCTION(float128, scalbnf128, (float128 x, int n)) {
16-
// TODO: should be switched to use `FLT_RADIX` in hdr/float_macros.h" instead
17-
// see: https://github.com/llvm/llvm-project/issues/90496
18-
#if !defined(__FLT_RADIX__)
19-
#error __FLT_RADIX__ undefined.
20-
#elif __FLT_RADIX__ != 2
21-
#error __FLT_RADIX__!=2, unimplemented.
22-
#else
2321
return fputil::ldexp(x, n);
24-
#endif
2522
}
2623

2724
} // namespace LIBC_NAMESPACE

libc/src/math/generic/scalbnl.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/math/scalbnl.h"
10+
#include "hdr/float_macros.h"
1011
#include "src/__support/FPUtil/ManipulationFunctions.h"
1112
#include "src/__support/common.h"
1213

14+
#if FLT_RADIX != 2
15+
#error "FLT_RADIX != 2 is not supported."
16+
#endif
17+
1318
namespace LIBC_NAMESPACE {
1419

1520
LLVM_LIBC_FUNCTION(long double, scalbnl, (long double x, int n)) {
16-
#if !defined(__FLT_RADIX__)
17-
#error __FLT_RADIX__ undefined.
18-
#elif __FLT_RADIX__ != 2
19-
#error __FLT_RADIX__!=2, unimplemented.
20-
#else
2121
return fputil::ldexp(x, n);
22-
#endif
2322
}
2423

2524
} // namespace LIBC_NAMESPACE

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ libc_support_library(
127127
hdrs = ["hdr/time_macros.h"],
128128
)
129129

130+
libc_support_library(
131+
name = "hdr_float_macros",
132+
hdrs = ["hdr/float_macros.h"],
133+
)
134+
130135
############################ Type Proxy Header Files ###########################
131136

132137
libc_support_library(
@@ -189,7 +194,7 @@ libc_support_library(
189194
":__support_macros_properties_compiler",
190195
":__support_macros_properties_cpu_features",
191196
":__support_macros_properties_os",
192-
":llvm_libc_macros_float_macros",
197+
":hdr_float_macros",
193198
":llvm_libc_types_float128",
194199
],
195200
)

0 commit comments

Comments
 (0)