libstdc++
std_abs.h
Go to the documentation of this file.
1// -*- C++ -*- C library enhancements header.
2
3// Copyright (C) 2016-2025 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/bits/std_abs.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{cmath, cstdlib}
28 */
29
30#ifndef _GLIBCXX_BITS_STD_ABS_H
31#define _GLIBCXX_BITS_STD_ABS_H
32
33#ifdef _GLIBCXX_SYSHDR
34#pragma GCC system_header
35#endif
36
37#include <bits/c++config.h>
38
39#pragma GCC diagnostic push
40#pragma GCC diagnostic ignored "-Wpedantic" // include_next
41#pragma GCC diagnostic ignored "-Wlong-long"
42
43#define _GLIBCXX_INCLUDE_NEXT_C_HEADERS
44#include_next <stdlib.h>
45#ifdef __CORRECT_ISO_CPP_MATH_H_PROTO
46# include_next <math.h>
47#endif
48#undef _GLIBCXX_INCLUDE_NEXT_C_HEADERS
49
50#undef abs
51
52extern "C++"
53{
54namespace std _GLIBCXX_VISIBILITY(default)
55{
56_GLIBCXX_BEGIN_NAMESPACE_VERSION
57
58 using ::abs;
59
60#ifndef __CORRECT_ISO_CPP_STDLIB_H_PROTO
61 inline long
62 abs(long __i) { return __builtin_labs(__i); }
63#endif
64
65#ifdef _GLIBCXX_USE_LONG_LONG
66 inline long long
67 abs(long long __x) { return __builtin_llabs (__x); }
68#endif
69
70// _GLIBCXX_RESOLVE_LIB_DEFECTS
71// 2192. Validity and return type of std::abs(0u) is unclear
72// 2294. <cstdlib> should declare abs(double)
73// 2735. std::abs(short), std::abs(signed char) and others should return int
74
75#ifndef __CORRECT_ISO_CPP_MATH_H_PROTO
76 inline _GLIBCXX_CONSTEXPR double
77 abs(double __x)
78 { return __builtin_fabs(__x); }
79
80 inline _GLIBCXX_CONSTEXPR float
81 abs(float __x)
82 { return __builtin_fabsf(__x); }
83
84 inline _GLIBCXX_CONSTEXPR long double
85 abs(long double __x)
86 { return __builtin_fabsl(__x); }
87#endif
88
89#if defined(__GLIBCXX_TYPE_INT_N_0)
90 __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_0
91 abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
92#endif
93#if defined(__GLIBCXX_TYPE_INT_N_1)
94 __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_1
95 abs(__GLIBCXX_TYPE_INT_N_1 __x) { return __x >= 0 ? __x : -__x; }
96#endif
97#if defined(__GLIBCXX_TYPE_INT_N_2)
98 __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_2
99 abs(__GLIBCXX_TYPE_INT_N_2 __x) { return __x >= 0 ? __x : -__x; }
100#endif
101#if defined(__GLIBCXX_TYPE_INT_N_3)
102 __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_3
103 abs(__GLIBCXX_TYPE_INT_N_3 __x) { return __x >= 0 ? __x : -__x; }
104#endif
105
106#if defined __STRICT_ANSI__ && defined __SIZEOF_INT128__
107 // In strict modes __GLIBCXX_TYPE_INT_N_0 is not defined for __int128,
108 // but we want to always define std::abs(__int128).
109 __extension__ inline _GLIBCXX_CONSTEXPR __int128
110 abs(__int128 __x) { return __x >= 0 ? __x : -__x; }
111#endif
112
113#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
114 constexpr _Float16
115 abs(_Float16 __x)
116 { return _Float16(__builtin_fabsf(__x)); }
117#endif
118
119#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
120 constexpr _Float32
121 abs(_Float32 __x)
122 { return __builtin_fabsf(__x); }
123#endif
124
125#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
126 constexpr _Float64
127 abs(_Float64 __x)
128 { return __builtin_fabs(__x); }
129#endif
130
131#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
132 constexpr _Float128
133 abs(_Float128 __x)
134 { return __builtin_fabsl(__x); }
135#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
136 constexpr _Float128
137 abs(_Float128 __x)
138 { return __builtin_fabsf128(__x); }
139#endif
140
141#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
142 constexpr __gnu_cxx::__bfloat16_t
143 abs(__gnu_cxx::__bfloat16_t __x)
144 { return __gnu_cxx::__bfloat16_t(__builtin_fabsf(__x)); }
145#endif
146
147#if defined(_GLIBCXX_USE_FLOAT128)
148 __extension__ inline _GLIBCXX_CONSTEXPR
149 __float128
150 abs(__float128 __x)
151 {
152#if defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
153 return __builtin_fabsl(__x);
154#elif defined(_GLIBCXX_HAVE_FLOAT128_MATH)
155 return __builtin_fabsf128(__x);
156#else
157 // Assume that __builtin_signbit works for __float128.
158 return __builtin_signbit(__x) ? -__x : __x;
159#endif
160 }
161#endif
162
163_GLIBCXX_END_NAMESPACE_VERSION
164} // namespace
165} // extern "C++"
166
167#pragma GCC diagnostic pop
168
169#endif // _GLIBCXX_BITS_STD_ABS_H
_Tp abs(const complex< _Tp > &)
Return magnitude of z.
Definition complex:968
ISO C++ entities toplevel namespace is std.