Embedded Template Library 1.0
Loading...
Searching...
No Matches
gcd.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2024 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_GCD_INCLUDED
32#define ETL_GCD_INCLUDED
33
34#include "platform.h"
35#include "absolute.h"
36#include "static_assert.h"
37#include "type_traits.h"
38
39namespace etl
40{
41 //***************************************************************************
42 // Greatest Common Divisor.
43 // Compile time.
44 //***************************************************************************
45 template <intmax_t Value1, intmax_t Value2>
46 struct gcd_const
47 {
48 static ETL_CONSTANT intmax_t value = gcd_const<Value2, Value1 % Value2>::value;
49 };
50
51 template <intmax_t Value1>
52 struct gcd_const<Value1, 0>
53 {
54 static ETL_CONSTANT intmax_t value = Value1;
55 };
56
57 //***************************************************************************
58 // Greatest Common Divisor.
59 // For unsigned types.
60 //***************************************************************************
61 template <typename T>
62 ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, T>::type gcd(T a, T b) ETL_NOEXCEPT
63 {
64 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Integral type required");
65
66 if ((a == 0 || b == 0))
67 {
68 return (a + b);
69 }
70
71 while (b != 0)
72 {
73 T t = b;
74 b = a % b;
75 a = t;
76 }
77
78 return a;
79 }
80
81 //***************************************************************************
82 // Greatest Common Divisor.
83 // For signed types.
84 //***************************************************************************
85 template <typename T>
86 ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_signed<T>::value, T>::type gcd(T a, T b) ETL_NOEXCEPT
87 {
88 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Integral type required");
89
90 typedef typename etl::make_unsigned<T>::type utype;
91
92 utype ua = etl::absolute_unsigned(a);
93 utype ub = etl::absolute_unsigned(b);
94
95 return static_cast<T>(gcd(ua, ub));
96 }
97
98#if ETL_USING_CPP11
99 #if ETL_HAS_INITIALIZER_LIST
100 //***************************************************************************
101 // Greatest Common Divisor.
102 // Non-recursive, using an initializer_list.
103 // Top level variadic function.
104 //***************************************************************************
105 template <typename T, typename... TRest>
106 ETL_NODISCARD ETL_CONSTEXPR14 T gcd(T first, TRest... rest) ETL_NOEXCEPT
107 {
108 T result = first;
109
110 for (T value : {rest...})
111 {
112 result = gcd(result, value);
113
114 if (result == 1)
115 {
116 // Early termination: if the GCD is one, it will remain one
117 // no matter what other numbers are processed.
118 return 1;
119 }
120 }
121
122 return result;
123 }
124 #else
125 //***************************************************************************
126 // Greatest Common Divisor.
127 // Recursive.
128 // Top level variadic function.
129 //***************************************************************************
130 template <typename T, typename... TRest>
131 ETL_NODISCARD ETL_CONSTEXPR14 T gcd(T a, T b, TRest... rest) ETL_NOEXCEPT
132 {
133 T gcd_ab = gcd(a, b);
134
135 if (gcd_ab == 1)
136 {
137 // Early termination: if the GCD is one, it will remain one
138 // no matter what other numbers are processed.
139 return 1;
140 }
141 else
142 {
143 return gcd(gcd_ab, rest...);
144 }
145 }
146 #endif
147#endif
148} // namespace etl
149
150#endif
bitset_ext
Definition absolute.h:40
Definition gcd.h:47