Skip to content

[libc] Add proxy header for float.h. #93504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we file bug reports against clang and GCC to provide __FLT_ROUNDS__?

Copy link
Member

@nickdesaulniers nickdesaulniers May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or define as:

Suggested change
#define FLT_ROUNDS 1
#define FLT_ROUNDS FE_TONEAREST

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lntue also pointed me to https://github.com/emscripten-core/emscripten/blob/main/system/lib/libc/musl/src/fenv/__flt_rounds.c. That's more sophisticated than what GCC's float.h is doing. Consider doing something similar when the builtin is available, or at least leaving behind a comment that we may want or need to change that non-builtin version some day.

#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
Loading