Embedded Template Library 1.0
power.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) 2014 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_POW_INCLUDED
32#define ETL_POW_INCLUDED
33
34#include "platform.h"
35#include "log.h"
36
37#include <stddef.h>
38#include <stdint.h>
39
43
44namespace etl
45{
46 namespace private_power
47 {
48#if ETL_NOT_USING_64BIT_TYPES
49 typedef uint32_t type;
50#else
51 typedef uint64_t type;
52#endif
53 }
54
55 //***************************************************************************
59 //***************************************************************************
60 template <size_t NV, size_t POWER>
61 struct power
62 {
63 static ETL_CONSTANT private_power::type value = NV * power<NV, POWER - 1>::value;
64 };
65
66 template <size_t NV, size_t POWER>
67 ETL_CONSTANT private_power::type power<NV, POWER>::value;
68
69 //***************************************************************************
73 //***************************************************************************
74 template <size_t NV>
75 struct power<NV, 0>
76 {
77 static ETL_CONSTANT private_power::type value = 1;
78 };
79
80 template <size_t NV>
81 ETL_CONSTANT private_power::type power<NV, 0>::value;
82
83#if ETL_USING_CPP17
84 template <size_t NV, size_t POWER>
85 inline constexpr size_t power_v = power<NV, POWER>::value;
86#endif
87
88 //***************************************************************************
91 //***************************************************************************
92 template <size_t NV>
94 {
95 enum value_type
96 {
97 value = 1 << (etl::log2<NV - 1>::value + 1)
98 };
99 };
100
101 //***************************************************************************
105 //***************************************************************************
106 template <>
108 {
109 enum value_type
110 {
111 value = 2
112 };
113 };
114
115#if ETL_USING_CPP17
116 template <size_t NV>
117 inline constexpr size_t power_of_2_round_up_v = power_of_2_round_up<NV>::value;
118#endif
119
120 //***************************************************************************
123 //***************************************************************************
124 template <size_t NV>
126 {
127 enum value_type
128 {
129 value = 1 << (etl::log2<NV - 1>::value)
130 };
131 };
132
133 //***************************************************************************
137 //***************************************************************************
138 template <>
140 {
141 enum value_type
142 {
143 value = 2
144 };
145 };
146
147 //***************************************************************************
151 //***************************************************************************
152 template <>
154 {
155 enum value_type
156 {
157 value = 2
158 };
159 };
160
161 //***************************************************************************
165 //***************************************************************************
166 template <>
168 {
169 enum value_type
170 {
171 value = 2
172 };
173 };
174
175#if ETL_USING_CPP17
176 template <size_t NV>
177 inline constexpr size_t power_of_2_round_down_v = power_of_2_round_down<NV>::value;
178#endif
179
180 //***************************************************************************
183 //***************************************************************************
184 template <size_t NV>
186 {
187 static ETL_CONSTANT bool value = (NV & (NV - 1)) == 0;
188 };
189
190 //***************************************************************************
194 //***************************************************************************
195 template <>
197 {
198 static ETL_CONSTANT bool value = false;
199 };
200
201 //***************************************************************************
205 //***************************************************************************
206 template <>
208 {
209 static ETL_CONSTANT bool value = false;
210 };
211
212 template <size_t NV>
213 ETL_CONSTANT bool is_power_of_2<NV>::value;
214
215#if ETL_USING_CPP17
216 template <size_t NV>
217 inline constexpr size_t is_power_of_2_v = is_power_of_2<NV>::value;
218#endif
219}
220
221#endif
Definition: log.h:99
Definition: power.h:186
Definition: power.h:62
Definition: power.h:126
Definition: power.h:94
bitset_ext
Definition: absolute.h:38