Embedded Template Library 1.0
Loading...
Searching...
No Matches
smallest.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_SMALLEST_INCLUDED
32#define ETL_SMALLEST_INCLUDED
33
34#include "platform.h"
35#include "integral_limits.h"
36
37#include <stdint.h>
38
41
42namespace 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>
113
114 //*************************************************************************
115 // Less than or equal to 8 bits.
116 //*************************************************************************
117 template <>
119 {
120 typedef uint_least8_t type;
121 };
122
123 //*************************************************************************
124 // 9 to 16 bits.
125 //*************************************************************************
126 template <>
128 {
129 typedef uint_least16_t type;
130 };
131
132 //*************************************************************************
133 // 17 to 31 bits.
134 //*************************************************************************
135 template <>
137 {
138 typedef uint_least32_t type;
139 };
140
141#if ETL_USING_64BIT_TYPES
142 //*************************************************************************
143 // Greater than 32 bits.
144 //*************************************************************************
145 template <>
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>
157
158 //*************************************************************************
159 // Less than or equal to 8 bits.
160 //*************************************************************************
161 template <>
163 {
164 typedef int_least8_t type;
165 };
166
167 //*************************************************************************
168 // 9 to 16 bits.
169 //*************************************************************************
170 template <>
172 {
173 typedef int_least16_t type;
174 };
175
176 //*************************************************************************
177 // 17 to 31 bits.
178 //*************************************************************************
179 template <>
181 {
182 typedef int_least32_t type;
183 };
184
185#if ETL_USING_64BIT_TYPES
186 //*************************************************************************
187 // Greater than 32 bits.
188 //*************************************************************************
189 template <>
191 {
192 typedef int_least64_t type;
193 };
194#endif
195 } // namespace private_smallest
196
197 //***************************************************************************
202 //***************************************************************************
203 template <size_t NBITS>
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
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>
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
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 <uintmax_t 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
270 };
271
272 template <uintmax_t VALUE>
273 ETL_CONSTANT int smallest_uint_for_value<VALUE>::TYPE_INDEX;
274
275#if ETL_USING_CPP11
276 template <uintmax_t VALUE>
277 using smallest_uint_for_value_t = typename smallest_uint_for_value<VALUE>::type;
278#endif
279
280 //***************************************************************************
285 //***************************************************************************
286 template <intmax_t 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
299 };
300
301 template <intmax_t VALUE>
302 ETL_CONSTANT int smallest_int_for_value<VALUE>::TYPE_INDEX;
303
304#if ETL_USING_CPP11
305 template <intmax_t VALUE>
306 using smallest_int_for_value_t = typename smallest_int_for_value<VALUE>::type;
307#endif
308} // namespace etl
309
310#endif
Definition smallest.h:233
Definition smallest.h:288
Definition smallest.h:46
Definition smallest.h:205
Definition smallest.h:261
bitset_ext
Definition absolute.h:40
ETL_CONSTEXPR TContainer::size_type size(const TContainer &container)
Definition iterator.h:1192