blob: 99417703e9bdb4a0f951fa7838ed5dc5f3694d4c [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- numeric ---------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_NUMERIC
11#define _LIBCPP_NUMERIC
12
13/*
14 numeric synopsis
15
16namespace std
17{
18
19template <class InputIterator, class T>
20 T
21 accumulate(InputIterator first, InputIterator last, T init);
22
23template <class InputIterator, class T, class BinaryOperation>
24 T
25 accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
26
Marshall Clow3b7cad22017-06-14 04:48:45 +000027template<class InputIterator>
28 typename iterator_traits<InputIterator>::value_type
29 reduce(InputIterator first, InputIterator last); // C++17
30
31template<class InputIterator, class T>
32 T
33 reduce(InputIterator first, InputIterator last, T init); // C++17
34
35template<class InputIterator, class T, class BinaryOperation>
36 T
37 reduce(InputIterator first, InputIterator last, T init, BinaryOperation binary_op); // C++17
38
Howard Hinnantc51e1022010-05-11 19:42:16 +000039template <class InputIterator1, class InputIterator2, class T>
40 T
41 inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);
42
43template <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
44 T
45 inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
46 T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);
47
Marshall Clow3b7cad22017-06-14 04:48:45 +000048
49template<class InputIterator1, class InputIterator2, class T>
50 T
51 transform_reduce(InputIterator1 first1, InputIterator1 last1,
52 InputIterator2 first2, T init); // C++17
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +000053
Marshall Clow3b7cad22017-06-14 04:48:45 +000054template<class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
55 T
56 transform_reduce(InputIterator1 first1, InputIterator1 last1,
57 InputIterator2 first2, T init,
58 BinaryOperation1 binary_op1, BinaryOperation2 binary_op2); // C++17
59
60template<class InputIterator, class T, class BinaryOperation, class UnaryOperation>
61 T
62 transform_reduce(InputIterator first, InputIterator last, T init,
63 BinaryOperation binary_op, UnaryOperation unary_op); // C++17
64
Howard Hinnantc51e1022010-05-11 19:42:16 +000065template <class InputIterator, class OutputIterator>
66 OutputIterator
67 partial_sum(InputIterator first, InputIterator last, OutputIterator result);
68
69template <class InputIterator, class OutputIterator, class BinaryOperation>
70 OutputIterator
71 partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
72
Marshall Clow0ce24992017-06-10 02:22:13 +000073template<class InputIterator, class OutputIterator, class T>
74 OutputIterator
75 exclusive_scan(InputIterator first, InputIterator last,
76 OutputIterator result, T init); // C++17
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +000077
Marshall Clow0ce24992017-06-10 02:22:13 +000078template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
79 OutputIterator
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +000080 exclusive_scan(InputIterator first, InputIterator last,
Marshall Clow0ce24992017-06-10 02:22:13 +000081 OutputIterator result, T init, BinaryOperation binary_op); // C++17
82
Marshall Clow4c561442017-06-23 05:12:42 +000083template<class InputIterator, class OutputIterator>
84 OutputIterator
85 inclusive_scan(InputIterator first, InputIterator last, OutputIterator result); // C++17
86
87template<class InputIterator, class OutputIterator, class BinaryOperation>
88 OutputIterator
89 inclusive_scan(InputIterator first, InputIterator last,
90 OutputIterator result, BinaryOperation binary_op); // C++17
91
92template<class InputIterator, class OutputIterator, class BinaryOperation, class T>
93 OutputIterator
94 inclusive_scan(InputIterator first, InputIterator last,
95 OutputIterator result, BinaryOperation binary_op, T init); // C++17
96
Marshall Clow0ce24992017-06-10 02:22:13 +000097template<class InputIterator, class OutputIterator, class T,
98 class BinaryOperation, class UnaryOperation>
99 OutputIterator
100 transform_exclusive_scan(InputIterator first, InputIterator last,
101 OutputIterator result, T init,
102 BinaryOperation binary_op, UnaryOperation unary_op); // C++17
103
Marshall Clow4c561442017-06-23 05:12:42 +0000104template<class InputIterator, class OutputIterator,
105 class BinaryOperation, class UnaryOperation>
Louis Dionne44bcff92018-08-03 22:36:53 +0000106 OutputIterator
107 transform_inclusive_scan(InputIterator first, InputIterator last,
Marshall Clow4c561442017-06-23 05:12:42 +0000108 OutputIterator result,
109 BinaryOperation binary_op, UnaryOperation unary_op); // C++17
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000110
Marshall Clow4c561442017-06-23 05:12:42 +0000111template<class InputIterator, class OutputIterator,
112 class BinaryOperation, class UnaryOperation, class T>
Louis Dionne44bcff92018-08-03 22:36:53 +0000113 OutputIterator
114 transform_inclusive_scan(InputIterator first, InputIterator last,
Marshall Clow4c561442017-06-23 05:12:42 +0000115 OutputIterator result,
116 BinaryOperation binary_op, UnaryOperation unary_op,
117 T init); // C++17
118
Howard Hinnantc51e1022010-05-11 19:42:16 +0000119template <class InputIterator, class OutputIterator>
120 OutputIterator
121 adjacent_difference(InputIterator first, InputIterator last, OutputIterator result);
122
123template <class InputIterator, class OutputIterator, class BinaryOperation>
124 OutputIterator
125 adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
126
Howard Hinnantf45797f2010-05-26 18:53:44 +0000127template <class ForwardIterator, class T>
128 void iota(ForwardIterator first, ForwardIterator last, T value);
129
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000130template <class M, class N>
131 constexpr common_type_t<M,N> gcd(M m, N n); // C++17
132
133template <class M, class N>
134 constexpr common_type_t<M,N> lcm(M m, N n); // C++17
135
Howard Hinnantc51e1022010-05-11 19:42:16 +0000136} // std
137
138*/
139
140#include <__config>
141#include <iterator>
Eric Fiseliere7af1d82017-02-08 00:14:13 +0000142#include <limits> // for numeric_limits
Marshall Clow0ce24992017-06-10 02:22:13 +0000143#include <functional>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000144#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000145
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000146#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000148#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000149
Eric Fiselierf4433a32017-05-31 22:07:49 +0000150_LIBCPP_PUSH_MACROS
151#include <__undef_macros>
152
Howard Hinnantc51e1022010-05-11 19:42:16 +0000153_LIBCPP_BEGIN_NAMESPACE_STD
154
155template <class _InputIterator, class _Tp>
156inline _LIBCPP_INLINE_VISIBILITY
157_Tp
158accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
159{
160 for (; __first != __last; ++__first)
161 __init = __init + *__first;
162 return __init;
163}
164
165template <class _InputIterator, class _Tp, class _BinaryOperation>
166inline _LIBCPP_INLINE_VISIBILITY
167_Tp
168accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
169{
170 for (; __first != __last; ++__first)
171 __init = __binary_op(__init, *__first);
172 return __init;
173}
174
Marshall Clow3b7cad22017-06-14 04:48:45 +0000175#if _LIBCPP_STD_VER > 14
176template <class _InputIterator, class _Tp, class _BinaryOp>
177inline _LIBCPP_INLINE_VISIBILITY
178_Tp
179reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b)
180{
181 for (; __first != __last; ++__first)
182 __init = __b(__init, *__first);
183 return __init;
184}
185
186template <class _InputIterator, class _Tp>
187inline _LIBCPP_INLINE_VISIBILITY
188_Tp
189reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
190{
191 return _VSTD::reduce(__first, __last, __init, _VSTD::plus<>());
192}
193
194template <class _InputIterator>
195inline _LIBCPP_INLINE_VISIBILITY
196typename iterator_traits<_InputIterator>::value_type
197reduce(_InputIterator __first, _InputIterator __last)
198{
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000199 return _VSTD::reduce(__first, __last,
Marshall Clow3b7cad22017-06-14 04:48:45 +0000200 typename iterator_traits<_InputIterator>::value_type{});
201}
202#endif
203
Howard Hinnantc51e1022010-05-11 19:42:16 +0000204template <class _InputIterator1, class _InputIterator2, class _Tp>
205inline _LIBCPP_INLINE_VISIBILITY
206_Tp
207inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init)
208{
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000209 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000210 __init = __init + *__first1 * *__first2;
211 return __init;
212}
213
214template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
215inline _LIBCPP_INLINE_VISIBILITY
216_Tp
217inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
218 _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
219{
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000220 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000221 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
222 return __init;
223}
224
Marshall Clow3b7cad22017-06-14 04:48:45 +0000225#if _LIBCPP_STD_VER > 14
226template <class _InputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
227inline _LIBCPP_INLINE_VISIBILITY
228_Tp
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000229transform_reduce(_InputIterator __first, _InputIterator __last,
Marshall Clow3b7cad22017-06-14 04:48:45 +0000230 _Tp __init, _BinaryOp __b, _UnaryOp __u)
231{
232 for (; __first != __last; ++__first)
233 __init = __b(__init, __u(*__first));
234 return __init;
235}
236
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000237template <class _InputIterator1, class _InputIterator2,
Marshall Clow3b7cad22017-06-14 04:48:45 +0000238 class _Tp, class _BinaryOp1, class _BinaryOp2>
239inline _LIBCPP_INLINE_VISIBILITY
240_Tp
241transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
242 _InputIterator2 __first2, _Tp __init, _BinaryOp1 __b1, _BinaryOp2 __b2)
243{
244 for (; __first1 != __last1; ++__first1, (void) ++__first2)
245 __init = __b1(__init, __b2(*__first1, *__first2));
246 return __init;
247}
248
249template <class _InputIterator1, class _InputIterator2, class _Tp>
250inline _LIBCPP_INLINE_VISIBILITY
251_Tp
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000252transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow3b7cad22017-06-14 04:48:45 +0000253 _InputIterator2 __first2, _Tp __init)
254{
Billy Robert O'Neal III497250a2018-01-05 01:31:57 +0000255 return _VSTD::transform_reduce(__first1, __last1, __first2, _VSTD::move(__init),
Marshall Clow3b7cad22017-06-14 04:48:45 +0000256 _VSTD::plus<>(), _VSTD::multiplies<>());
257}
258#endif
259
Howard Hinnantc51e1022010-05-11 19:42:16 +0000260template <class _InputIterator, class _OutputIterator>
261inline _LIBCPP_INLINE_VISIBILITY
262_OutputIterator
263partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
264{
265 if (__first != __last)
266 {
267 typename iterator_traits<_InputIterator>::value_type __t(*__first);
268 *__result = __t;
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000269 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270 {
271 __t = __t + *__first;
272 *__result = __t;
273 }
274 }
275 return __result;
276}
277
278template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
279inline _LIBCPP_INLINE_VISIBILITY
280_OutputIterator
281partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
282 _BinaryOperation __binary_op)
283{
284 if (__first != __last)
285 {
286 typename iterator_traits<_InputIterator>::value_type __t(*__first);
287 *__result = __t;
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000288 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289 {
290 __t = __binary_op(__t, *__first);
291 *__result = __t;
292 }
293 }
294 return __result;
295}
296
Marshall Clow0ce24992017-06-10 02:22:13 +0000297#if _LIBCPP_STD_VER > 14
298template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
299inline _LIBCPP_INLINE_VISIBILITY
300_OutputIterator
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000301exclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow0ce24992017-06-10 02:22:13 +0000302 _OutputIterator __result, _Tp __init, _BinaryOp __b)
303{
304 if (__first != __last)
305 {
306 _Tp __saved = __init;
307 do
308 {
309 __init = __b(__init, *__first);
310 *__result = __saved;
311 __saved = __init;
312 ++__result;
313 } while (++__first != __last);
314 }
315 return __result;
316}
317
318template <class _InputIterator, class _OutputIterator, class _Tp>
319inline _LIBCPP_INLINE_VISIBILITY
320_OutputIterator
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000321exclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow0ce24992017-06-10 02:22:13 +0000322 _OutputIterator __result, _Tp __init)
323{
324 return _VSTD::exclusive_scan(__first, __last, __result, __init, _VSTD::plus<>());
325}
326
Marshall Clow4c561442017-06-23 05:12:42 +0000327template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000328_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow4c561442017-06-23 05:12:42 +0000329 _OutputIterator __result, _BinaryOp __b, _Tp __init)
330{
331 for (; __first != __last; ++__first, (void) ++__result) {
332 __init = __b(__init, *__first);
333 *__result = __init;
334 }
335 return __result;
336}
337
338template <class _InputIterator, class _OutputIterator, class _BinaryOp>
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000339_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow4c561442017-06-23 05:12:42 +0000340 _OutputIterator __result, _BinaryOp __b)
341{
342 if (__first != __last) {
343 typename std::iterator_traits<_InputIterator>::value_type __init = *__first;
344 *__result++ = __init;
345 if (++__first != __last)
346 return _VSTD::inclusive_scan(__first, __last, __result, __b, __init);
347 }
348
349 return __result;
350}
351
352template <class _InputIterator, class _OutputIterator>
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000353_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow4c561442017-06-23 05:12:42 +0000354 _OutputIterator __result)
355{
356 return _VSTD::inclusive_scan(__first, __last, __result, std::plus<>());
357}
358
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000359template <class _InputIterator, class _OutputIterator, class _Tp,
Marshall Clow0ce24992017-06-10 02:22:13 +0000360 class _BinaryOp, class _UnaryOp>
361inline _LIBCPP_INLINE_VISIBILITY
362_OutputIterator
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000363transform_exclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow0ce24992017-06-10 02:22:13 +0000364 _OutputIterator __result, _Tp __init,
365 _BinaryOp __b, _UnaryOp __u)
366{
367 if (__first != __last)
368 {
369 _Tp __saved = __init;
370 do
371 {
372 __init = __b(__init, __u(*__first));
373 *__result = __saved;
374 __saved = __init;
375 ++__result;
376 } while (++__first != __last);
377 }
378 return __result;
379}
Marshall Clow4c561442017-06-23 05:12:42 +0000380
381template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000382_OutputIterator transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow4c561442017-06-23 05:12:42 +0000383 _OutputIterator __result, _BinaryOp __b, _UnaryOp __u, _Tp __init)
384{
385 for (; __first != __last; ++__first, (void) ++__result) {
386 __init = __b(__init, __u(*__first));
387 *__result = __init;
388 }
389
390 return __result;
391}
392
393template <class _InputIterator, class _OutputIterator, class _BinaryOp, class _UnaryOp>
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000394_OutputIterator transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow4c561442017-06-23 05:12:42 +0000395 _OutputIterator __result, _BinaryOp __b, _UnaryOp __u)
396{
397 if (__first != __last) {
398 typename std::iterator_traits<_InputIterator>::value_type __init = __u(*__first);
399 *__result++ = __init;
400 if (++__first != __last)
401 return _VSTD::transform_inclusive_scan(__first, __last, __result, __b, __u, __init);
402 }
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000403
Marshall Clow4c561442017-06-23 05:12:42 +0000404 return __result;
405}
Marshall Clow0ce24992017-06-10 02:22:13 +0000406#endif
407
Howard Hinnantc51e1022010-05-11 19:42:16 +0000408template <class _InputIterator, class _OutputIterator>
409inline _LIBCPP_INLINE_VISIBILITY
410_OutputIterator
411adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
412{
413 if (__first != __last)
414 {
415 typename iterator_traits<_InputIterator>::value_type __t1(*__first);
416 *__result = __t1;
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000417 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000418 {
419 typename iterator_traits<_InputIterator>::value_type __t2(*__first);
420 *__result = __t2 - __t1;
Howard Hinnant528ceb72013-08-22 18:02:34 +0000421 __t1 = _VSTD::move(__t2);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000422 }
423 }
424 return __result;
425}
426
427template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
428inline _LIBCPP_INLINE_VISIBILITY
429_OutputIterator
430adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
431 _BinaryOperation __binary_op)
432{
433 if (__first != __last)
434 {
435 typename iterator_traits<_InputIterator>::value_type __t1(*__first);
436 *__result = __t1;
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000437 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000438 {
439 typename iterator_traits<_InputIterator>::value_type __t2(*__first);
440 *__result = __binary_op(__t2, __t1);
Howard Hinnant528ceb72013-08-22 18:02:34 +0000441 __t1 = _VSTD::move(__t2);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000442 }
443 }
444 return __result;
445}
446
Howard Hinnantf45797f2010-05-26 18:53:44 +0000447template <class _ForwardIterator, class _Tp>
448inline _LIBCPP_INLINE_VISIBILITY
449void
Howard Hinnantbf074022011-10-22 20:59:45 +0000450iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_)
Howard Hinnantf45797f2010-05-26 18:53:44 +0000451{
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000452 for (; __first != __last; ++__first, (void) ++__value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000453 *__first = __value_;
Howard Hinnantf45797f2010-05-26 18:53:44 +0000454}
455
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000456
457#if _LIBCPP_STD_VER > 14
Marshall Clowf9316722017-02-10 20:49:08 +0000458template <typename _Result, typename _Source, bool _IsSigned = is_signed<_Source>::value> struct __abs;
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000459
Marshall Clowf9316722017-02-10 20:49:08 +0000460template <typename _Result, typename _Source>
461struct __abs<_Result, _Source, true> {
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000462 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clowf9316722017-02-10 20:49:08 +0000463 _Result operator()(_Source __t) const noexcept
464 {
465 if (__t >= 0) return __t;
466 if (__t == numeric_limits<_Source>::min()) return -static_cast<_Result>(__t);
467 return -__t;
468 }
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000469};
470
Marshall Clowf9316722017-02-10 20:49:08 +0000471template <typename _Result, typename _Source>
472struct __abs<_Result, _Source, false> {
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000473 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clowf9316722017-02-10 20:49:08 +0000474 _Result operator()(_Source __t) const noexcept { return __t; }
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000475};
476
477
478template<class _Tp>
Eric Fiseliered86f1f2017-05-09 00:00:00 +0000479_LIBCPP_CONSTEXPR _LIBCPP_HIDDEN
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000480_Tp __gcd(_Tp __m, _Tp __n)
481{
Marshall Clowf9316722017-02-10 20:49:08 +0000482 static_assert((!is_signed<_Tp>::value), "");
Eric Fiseliered86f1f2017-05-09 00:00:00 +0000483 return __n == 0 ? __m : _VSTD::__gcd<_Tp>(__n, __m % __n);
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000484}
485
486
487template<class _Tp, class _Up>
488_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
489common_type_t<_Tp,_Up>
490gcd(_Tp __m, _Up __n)
491{
492 static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types");
Marshall Clow78dbe462016-11-14 18:22:19 +0000493 static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to gcd cannot be bool" );
494 static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to gcd cannot be bool" );
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000495 using _Rp = common_type_t<_Tp,_Up>;
496 using _Wp = make_unsigned_t<_Rp>;
Eric Fiseliered86f1f2017-05-09 00:00:00 +0000497 return static_cast<_Rp>(_VSTD::__gcd(
498 static_cast<_Wp>(__abs<_Rp, _Tp>()(__m)),
499 static_cast<_Wp>(__abs<_Rp, _Up>()(__n))));
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000500}
501
502template<class _Tp, class _Up>
503_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
504common_type_t<_Tp,_Up>
505lcm(_Tp __m, _Up __n)
506{
507 static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types");
Marshall Clow78dbe462016-11-14 18:22:19 +0000508 static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to lcm cannot be bool" );
509 static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to lcm cannot be bool" );
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000510 if (__m == 0 || __n == 0)
511 return 0;
512
513 using _Rp = common_type_t<_Tp,_Up>;
Eric Fiseliered86f1f2017-05-09 00:00:00 +0000514 _Rp __val1 = __abs<_Rp, _Tp>()(__m) / _VSTD::gcd(__m, __n);
Marshall Clowf9316722017-02-10 20:49:08 +0000515 _Rp __val2 = __abs<_Rp, _Up>()(__n);
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000516 _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm");
517 return __val1 * __val2;
518}
519
520#endif /* _LIBCPP_STD_VER > 14 */
521
Howard Hinnantc51e1022010-05-11 19:42:16 +0000522_LIBCPP_END_NAMESPACE_STD
523
Eric Fiselierf4433a32017-05-31 22:07:49 +0000524_LIBCPP_POP_MACROS
525
Howard Hinnantc51e1022010-05-11 19:42:16 +0000526#endif // _LIBCPP_NUMERIC