WPILibC++ 2023.4.3-108-ge5452e3
gcd.hpp
Go to the documentation of this file.
1/*################################################################################
2 ##
3 ## Copyright (C) 2016-2022 Keith O'Hara
4 ##
5 ## This file is part of the GCE-Math C++ library.
6 ##
7 ## Licensed under the Apache License, Version 2.0 (the "License");
8 ## you may not use this file except in compliance with the License.
9 ## You may obtain a copy of the License at
10 ##
11 ## http://www.apache.org/licenses/LICENSE-2.0
12 ##
13 ## Unless required by applicable law or agreed to in writing, software
14 ## distributed under the License is distributed on an "AS IS" BASIS,
15 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 ## See the License for the specific language governing permissions and
17 ## limitations under the License.
18 ##
19 ################################################################################*/
20
21#ifndef _gcem_gcd_HPP
22#define _gcem_gcd_HPP
23
24namespace internal
25{
26
27template<typename T>
28constexpr
29T
30gcd_recur(const T a, const T b)
31noexcept
32{
33 return( b == T(0) ? a : gcd_recur(b, a % b) );
34}
35
36template<typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
37constexpr
38T
39gcd_int_check(const T a, const T b)
40noexcept
41{
42 return gcd_recur(a,b);
43}
44
45template<typename T, typename std::enable_if<!std::is_integral<T>::value>::type* = nullptr>
46constexpr
47T
48gcd_int_check(const T a, const T b)
49noexcept
50{
51 return gcd_recur( static_cast<ullint_t>(a), static_cast<ullint_t>(b) );
52}
53
54template<typename T1, typename T2, typename TC = common_t<T1,T2>>
55constexpr
56TC
57gcd_type_check(const T1 a, const T2 b)
58noexcept
59{
60 return gcd_int_check( static_cast<TC>(abs(a)), static_cast<TC>(abs(b)) );
61}
62
63}
64
65/**
66 * Compile-time greatest common divisor (GCD) function
67 *
68 * @param a integral-valued input.
69 * @param b integral-valued input.
70 * @return the greatest common divisor between integers \c a and \c b using a Euclidean algorithm.
71 */
72
73template<typename T1, typename T2>
74constexpr
75common_t<T1,T2>
76gcd(const T1 a, const T2 b)
77noexcept
78{
80}
81
82#endif
type
Definition: core.h:575
constexpr common_t< T1, T2 > gcd(const T1 a, const T2 b) noexcept
Compile-time greatest common divisor (GCD) function.
Definition: gcd.hpp:76
UnitType abs(const UnitType x) noexcept
Compute absolute value.
Definition: math.h:721
unsigned long long int ullint_t
Definition: gcem_options.hpp:69
Definition: Eigen_Colamd.h:50
constexpr T gcd_recur(const T a, const T b) noexcept
Definition: gcd.hpp:30
constexpr TC gcd_type_check(const T1 a, const T2 b) noexcept
Definition: gcd.hpp:57
constexpr T gcd_int_check(const T a, const T b) noexcept
Definition: gcd.hpp:39
b
Definition: data.h:44