diff --git a/src/libc/include/__cxx_abs.h b/src/libc/include/__cxx_abs.h new file mode 100644 index 000000000..719110365 --- /dev/null +++ b/src/libc/include/__cxx_abs.h @@ -0,0 +1,30 @@ +#ifndef _CXX_ABS_H +#define _CXX_ABS_H + +#include <__stdlib_abs.h> +#include <__math_abs.h> + +#pragma clang system_header + +// https://cplusplus.github.io/LWG/issue2192 + +namespace std { +using ::abs; + +inline constexpr long abs(long __x) { return labs(__x); } + +#ifdef __SIZEOF_INT48__ +inline signed __int48 abs(signed __int48 __x) { return i48abs(__x); } +#endif // __SIZEOF_INT48__ + +inline constexpr long long abs(long long __x) { return llabs(__x); } + +inline constexpr float abs(float __x) { return fabsf(__x); } + +inline constexpr double abs(double __x) { return fabs(__x); } + +inline constexpr long double abs(long double __x) { return fabsl(__x); } + +} // namespace std + +#endif /* _CXX_ABS_H */ diff --git a/src/libc/include/__math_abs.h b/src/libc/include/__math_abs.h new file mode 100644 index 000000000..72dc41888 --- /dev/null +++ b/src/libc/include/__math_abs.h @@ -0,0 +1,16 @@ +#ifndef _MATH_ABS_H +#define _MATH_ABS_H + +#include + +__BEGIN_DECLS + +float fabsf(float); + +double fabs(double); + +long double fabsl(long double); + +__END_DECLS + +#endif /* _MATH_ABS_H */ diff --git a/src/libc/include/__math_def.h b/src/libc/include/__math_def.h index 3b704a8c0..502bc601f 100644 --- a/src/libc/include/__math_def.h +++ b/src/libc/include/__math_def.h @@ -3,6 +3,11 @@ #include #include +#include <__math_abs.h> + +#ifdef __cplusplus +#include <__cxx_abs.h> +#endif /* __cplusplus */ #define NAN __builtin_nanf("") #define INFINITY __builtin_inff() @@ -37,9 +42,7 @@ typedef float float_t; typedef double double_t; -#ifdef __cplusplus -extern "C" { -#endif +__BEGIN_DECLS int _fpclassifyf(float) __NOEXCEPT_CONST; int _fpclassifyl(long double) __NOEXCEPT_CONST; @@ -141,13 +144,6 @@ double expm1(double); float expm1f(float); long double expm1l(long double); -#ifndef _ABS_FLOAT_DEFINED -#define _ABS_FLOAT_DEFINED -double fabs(double); -float fabsf(float); -long double fabsl(long double); -#endif /* _ABS_FLOAT_DEFINED */ - double fdim(double, double); float fdimf(float, float); long double fdiml(long double, long double); @@ -328,8 +324,6 @@ double trunc(double); float truncf(float); long double truncl(long double); -#ifdef __cplusplus -} -#endif +__END_DECLS #endif /* _MATH_DEF_H */ diff --git a/src/libc/include/__stdlib_abs.h b/src/libc/include/__stdlib_abs.h new file mode 100644 index 000000000..893446bd2 --- /dev/null +++ b/src/libc/include/__stdlib_abs.h @@ -0,0 +1,20 @@ +#ifndef _STDLIB_ABS_H +#define _STDLIB_ABS_H + +#include + +__BEGIN_DECLS + +int abs(int); + +long labs(long); + +#ifdef __SIZEOF_INT48__ +signed __int48 i48abs(signed __int48 n) __NOEXCEPT_CONST; +#endif /* __SIZEOF_INT48__ */ + +long long llabs(long long); + +__END_DECLS + +#endif /* _STDLIB_ABS_H */ diff --git a/src/libc/include/inttypes.h b/src/libc/include/inttypes.h index d762282e7..f62b57efe 100644 --- a/src/libc/include/inttypes.h +++ b/src/libc/include/inttypes.h @@ -4,6 +4,10 @@ #include #include +#ifdef __cplusplus +#include <__cxx_abs.h> +#endif /* __cplusplus */ + #define PRId8 __INT8_FMTd__ #define PRIi8 __INT8_FMTi__ #define PRIo8 __UINT8_FMTo__ diff --git a/src/libc/include/stdlib.h b/src/libc/include/stdlib.h index d4a0d8920..c629054ee 100644 --- a/src/libc/include/stdlib.h +++ b/src/libc/include/stdlib.h @@ -2,6 +2,11 @@ #define _STDLIB_H #include +#include <__stdlib_abs.h> + +#ifdef __cplusplus +#include <__cxx_abs.h> +#endif /* __cplusplus */ typedef struct { int quot; @@ -100,19 +105,6 @@ void quick_exit(int) __NOEXCEPT __attribute__((noreturn)); void _Exit(int) __NOEXCEPT __attribute__((noreturn)); -#ifndef _ABS_INT_DEFINED -#define _ABS_INT_DEFINED - -int abs(int n); -long labs(long n); -long long llabs(long long n); - -#ifdef __SIZEOF_INT48__ -signed __int48 i48abs(signed __int48 n) __NOEXCEPT_CONST; -#endif /* __SIZEOF_INT48__ */ - -#endif /* _ABS_INT_DEFINED */ - div_t div(int numer, int denom); ldiv_t ldiv(long numer, long denom); diff --git a/src/libcxx/include/__abs_overloads b/src/libcxx/include/__abs_overloads deleted file mode 100644 index e4e8518c1..000000000 --- a/src/libcxx/include/__abs_overloads +++ /dev/null @@ -1,54 +0,0 @@ -// -*- C++ -*- -#ifndef _EZCXX_ABS_OVERLOADS -#define _EZCXX_ABS_OVERLOADS - -#include - -#pragma clang system_header - -// https://cplusplus.github.io/LWG/issue2192 - -#ifndef _ABS_INT_DEFINED -#define _ABS_INT_DEFINED - -__BEGIN_DECLS - -int abs(int n); -long labs(long n); -long long llabs(long long n); - -#ifdef __SIZEOF_INT48__ -signed __int48 i48abs(signed __int48 n) __NOEXCEPT_CONST; -#endif // __SIZEOF_INT48__ - -__END_DECLS - -#endif // _ABS_INT_DEFINED - -#ifndef _ABS_FLOAT_DEFINED -#define _ABS_FLOAT_DEFINED - -extern "C" { - float fabsf(float); - double fabs(double); - long double fabsl(long double); -} // extern "C" - -#endif // _ABS_FLOAT_DEFINED - -namespace std { -using ::abs; -inline constexpr long abs(long __x) { return labs(__x); } -inline constexpr long long abs(long long __x) { return llabs(__x); } - -#ifdef __SIZEOF_INT48__ -using ::i48abs; -inline signed __int48 abs(signed __int48 __x) { return i48abs(__x); } -#endif // __SIZEOF_INT48__ - -inline constexpr float abs(float __x) { return fabsf(__x); } -inline constexpr double abs(double __x) { return fabs(__x); } -inline constexpr long double abs(long double __x) { return fabsl(__x); } -} - -#endif // _EZCXX_ABS_OVERLOADS diff --git a/src/libcxx/include/cmath b/src/libcxx/include/cmath index d29fe3cec..76931ddc7 100644 --- a/src/libcxx/include/cmath +++ b/src/libcxx/include/cmath @@ -4,7 +4,6 @@ #include <__math_def.h> #include <__cmath_type_traits> -#include <__abs_overloads> #pragma clang system_header diff --git a/src/libcxx/include/cstdlib b/src/libcxx/include/cstdlib index 135cc3369..10b8767a8 100644 --- a/src/libcxx/include/cstdlib +++ b/src/libcxx/include/cstdlib @@ -3,7 +3,6 @@ #define _EZCXX_CSTDLIB #include -#include <__abs_overloads> #pragma clang system_header @@ -41,6 +40,9 @@ using ::_Exit; using ::labs; using ::llabs; +#ifdef __SIZEOF_INT48__ +using ::i48abs; +#endif // __SIZEOF_INT48__ using ::div; using ::ldiv;