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