1#ifndef CONVERSION_UTILITIES_BINARY_CONVERSION_H
2#define CONVERSION_UTILITIES_BINARY_CONVERSION_H
11#if __cplusplus >= 202002L
16#if defined(__BYTE_ORDER__)
17#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
18#define CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN false
19#define CONVERSION_UTILITIES_IS_BYTE_ORDER_BIG_ENDIAN true
20#define CONVERSION_UTILITIES_BYTE_ORDER_BIG_ENDIAN
21#elif __BYTE_ORDER__ == __ORDER_PDP_ENDIAN__
22#define CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN false
23#define CONVERSION_UTILITIES_IS_BYTE_ORDER_BIG_ENDIAN false
24#define CONVERSION_UTILITIES_BYTE_ORDER_MIDDLE_ENDIAN
25#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
26#define CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN true
27#define CONVERSION_UTILITIES_IS_BYTE_ORDER_BIG_ENDIAN false
28#define CONVERSION_UTILITIES_BYTE_ORDER_LITTLE_ENDIAN
33#if defined(__FLOAT_WORD_ORDER__)
34#if __FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__
35#define CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_BIG_ENDIAN
36#elif __FLOAT_WORD_ORDER__ == __ORDER_PDP_ENDIAN__
37#define CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_MIDDLE_ENDIAN
38#elif __FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__
39#define CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_LITTLE_ENDIAN
44#if !defined(__BYTE_ORDER__) || !defined(__FLOAT_WORD_ORDER__)
47#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(_M_X64) \
48 || defined(__LITTLE_ENDIAN__) || defined(_little_endian__) || defined(_LITTLE_ENDIAN) || defined(_WIN32_WCE) || defined(WINAPI_FAMILY)
49#if !defined(__BYTE_ORDER__)
50#define CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN true
51#define CONVERSION_UTILITIES_IS_BYTE_ORDER_BIG_ENDIAN false
52#define CONVERSION_UTILITIES_BYTE_ORDER_LITTLE_ENDIAN
54#if !defined(__FLOAT_WORD_ORDER__)
55#define CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_LITTLE_ENDIAN
59#elif defined(__MIPSEB__) || defined(__s390__) || defined(__BIG_ENDIAN__) || defined(_big_endian__) || defined(_BIG_ENDIAN)
60#if !defined(__BYTE_ORDER__)
61#define CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN false
62#define CONVERSION_UTILITIES_IS_BYTE_ORDER_BIG_ENDIAN true
63#define CONVERSION_UTILITIES_BYTE_ORDER_BIG_ENDIAN
65#if !defined(__FLOAT_WORD_ORDER__)
66#define CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_BIG_ENDIAN
71#error "Unable to determine byte order!"
77#if defined(CONVERSION_UTILITIES_BYTE_ORDER_MIDDLE_ENDIAN) || defined(CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_MIDDLE_ENDIAN)
78#error "Middle endian byte order is not supported!"
88 return static_cast<std::uint16_t
>(float32value * 256.0f);
96 return static_cast<float>(fixed8value) / 256.0f;
104 return static_cast<std::uint32_t
>(float32value * 65536.0f);
112 return static_cast<float>(fixed16value) / 65536.0f;
122 return ((normalInt & 0x0000007fu)) | ((normalInt & 0x00003f80u) << 1) | ((normalInt & 0x001fc000u) << 2) | ((normalInt & 0x0fe00000u) << 3);
132 return ((synchsafeInt & 0x0000007fu)) | ((synchsafeInt & 0x00007f00u) >> 1) | ((synchsafeInt & 0x007f0000u) >> 2)
133 | ((synchsafeInt & 0x7f000000u) >> 3);
137#ifdef __cpp_lib_byteswap
140 return std::byteswap(value);
150 return static_cast<std::uint16_t
>(((value >> 8) & 0x00FF) | ((value << 8) & 0xFF00));
158 return (value >> 24) | ((value & 0x00FF0000) >> 8) | ((value & 0x0000FF00) << 8) | (value << 24);
166 return (value >> (7 * 8)) | ((value & 0x00FF000000000000) >> (5 * 8)) | ((value & 0x0000FF0000000000) >> (3 * 8))
167 | ((value & 0x000000FF00000000) >> (1 * 8)) | ((value & 0x00000000FF000000) << (1 * 8)) | ((value & 0x0000000000FF0000) << (3 * 8))
168 | ((value & 0x000000000000FF00) << (5 * 8)) | ((value) << (7 * 8));
176 return static_cast<std::int16_t
>(((
static_cast<std::uint16_t
>(value) >> 8) & 0x00FF) | ((
static_cast<std::uint16_t
>(value) << 8) & 0xFF00));
184 return static_cast<std::int32_t
>((
static_cast<std::uint32_t
>(value) >> 24) | ((
static_cast<std::uint32_t
>(value) & 0x00FF0000) >> 8)
185 | ((
static_cast<std::uint32_t
>(value) & 0x0000FF00) << 8) | (
static_cast<std::uint32_t
>(value) << 24));
193 return static_cast<std::int64_t
>((
static_cast<std::uint64_t
>(value) >> (7 * 8))
194 | ((
static_cast<std::uint64_t
>(value) & 0x00FF000000000000) >> (5 * 8))
195 | ((
static_cast<std::uint64_t
>(value) & 0x0000FF0000000000) >> (3 * 8))
196 | ((
static_cast<std::uint64_t
>(value) & 0x000000FF00000000) >> (1 * 8))
197 | ((
static_cast<std::uint64_t
>(value) & 0x00000000FF000000) << (1 * 8))
198 | ((
static_cast<std::uint64_t
>(value) & 0x0000000000FF0000) << (3 * 8))
199 | ((
static_cast<std::uint64_t
>(value) & 0x000000000000FF00) << (5 * 8)) | ((
static_cast<std::uint64_t
>(value)) << (7 * 8)));
209#define CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL 0
211#undef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
220#define CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL 1
222#undef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Contains all utilities provides by the c++utilities library.
CPP_UTILITIES_EXPORT constexpr float toFloat32(std::uint16_t fixed8value)
Returns a 32-bit floating point number converted from the specified 8.8 fixed point representation.
CPP_UTILITIES_EXPORT constexpr std::uint32_t toNormalInt(std::uint32_t synchsafeInt)
Returns a normal 32-bit integer converted from a 32-bit synchsafe integer.
CPP_UTILITIES_EXPORT constexpr std::uint16_t swapOrder(std::uint16_t value)
Swaps the byte order of the specified 16-bit unsigned integer.
CPP_UTILITIES_EXPORT constexpr std::uint32_t toFixed16(float float32value)
Returns the 16.16 fixed point representation converted from the specified 32-bit floating point numbe...
CPP_UTILITIES_EXPORT constexpr std::uint16_t toFixed8(float float32value)
Returns the 8.8 fixed point representation converted from the specified 32-bit floating point number.
CPP_UTILITIES_EXPORT constexpr std::uint32_t toSynchsafeInt(std::uint32_t normalInt)
Returns a 32-bit synchsafe integer converted from a normal 32-bit integer.