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 "type_traits.h"
36#include "absolute.h"
37#include "static_assert.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
63 ETL_CONSTEXPR14
65 gcd(T a, T b) ETL_NOEXCEPT
66 {
67 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Integral type required");
68
69 if ((a == 0 || b == 0))
70 {
71 return (a + b);
72 }
73
74 while (b != 0)
75 {
76 T t = b;
77 b = a % b;
78 a = t;
79 }
80
81 return a;
82 }
83
84 //***************************************************************************
85 // Greatest Common Divisor.
86 // For signed types.
87 //***************************************************************************
88 template <typename T>
89 ETL_NODISCARD
90 ETL_CONSTEXPR14
91 typename etl::enable_if<etl::is_signed<T>::value, T>::type
92 gcd(T a, T b) ETL_NOEXCEPT
93 {
94 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Integral type required");
95
96 typedef typename etl::make_unsigned<T>::type utype;
97
98 utype ua = etl::absolute_unsigned(a);
99 utype ub = etl::absolute_unsigned(b);
100
101 return static_cast<T>(gcd(ua, ub));
102 }
103
104#if ETL_USING_CPP11
105 #if ETL_HAS_INITIALIZER_LIST
106 //***************************************************************************
107 // Greatest Common Divisor.
108 // Non-recursive, using an initializer_list.
109 // Top level variadic function.
110 //***************************************************************************
111 template<typename T, typename... TRest>
112 ETL_NODISCARD
113 ETL_CONSTEXPR14
114 T gcd(T first, TRest... rest) ETL_NOEXCEPT
115 {
116 T result = first;
117
118 for (T value : {rest...})
119 {
120 result = gcd(result, value);
121
122 if (result == 1)
123 {
124 // Early termination: if the GCD is one, it will remain one
125 // no matter what other numbers are processed.
126 return 1;
127 }
128 }
129
130 return result;
131 }
132 #else
133 //***************************************************************************
134 // Greatest Common Divisor.
135 // Recursive.
136 // Top level variadic function.
137 //***************************************************************************
138 template<typename T, typename... TRest>
139 ETL_NODISCARD
140 ETL_CONSTEXPR14
141 T gcd(T a, T b, TRest... rest) ETL_NOEXCEPT
142 {
143 T gcd_ab = gcd(a, b);
144
145 if (gcd_ab == 1)
146 {
147 // Early termination: if the GCD is one, it will remain one
148 // no matter what other numbers are processed.
149 return 1;
150 }
151 else
152 {
153 return gcd(gcd_ab, rest...);
154 }
155 }
156 #endif
157#endif
158}
159
160#endif
161
enable_if
Definition type_traits_generator.h:1254
bitset_ext
Definition absolute.h:39
Definition gcd.h:47