1// <numeric> -*- C++ -*-
2
3// Copyright (C) 2001-2018 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/*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
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.
37 *
38 *
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
41 *
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.
49 */
50
51/** @file include/numeric
52 *  This is a Standard C++ Library header.
53 */
54
55#ifndef _GLIBCXX_NUMERIC
56#define _GLIBCXX_NUMERIC 1
57
58#pragma GCC system_header
59
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>
64
65#ifdef _GLIBCXX_PARALLEL
66# include <parallel/numeric>
67#endif
68
69/**
70 * @defgroup numerics Numerics
71 *
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
75 * math functions.
76 */
77
78#if __cplusplus >= 201402L
79#include <type_traits>
80
81namespace std _GLIBCXX_VISIBILITY(default)
82{
83_GLIBCXX_BEGIN_NAMESPACE_VERSION
84
85namespace __detail
86{
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>
90    constexpr _Up
91    __absu(_Tp __val)
92    {
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;
97    }
98
99  template<typename _Up> void __absu(bool) = delete;
100
101  // GCD implementation
102  template<typename _Tp>
103    constexpr _Tp
104    __gcd(_Tp __m, _Tp __n)
105    {
106      static_assert(is_unsigned<_Tp>::value, "type must be unsigned");
107      return __m == 0 ? __n
108	: __n == 0 ? __m
109	: __detail::__gcd(__n, _Tp(__m % __n));
110    }
111
112  // LCM implementation
113  template<typename _Tp>
114    constexpr _Tp
115    __lcm(_Tp __m, _Tp __n)
116    {
117      return (__m != 0 && __n != 0)
118	? (__m / __detail::__gcd(__m, __n)) * __n
119	: 0;
120    }
121} // namespace __detail
122
123#if __cplusplus > 201402L
124
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
129
130  /// Greatest common divisor
131  template<typename _Mn, typename _Nn>
132    constexpr common_type_t<_Mn, _Nn>
133    gcd(_Mn __m, _Nn __n) noexcept
134    {
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));
142    }
143
144  /// Least common multiple
145  template<typename _Mn, typename _Nn>
146    constexpr common_type_t<_Mn, _Nn>
147    lcm(_Mn __m, _Nn __n) noexcept
148    {
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));
156    }
157
158#endif // C++17
159
160_GLIBCXX_END_NAMESPACE_VERSION
161} // namespace std
162
163#endif // C++14
164
165
166#endif /* _GLIBCXX_NUMERIC */
167