Embedded Template Library
1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
smallest.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_SMALLEST_INCLUDED
32
#define ETL_SMALLEST_INCLUDED
33
34
#include "
platform.h
"
35
#include "
integral_limits.h
"
36
37
#include <stdint.h>
38
41
42
namespace
etl
43
{
44
#if ETL_USING_CPP11 && !defined(ETL_SMALLEST_TYPE_FORCE_CPP03_IMPLEMENTATION)
45
//***************************************************************************
50
//***************************************************************************
51
template
<
typename
T1,
typename
... TRest>
52
class
smallest_type
53
{
54
private
:
55
56
// Define 'smallest_other' as 'smallest_type' with all but the first
57
// parameter.
58
using
smallest_other =
typename
smallest_type
<TRest...>::type;
59
60
public
:
61
62
// Set 'type' to be the smallest of the first parameter and any of the
63
// others. This is recursive.
64
using
type =
typename
etl::conditional<(etl::size_of<T1>::value < etl::size_of< smallest_other>::value),
// Boolean
65
T1,
// TrueType
66
smallest_other>
// FalseType
67
::type;
// The smallest type of the two.
68
69
// The size of the smallest type.
70
enum
71
{
72
size = etl::size_of<type>::value
73
};
74
};
75
76
//***************************************************************************
77
// Specialisation for one template parameter.
78
//***************************************************************************
79
template
<
typename
T1>
80
class
smallest_type
<T1>
81
{
82
public
:
83
84
using
type
= T1;
85
86
enum
87
{
88
size
= etl::size_of<type>::value
89
};
90
};
91
92
#if ETL_USING_CPP11
93
template
<
typename
... T>
94
using
smallest_type_t =
typename
smallest_type
<T...>::type;
95
#endif
96
97
#if ETL_USING_CPP17
98
template
<
typename
... T>
99
constexpr
size_t
smallest_type_v =
smallest_type
<T...>::size;
100
#endif
101
102
#else
103
#include "private/smallest_cpp03.h"
104
#endif
105
106
namespace
private_smallest
107
{
108
//*************************************************************************
109
// Determine the type to hold the number of bits based on the index.
110
//*************************************************************************
111
template
<
int
index>
112
struct
best_fit_uint_type
;
113
114
//*************************************************************************
115
// Less than or equal to 8 bits.
116
//*************************************************************************
117
template
<>
118
struct
best_fit_uint_type
<0>
119
{
120
typedef
uint_least8_t type;
121
};
122
123
//*************************************************************************
124
// 9 to 16 bits.
125
//*************************************************************************
126
template
<>
127
struct
best_fit_uint_type
<1>
128
{
129
typedef
uint_least16_t type;
130
};
131
132
//*************************************************************************
133
// 17 to 31 bits.
134
//*************************************************************************
135
template
<>
136
struct
best_fit_uint_type
<2>
137
{
138
typedef
uint_least32_t type;
139
};
140
141
#if ETL_USING_64BIT_TYPES
142
//*************************************************************************
143
// Greater than 32 bits.
144
//*************************************************************************
145
template
<>
146
struct
best_fit_uint_type
<3>
147
{
148
typedef
uint_least64_t type;
149
};
150
#endif
151
152
//*************************************************************************
153
// Determine the type to hold the number of bits based on the index.
154
//*************************************************************************
155
template
<
int
index>
156
struct
best_fit_int_type
;
157
158
//*************************************************************************
159
// Less than or equal to 8 bits.
160
//*************************************************************************
161
template
<>
162
struct
best_fit_int_type
<0>
163
{
164
typedef
int_least8_t type;
165
};
166
167
//*************************************************************************
168
// 9 to 16 bits.
169
//*************************************************************************
170
template
<>
171
struct
best_fit_int_type
<1>
172
{
173
typedef
int_least16_t type;
174
};
175
176
//*************************************************************************
177
// 17 to 31 bits.
178
//*************************************************************************
179
template
<>
180
struct
best_fit_int_type
<2>
181
{
182
typedef
int_least32_t type;
183
};
184
185
#if ETL_USING_64BIT_TYPES
186
//*************************************************************************
187
// Greater than 32 bits.
188
//*************************************************************************
189
template
<>
190
struct
best_fit_int_type
<3>
191
{
192
typedef
int_least64_t type;
193
};
194
#endif
195
}
// namespace private_smallest
196
197
//***************************************************************************
202
//***************************************************************************
203
template
<
size_t
NBITS>
204
struct
smallest_uint_for_bits
205
{
206
private
:
207
208
// Determines the index of the best unsigned type for the required number of
209
// bits.
210
static
ETL_CONSTANT
int
TYPE_INDEX = ((NBITS > 8) ? 1 : 0) + ((NBITS > 16) ? 1 : 0) + ((NBITS > 32) ? 1 : 0);
211
212
public
:
213
214
typedef
typename
private_smallest::best_fit_uint_type<TYPE_INDEX>::type
type;
215
};
216
217
template
<
size_t
NBITS>
218
ETL_CONSTANT
int
smallest_uint_for_bits<NBITS>::TYPE_INDEX;
219
220
#if ETL_USING_CPP11
221
template
<
size_t
NBITS>
222
using
smallest_uint_for_bits_t =
typename
smallest_uint_for_bits<NBITS>::type;
223
#endif
224
225
//***************************************************************************
230
//***************************************************************************
231
template
<
size_t
NBITS>
232
struct
smallest_int_for_bits
233
{
234
private
:
235
236
// Determines the index of the best unsigned type for the required number of
237
// bits.
238
static
ETL_CONSTANT
int
TYPE_INDEX = ((NBITS > 8) ? 1 : 0) + ((NBITS > 16) ? 1 : 0) + ((NBITS > 32) ? 1 : 0);
239
240
public
:
241
242
typedef
typename
private_smallest::best_fit_int_type<TYPE_INDEX>::type
type;
243
};
244
245
template
<
size_t
NBITS>
246
ETL_CONSTANT
int
smallest_int_for_bits<NBITS>::TYPE_INDEX;
247
248
#if ETL_USING_CPP11
249
template
<
size_t
NBITS>
250
using
smallest_int_for_bits_t =
typename
smallest_int_for_bits<NBITS>::type;
251
#endif
252
253
//***************************************************************************
258
//***************************************************************************
259
template
<u
int
max_t VALUE>
260
struct
smallest_uint_for_value
261
{
262
private
:
263
264
// Determines the index of the best unsigned type for the required value.
265
static
ETL_CONSTANT
int
TYPE_INDEX = ((VALUE > UINT_LEAST8_MAX) ? 1 : 0) + ((VALUE > UINT16_MAX) ? 1 : 0) + ((VALUE > UINT32_MAX) ? 1 : 0);
266
267
public
:
268
269
typedef
typename
private_smallest::best_fit_uint_type<TYPE_INDEX>::type
type;
270
};
271
272
template
<u
int
max_t VALUE>
273
ETL_CONSTANT
int
smallest_uint_for_value<VALUE>::TYPE_INDEX;
274
275
#if ETL_USING_CPP11
276
template
<u
int
max_t VALUE>
277
using
smallest_uint_for_value_t =
typename
smallest_uint_for_value<VALUE>::type;
278
#endif
279
280
//***************************************************************************
285
//***************************************************************************
286
template
<
int
max_t VALUE>
287
struct
smallest_int_for_value
288
{
289
private
:
290
291
// Determines the index of the best signed type for the required value.
292
static
ETL_CONSTANT
int
TYPE_INDEX = (((VALUE > intmax_t(INT_LEAST8_MAX)) || (VALUE < intmax_t(INT_LEAST8_MIN))) ? 1 : 0)
293
+ (((VALUE > intmax_t(INT16_MAX)) || (VALUE < intmax_t(INT16_MIN))) ? 1 : 0)
294
+ (((VALUE > intmax_t(INT32_MAX)) || (VALUE < intmax_t(INT32_MIN))) ? 1 : 0);
295
296
public
:
297
298
typedef
typename
private_smallest::best_fit_int_type<TYPE_INDEX>::type
type;
299
};
300
301
template
<
int
max_t VALUE>
302
ETL_CONSTANT
int
smallest_int_for_value<VALUE>::TYPE_INDEX;
303
304
#if ETL_USING_CPP11
305
template
<
int
max_t VALUE>
306
using
smallest_int_for_value_t =
typename
smallest_int_for_value<VALUE>::type;
307
#endif
308
}
// namespace etl
309
310
#endif
etl::smallest_int_for_bits
Definition
smallest.h:233
etl::smallest_int_for_value
Definition
smallest.h:288
etl::smallest_type
Definition
smallest.h:46
etl::smallest_uint_for_bits
Definition
smallest.h:205
etl::smallest_uint_for_value
Definition
smallest.h:261
integral_limits.h
etl
Definition
absolute.h:40
etl::size
ETL_CONSTEXPR TContainer::size_type size(const TContainer &container)
Definition
iterator.h:1434
platform.h
etl::type
etl::private_smallest::best_fit_int_type
Definition
smallest.h:156
etl::private_smallest::best_fit_uint_type
Definition
smallest.h:112
include
etl
smallest.h
Generated on
for Embedded Template Library by
1.17.0