Embedded Template Library
1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
gcd.h
Go to the documentation of this file.
1
2
3
/******************************************************************************
4
The MIT License(MIT)
5
6
Embedded Template Library.
7
https://github.com/ETLCPP/etl
8
https://www.etlcpp.com
9
10
Copyright(c) 2024 John Wellbelove
11
12
Permission is hereby granted, free of charge, to any person obtaining a copy
13
of this software and associated documentation files(the "Software"), to deal
14
in the Software without restriction, including without limitation the rights
15
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16
copies of the Software, and to permit persons to whom the Software is
17
furnished to do so, subject to the following conditions :
18
19
The above copyright notice and this permission notice shall be included in all
20
copies or substantial portions of the Software.
21
22
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
SOFTWARE.
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
39
namespace
etl
40
{
41
//***************************************************************************
42
// Greatest Common Divisor.
43
// Compile time.
44
//***************************************************************************
45
template
<
int
max_t Value1,
int
max_t Value2>
46
struct
gcd_const
47
{
48
static
ETL_CONSTANT intmax_t value = gcd_const<Value2, Value1 % Value2>::value;
49
};
50
51
template
<
int
max_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
absolute.h
etl
Definition
absolute.h:40
platform.h
etl::gcd_const
Definition
gcd.h:47
etl::type
type_traits.h
include
etl
gcd.h
Generated on
for Embedded Template Library by
1.17.0