Elementa v8.0.0
Minimalistic library for any C++ application (C++11 and up)
Loading...
Searching...
No Matches
fundamentaltypes.h
Go to the documentation of this file.
1
3#include "elementa/license.inc"
4#include "elementa/checks.inc"
5
6#ifndef ELEMENTA_BASE_FUNDAMENTALTYPES_H
7#define ELEMENTA_BASE_FUNDAMENTALTYPES_H
8
9#include <cstdint>
10#include <climits>
11#include <cstddef>
12#include <type_traits>
13#include <cmath>
14
15
16namespace elementa
17{
18
19namespace base
20{
21
41/* ====================================================================== */
42
46/* ========================================================================= */
47
48/* *************************************************************************
49
50 Enum class: endian
51
52****************************************************************************/
53
54#if ELE_PRE_CPPVER < 2020
56
74 enum class endian
75 {
76 #ifdef ELE_MAKE_LINUX
77 little = __ORDER_LITTLE_ENDIAN__,
78 big = __ORDER_BIG_ENDIAN__,
79 native = __BYTE_ORDER__
80 #else
81 little = 0,
82 big = 1,
83 native = little
84 #endif
85 };
86#endif
87
88
89/* *************************************************************************
90
91 Types: sizebits_t, sizebytes_t, LongestUnsigned, LongestInt
92
93****************************************************************************/
94
96
102using sizebits_t = unsigned;
103
105
111using sizebytes_t = unsigned;
112
114
118using LongestUnsigned = unsigned long long;
119
121using LongestInt = long long;
122
124using LongestReal = long double;
125
126
127/* *************************************************************************
128
129 Enum: SignSpec
130
131****************************************************************************/
132
134enum class SignSpec: uint8_t { kSigned = 0, kUnsigned };
135
136
137/* *************************************************************************
138
139 Const expression: kMaxBitsInt
140
141****************************************************************************/
142
144
149#if ULLONG_MAX > 65535LLU
150 #if ULLONG_MAX > 4294967295LLU
151 constexpr sizebits_t kMaxBitsInt = 64;
152 #else
153 constexpr sizebits_t kMaxBitsInt = 32;
154 #endif
155#else
156 constexpr sizebits_t kMaxBitsInt = 16;
157#endif
158
159
160/* *************************************************************************
161
162 Const expression: kLeastSizeForBits
163
164****************************************************************************/
165
167
172{
173 return( (nbs <= 8 ? 8:
174 (nbs <= 16 ? 16 :
175 (nbs <= 32 ? 32 :
176 (nbs <= 64 ? 64 : 0)
177 ))));
178}
179
180
181/* *************************************************************************
182
183 Template: ExactInteger
184
185****************************************************************************/
186
188
190template <sizebits_t NBITS, SignSpec SIGN>
192 typename std::conditional< ((NBITS == 8) && (SIGN==SignSpec::kSigned)),
193 int8_t,
194 typename std::conditional< ((NBITS == 8) && (SIGN==SignSpec::kUnsigned)),
195 uint8_t,
196 typename std::conditional< ((NBITS == 16) && (SIGN==SignSpec::kSigned)),
197 int16_t,
198 typename std::conditional< ((NBITS == 16) && (SIGN==SignSpec::kUnsigned)),
199 uint16_t,
200 typename std::conditional< ((NBITS == 32) && (SIGN==SignSpec::kSigned) &&
201 (kMaxBitsInt > 16)),
202 int32_t,
203 typename std::conditional< ((NBITS == 32) && (SIGN==SignSpec::kUnsigned) &&
204 (kMaxBitsInt > 16)),
205 uint32_t,
206 typename std::conditional< ((NBITS == 64) && (SIGN==SignSpec::kSigned) &&
207 (kMaxBitsInt > 32)),
208 int64_t,
209 typename std::conditional< ((NBITS == 64) && (SIGN==SignSpec::kUnsigned) &&
210 (kMaxBitsInt > 32)),
211 uint64_t,
212 void >::type >::type >::type >::type >::type >::type >::type >::type;
213
214
215/* *************************************************************************
216
217 Template const expression: kNBitsSet
218
219**************************************************************************/
220
222
225template <typename I>
226constexpr I kNBitsSet(sizebits_t n)
227{
228 return(static_cast<I>( (static_cast<I>(1) << n) - 1));
229}
230
231/* *************************************************************************
232
233 Function: len_for_bits
234
235******************************************************************************/
236
238uint8_t len_for_bits(uint8_t nbits);
239
240/* *************************************************************************
241
242 Template function: count_bits
243
244**************************************************************************/
245
247template <typename T>
249{
250 sizebits_t n = 0;
251 for (sizebits_t f = 0; f < sizeof(T)*CHAR_BIT; ++f)
252 {
253 n += static_cast<sizebits_t>((v & 1) != 0);
254 v >>= 1;
255 }
256 return(n);
257}
258
259/* *************************************************************************
260
261 Template function: most_significant_bit
262
263**************************************************************************/
264
266template <typename T, sizebits_t N = sizeof(T)*CHAR_BIT>
268{
269 T mask = static_cast<T>(1) << (N - 1);
270 sizebits_t res = N - 1;
271 for (sizebits_t f = 0; f < N; ++f)
272 {
273 if ((b & mask) != 0) return(res);
274 --res;
275 mask >>= 1;
276 }
277 return(N);
278}
279
280/* *************************************************************************
281
282 Template const expression: kPtrPlusBytes
283 Functions: to_number
284
285**************************************************************************/
286
288
289template <typename PT>
290const PT * kPtrPlusBytes(const PT * p, size_t boff)
291{
292 return(reinterpret_cast<const PT *>(
293 reinterpret_cast<const char *>(p) + boff));
294}
295
298
300int to_number(char n);
301
303unsigned to_number(unsigned char n);
304
305
306 // Integral types extensions
308
309 // Fundamental types
311
312
313} // end namespace base
314
315} // end namespace elementa
316
317#endif
typename std::conditional<((NBITS==8) &&(SIGN==SignSpec::kSigned)), int8_t, typename std::conditional<((NBITS==8) &&(SIGN==SignSpec::kUnsigned)), uint8_t, typename std::conditional<((NBITS==16) &&(SIGN==SignSpec::kSigned)), int16_t, typename std::conditional<((NBITS==16) &&(SIGN==SignSpec::kUnsigned)), uint16_t, typename std::conditional<((NBITS==32) &&(SIGN==SignSpec::kSigned) &&(kMaxBitsInt > 16)), int32_t, typename std::conditional<((NBITS==32) &&(SIGN==SignSpec::kUnsigned) &&(kMaxBitsInt > 16)), uint32_t, typename std::conditional<((NBITS==64) &&(SIGN==SignSpec::kSigned) &&(kMaxBitsInt > 32)), int64_t, typename std::conditional<((NBITS==64) &&(SIGN==SignSpec::kUnsigned) &&(kMaxBitsInt > 32)), uint64_t, void >::type >::type >::type >::type >::type >::type >::type >::type ExactInteger
To get an int. type with given characteristics of number of bits and sign.
LongestUnsigned to_number(const void *p)
Convert a pointer address to a number.
long long LongestInt
Longest signed integral type in the target machine.
endian
Endianness of the target machine, as defined in the C++20 standard.
unsigned sizebytes_t
Type able to index all bytes in the longest int. type in the target machine.
const PT * kPtrPlusBytes(const PT *p, size_t boff)
Add a number of bytes to the address of a pointer without changing its type.
unsigned long long LongestUnsigned
Longest unsigned integral type in the target machine.
sizebits_t count_bits(T v)
Count the number of bits that are set in an integral value.
constexpr I kNBitsSet(sizebits_t n)
Compute an integral constant with its N least-significant bits set.
long double LongestReal
Longest real type.
uint8_t len_for_bits(uint8_t nbits)
Maximum number of digits needed for the decimal form of NBITS binary numbers.
sizebits_t most_significant_bit(T b)
Return the index of the MSb set in B, or the number of bits in T if none.
unsigned sizebits_t
Type able to index all bits in the longest int. type in the target machine.
constexpr sizebits_t kMaxBitsInt
Maximum number of bits in an integer type in the target machine.
constexpr sizebits_t kLeastSizeForBits(sizebits_t nbs)
Shortest integral type for holding a number of bits in the target machine.
SignSpec
Defines the possible sign of a fundamental (integral) type.