Embedded Template Library 1.0
Loading...
Searching...
No Matches
lcm.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_LCM_INCLUDED
32#define ETL_LCM_INCLUDED
33
34#include "platform.h"
35#include "type_traits.h"
36#include "absolute.h"
37#include "static_assert.h"
38#include "gcd.h"
39
40namespace etl
41{
42 //***************************************************************************
43 // Least Common Multiple.
44 // Compile time.
45 //***************************************************************************
46 template <intmax_t Value1, intmax_t Value2>
47 struct lcm_const
48 {
49 static ETL_CONSTANT intmax_t value = (Value1 / gcd_const<Value1, Value2>::value) * Value2;
50 };
51
52 //***************************************************************************
53 // Least Common Multiple.
54 // For unsigned types.
55 //***************************************************************************
56 template <typename T>
57 ETL_NODISCARD
58 ETL_CONSTEXPR14
60 lcm(T a, T b) ETL_NOEXCEPT
61 {
62 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Integral type required");
63
64 // Early termination: if either number is zero, the LCM is zero.
65 if (a == 0 || b == 0)
66 {
67 return 0;
68 }
69 else
70 {
71 return a * (b / gcd(a, b));
72 }
73 }
74
75 //***************************************************************************
76 // Least Common Multiple.
77 // For signed types.
78 //***************************************************************************
79 template <typename T>
80 ETL_NODISCARD
81 ETL_CONSTEXPR14
82 typename etl::enable_if<etl::is_signed<T>::value, T>::type
83 lcm(T a, T b) ETL_NOEXCEPT
84 {
85 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Integral type required");
86
87 typedef typename etl::make_unsigned<T>::type utype;
88
89 utype ua = etl::absolute_unsigned(a);
90 utype ub = etl::absolute_unsigned(b);
91
92 return static_cast<T>(lcm(ua, ub));
93 }
94
95#if ETL_USING_CPP11
96 #if ETL_HAS_INITIALIZER_LIST
97 //***************************************************************************
98 // Least Common Multiple.
99 // Non-recursive, using an initializer_list.
100 // Top level variadic function.
101 //***************************************************************************
102 template<typename T, typename... TRest>
103 ETL_NODISCARD
104 ETL_CONSTEXPR14
105 T lcm(T first, TRest... rest) ETL_NOEXCEPT
106 {
107 T result = first;
108
109 for (T value : {rest...})
110 {
111 result = lcm(result, value);
112
113 if (result == 0)
114 {
115 // Early termination: if the LCM is zero, it will remain zero
116 // no matter what other numbers are processed.
117 return 0;
118 }
119 }
120
121 return result;
122 }
123 #else
124 //***************************************************************************
125 // Least Common Multiple.
126 // Recursive
127 // Top level variadic function.
128 //***************************************************************************
129 template<typename T, typename... TRest>
130 ETL_NODISCARD
131 ETL_CONSTEXPR14
132 T lcm(T a, T b, TRest... rest) ETL_NOEXCEPT
133 {
134 T lcm_ab = lcm(a, b);
135
136 if (lcm_ab == 0)
137 {
138 // Early termination: if the LCM is zero, it will remain zero
139 // no matter what other numbers are processed.
140 return 0;
141 }
142 else
143 {
144 return lcm(lcm_ab, rest...);
145 }
146 }
147 #endif
148#endif
149}
150
151#endif
152
enable_if
Definition type_traits_generator.h:1254
bitset_ext
Definition absolute.h:39
Definition lcm.h:48