1// <numeric> -*- C++ -*-
3// Copyright (C) 2001-2018 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>
63#include <ext/numeric_traits.h>
65#ifdef _GLIBCXX_PARALLEL
66# include <parallel/numeric>
70 * @defgroup numerics Numerics
72 * Components for performing numeric operations. Includes support for
73 * for complex number types, random number generation, numeric
74 * (n-at-a-time) arrays, generalized numeric algorithms, and special
78#if __cplusplus >= 201402L
81namespace std _GLIBCXX_VISIBILITY(default)
83_GLIBCXX_BEGIN_NAMESPACE_VERSION
87 // std::abs is not constexpr, doesn't support unsigned integers,
88 // and std::abs(std::numeric_limits<T>::min()) is undefined.
89 template<typename _Up, typename _Tp>
93 static_assert(is_unsigned<_Up>::value, "result type must be unsigned");
94 static_assert(sizeof(_Up) >= sizeof(_Tp),
95 "result type must be at least as wide as the input type");
96 return __val < 0 ? -(_Up)__val : (_Up)__val;
99 template<typename _Up> void __absu(bool) = delete;
101 // GCD implementation
102 template<typename _Tp>
104 __gcd(_Tp __m, _Tp __n)
106 static_assert(is_unsigned<_Tp>::value, "type must be unsigned");
107 return __m == 0 ? __n
109 : __detail::__gcd(__n, _Tp(__m % __n));
112 // LCM implementation
113 template<typename _Tp>
115 __lcm(_Tp __m, _Tp __n)
117 return (__m != 0 && __n != 0)
118 ? (__m / __detail::__gcd(__m, __n)) * __n
121} // namespace __detail
123#if __cplusplus > 201402L
125#define __cpp_lib_gcd_lcm 201606
126// These were used in drafts of SD-6:
127#define __cpp_lib_gcd 201606
128#define __cpp_lib_lcm 201606
130 /// Greatest common divisor
131 template<typename _Mn, typename _Nn>
132 constexpr common_type_t<_Mn, _Nn>
133 gcd(_Mn __m, _Nn __n) noexcept
135 static_assert(is_integral_v<_Mn>, "std::gcd arguments must be integers");
136 static_assert(is_integral_v<_Nn>, "std::gcd arguments must be integers");
137 static_assert(_Mn(2) != _Mn(1), "std::gcd arguments must not be bool");
138 static_assert(_Nn(2) != _Nn(1), "std::gcd arguments must not be bool");
139 using _Up = make_unsigned_t<common_type_t<_Mn, _Nn>>;
140 return __detail::__gcd(__detail::__absu<_Up>(__m),
141 __detail::__absu<_Up>(__n));
144 /// Least common multiple
145 template<typename _Mn, typename _Nn>
146 constexpr common_type_t<_Mn, _Nn>
147 lcm(_Mn __m, _Nn __n) noexcept
149 static_assert(is_integral_v<_Mn>, "std::lcm arguments must be integers");
150 static_assert(is_integral_v<_Nn>, "std::lcm arguments must be integers");
151 static_assert(_Mn(2) == 2, "std::lcm arguments must not be bool");
152 static_assert(_Nn(2) == 2, "std::lcm arguments must not be bool");
153 using _Up = make_unsigned_t<common_type_t<_Mn, _Nn>>;
154 return __detail::__lcm(__detail::__absu<_Up>(__m),
155 __detail::__absu<_Up>(__n));
160_GLIBCXX_END_NAMESPACE_VERSION
166#endif /* _GLIBCXX_NUMERIC */