Debian Codegraph @

conversion/binaryconversionprivate.h

raw symbols SCIP
1 // assert that CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL is defined; a value of:
2 // - 0 means to define functions for BE namespace
3 // - 1 means to define functions for LE namespace
4 #ifndef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
5 #error "Do not include binaryconversionprivate.h directly."
6 #else
7
8 // define macro if swapping byte order is required
9 #if (CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0 && defined(CONVERSION_UTILITIES_BYTE_ORDER_LITTLE_ENDIAN)) \
10 || (CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 1 && defined(CONVERSION_UTILITIES_BYTE_ORDER_BIG_ENDIAN))
11 #define CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL_NEEDS_SWAP
12 #endif
13
14 // disable warnings about sign conversions when using GCC or Clang
15 #ifdef __GNUC__
16 #pragma GCC diagnostic push
17 #pragma GCC diagnostic ignored "-Wsign-conversion"
18 #endif
19
20 /*!
21 * \brief Returns a 16-bit signed integer converted from two bytes at a specified position in a char array.
22 */
23 CPP_UTILITIES_EXPORT constexpr std::int16_t toInt16(const char *value)
24 {
25 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
26 return static_cast<std::int16_t>((static_cast<std::int16_t>(value[0]) << 8 & 0xFF00) | (static_cast<std::int16_t>(value[1]) & 0x00FF));
27 #else
28 return static_cast<std::int16_t>((static_cast<std::int16_t>(value[1]) << 8 & 0xFF00) | (static_cast<std::int16_t>(value[0]) & 0x00FF));
29 #endif
30 }
31
32 /*!
33 * \brief Returns a 16-bit unsigned integer converted from two bytes at a specified position in a char array.
34 */
35 CPP_UTILITIES_EXPORT constexpr std::uint16_t toUInt16(const char *value)
36 {
37 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
38 return static_cast<std::uint16_t>((static_cast<std::uint16_t>(value[0]) << 8 & 0xFF00) | (static_cast<std::uint16_t>(value[1]) & 0x00FF));
39 #else
40 return static_cast<std::uint16_t>((static_cast<std::uint16_t>(value[1]) << 8 & 0xFF00) | (static_cast<std::uint16_t>(value[0]) & 0x00FF));
41 #endif
42 }
43
44 /*!
45 * \brief Returns a 32-bit signed integer converted from four bytes at a specified position in a char array.
46 */
47 CPP_UTILITIES_EXPORT constexpr std::int32_t toInt32(const char *value)
48 {
49 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
50 return static_cast<std::int32_t>((static_cast<std::int32_t>(value[0]) << 24 & 0xFF000000)
51 | (static_cast<std::int32_t>(value[1]) << 16 & 0x00FF0000) | (static_cast<std::int32_t>(value[2]) << 8 & 0x0000FF00)
52 | (static_cast<std::int32_t>(value[3]) & 0x000000FF));
53 #else
54 return static_cast<std::int32_t>((static_cast<std::int32_t>(value[3]) << 24 & 0xFF000000)
55 | (static_cast<std::int32_t>(value[2]) << 16 & 0x00FF0000) | (static_cast<std::int32_t>(value[1]) << 8 & 0x0000FF00)
56 | (static_cast<std::int32_t>(value[0]) & 0x000000FF));
57 #endif
58 }
59
60 /*!
61 * \brief Returns a 32-bit unsigned integer converted from three bytes at a specified position in a char array.
62 */
63 CPP_UTILITIES_EXPORT constexpr std::uint32_t toUInt24(const char *value)
64 {
65 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
66 return (static_cast<std::uint32_t>(value[0]) << 16 & 0x00FF0000) | (static_cast<std::uint32_t>(value[1]) << 8 & 0x0000FF00)
67 | (static_cast<std::uint32_t>(value[2]) & 0x000000FF);
68 #else
69 return (static_cast<std::uint32_t>(value[2]) << 16 & 0x00FF0000) | (static_cast<std::uint32_t>(value[1]) << 8 & 0x0000FF00)
70 | (static_cast<std::uint32_t>(value[0]) & 0x000000FF);
71 #endif
72 }
73
74 /*!
75 * \brief Returns a 32-bit unsigned integer converted from four bytes at a specified position in a char array.
76 */
77 CPP_UTILITIES_EXPORT constexpr std::uint32_t toUInt32(const char *value)
78 {
79 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
80 return (static_cast<std::uint32_t>(value[0]) << 24 & 0xFF000000) | (static_cast<std::uint32_t>(value[1]) << 16 & 0x00FF0000)
81 | (static_cast<std::uint32_t>(value[2]) << 8 & 0x0000FF00) | (static_cast<std::uint32_t>(value[3]) & 0x000000FF);
82 #else
83 return (static_cast<std::uint32_t>(value[3]) << 24 & 0xFF000000) | (static_cast<std::uint32_t>(value[2]) << 16 & 0x00FF0000)
84 | (static_cast<std::uint32_t>(value[1]) << 8 & 0x0000FF00) | (static_cast<std::uint32_t>(value[0]) & 0x000000FF);
85 #endif
86 }
87
88 /*!
89 * \brief Returns a 64-bit signed integer converted from eight bytes at a specified position in a char array.
90 */
91 CPP_UTILITIES_EXPORT constexpr std::int64_t toInt64(const char *value)
92 {
93 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
94 return (static_cast<std::int64_t>(value[0]) << 56 & 0xFF00000000000000) | (static_cast<std::int64_t>(value[1]) << 48 & 0x00FF000000000000)
95 | (static_cast<std::int64_t>(value[2]) << 40 & 0x0000FF0000000000) | (static_cast<std::int64_t>(value[3]) << 32 & 0x000000FF00000000)
96 | (static_cast<std::int64_t>(value[4]) << 24 & 0x00000000FF000000) | (static_cast<std::int64_t>(value[5]) << 16 & 0x0000000000FF0000)
97 | (static_cast<std::int64_t>(value[6]) << 8 & 0x000000000000FF00) | (static_cast<std::int64_t>(value[7]) & 0x00000000000000FF);
98 #else
99 return (static_cast<std::int64_t>(value[7]) << 56 & 0xFF00000000000000) | (static_cast<std::int64_t>(value[6]) << 48 & 0x00FF000000000000)
100 | (static_cast<std::int64_t>(value[5]) << 40 & 0x0000FF0000000000) | (static_cast<std::int64_t>(value[4]) << 32 & 0x000000FF00000000)
101 | (static_cast<std::int64_t>(value[3]) << 24 & 0x00000000FF000000) | (static_cast<std::int64_t>(value[2]) << 16 & 0x0000000000FF0000)
102 | (static_cast<std::int64_t>(value[1]) << 8 & 0x000000000000FF00) | (static_cast<std::int64_t>(value[0]) & 0x00000000000000FF);
103 #endif
104 }
105
106 /*!
107 * \brief Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a char array.
108 */
109 CPP_UTILITIES_EXPORT constexpr std::uint64_t toUInt64(const char *value)
110 {
111 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
112 return (static_cast<std::uint64_t>(value[0]) << 56 & 0xFF00000000000000) | (static_cast<std::uint64_t>(value[1]) << 48 & 0x00FF000000000000)
113 | (static_cast<std::uint64_t>(value[2]) << 40 & 0x0000FF0000000000) | (static_cast<std::uint64_t>(value[3]) << 32 & 0x000000FF00000000)
114 | (static_cast<std::uint64_t>(value[4]) << 24 & 0x00000000FF000000) | (static_cast<std::uint64_t>(value[5]) << 16 & 0x0000000000FF0000)
115 | (static_cast<std::uint64_t>(value[6]) << 8 & 0x000000000000FF00) | (static_cast<std::uint64_t>(value[7]) & 0x00000000000000FF);
116 #else
117 return (static_cast<std::uint64_t>(value[7]) << 56 & 0xFF00000000000000) | (static_cast<std::uint64_t>(value[6]) << 48 & 0x00FF000000000000)
118 | (static_cast<std::uint64_t>(value[5]) << 40 & 0x0000FF0000000000) | (static_cast<std::uint64_t>(value[4]) << 32 & 0x000000FF00000000)
119 | (static_cast<std::uint64_t>(value[3]) << 24 & 0x00000000FF000000) | (static_cast<std::uint64_t>(value[2]) << 16 & 0x0000000000FF0000)
120 | (static_cast<std::uint64_t>(value[1]) << 8 & 0x000000000000FF00) | (static_cast<std::uint64_t>(value[0]) & 0x00000000000000FF);
121 #endif
122 }
123
124 /*!
125 * \brief Returns a 32-bit floating point number converted from four bytes at a specified position in a char array.
126 */
127 CPP_UTILITIES_EXPORT inline float toFloat32(const char *value)
128 {
129 const auto val = toInt32(value);
130 const auto *const c = reinterpret_cast<const char *>(&val);
131 return *reinterpret_cast<const float *>(c);
132 }
133
134 /*!
135 * \brief Returns a 64-bit floating point number converted from eight bytes at a specified position in a char array.
136 */
137 CPP_UTILITIES_EXPORT inline double toFloat64(const char *value)
138 {
139 const auto val = toInt64(value);
140 const auto *const c = reinterpret_cast<const char *>(&val);
141 return *reinterpret_cast<const double *>(c);
142 }
143
144 /*!
145 * \brief Returns the specified (unsigned) integer converted from the specified char array.
146 * \remarks
147 * - The \a value must point to a sequence of characters that is at least as long as the specified integer type.
148 * - This function is potentially faster than the width-specific toInt…() because the width-specific functions use
149 * custom code that may not be optimized by the compiler. (Only GCC optimized it but not Clang and MSVC.) Note
150 * that the width-specific functions cannot be changed as they are constexpr and thus cannot use memcpy().
151 */
152 template <class T, Traits::EnableIf<std::is_integral<T>> * = nullptr> CPP_UTILITIES_EXPORT inline T toInt(const char *value)
153 {
154 auto dst = T();
155 std::memcpy(&dst, value, sizeof(T));
156 #ifdef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL_NEEDS_SWAP
157 dst = swapOrder(dst);
158 #endif
159 return dst;
160 }
161
162 /*!
163 * \brief Stores the specified 24-bit unsigned integer value at a specified position in a char array.
164 * \remarks Ignores the most significant byte.
165 */
166 CPP_UTILITIES_EXPORT inline void getBytes24(std::uint32_t value, char *outputbuffer)
167 {
168 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
169 outputbuffer[0] = static_cast<char>((value >> 16) & 0xFF);
170 outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
171 outputbuffer[2] = static_cast<char>((value) & 0xFF);
172 #else
173 outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
174 outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
175 outputbuffer[0] = static_cast<char>((value) & 0xFF);
176 #endif
177 }
178
179 /*!
180 * \brief Stores the specified (unsigned) integer value in a char array.
181 * \remarks
182 * - The \a value outputbuffer must point to a sequence of characters that is at least as long as the specified integer type.
183 * - This function is potentially faster than the width-specific toInt…() because the width-specific functions use
184 * custom code that may not be optimized by the compiler. (Only GCC optimized it but not Clang and MSVC.)
185 */
186 template <class T, Traits::EnableIf<std::is_integral<T>> * = nullptr> CPP_UTILITIES_EXPORT inline void getBytes(T value, char *outputbuffer)
187 {
188 #ifdef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL_NEEDS_SWAP
189 value = swapOrder(value);
190 #endif
191 std::memcpy(outputbuffer, &value, sizeof(T));
192 }
193
194 /*!
195 * \brief Stores the specified 32-bit floating point value at a specified position in a char array.
196 */
197 CPP_UTILITIES_EXPORT inline void getBytes(float value, char *outputbuffer)
198 {
199 auto *c = reinterpret_cast<char *>(&value);
200 auto i = *reinterpret_cast<std::int32_t *>(c);
201 getBytes(i, outputbuffer);
202 }
203
204 /*!
205 * \brief Stores the specified 64-bit floating point value at a specified position in a char array.
206 */
207 CPP_UTILITIES_EXPORT inline void getBytes(double value, char *outputbuffer)
208 {
209 auto *c = reinterpret_cast<char *>(&value);
210 auto i = *reinterpret_cast<std::int64_t *>(c);
211 getBytes(i, outputbuffer);
212 }
213
214 #ifdef __GNUC__
215 #pragma GCC diagnostic pop
216 #endif
217
218 #undef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL_NEEDS_SWAP
219
220 #endif // CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL