Skip to content

Commit 164eca5

Browse files
committed
fix C++ std::abs
1 parent a494909 commit 164eca5

File tree

9 files changed

+85
-82
lines changed

9 files changed

+85
-82
lines changed

src/libc/include/__cxx_abs.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef _CXX_ABS_H
2+
#define _CXX_ABS_H
3+
4+
#include <__stdlib_abs.h>
5+
#include <__math_abs.h>
6+
7+
#pragma clang system_header
8+
9+
// https://cplusplus.github.io/LWG/issue2192
10+
11+
namespace std {
12+
using ::abs;
13+
14+
inline constexpr long abs(long __x) { return labs(__x); }
15+
16+
#ifdef __SIZEOF_INT48__
17+
inline signed __int48 abs(signed __int48 __x) { return i48abs(__x); }
18+
#endif // __SIZEOF_INT48__
19+
20+
inline constexpr long long abs(long long __x) { return llabs(__x); }
21+
22+
inline constexpr float abs(float __x) { return fabsf(__x); }
23+
24+
inline constexpr double abs(double __x) { return fabs(__x); }
25+
26+
inline constexpr long double abs(long double __x) { return fabsl(__x); }
27+
28+
} // namespace std
29+
30+
#endif /* _CXX_ABS_H */

src/libc/include/__math_abs.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef _MATH_ABS_H
2+
#define _MATH_ABS_H
3+
4+
#include <cdefs.h>
5+
6+
__BEGIN_DECLS
7+
8+
float fabsf(float);
9+
10+
double fabs(double);
11+
12+
long double fabsl(long double);
13+
14+
__END_DECLS
15+
16+
#endif /* _MATH_ABS_H */

src/libc/include/__math_def.h

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
#include <cdefs.h>
55
#include <stdbool.h>
6+
#include <__math_abs.h>
7+
8+
#ifdef __cplusplus
9+
#include <__cxx_abs.h>
10+
#endif /* __cplusplus */
611

712
#define NAN __builtin_nanf("")
813
#define INFINITY __builtin_inff()
@@ -37,9 +42,7 @@
3742
typedef float float_t;
3843
typedef double double_t;
3944

40-
#ifdef __cplusplus
41-
extern "C" {
42-
#endif
45+
__BEGIN_DECLS
4346

4447
int _fpclassifyf(float) __NOEXCEPT_CONST;
4548
int _fpclassifyl(long double) __NOEXCEPT_CONST;
@@ -141,13 +144,6 @@ double expm1(double);
141144
float expm1f(float);
142145
long double expm1l(long double);
143146

144-
#ifndef _ABS_FLOAT_DEFINED
145-
#define _ABS_FLOAT_DEFINED
146-
double fabs(double);
147-
float fabsf(float);
148-
long double fabsl(long double);
149-
#endif /* _ABS_FLOAT_DEFINED */
150-
151147
double fdim(double, double);
152148
float fdimf(float, float);
153149
long double fdiml(long double, long double);
@@ -328,8 +324,6 @@ double trunc(double);
328324
float truncf(float);
329325
long double truncl(long double);
330326

331-
#ifdef __cplusplus
332-
}
333-
#endif
327+
__END_DECLS
334328

335329
#endif /* _MATH_DEF_H */

src/libc/include/__stdlib_abs.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef _STDLIB_ABS_H
2+
#define _STDLIB_ABS_H
3+
4+
#include <cdefs.h>
5+
6+
__BEGIN_DECLS
7+
8+
int abs(int);
9+
10+
long labs(long);
11+
12+
#ifdef __SIZEOF_INT48__
13+
signed __int48 i48abs(signed __int48 n) __NOEXCEPT_CONST;
14+
#endif /* __SIZEOF_INT48__ */
15+
16+
long long llabs(long long);
17+
18+
__END_DECLS
19+
20+
#endif /* _STDLIB_ABS_H */

src/libc/include/inttypes.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#include <cdefs.h>
55
#include <stdint.h>
66

7+
#ifdef __cplusplus
8+
#include <__cxx_abs.h>
9+
#endif /* __cplusplus */
10+
711
#define PRId8 __INT8_FMTd__
812
#define PRIi8 __INT8_FMTi__
913
#define PRIo8 __UINT8_FMTo__

src/libc/include/stdlib.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
#define _STDLIB_H
33

44
#include <cdefs.h>
5+
#include <__stdlib_abs.h>
6+
7+
#ifdef __cplusplus
8+
#include <__cxx_abs.h>
9+
#endif /* __cplusplus */
510

611
typedef struct {
712
int quot;
@@ -100,19 +105,6 @@ void quick_exit(int) __NOEXCEPT __attribute__((noreturn));
100105

101106
void _Exit(int) __NOEXCEPT __attribute__((noreturn));
102107

103-
#ifndef _ABS_INT_DEFINED
104-
#define _ABS_INT_DEFINED
105-
106-
int abs(int n);
107-
long labs(long n);
108-
long long llabs(long long n);
109-
110-
#ifdef __SIZEOF_INT48__
111-
signed __int48 i48abs(signed __int48 n) __NOEXCEPT_CONST;
112-
#endif /* __SIZEOF_INT48__ */
113-
114-
#endif /* _ABS_INT_DEFINED */
115-
116108
div_t div(int numer, int denom);
117109

118110
ldiv_t ldiv(long numer, long denom);

src/libcxx/include/__abs_overloads

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/libcxx/include/cmath

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include <__math_def.h>
66
#include <__cmath_type_traits>
7-
#include <__abs_overloads>
87

98
#pragma clang system_header
109

src/libcxx/include/cstdlib

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#define _EZCXX_CSTDLIB
44

55
#include <stdlib.h>
6-
#include <__abs_overloads>
76

87
#pragma clang system_header
98

@@ -41,6 +40,9 @@ using ::_Exit;
4140

4241
using ::labs;
4342
using ::llabs;
43+
#ifdef __SIZEOF_INT48__
44+
using ::i48abs;
45+
#endif // __SIZEOF_INT48__
4446

4547
using ::div;
4648
using ::ldiv;

0 commit comments

Comments
 (0)