1 // <numeric> -*- C++ -*-
3 // Copyright (C) 2001-2019 Free Software Foundation, Inc.
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)
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.
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.
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/>.
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file include/numeric
52 * This is a Standard C++ Library header.
55 #ifndef _GLIBCXX_NUMERIC
56 #define _GLIBCXX_NUMERIC 1
58 #pragma GCC system_header
60 #include <bits/c++config.h>
61 #include <bits/stl_iterator_base_types.h>
62 #include <bits/stl_numeric.h>
64 #ifdef _GLIBCXX_PARALLEL
65 # include <parallel/numeric>
69 * @defgroup numerics Numerics
71 * Components for performing numeric operations. Includes support for
72 * complex number types, random number generation, numeric (n-at-a-time)
73 * arrays, generalized numeric algorithms, and mathematical special functions.
76 #if __cplusplus >= 201402L
77 #include <type_traits>
79 namespace std _GLIBCXX_VISIBILITY(default)
81 _GLIBCXX_BEGIN_NAMESPACE_VERSION
85 // std::abs is not constexpr and doesn't support unsigned integers.
86 template<typename _Tp>
88 enable_if_t<__and_<is_integral<_Tp>, is_signed<_Tp>>::value, _Tp>
89 __abs_integral(_Tp __val)
90 { return __val < 0 ? -__val : __val; }
92 template<typename _Tp>
94 enable_if_t<__and_<is_integral<_Tp>, is_unsigned<_Tp>>::value, _Tp>
95 __abs_integral(_Tp __val)
98 void __abs_integral(bool) = delete;
100 template<typename _Mn, typename _Nn>
101 constexpr common_type_t<_Mn, _Nn>
102 __gcd(_Mn __m, _Nn __n)
104 return __m == 0 ? __detail::__abs_integral(__n)
105 : __n == 0 ? __detail::__abs_integral(__m)
106 : __detail::__gcd(__n, __m % __n);
109 /// Least common multiple
110 template<typename _Mn, typename _Nn>
111 constexpr common_type_t<_Mn, _Nn>
112 __lcm(_Mn __m, _Nn __n)
114 return (__m != 0 && __n != 0)
115 ? (__detail::__abs_integral(__m) / __detail::__gcd(__m, __n))
116 * __detail::__abs_integral(__n)
119 } // namespace __detail
121 #if __cplusplus >= 201703L
123 #define __cpp_lib_gcd_lcm 201606
124 // These were used in drafts of SD-6:
125 #define __cpp_lib_gcd 201606
126 #define __cpp_lib_lcm 201606
128 /// Greatest common divisor
129 template<typename _Mn, typename _Nn>
130 constexpr common_type_t<_Mn, _Nn>
131 gcd(_Mn __m, _Nn __n)
133 static_assert(is_integral_v<_Mn>, "gcd arguments are integers");
134 static_assert(is_integral_v<_Nn>, "gcd arguments are integers");
135 static_assert(!is_same_v<remove_cv_t<_Mn>, bool>,
136 "gcd arguments are not bools");
137 static_assert(!is_same_v<remove_cv_t<_Nn>, bool>,
138 "gcd arguments are not bools");
139 return __detail::__gcd(__m, __n);
142 /// Least common multiple
143 template<typename _Mn, typename _Nn>
144 constexpr common_type_t<_Mn, _Nn>
145 lcm(_Mn __m, _Nn __n)
147 static_assert(is_integral_v<_Mn>, "lcm arguments are integers");
148 static_assert(is_integral_v<_Nn>, "lcm arguments are integers");
149 static_assert(!is_same_v<remove_cv_t<_Mn>, bool>,
150 "lcm arguments are not bools");
151 static_assert(!is_same_v<remove_cv_t<_Nn>, bool>,
152 "lcm arguments are not bools");
153 return __detail::__lcm(__m, __n);
158 _GLIBCXX_END_NAMESPACE_VERSION
163 #if __cplusplus > 201703L
166 namespace std _GLIBCXX_VISIBILITY(default)
168 _GLIBCXX_BEGIN_NAMESPACE_VERSION
170 # define __cpp_lib_interpolate 201902L
172 template<typename _Tp>
174 enable_if_t<__and_v<is_arithmetic<_Tp>, is_same<remove_cv_t<_Tp>, _Tp>,
175 __not_<is_same<_Tp, bool>>>,
177 midpoint(_Tp __a, _Tp __b) noexcept
179 if constexpr (is_integral_v<_Tp>)
181 using _Up = make_unsigned_t<_Tp>;
192 return __a + __k * _Tp(_Up(__M - __m) / 2);
196 constexpr _Tp __lo = numeric_limits<_Tp>::min() * 2;
197 constexpr _Tp __hi = numeric_limits<_Tp>::max() / 2;
198 const _Tp __abs_a = __a < 0 ? -__a : __a;
199 const _Tp __abs_b = __b < 0 ? -__b : __b;
200 if (__abs_a <= __hi && __abs_b <= __hi) [[likely]]
201 return (__a + __b) / 2; // always correctly rounded
202 if (__abs_a < __lo) // not safe to halve __a
204 if (__abs_b < __lo) // not safe to halve __b
206 return __a/2 + __b/2; // otherwise correctly rounded
210 template<typename _Tp>
212 enable_if_t<__and_v<is_object<_Tp>, bool_constant<sizeof(_Tp) != 0>>, _Tp*>
213 midpoint(_Tp* __a, _Tp* __b) noexcept
215 return __a + (__b - __a) / 2;
217 _GLIBCXX_END_NAMESPACE_VERSION
222 #if __cplusplus > 201402L
223 // Parallel STL algorithms
224 # if __PSTL_EXECUTION_POLICIES_DEFINED
225 // If <execution> has already been included, pull in implementations
226 # include <pstl/glue_numeric_impl.h>
228 // Otherwise just pull in forward declarations
229 # include <pstl/glue_numeric_defs.h>
230 # define __PSTL_NUMERIC_FORWARD_DECLARED 1
233 // Feature test macro for parallel algorithms
234 # define __cpp_lib_parallel_algorithm 201603L
237 #endif /* _GLIBCXX_NUMERIC */