Skip to content

Commit 5e5a22c

Browse files
authored
[libc][NFC] Move float macro into its own header / add target os detection (llvm#73311)
Floating point properties are a combination of target OS, target architecture and compiler support. - Adding target OS detection, - Moving floating point type detection to its own file. This is in preparation of adding support for `_Float16` which requires testing compiler **version** and target architecture.
1 parent 50c298f commit 5e5a22c

File tree

14 files changed

+132
-41
lines changed

14 files changed

+132
-41
lines changed

libc/cmake/modules/compiler_features/check_float128.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "src/__support/macros/properties/compiler.h"
1+
#include "src/__support/macros/properties/float.h"
22

33
#ifndef LIBC_COMPILER_HAS_FLOAT128
44
#error unsupported

libc/docs/dev/code_style.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ We define two kinds of macros:
4545
e.g., ``LIBC_TARGET_ARCH_IS_ARM``.
4646
* ``compiler.h`` - Host compiler properties.
4747
e.g., ``LIBC_COMPILER_IS_CLANG``.
48-
* ``cpu_features.h`` - Target cpu apu feature availability.
48+
* ``cpu_features.h`` - Target cpu feature availability.
4949
e.g., ``LIBC_TARGET_CPU_HAS_AVX2``.
50+
* ``float.h`` - Floating point type properties and availability.
51+
e.g., ``LIBC_COMPILER_HAS_FLOAT128``.
52+
* ``os.h`` - Target os properties.
53+
e.g., ``LIBC_TARGET_OS_IS_LINUX``.
5054

5155
* ``src/__support/macros/config.h`` - Important compiler and platform
5256
features. Such macros can be used to produce portable code by

libc/src/__support/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ add_header_library(
4242
.named_pair
4343
libc.src.__support.CPP.type_traits
4444
libc.src.__support.macros.attributes
45-
libc.src.__support.macros.properties.compiler
45+
libc.src.__support.macros.config
4646
)
4747

4848
add_header_library(

libc/src/__support/CPP/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ add_header_library(
147147
type_traits/true_type.h
148148
type_traits/type_identity.h
149149
type_traits/void_t.h
150+
DEPENDS
151+
libc.src.__support.macros.properties.float
150152
)
151153

152154
add_header_library(

libc/src/__support/CPP/type_traits/is_floating_point.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "src/__support/CPP/type_traits/is_same.h"
1212
#include "src/__support/CPP/type_traits/remove_cv.h"
1313
#include "src/__support/macros/attributes.h"
14-
#include "src/__support/macros/properties/compiler.h"
14+
#include "src/__support/macros/properties/float.h"
1515

1616
namespace LIBC_NAMESPACE::cpp {
1717

libc/src/__support/FPUtil/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ add_header_library(
2828
HDRS
2929
FloatProperties.h
3030
DEPENDS
31+
libc.src.__support.macros.properties.float
3132
libc.src.__support.uint128
3233
)
3334

libc/src/__support/FPUtil/FloatProperties.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,10 @@
1010
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_FLOATPROPERTIES_H
1111

1212
#include "src/__support/UInt128.h"
13-
#include "src/__support/macros/properties/architectures.h" // LIBC_TARGET_ARCH_XXX
13+
#include "src/__support/macros/properties/float.h" // LIBC_COMPILER_HAS_FLOAT128
1414

1515
#include <stdint.h>
1616

17-
// https://developer.arm.com/documentation/dui0491/i/C-and-C---Implementation-Details/Basic-data-types
18-
// https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms
19-
// https://docs.amd.com/bundle/HIP-Programming-Guide-v5.1/page/Programming_with_HIP.html
20-
#if defined(_WIN32) || defined(__arm__) || defined(__NVPTX__) || \
21-
defined(__AMDGPU__) || (defined(__APPLE__) && defined(__aarch64__))
22-
#define LONG_DOUBLE_IS_DOUBLE
23-
#endif
24-
25-
#if !defined(LONG_DOUBLE_IS_DOUBLE) && defined(LIBC_TARGET_ARCH_IS_X86)
26-
#define SPECIAL_X86_LONG_DOUBLE
27-
#endif
28-
2917
namespace LIBC_NAMESPACE {
3018
namespace fputil {
3119

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,26 @@ add_header_library(
1010
compiler.h
1111
)
1212

13+
add_header_library(
14+
os
15+
HDRS
16+
os.h
17+
)
18+
1319
add_header_library(
1420
cpu_features
1521
HDRS
1622
cpu_features.h
1723
DEPENDS
1824
.architectures
1925
)
26+
27+
add_header_library(
28+
float
29+
HDRS
30+
float.h
31+
DEPENDS
32+
.architectures
33+
.compiler
34+
.os
35+
)

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,4 @@
2121
#define LIBC_COMPILER_IS_MSC
2222
#endif
2323

24-
// Check compiler features
25-
#if defined(FLT128_MANT_DIG)
26-
// C23 _Float128 type is available.
27-
#define LIBC_COMPILER_HAS_FLOAT128
28-
#define LIBC_FLOAT128_IS_C23
29-
using float128 = _Float128;
30-
31-
#elif defined(__SIZEOF_FLOAT128__)
32-
// Builtin __float128 is available.
33-
#define LIBC_COMPILER_HAS_FLOAT128
34-
#define LIBC_FLOAT128_IS_BUILTIN
35-
using float128 = __float128;
36-
37-
#elif (defined(__linux__) && defined(__aarch64__))
38-
// long double on Linux aarch64 is 128-bit floating point.
39-
#define LIBC_COMPILER_HAS_FLOAT128
40-
#define LIBC_FLOAT128_IS_LONG_DOUBLE
41-
using float128 = long double;
42-
43-
#endif
44-
4524
#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_COMPILER_H
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===-- Float type support --------------------------------------*- C++ -*-===//
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+
// Floating point properties are a combination of compiler support, target OS
9+
// and target architecture.
10+
11+
#ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_FLOAT_H
12+
#define LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_FLOAT_H
13+
14+
#include "src/__support/macros/properties/architectures.h"
15+
#include "src/__support/macros/properties/compiler.h"
16+
#include "src/__support/macros/properties/os.h"
17+
18+
// https://developer.arm.com/documentation/dui0491/i/C-and-C---Implementation-Details/Basic-data-types
19+
// https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms
20+
// https://docs.amd.com/bundle/HIP-Programming-Guide-v5.1/page/Programming_with_HIP.html
21+
#if defined(LIBC_TARGET_OS_IS_WINDOWS) || \
22+
(defined(LIBC_TARGET_OS_IS_MACOS) && \
23+
defined(LIBC_TARGET_ARCH_IS_AARCH64)) || \
24+
defined(LIBC_TARGET_ARCH_IS_ARM) || defined(LIBC_TARGET_ARCH_IS_NVPTX) || \
25+
defined(LIBC_TARGET_ARCH_IS_AMDGPU)
26+
#define LONG_DOUBLE_IS_DOUBLE
27+
#endif
28+
29+
#if !defined(LONG_DOUBLE_IS_DOUBLE) && defined(LIBC_TARGET_ARCH_IS_X86)
30+
#define SPECIAL_X86_LONG_DOUBLE
31+
#endif
32+
33+
// Check compiler features
34+
#if defined(FLT128_MANT_DIG)
35+
#define LIBC_COMPILER_HAS_FLOAT128
36+
using float128 = _Float128;
37+
#elif defined(__SIZEOF_FLOAT128__)
38+
#define LIBC_COMPILER_HAS_FLOAT128
39+
using float128 = __float128;
40+
#elif (defined(__linux__) && defined(__aarch64__))
41+
#define LIBC_COMPILER_HAS_FLOAT128
42+
#define LIBC_FLOAT128_IS_LONG_DOUBLE
43+
using float128 = long double;
44+
#endif
45+
46+
#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_FLOAT_H
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===-- Target OS detection -------------------------------------*- C++ -*-===//
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+
#ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_OS_H
9+
#define LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_OS_H
10+
11+
#if (defined(__freebsd__) || defined(__FreeBSD__))
12+
#define LIBC_TARGET_OS_IS_FREEBSD
13+
#endif
14+
15+
#if defined(__ANDROID__)
16+
#define LIBC_TARGET_OS_IS_ANDROID
17+
#endif
18+
19+
#if defined(__linux__) && !defined(LIBC_TARGET_OS_IS_FREEBSD) && \
20+
!defined(LIBC_TARGET_OS_IS_ANDROID)
21+
#define LIBC_TARGET_OS_IS_LINUX
22+
#endif
23+
24+
#if (defined(_WIN64) || defined(_WIN32))
25+
#define LIBC_TARGET_OS_IS_WINDOWS
26+
#endif
27+
28+
#if (defined(__apple__) || defined(__APPLE__) || defined(__MACH__))
29+
// From https://stackoverflow.com/a/49560690
30+
#include "TargetConditionals.h"
31+
#if defined(TARGET_OS_OSX)
32+
#define LIBC_TARGET_OS_IS_MACOS
33+
#endif
34+
#if defined(TARGET_OS_IPHONE)
35+
// This is set for any non-Mac Apple products (IOS, TV, WATCH)
36+
#define LIBC_TARGET_OS_IS_IPHONE
37+
#endif
38+
#endif
39+
40+
#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_OS_H

libc/src/math/copysignf128.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#ifndef LLVM_LIBC_SRC_MATH_COPYSIGNF128_H
1010
#define LLVM_LIBC_SRC_MATH_COPYSIGNF128_H
1111

12-
#include "src/__support/macros/properties/compiler.h"
12+
#include "src/__support/macros/properties/float.h"
1313

1414
namespace LIBC_NAMESPACE {
1515

libc/src/math/generic/copysignf128.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "src/math/copysignf128.h"
1010
#include "src/__support/FPUtil/ManipulationFunctions.h"
1111
#include "src/__support/common.h"
12-
#include "src/__support/macros/properties/compiler.h"
1312

1413
namespace LIBC_NAMESPACE {
1514

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,21 @@ libc_support_library(
7575
hdrs = ["src/__support/macros/properties/compiler.h"],
7676
)
7777

78+
libc_support_library(
79+
name = "__support_macros_properties_os",
80+
hdrs = ["src/__support/macros/properties/os.h"],
81+
)
82+
83+
libc_support_library(
84+
name = "__support_macros_properties_float",
85+
hdrs = ["src/__support/macros/properties/float.h"],
86+
deps = [
87+
":__support_macros_properties_architectures",
88+
":__support_macros_properties_compiler",
89+
":__support_macros_properties_os",
90+
],
91+
)
92+
7893
libc_support_library(
7994
name = "__support_macros_properties_cpu_features",
8095
hdrs = ["src/__support/macros/properties/cpu_features.h"],
@@ -308,7 +323,7 @@ libc_support_library(
308323
deps = [
309324
":__support_macros_attributes",
310325
":__support_macros_config",
311-
":__support_macros_properties_compiler",
326+
":__support_macros_properties_float",
312327
],
313328
)
314329

@@ -657,6 +672,7 @@ libc_support_library(
657672
name = "__support_fputil_float_properties",
658673
hdrs = ["src/__support/FPUtil/FloatProperties.h"],
659674
deps = [
675+
":__support_macros_properties_float",
660676
":__support_uint128",
661677
],
662678
)

0 commit comments

Comments
 (0)