Skip to content

Commit 1b5f691

Browse files
authored
[libc++] Avoid including <cmath> in <compare> (#80418)
This reduces the time to include `<compare>` from 84ms to 36ms.
1 parent d272d94 commit 1b5f691

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

libcxx/include/__compare/strong_order.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
#include <__compare/compare_three_way.h>
1414
#include <__compare/ordering.h>
1515
#include <__config>
16+
#include <__math/exponential_functions.h>
17+
#include <__math/traits.h>
1618
#include <__type_traits/conditional.h>
1719
#include <__type_traits/decay.h>
20+
#include <__type_traits/is_floating_point.h>
21+
#include <__type_traits/is_same.h>
1822
#include <__utility/forward.h>
1923
#include <__utility/priority_tag.h>
20-
#include <cmath>
2124
#include <cstdint>
2225
#include <limits>
2326

@@ -66,27 +69,27 @@ struct __fn {
6669
return strong_ordering::greater;
6770
} else if (__t == __u) {
6871
if constexpr (numeric_limits<_Dp>::radix == 2) {
69-
return std::signbit(__u) <=> std::signbit(__t);
72+
return __math::signbit(__u) <=> __math::signbit(__t);
7073
} else {
7174
// This is bullet 3 of the IEEE754 algorithm, relevant
7275
// only for decimal floating-point;
7376
// see https://stackoverflow.com/questions/69068075/
74-
if (__t == 0 || std::isinf(__t)) {
75-
return std::signbit(__u) <=> std::signbit(__t);
77+
if (__t == 0 || __math::isinf(__t)) {
78+
return __math::signbit(__u) <=> __math::signbit(__t);
7679
} else {
7780
int __texp, __uexp;
78-
(void)std::frexp(__t, &__texp);
79-
(void)std::frexp(__u, &__uexp);
81+
(void)__math::frexp(__t, &__texp);
82+
(void)__math::frexp(__u, &__uexp);
8083
return (__t < 0) ? (__texp <=> __uexp) : (__uexp <=> __texp);
8184
}
8285
}
8386
} else {
8487
// They're unordered, so one of them must be a NAN.
8588
// The order is -QNAN, -SNAN, numbers, +SNAN, +QNAN.
86-
bool __t_is_nan = std::isnan(__t);
87-
bool __u_is_nan = std::isnan(__u);
88-
bool __t_is_negative = std::signbit(__t);
89-
bool __u_is_negative = std::signbit(__u);
89+
bool __t_is_nan = __math::isnan(__t);
90+
bool __u_is_nan = __math::isnan(__u);
91+
bool __t_is_negative = __math::signbit(__t);
92+
bool __u_is_negative = __math::signbit(__u);
9093
using _IntType =
9194
conditional_t< sizeof(__t) == sizeof(int32_t),
9295
int32_t,

libcxx/include/__compare/weak_order.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
#include <__compare/ordering.h>
1414
#include <__compare/strong_order.h>
1515
#include <__config>
16+
#include <__math/traits.h>
1617
#include <__type_traits/decay.h>
18+
#include <__type_traits/is_floating_point.h>
19+
#include <__type_traits/is_same.h>
1720
#include <__utility/forward.h>
1821
#include <__utility/priority_tag.h>
19-
#include <cmath>
2022

2123
#ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
2224
# pragma GCC system_header
@@ -51,10 +53,10 @@ struct __fn {
5153
return weak_ordering::greater;
5254
} else {
5355
// Otherwise, at least one of them is a NaN.
54-
bool __t_is_nan = std::isnan(__t);
55-
bool __u_is_nan = std::isnan(__u);
56-
bool __t_is_negative = std::signbit(__t);
57-
bool __u_is_negative = std::signbit(__u);
56+
bool __t_is_nan = __math::isnan(__t);
57+
bool __u_is_nan = __math::isnan(__u);
58+
bool __t_is_negative = __math::signbit(__t);
59+
bool __u_is_negative = __math::signbit(__u);
5860
if (__t_is_nan && __u_is_nan) {
5961
return (__u_is_negative <=> __t_is_negative);
6062
} else if (__t_is_nan) {

libcxx/include/compare

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ namespace std {
162162
#endif
163163

164164
#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
165+
# include <cmath>
165166
# include <type_traits>
166167
#endif
167168

libcxx/test/libcxx/transitive_includes/cxx23.csv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ codecvt string
105105
codecvt tuple
106106
codecvt typeinfo
107107
codecvt version
108-
compare cmath
109108
compare cstddef
110109
compare cstdint
111110
compare limits

libcxx/test/libcxx/transitive_includes/cxx26.csv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ codecvt string
105105
codecvt tuple
106106
codecvt typeinfo
107107
codecvt version
108-
compare cmath
109108
compare cstddef
110109
compare cstdint
111110
compare limits

0 commit comments

Comments
 (0)