Skip to content

Commit 584cc37

Browse files
authored
[libc++] Move std::abs into __math/abs.h (#139586)
`template <class = int>` is also added to our implementations to avoid an ambiguity between the libc's version and our version when both are visible. This avoids including `<stdlib.h>` in `<math.h>`.
1 parent 681db06 commit 584cc37

File tree

4 files changed

+29
-29
lines changed

4 files changed

+29
-29
lines changed

libcxx/include/__math/abs.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,30 @@ template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
3939
return __builtin_fabs((double)__x);
4040
}
4141

42+
// abs
43+
44+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI inline float abs(float __x) _NOEXCEPT { return __builtin_fabsf(__x); }
45+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI inline double abs(double __x) _NOEXCEPT { return __builtin_fabs(__x); }
46+
47+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI inline long double abs(long double __x) _NOEXCEPT {
48+
return __builtin_fabsl(__x);
49+
}
50+
51+
template <class = int>
52+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI inline int abs(int __x) _NOEXCEPT {
53+
return __builtin_abs(__x);
54+
}
55+
56+
template <class = int>
57+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI inline long abs(long __x) _NOEXCEPT {
58+
return __builtin_labs(__x);
59+
}
60+
61+
template <class = int>
62+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI inline long long abs(long long __x) _NOEXCEPT {
63+
return __builtin_llabs(__x);
64+
}
65+
4266
} // namespace __math
4367

4468
_LIBCPP_END_NAMESPACE_STD

libcxx/include/math.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,7 @@ extern "C++" {
378378
# include <__math/traits.h>
379379
# include <__math/trigonometric_functions.h>
380380
# include <__type_traits/enable_if.h>
381-
# include <__type_traits/is_floating_point.h>
382381
# include <__type_traits/is_integral.h>
383-
# include <stdlib.h>
384382

385383
// fpclassify relies on implementation-defined constants, so we can't move it to a detail header
386384
_LIBCPP_BEGIN_NAMESPACE_STD
@@ -431,19 +429,12 @@ using std::__math::isnormal;
431429
using std::__math::isunordered;
432430
# endif // _LIBCPP_MSVCRT
433431

434-
// abs
435-
//
436-
// handled in stdlib.h
437-
438-
// div
439-
//
440-
// handled in stdlib.h
441-
442432
// We have to provide double overloads for <math.h> to work on platforms that don't provide the full set of math
443433
// functions. To make the overload set work with multiple functions that take the same arguments, we make our overloads
444434
// templates. Functions are preferred over function templates during overload resolution, which means that our overload
445435
// will only be selected when the C library doesn't provide one.
446436

437+
using std::__math::abs;
447438
using std::__math::acos;
448439
using std::__math::acosh;
449440
using std::__math::asin;

libcxx/include/stdlib.h

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,8 @@ extern "C++" {
106106
# undef llabs
107107
# endif
108108

109-
// MSVCRT already has the correct prototype in <stdlib.h> if __cplusplus is defined
110-
# if !defined(_LIBCPP_MSVCRT)
111-
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long abs(long __x) _NOEXCEPT { return __builtin_labs(__x); }
112-
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long long abs(long long __x) _NOEXCEPT { return __builtin_llabs(__x); }
113-
# endif // !defined(_LIBCPP_MSVCRT)
114-
115-
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI float abs(float __lcpp_x) _NOEXCEPT {
116-
return __builtin_fabsf(__lcpp_x); // Use builtins to prevent needing math.h
117-
}
118-
119-
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI double abs(double __lcpp_x) _NOEXCEPT {
120-
return __builtin_fabs(__lcpp_x);
121-
}
122-
123-
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long double abs(long double __lcpp_x) _NOEXCEPT {
124-
return __builtin_fabsl(__lcpp_x);
125-
}
109+
# include <__math/abs.h>
110+
using std::__math::abs;
126111

127112
// div
128113

libcxx/test/std/numerics/c.math/abs.verify.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ void f() {
1313
(void)std::abs(ui); // expected-error {{call to 'abs' is ambiguous}}
1414

1515
unsigned char uc = -5;
16-
(void)std::abs(uc); // expected-warning {{taking the absolute value of unsigned type 'unsigned char' has no effect}}
16+
(void)std::abs(uc); // expected-warning 0-1 {{taking the absolute value of unsigned type 'unsigned char' has no effect}}
1717

1818
unsigned short us = -5;
19-
(void)std::abs(us); // expected-warning {{taking the absolute value of unsigned type 'unsigned short' has no effect}}
19+
(void)std::abs(us); // expected-warning 0-1 {{taking the absolute value of unsigned type 'unsigned short' has no effect}}
2020

2121
unsigned long ul = -5;
2222
(void)std::abs(ul); // expected-error {{call to 'abs' is ambiguous}}

0 commit comments

Comments
 (0)