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_GDC_INCLUDED
32#define ETL_GDC_INCLUDED
33
34#include "type_traits.h"
35#include "absolute.h"
36#include "static_assert.h"
37
38namespace etl
39{
40 //***************************************************************************
41 // Greatest Common Divisor.
42 // Compile time.
43 //***************************************************************************
44 template <intmax_t A, intmax_t B>
45 struct gcd_const
46 {
47 static ETL_CONSTANT intmax_t value = gcd_const<B, A % B>::value;
48 };
49
50 template <intmax_t A>
51 struct gcd_const<A, 0>
52 {
53 static ETL_CONSTANT intmax_t value = A;
54 };
55
56 //***************************************************************************
57 // Greatest Common Divisor.
58 // For unsigned types.
59 //***************************************************************************
60 template <typename T>
61 ETL_NODISCARD
62 ETL_CONSTEXPR14
64 gcd(T a, T b) ETL_NOEXCEPT
65 {
66 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Integral type required");
67
68 if ((a == 0 || b == 0))
69 {
70 return (a + b);
71 }
72
73 while (b != 0)
74 {
75 T t = b;
76 b = a % b;
77 a = t;
78 }
79
80 return a;
81 }
82
83 //***************************************************************************
84 // Greatest Common Divisor.
85 // For signed types.
86 //***************************************************************************
87 template <typename T>
88 ETL_NODISCARD
89 ETL_CONSTEXPR14
91 gcd(T a, T b) ETL_NOEXCEPT
92 {
93 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Integral type required");
94
95 typedef typename etl::make_unsigned<T>::type utype;
96
97 utype ua = etl::absolute_unsigned(a);
98 utype ub = etl::absolute_unsigned(b);
99
100 return static_cast<T>(gcd(ua, ub));
101 }
102
103#if ETL_USING_CPP11
104 #if ETL_HAS_INITIALIZER_LIST
105 //***************************************************************************
106 // Greatest Common Divisor.
107 // Non-recursive, using an initializer_list.
108 // Top level variadic function.
109 //***************************************************************************
110 template<typename T, typename... TRest>
111 ETL_NODISCARD
112 ETL_CONSTEXPR14
113 T gcd(T first, TRest... rest) ETL_NOEXCEPT
114 {
115 T result = first;
116
117 for (T value : {rest...})
118 {
119 result = gcd(result, value);
120
121 if (result == 1)
122 {
123 // Early termination: if the GCD is one, it will remain one
124 // no matter what other numbers are processed.
125 return 1;
126 }
127 }
128
129 return result;
130 }
131 #else
132 //***************************************************************************
133 // Greatest Common Divisor.
134 // Recursive.
135 // Top level variadic function.
136 //***************************************************************************
137 template<typename T, typename... TRest>
138 ETL_NODISCARD
139 ETL_CONSTEXPR14
140 T gcd(T a, T b, TRest... rest) ETL_NOEXCEPT
141 {
142 T gcd_ab = gcd(a, b);
143
144 if (gcd_ab == 1)
145 {
146 // Early termination: if the GCD is one, it will remain one
147 // no matter what other numbers are processed.
148 return 1;
149 }
150 else
151 {
152 return gcd(gcd_ab, rest...);
153 }
154 }
155 #endif
156#endif
157}
158
159#endif
160
enable_if
Definition type_traits_generator.h:1230
is_integral
Definition type_traits_generator.h:1040
make_unsigned
Definition type_traits_generator.h:1220
bitset_ext
Definition absolute.h:38
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_even_unscaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:314
Definition gcd.h:46