blob: 32b19a46635884f27f376a14e24c019463d3a647 [file] [log] [blame]
Marshall Clowab581e72016-07-26 14:28:34 +00001// -*- C++ -*-
2//===--------------------------- numeric ----------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_EXPERIMENTAL_NUMERIC
12#define _LIBCPP_EXPERIMENTAL_NUMERIC
13/*
14 experimental/numeric synopsis
15
16// C++1z
17namespace std {
18namespace experimental {
19inline namespace fundamentals_v2 {
20
21 // 13.1.2, Greatest common divisor
22 template<class M, class N>
23 constexpr common_type_t<M,N> gcd(M m, N n);
24
25 // 13.1.3, Least common multiple
26 template<class M, class N>
27 constexpr common_type_t<M,N> lcm(M m, N n);
28
29} // namespace fundamentals_v2
30} // namespace experimental
31} // namespace std
32
33 */
34
35#include <experimental/__config>
36#include <numeric>
37#include <type_traits> // is_integral
38#include <limits> // numeric_limits
39
40#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
41#pragma GCC system_header
42#endif
43
44#if _LIBCPP_STD_VER > 11
45
46_LIBCPP_BEGIN_NAMESPACE_LFTS_V2
47
48template <typename _Tp, bool _IsSigned = is_signed<_Tp>::value> struct __abs;
49
50template <typename _Tp>
51struct __abs<_Tp, true> {
52 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
53 _Tp operator()(_Tp __t) const noexcept { return __t >= 0 ? __t : -__t; }
54};
55
56template <typename _Tp>
57struct __abs<_Tp, false> {
58 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
59 _Tp operator()(_Tp __t) const noexcept { return __t; }
60};
61
62
63template<class _Tp>
64_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
65_Tp __gcd(_Tp __m, _Tp __n)
66{
67 static_assert((!is_signed<_Tp>::value), "" );
68 return __n == 0 ? __m : __gcd<_Tp>(__n, __m % __n);
69}
70
71
72template<class _Tp, class _Up>
73_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
74common_type_t<_Tp,_Up>
75gcd(_Tp __m, _Up __n)
76{
77 static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types");
78 using _Rp = common_type_t<_Tp,_Up>;
79 using _Wp = make_unsigned_t<_Rp>;
80 return static_cast<_Rp>(__gcd(static_cast<_Wp>(__abs<_Tp>()(__m)),
81 static_cast<_Wp>(__abs<_Up>()(__n))));
82}
83
84template<class _Tp, class _Up>
85_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
86common_type_t<_Tp,_Up>
87lcm(_Tp __m, _Up __n)
88{
89 static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types");
90 if (__m == 0 || __n == 0)
91 return 0;
92
93 using _Rp = common_type_t<_Tp,_Up>;
94 _Rp __val1 = __abs<_Tp>()(__m) / gcd(__m,__n);
95 _Up __val2 = __abs<_Up>()(__n);
96 _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm");
97 return __val1 * __val2;
98}
99
100_LIBCPP_END_NAMESPACE_LFTS_V2
101
102#endif /* _LIBCPP_STD_VER > 11 */
103#endif /* _LIBCPP_EXPERIMENTAL_NUMERIC */