Skip to content

Commit

Permalink
[libc] Add proxy header for float.h. (#93504)
Browse files Browse the repository at this point in the history
This is the continuation of
#88674.

Fixes #88433, #90496.

---------

Co-authored-by: aniplcc <aniplccode@gmail.com>
  • Loading branch information
lntue and aniplcc authored May 28, 2024
1 parent c179d50 commit 4486fcb
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 50 deletions.
10 changes: 10 additions & 0 deletions libc/hdr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
22 changes: 22 additions & 0 deletions libc/hdr/float_macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- 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

#ifdef LIBC_FULL_BUILD

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

#else // Overlay mode

#include <float.h>

#endif // LLVM_LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_FLOAT_MACROS_H
35 changes: 14 additions & 21 deletions libc/include/llvm-libc-macros/float-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,6 @@
#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__

#ifndef FLT_RADIX
#define FLT_RADIX __FLT_RADIX__
#endif // FLT_RADIX
Expand All @@ -32,9 +17,13 @@
#define FLT_EVAL_METHOD __FLT_EVAL_METHOD__
#endif // FLT_EVAL_METHOD

#ifndef DECIMAL_DIG
#define DECIMAL_DIG __DECIMAL_DIG__
#endif // DECIMAL_DIG
#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
#define FLT_DECIMAL_DIG __FLT_DECIMAL_DIG__
Expand All @@ -48,6 +37,10 @@
#define LDBL_DECIMAL_DIG __LDBL_DECIMAL_DIG__
#endif // LDBL_DECIMAL_DIG

#ifndef DECIMAL_DIG
#define DECIMAL_DIG __DECIMAL_DIG__
#endif // DECIMAL_DIG

#ifndef FLT_DIG
#define FLT_DIG __FLT_DIG__
#endif // FLT_DIG
Expand Down Expand Up @@ -97,15 +90,15 @@
#endif // LDBL_MAX

#ifndef FLT_TRUE_MIN
#define FLT_TRUE_MIN __FLT_TRUE_MIN__
#define FLT_TRUE_MIN __FLT_DENORM_MIN__
#endif // FLT_TRUE_MIN

#ifndef DBL_TRUE_MIN
#define DBL_TRUE_MIN __DBL_TRUE_MIN__
#define DBL_TRUE_MIN __DBL_DENORM_MIN__
#endif // DBL_TRUE_MIN

#ifndef LDBL_TRUE_MIN
#define LDBL_TRUE_MIN __LDBL_TRUE_MIN__
#define LDBL_TRUE_MIN __LDBL_DENORM_MIN__
#endif // LDBL_TRUE_MIN

#ifndef FLT_EPSILON
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/macros/properties/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 1 addition & 1 deletion libc/src/__support/macros/properties/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2933,6 +2933,7 @@ add_entrypoint_object(
HDRS
../scalbn.h
DEPENDS
libc.hdr.float_macros
libc.src.__support.FPUtil.manipulation_functions
COMPILE_OPTIONS
-O3
Expand All @@ -2945,6 +2946,7 @@ add_entrypoint_object(
HDRS
../scalbnf.h
DEPENDS
libc.hdr.float_macros
libc.src.__support.FPUtil.manipulation_functions
COMPILE_OPTIONS
-O3
Expand All @@ -2957,6 +2959,7 @@ add_entrypoint_object(
HDRS
../scalbnl.h
DEPENDS
libc.hdr.float_macros
libc.src.__support.FPUtil.manipulation_functions
COMPILE_OPTIONS
-O3
Expand All @@ -2969,6 +2972,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
Expand Down
11 changes: 5 additions & 6 deletions libc/src/math/generic/scalbn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 5 additions & 6 deletions libc/src/math/generic/scalbnf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 5 additions & 8 deletions libc/src/math/generic/scalbnf128.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@
//===----------------------------------------------------------------------===//

#include "src/math/scalbnf128.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(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.
#else
return fputil::ldexp(x, n);
#endif
}

} // namespace LIBC_NAMESPACE
11 changes: 5 additions & 6 deletions libc/src/math/generic/scalbnl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 6 additions & 1 deletion utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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",
],
)
Expand Down

0 comments on commit 4486fcb

Please sign in to comment.