Embedded Template Library
1.0
Loading...
Searching...
No Matches
power.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) 2014 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_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
44
namespace
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>
93
struct
power_of_2_round_up
94
{
95
enum
value_type
96
{
97
value = 1 << (
etl::log2
<
NV
- 1>::value + 1)
98
};
99
};
100
101
//***************************************************************************
105
//***************************************************************************
106
template
<>
107
struct
power_of_2_round_up
<0>
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>
125
struct
power_of_2_round_down
126
{
127
enum
value_type
128
{
129
value = 1 << (
etl::log2
<
NV
- 1>::value)
130
};
131
};
132
133
//***************************************************************************
137
//***************************************************************************
138
template
<>
139
struct
power_of_2_round_down
<0>
140
{
141
enum
value_type
142
{
143
value = 2
144
};
145
};
146
147
//***************************************************************************
151
//***************************************************************************
152
template
<>
153
struct
power_of_2_round_down
<1>
154
{
155
enum
value_type
156
{
157
value = 2
158
};
159
};
160
161
//***************************************************************************
165
//***************************************************************************
166
template
<>
167
struct
power_of_2_round_down
<2>
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>
185
struct
is_power_of_2
186
{
187
static
ETL_CONSTANT
bool
value = (
NV
& (
NV
- 1)) == 0;
188
};
189
190
//***************************************************************************
194
//***************************************************************************
195
template
<>
196
struct
is_power_of_2
<0>
197
{
198
static
ETL_CONSTANT
bool
value =
false
;
199
};
200
201
//***************************************************************************
205
//***************************************************************************
206
template
<>
207
struct
is_power_of_2
<1>
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
etl::log2
Definition
log.h:99
etl::is_power_of_2
Definition
power.h:186
etl::power
Definition
power.h:62
etl::power_of_2_round_down
Definition
power.h:126
etl::power_of_2_round_up
Definition
power.h:94
log.h
etl
bitset_ext
Definition
absolute.h:38
platform.h
etl::pair
pair holds two objects of arbitrary type
Definition
utility.h:164
include
etl
power.h
Generated on Sat Jan 11 2025 23:21:45 for Embedded Template Library by
1.9.8