Skip to content

[libc][NFC] Move float macro into its own header / add target os detection #73311

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 3 commits into from
Nov 24, 2023
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
2 changes: 1 addition & 1 deletion libc/cmake/modules/compiler_features/check_float128.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "src/__support/macros/properties/compiler.h"
#include "src/__support/macros/properties/float.h"

#ifndef LIBC_COMPILER_HAS_FLOAT128
#error unsupported
Expand Down
6 changes: 5 additions & 1 deletion libc/docs/dev/code_style.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ We define two kinds of macros:
e.g., ``LIBC_TARGET_ARCH_IS_ARM``.
* ``compiler.h`` - Host compiler properties.
e.g., ``LIBC_COMPILER_IS_CLANG``.
* ``cpu_features.h`` - Target cpu apu feature availability.
* ``cpu_features.h`` - Target cpu feature availability.
e.g., ``LIBC_TARGET_CPU_HAS_AVX2``.
* ``float.h`` - Floating point type properties and availability.
e.g., ``LIBC_COMPILER_HAS_FLOAT128``.
* ``os.h`` - Target os properties.
e.g., ``LIBC_TARGET_OS_IS_LINUX``.

* ``src/__support/macros/config.h`` - Important compiler and platform
features. Such macros can be used to produce portable code by
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ add_header_library(
.named_pair
libc.src.__support.CPP.type_traits
libc.src.__support.macros.attributes
libc.src.__support.macros.properties.compiler
libc.src.__support.macros.config
)

add_header_library(
Expand Down
2 changes: 2 additions & 0 deletions libc/src/__support/CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ add_header_library(
type_traits/true_type.h
type_traits/type_identity.h
type_traits/void_t.h
DEPENDS
libc.src.__support.macros.properties.float
Copy link
Contributor

Choose a reason for hiding this comment

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

This is missing a bunch of deps, you might want to add them too (in a separate patch)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thx for noticing I'll fix this in a separate patch.

)

add_header_library(
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/CPP/type_traits/is_floating_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "src/__support/CPP/type_traits/is_same.h"
#include "src/__support/CPP/type_traits/remove_cv.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/properties/compiler.h"
#include "src/__support/macros/properties/float.h"

namespace LIBC_NAMESPACE::cpp {

Expand Down
1 change: 1 addition & 0 deletions libc/src/__support/FPUtil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ add_header_library(
HDRS
FloatProperties.h
DEPENDS
libc.src.__support.macros.properties.float
libc.src.__support.uint128
)

Expand Down
14 changes: 1 addition & 13 deletions libc/src/__support/FPUtil/FloatProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,10 @@
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_FLOATPROPERTIES_H

#include "src/__support/UInt128.h"
#include "src/__support/macros/properties/architectures.h" // LIBC_TARGET_ARCH_XXX
#include "src/__support/macros/properties/float.h" // LIBC_COMPILER_HAS_FLOAT128

#include <stdint.h>

// https://developer.arm.com/documentation/dui0491/i/C-and-C---Implementation-Details/Basic-data-types
// https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms
// https://docs.amd.com/bundle/HIP-Programming-Guide-v5.1/page/Programming_with_HIP.html
#if defined(_WIN32) || defined(__arm__) || defined(__NVPTX__) || \
defined(__AMDGPU__) || (defined(__APPLE__) && defined(__aarch64__))
#define LONG_DOUBLE_IS_DOUBLE
#endif

#if !defined(LONG_DOUBLE_IS_DOUBLE) && defined(LIBC_TARGET_ARCH_IS_X86)
#define SPECIAL_X86_LONG_DOUBLE
#endif

namespace LIBC_NAMESPACE {
namespace fputil {

Expand Down
16 changes: 16 additions & 0 deletions libc/src/__support/macros/properties/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,26 @@ add_header_library(
compiler.h
)

add_header_library(
os
HDRS
os.h
)

add_header_library(
cpu_features
HDRS
cpu_features.h
DEPENDS
.architectures
)

add_header_library(
float
HDRS
float.h
DEPENDS
.architectures
.compiler
.os
)
21 changes: 0 additions & 21 deletions libc/src/__support/macros/properties/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,4 @@
#define LIBC_COMPILER_IS_MSC
#endif

// Check compiler features
#if defined(FLT128_MANT_DIG)
// C23 _Float128 type is available.
#define LIBC_COMPILER_HAS_FLOAT128
#define LIBC_FLOAT128_IS_C23
using float128 = _Float128;

#elif defined(__SIZEOF_FLOAT128__)
// Builtin __float128 is available.
#define LIBC_COMPILER_HAS_FLOAT128
#define LIBC_FLOAT128_IS_BUILTIN
using float128 = __float128;

#elif (defined(__linux__) && defined(__aarch64__))
// long double on Linux aarch64 is 128-bit floating point.
#define LIBC_COMPILER_HAS_FLOAT128
#define LIBC_FLOAT128_IS_LONG_DOUBLE
using float128 = long double;

#endif

#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_COMPILER_H
46 changes: 46 additions & 0 deletions libc/src/__support/macros/properties/float.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//===-- Float type support --------------------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
// Floating point properties are a combination of compiler support, target OS
// and target architecture.

#ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_FLOAT_H
#define LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_FLOAT_H

#include "src/__support/macros/properties/architectures.h"
#include "src/__support/macros/properties/compiler.h"
#include "src/__support/macros/properties/os.h"

// https://developer.arm.com/documentation/dui0491/i/C-and-C---Implementation-Details/Basic-data-types
// https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms
// https://docs.amd.com/bundle/HIP-Programming-Guide-v5.1/page/Programming_with_HIP.html
#if defined(LIBC_TARGET_OS_IS_WINDOWS) || \
(defined(LIBC_TARGET_OS_IS_MACOS) && \
defined(LIBC_TARGET_ARCH_IS_AARCH64)) || \
defined(LIBC_TARGET_ARCH_IS_ARM) || defined(LIBC_TARGET_ARCH_IS_NVPTX) || \
defined(LIBC_TARGET_ARCH_IS_AMDGPU)
#define LONG_DOUBLE_IS_DOUBLE
#endif

#if !defined(LONG_DOUBLE_IS_DOUBLE) && defined(LIBC_TARGET_ARCH_IS_X86)
#define SPECIAL_X86_LONG_DOUBLE
#endif

// Check compiler features
#if defined(FLT128_MANT_DIG)
#define LIBC_COMPILER_HAS_FLOAT128
using float128 = _Float128;
#elif defined(__SIZEOF_FLOAT128__)
#define LIBC_COMPILER_HAS_FLOAT128
using float128 = __float128;
#elif (defined(__linux__) && defined(__aarch64__))
#define LIBC_COMPILER_HAS_FLOAT128
#define LIBC_FLOAT128_IS_LONG_DOUBLE
using float128 = long double;
#endif

#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_FLOAT_H
40 changes: 40 additions & 0 deletions libc/src/__support/macros/properties/os.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===-- Target OS detection -------------------------------------*- C++ -*-===//
//
// 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_SRC___SUPPORT_MACROS_PROPERTIES_OS_H
#define LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_OS_H

#if (defined(__freebsd__) || defined(__FreeBSD__))
#define LIBC_TARGET_OS_IS_FREEBSD
#endif

#if defined(__ANDROID__)
#define LIBC_TARGET_OS_IS_ANDROID
#endif

#if defined(__linux__) && !defined(LIBC_TARGET_OS_IS_FREEBSD) && \
!defined(LIBC_TARGET_OS_IS_ANDROID)
#define LIBC_TARGET_OS_IS_LINUX
#endif

#if (defined(_WIN64) || defined(_WIN32))
#define LIBC_TARGET_OS_IS_WINDOWS
#endif

#if (defined(__apple__) || defined(__APPLE__) || defined(__MACH__))
// From https://stackoverflow.com/a/49560690
#include "TargetConditionals.h"
#if defined(TARGET_OS_OSX)
#define LIBC_TARGET_OS_IS_MACOS
#endif
#if defined(TARGET_OS_IPHONE)
// This is set for any non-Mac Apple products (IOS, TV, WATCH)
#define LIBC_TARGET_OS_IS_IPHONE
#endif
#endif

#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_OS_H
2 changes: 1 addition & 1 deletion libc/src/math/copysignf128.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef LLVM_LIBC_SRC_MATH_COPYSIGNF128_H
#define LLVM_LIBC_SRC_MATH_COPYSIGNF128_H

#include "src/__support/macros/properties/compiler.h"
#include "src/__support/macros/properties/float.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

You probably want to add a cmake dep for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll do this is a separate patch as well because libc/src/math/CMakeLists.txt needs to be tweaked to pass dependencies.


namespace LIBC_NAMESPACE {

Expand Down
1 change: 0 additions & 1 deletion libc/src/math/generic/copysignf128.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "src/math/copysignf128.h"
#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "src/__support/common.h"
#include "src/__support/macros/properties/compiler.h"

namespace LIBC_NAMESPACE {

Expand Down
18 changes: 17 additions & 1 deletion utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ libc_support_library(
hdrs = ["src/__support/macros/properties/compiler.h"],
)

libc_support_library(
name = "__support_macros_properties_os",
hdrs = ["src/__support/macros/properties/os.h"],
)

libc_support_library(
name = "__support_macros_properties_float",
hdrs = ["src/__support/macros/properties/float.h"],
deps = [
":__support_macros_properties_architectures",
":__support_macros_properties_compiler",
":__support_macros_properties_os",
],
)

libc_support_library(
name = "__support_macros_properties_cpu_features",
hdrs = ["src/__support/macros/properties/cpu_features.h"],
Expand Down Expand Up @@ -308,7 +323,7 @@ libc_support_library(
deps = [
":__support_macros_attributes",
":__support_macros_config",
":__support_macros_properties_compiler",
":__support_macros_properties_float",
],
)

Expand Down Expand Up @@ -657,6 +672,7 @@ libc_support_library(
name = "__support_fputil_float_properties",
hdrs = ["src/__support/FPUtil/FloatProperties.h"],
deps = [
":__support_macros_properties_float",
":__support_uint128",
],
)
Expand Down