C++ Utilities  4.6.1
Common C++ classes and routines used by my applications 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 "./types.h"
5 
6 #include "../global.h"
7 
8 #ifdef __BYTE_ORDER__
9 # if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
10 # define CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN false
11 # define CONVERSION_UTILITIES_IS_BYTE_ORDER_BIG_ENDIAN true
12 # define CONVERSION_UTILITIES_BYTE_ORDER_BIG_ENDIAN
13 # elif __BYTE_ORDER__ == __ORDER_PDP_ENDIAN__
14 # define CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN false
15 # define CONVERSION_UTILITIES_IS_BYTE_ORDER_BIG_ENDIAN false
16 # define CONVERSION_UTILITIES_BYTE_ORDER_MIDDLE_ENDIAN
17 # elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
18 # define CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN true
19 # define CONVERSION_UTILITIES_IS_BYTE_ORDER_BIG_ENDIAN false
20 # define CONVERSION_UTILITIES_BYTE_ORDER_LITTLE_ENDIAN
21 # endif
22 #endif
23 #ifdef __FLOAT_WORD_ORDER__
24 # if __FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__
25 # define CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_BIG_ENDIAN
26 # elif __FLOAT_WORD_ORDER__ == __ORDER_PDP_ENDIAN__
27 # define CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_MIDDLE_ENDIAN
28 # elif __FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__
29 # define CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_LITTLE_ENDIAN
30 # endif
31 #endif
32 #if defined(__BYTE_ORDER__) && defined(__FLOAT_WORD_ORDER__)
33 #else
34 # if defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(_M_X64) || defined(__LITTLE_ENDIAN__) || defined(_little_endian__) || defined(_LITTLE_ENDIAN) || defined(_WIN32_WCE) || defined(WINAPI_FAMILY)
35 # ifndef __BYTE_ORDER__
36 # define CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN true
37 # define CONVERSION_UTILITIES_IS_BYTE_ORDER_BIG_ENDIAN false
38 # define CONVERSION_UTILITIES_BYTE_ORDER_LITTLE_ENDIAN
39 # endif
40 # ifndef __FLOAT_WORD_ORDER__
41 # define CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_LITTLE_ENDIAN
42 # endif
43 # elif defined(__MIPSEB__) || defined(__s390__) || defined(__BIG_ENDIAN__) || defined(_big_endian__) || defined(_BIG_ENDIAN)
44 # ifndef __BYTE_ORDER__
45 # define CONVERSION_UTILITIES_IS_BYTE_ORDER_LITTLE_ENDIAN false
46 # define CONVERSION_UTILITIES_IS_BYTE_ORDER_BIG_ENDIAN true
47 # define CONVERSION_UTILITIES_BYTE_ORDER_BIG_ENDIAN
48 # endif
49 # ifndef __FLOAT_WORD_ORDER__
50 # define CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_BIG_ENDIAN
51 # endif
52 # else
53 # error "Unable to determine byte order!"
54 # endif
55 #endif
56 
57 #if defined(CONVERSION_UTILITIES_BYTE_ORDER_MIDDLE_ENDIAN)
58 # error "Middle endian byte order is not supported!"
59 #endif
60 #if defined(CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_MIDDLE_ENDIAN)
61 # error "Middle endian byte order is not supported!"
62 #endif
63 
65 {
66 
71 namespace BE {
72 
73 #if defined(CONVERSION_UTILITIES_BYTE_ORDER_LITTLE_ENDIAN)
74 # define CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL 0
75 #elif defined(CONVERSION_UTILITIES_BYTE_ORDER_BIG_ENDIAN)
76 # define CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL 1
77 #endif
79 #undef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
80 
81 }
82 
87 namespace LE {
88 
89 #if defined(CONVERSION_UTILITIES_BYTE_ORDER_LITTLE_ENDIAN)
90 # define CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL 1
91 #elif defined(CONVERSION_UTILITIES_BYTE_ORDER_BIG_ENDIAN)
92 # define CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL 0
93 #endif
95 #undef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
96 
97 }
98 
102 CPP_UTILITIES_EXPORT constexpr uint16 toFixed8(float32 float32value)
103 {
104  return static_cast<uint16>(float32value * 256.0f);
105 }
106 
110 CPP_UTILITIES_EXPORT constexpr float32 toFloat32(uint16 fixed8value)
111 {
112  return static_cast<float32>(fixed8value) / 256.0f;
113 }
114 
118 CPP_UTILITIES_EXPORT constexpr uint32 toFixed16(float32 float32value)
119 {
120  return static_cast<uint32>(float32value * 65536.0f);
121 }
122 
126 CPP_UTILITIES_EXPORT constexpr float32 toFloat32(uint32 fixed16value)
127 {
128  return static_cast<float32>(fixed16value) / 65536.0f;
129 }
130 
137 {
138  return ((normalInt & 0x0000007fu) )
139  | ((normalInt & 0x00003f80u) << 1)
140  | ((normalInt & 0x001fc000u) << 2)
141  | ((normalInt & 0x0fe00000u) << 3);
142 }
143 
150 {
151  return ((synchsafeInt & 0x0000007fu) )
152  | ((synchsafeInt & 0x00007f00u) >> 1)
153  | ((synchsafeInt & 0x007f0000u) >> 2)
154  | ((synchsafeInt & 0x7f000000u) >> 3);
155 }
156 
161 {
162  return (value >> 8) | (value << 8);
163 }
164 
169 {
170  return (value >> 24)
171  | ((value & 0x00FF0000) >> 8)
172  | ((value & 0x0000FF00) << 8)
173  | (value << 24);
174 }
175 
180 {
181  return(value >> (7 * 8))
182  | ((value & 0x00FF000000000000) >> (5 * 8))
183  | ((value & 0x0000FF0000000000) >> (3 * 8))
184  | ((value & 0x000000FF00000000) >> (1 * 8))
185  | ((value & 0x00000000FF000000) << (1 * 8))
186  | ((value & 0x0000000000FF0000) << (3 * 8))
187  | ((value & 0x000000000000FF00) << (5 * 8))
188  | ((value) << (7 * 8));
189 }
190 
191 }
192 
193 #endif // CONVERSION_UTILITIES_BINARY_CONVERSION_H
CPP_UTILITIES_EXPORT constexpr uint16 toFixed8(float32 float32value)
Returns the 8.8 fixed point representation converted from the specified 32-bit floating point number...
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
CPP_UTILITIES_EXPORT constexpr uint16 swapOrder(uint16 value)
Swaps the byte order of the specified 16-bit unsigned integer.
Contains several functions providing conversions between different data types.
std::uint32_t uint32
unsigned 32-bit integer
Definition: types.h:44
CPP_UTILITIES_EXPORT constexpr uint32 toSynchsafeInt(uint32 normalInt)
Returns a 32-bit synchsafe integer converted from a normal 32-bit integer.
CPP_UTILITIES_EXPORT constexpr float32 toFloat32(uint16 fixed8value)
Returns a 32-bit floating point number converted from the specified 8.8 fixed point representation...
CPP_UTILITIES_EXPORT constexpr uint32 toNormalInt(uint32 synchsafeInt)
Returns a normal 32-bit integer converted from a 32-bit synchsafe integer.
CPP_UTILITIES_EXPORT constexpr uint32 toFixed16(float32 float32value)
Returns the 16.16 fixed point representation converted from the specified 32-bit floating point numbe...
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
std::uint16_t uint16
unsigned 16-bit integer
Definition: types.h:39