C++ Utilities 5.11.1
Useful C++ classes and routines such as argument parser, IO and conversion utilities
binaryconversion.h
Go to the documentation of this file.
1#ifndef CONVERSION_UTILITIES_BINARY_CONVERSION_H
2#define CONVERSION_UTILITIES_BINARY_CONVERSION_H
3
4#include "../global.h"
5
6#include <cstdint>
7
8// detect byte order according to __BYTE_ORDER__
9#if defined(__BYTE_ORDER__)
10#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
11#define CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN false
12#define CONVERSION_UTILITIES_IS_BYTE_ORDER_BIG_ENDIAN true
13#define CONVERSION_UTILITIES_BYTE_ORDER_BIG_ENDIAN
14#elif __BYTE_ORDER__ == __ORDER_PDP_ENDIAN__
15#define CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN false
16#define CONVERSION_UTILITIES_IS_BYTE_ORDER_BIG_ENDIAN false
17#define CONVERSION_UTILITIES_BYTE_ORDER_MIDDLE_ENDIAN
18#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
19#define CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN true
20#define CONVERSION_UTILITIES_IS_BYTE_ORDER_BIG_ENDIAN false
21#define CONVERSION_UTILITIES_BYTE_ORDER_LITTLE_ENDIAN
22#endif
23#endif // defined(__BYTE_ORDER__)
24
25// detect float byte order according to __FLOAT_WORD_ORDER__
26#if defined(__FLOAT_WORD_ORDER__)
27#if __FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__
28#define CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_BIG_ENDIAN
29#elif __FLOAT_WORD_ORDER__ == __ORDER_PDP_ENDIAN__
30#define CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_MIDDLE_ENDIAN
31#elif __FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__
32#define CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_LITTLE_ENDIAN
33#endif
34#endif // defined(__FLOAT_WORD_ORDER__)
35
36// detect (float) byte order according to other macros
37#if !defined(__BYTE_ORDER__) || !defined(__FLOAT_WORD_ORDER__)
38
39// assume little endian from the presence of several macros
40#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(_M_X64) \
41 || defined(__LITTLE_ENDIAN__) || defined(_little_endian__) || defined(_LITTLE_ENDIAN) || defined(_WIN32_WCE) || defined(WINAPI_FAMILY)
42#if !defined(__BYTE_ORDER__)
43#define CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN true
44#define CONVERSION_UTILITIES_IS_BYTE_ORDER_BIG_ENDIAN false
45#define CONVERSION_UTILITIES_BYTE_ORDER_LITTLE_ENDIAN
46#endif
47#if !defined(__FLOAT_WORD_ORDER__)
48#define CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_LITTLE_ENDIAN
49#endif
50
51// assume big endian from the presence of several macros
52#elif defined(__MIPSEB__) || defined(__s390__) || defined(__BIG_ENDIAN__) || defined(_big_endian__) || defined(_BIG_ENDIAN)
53#if !defined(__BYTE_ORDER__)
54#define CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN false
55#define CONVERSION_UTILITIES_IS_BYTE_ORDER_BIG_ENDIAN true
56#define CONVERSION_UTILITIES_BYTE_ORDER_BIG_ENDIAN
57#endif
58#if !defined(__FLOAT_WORD_ORDER__)
59#define CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_BIG_ENDIAN
60#endif
61
62// fail if unable to detect endianness
63#else
64#error "Unable to determine byte order!"
65#endif
66
67#endif // !defined(__BYTE_ORDER__) || !defined(__FLOAT_WORD_ORDER__)
68
69// fail if middle endian detected
70#if defined(CONVERSION_UTILITIES_BYTE_ORDER_MIDDLE_ENDIAN) || defined(CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_MIDDLE_ENDIAN)
71#error "Middle endian byte order is not supported!"
72#endif
73
74namespace CppUtilities {
75
80namespace BE {
81
82#define CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL 0
84#undef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
85} // namespace BE
86
91namespace LE {
92
93#define CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL 1
95#undef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
96} // namespace LE
97
101CPP_UTILITIES_EXPORT constexpr std::uint16_t toFixed8(float float32value)
102{
103 return static_cast<std::uint16_t>(float32value * 256.0f);
104}
105
109CPP_UTILITIES_EXPORT constexpr float toFloat32(std::uint16_t fixed8value)
110{
111 return static_cast<float>(fixed8value) / 256.0f;
112}
113
117CPP_UTILITIES_EXPORT constexpr std::uint32_t toFixed16(float float32value)
118{
119 return static_cast<std::uint32_t>(float32value * 65536.0f);
120}
121
125CPP_UTILITIES_EXPORT constexpr float toFloat32(std::uint32_t fixed16value)
126{
127 return static_cast<float>(fixed16value) / 65536.0f;
128}
129
135CPP_UTILITIES_EXPORT constexpr std::uint32_t toSynchsafeInt(std::uint32_t normalInt)
136{
137 return ((normalInt & 0x0000007fu)) | ((normalInt & 0x00003f80u) << 1) | ((normalInt & 0x001fc000u) << 2) | ((normalInt & 0x0fe00000u) << 3);
138}
139
145CPP_UTILITIES_EXPORT constexpr std::uint32_t toNormalInt(std::uint32_t synchsafeInt)
146{
147 return ((synchsafeInt & 0x0000007fu)) | ((synchsafeInt & 0x00007f00u) >> 1) | ((synchsafeInt & 0x007f0000u) >> 2)
148 | ((synchsafeInt & 0x7f000000u) >> 3);
149}
150
154CPP_UTILITIES_EXPORT constexpr std::uint16_t swapOrder(std::uint16_t value)
155{
156 return static_cast<std::uint16_t>(((value >> 8) & 0x00FF) | ((value << 8) & 0xFF00));
157}
158
162CPP_UTILITIES_EXPORT constexpr std::uint32_t swapOrder(std::uint32_t value)
163{
164 return (value >> 24) | ((value & 0x00FF0000) >> 8) | ((value & 0x0000FF00) << 8) | (value << 24);
165}
166
170CPP_UTILITIES_EXPORT constexpr std::uint64_t swapOrder(std::uint64_t value)
171{
172 return (value >> (7 * 8)) | ((value & 0x00FF000000000000) >> (5 * 8)) | ((value & 0x0000FF0000000000) >> (3 * 8))
173 | ((value & 0x000000FF00000000) >> (1 * 8)) | ((value & 0x00000000FF000000) << (1 * 8)) | ((value & 0x0000000000FF0000) << (3 * 8))
174 | ((value & 0x000000000000FF00) << (5 * 8)) | ((value) << (7 * 8));
175}
176} // namespace CppUtilities
177
178#endif // CONVERSION_UTILITIES_BINARY_CONVERSION_H
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Contains all utilities provides by the c++utilities library.
constexpr CPP_UTILITIES_EXPORT std::uint16_t toFixed8(float float32value)
Returns the 8.8 fixed point representation converted from the specified 32-bit floating point number.
constexpr CPP_UTILITIES_EXPORT std::uint16_t swapOrder(std::uint16_t value)
Swaps the byte order of the specified 16-bit unsigned integer.
constexpr CPP_UTILITIES_EXPORT std::uint32_t toNormalInt(std::uint32_t synchsafeInt)
Returns a normal 32-bit integer converted from a 32-bit synchsafe integer.
constexpr CPP_UTILITIES_EXPORT float toFloat32(std::uint16_t fixed8value)
Returns a 32-bit floating point number converted from the specified 8.8 fixed point representation.
constexpr CPP_UTILITIES_EXPORT std::uint32_t toSynchsafeInt(std::uint32_t normalInt)
Returns a 32-bit synchsafe integer converted from a normal 32-bit integer.
constexpr CPP_UTILITIES_EXPORT std::uint32_t toFixed16(float float32value)
Returns the 16.16 fixed point representation converted from the specified 32-bit floating point numbe...