3#include "elementa/license.inc"
4#include "elementa/checks.inc"
6#ifndef ELEMENTA_BASE_FUNDAMENTALTYPES_H
7#define ELEMENTA_BASE_FUNDAMENTALTYPES_H
54#if ELE_PRE_CPPVER < 2020
77 little = __ORDER_LITTLE_ENDIAN__,
78 big = __ORDER_BIG_ENDIAN__,
79 native = __BYTE_ORDER__
134enum class SignSpec: uint8_t { kSigned = 0, kUnsigned };
149#if ULLONG_MAX > 65535LLU
150 #if ULLONG_MAX > 4294967295LLU
173 return( (nbs <= 8 ? 8:
190template <sizebits_t NBITS, SignSpec SIGN>
192 typename std::conditional< ((NBITS == 8) && (SIGN==SignSpec::kSigned)),
194 typename std::conditional< ((NBITS == 8) && (SIGN==SignSpec::kUnsigned)),
196 typename std::conditional< ((NBITS == 16) && (SIGN==SignSpec::kSigned)),
198 typename std::conditional< ((NBITS == 16) && (SIGN==SignSpec::kUnsigned)),
200 typename std::conditional< ((NBITS == 32) && (SIGN==SignSpec::kSigned) &&
203 typename std::conditional< ((NBITS == 32) && (SIGN==SignSpec::kUnsigned) &&
206 typename std::conditional< ((NBITS == 64) && (SIGN==SignSpec::kSigned) &&
209 typename std::conditional< ((NBITS == 64) && (SIGN==SignSpec::kUnsigned) &&
212 void >::type >::type >::type >::type >::type >::type >::type >::type;
228 return(
static_cast<I
>( (
static_cast<I
>(1) << n) - 1));
251 for (
sizebits_t f = 0; f <
sizeof(T)*CHAR_BIT; ++f)
266template <
typename T, sizebits_t N = sizeof(T)*CHAR_BIT>
269 T mask =
static_cast<T
>(1) << (N - 1);
273 if ((b & mask) != 0)
return(res);
289template <
typename PT>
292 return(
reinterpret_cast<const PT *
>(
293 reinterpret_cast<const char *
>(p) + boff));
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.