1b17d1066Smrg // -*- C++ -*- C library enhancements header.
2b17d1066Smrg
3b1e83836Smrg // Copyright (C) 2016-2022 Free Software Foundation, Inc.
4b17d1066Smrg //
5b17d1066Smrg // This file is part of the GNU ISO C++ Library. This library is free
6b17d1066Smrg // software; you can redistribute it and/or modify it under the
7b17d1066Smrg // terms of the GNU General Public License as published by the
8b17d1066Smrg // Free Software Foundation; either version 3, or (at your option)
9b17d1066Smrg // any later version.
10b17d1066Smrg
11b17d1066Smrg // This library is distributed in the hope that it will be useful,
12b17d1066Smrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
13b17d1066Smrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14b17d1066Smrg // GNU General Public License for more details.
15b17d1066Smrg
16b17d1066Smrg // Under Section 7 of GPL version 3, you are granted additional
17b17d1066Smrg // permissions described in the GCC Runtime Library Exception, version
18b17d1066Smrg // 3.1, as published by the Free Software Foundation.
19b17d1066Smrg
20b17d1066Smrg // You should have received a copy of the GNU General Public License and
21b17d1066Smrg // a copy of the GCC Runtime Library Exception along with this program;
22b17d1066Smrg // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23b17d1066Smrg // <http://www.gnu.org/licenses/>.
24b17d1066Smrg
25b17d1066Smrg /** @file include/bits/std_abs.h
26b17d1066Smrg * This is an internal header file, included by other library headers.
27b17d1066Smrg * Do not attempt to use it directly. @headername{cmath, cstdlib}
28b17d1066Smrg */
29b17d1066Smrg
30b17d1066Smrg #ifndef _GLIBCXX_BITS_STD_ABS_H
31b17d1066Smrg #define _GLIBCXX_BITS_STD_ABS_H
32b17d1066Smrg
33b17d1066Smrg #pragma GCC system_header
34b17d1066Smrg
35b17d1066Smrg #include <bits/c++config.h>
36b17d1066Smrg
37b17d1066Smrg #define _GLIBCXX_INCLUDE_NEXT_C_HEADERS
38b17d1066Smrg #include_next <stdlib.h>
39b17d1066Smrg #ifdef __CORRECT_ISO_CPP_MATH_H_PROTO
40b17d1066Smrg # include_next <math.h>
41b17d1066Smrg #endif
42b17d1066Smrg #undef _GLIBCXX_INCLUDE_NEXT_C_HEADERS
43b17d1066Smrg
44b17d1066Smrg #undef abs
45b17d1066Smrg
46b17d1066Smrg extern "C++"
47b17d1066Smrg {
_GLIBCXX_VISIBILITY(default)48b17d1066Smrg namespace std _GLIBCXX_VISIBILITY(default)
49b17d1066Smrg {
50b17d1066Smrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
51b17d1066Smrg
52b17d1066Smrg using ::abs;
53b17d1066Smrg
54b17d1066Smrg #ifndef __CORRECT_ISO_CPP_STDLIB_H_PROTO
55b17d1066Smrg inline long
56b17d1066Smrg abs(long __i) { return __builtin_labs(__i); }
57b17d1066Smrg #endif
58b17d1066Smrg
59b17d1066Smrg #ifdef _GLIBCXX_USE_LONG_LONG
60b17d1066Smrg inline long long
61b17d1066Smrg abs(long long __x) { return __builtin_llabs (__x); }
62b17d1066Smrg #endif
63b17d1066Smrg
64b17d1066Smrg // _GLIBCXX_RESOLVE_LIB_DEFECTS
65b17d1066Smrg // 2192. Validity and return type of std::abs(0u) is unclear
66b17d1066Smrg // 2294. <cstdlib> should declare abs(double)
67181254a7Smrg // 2735. std::abs(short), std::abs(signed char) and others should return int
68b17d1066Smrg
69b17d1066Smrg #ifndef __CORRECT_ISO_CPP_MATH_H_PROTO
70b17d1066Smrg inline _GLIBCXX_CONSTEXPR double
71b17d1066Smrg abs(double __x)
72b17d1066Smrg { return __builtin_fabs(__x); }
73b17d1066Smrg
74b17d1066Smrg inline _GLIBCXX_CONSTEXPR float
75b17d1066Smrg abs(float __x)
76b17d1066Smrg { return __builtin_fabsf(__x); }
77b17d1066Smrg
78b17d1066Smrg inline _GLIBCXX_CONSTEXPR long double
79b17d1066Smrg abs(long double __x)
80b17d1066Smrg { return __builtin_fabsl(__x); }
81b17d1066Smrg #endif
82b17d1066Smrg
83b17d1066Smrg #if defined(__GLIBCXX_TYPE_INT_N_0)
84b1e83836Smrg __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_0
85b17d1066Smrg abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
86b17d1066Smrg #endif
87b17d1066Smrg #if defined(__GLIBCXX_TYPE_INT_N_1)
88b1e83836Smrg __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_1
89b17d1066Smrg abs(__GLIBCXX_TYPE_INT_N_1 __x) { return __x >= 0 ? __x : -__x; }
90b17d1066Smrg #endif
91b17d1066Smrg #if defined(__GLIBCXX_TYPE_INT_N_2)
92b1e83836Smrg __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_2
93b17d1066Smrg abs(__GLIBCXX_TYPE_INT_N_2 __x) { return __x >= 0 ? __x : -__x; }
94b17d1066Smrg #endif
95b17d1066Smrg #if defined(__GLIBCXX_TYPE_INT_N_3)
96b1e83836Smrg __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_3
97b17d1066Smrg abs(__GLIBCXX_TYPE_INT_N_3 __x) { return __x >= 0 ? __x : -__x; }
98b17d1066Smrg #endif
99b17d1066Smrg
100b17d1066Smrg #if !defined(__STRICT_ANSI__) && defined(_GLIBCXX_USE_FLOAT128)
101b1e83836Smrg __extension__ inline _GLIBCXX_CONSTEXPR
102b17d1066Smrg __float128
103b17d1066Smrg abs(__float128 __x)
104*0a307195Smrg {
105*0a307195Smrg #if defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
106*0a307195Smrg return __builtin_fabsl(__x);
107*0a307195Smrg #elif defined(_GLIBCXX_HAVE_FLOAT128_MATH)
108*0a307195Smrg return __builtin_fabsf128(__x);
109*0a307195Smrg #else
110*0a307195Smrg // Assume that __builtin_signbit works for __float128.
111*0a307195Smrg return __builtin_signbit(__x) ? -__x : __x;
112*0a307195Smrg #endif
113*0a307195Smrg }
114b17d1066Smrg #endif
115b17d1066Smrg
116b17d1066Smrg _GLIBCXX_END_NAMESPACE_VERSION
117b17d1066Smrg } // namespace
118*0a307195Smrg } // extern "C++"
119b17d1066Smrg
120b17d1066Smrg #endif // _GLIBCXX_BITS_STD_ABS_H
121