Debian Codegraph @

src/mpt/endian/floatingpoint.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_ENDIAN_FLOATINGPOINT_HPP
4 #define MPT_ENDIAN_FLOATINGPOINT_HPP
5
6
7
8 #include "mpt/base/bit.hpp"
9 #include "mpt/base/float.hpp"
10 #include "mpt/base/macros.hpp"
11 #include "mpt/base/memory.hpp"
12 #include "mpt/base/namespace.hpp"
13 #include "mpt/endian/type_traits.hpp"
14
15 #include <limits>
16
17 #include <cmath>
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstdlib>
21
22
23
24 namespace mpt {
25 inline namespace MPT_INLINE_NS {
26
27
28
29 // 1.0f --> 0x3f800000u
30 MPT_FORCEINLINE uint32 EncodeIEEE754binary32(somefloat32 f) {
31 if constexpr (mpt::float_traits<somefloat32>::is_float32 && mpt::float_traits<somefloat32>::is_ieee754_binary && mpt::float_traits<somefloat32>::is_native_endian) {
32 return mpt::bit_cast<uint32>(f);
33 } else {
34 int e = 0;
35 float m = std::frexp(f, &e);
36 if (e == 0 && std::fabs(m) == 0.0f) {
37 uint32 expo = 0u;
38 uint32 sign = std::signbit(m) ? 0x01u : 0x00u;
39 uint32 mant = 0u;
40 uint32 i = 0u;
41 i |= (mant << 0) & 0x007fffffu;
42 i |= (expo << 23) & 0x7f800000u;
43 i |= (sign << 31) & 0x80000000u;
44 return i;
45 } else {
46 uint32 expo = e + 127 - 1;
47 uint32 sign = std::signbit(m) ? 0x01u : 0x00u;
48 uint32 mant = static_cast<uint32>(std::fabs(std::ldexp(m, 24)));
49 uint32 i = 0u;
50 i |= (mant << 0) & 0x007fffffu;
51 i |= (expo << 23) & 0x7f800000u;
52 i |= (sign << 31) & 0x80000000u;
53 return i;
54 }
55 }
56 }
57
58 MPT_FORCEINLINE uint64 EncodeIEEE754binary64(somefloat64 f) {
59 if constexpr (mpt::float_traits<somefloat64>::is_float64 && mpt::float_traits<somefloat64>::is_ieee754_binary && mpt::float_traits<somefloat64>::is_native_endian) {
60 return mpt::bit_cast<uint64>(f);
61 } else {
62 int e = 0;
63 double m = std::frexp(f, &e);
64 if (e == 0 && std::fabs(m) == 0.0) {
65 uint64 expo = 0u;
66 uint64 sign = std::signbit(m) ? 0x01u : 0x00u;
67 uint64 mant = 0u;
68 uint64 i = 0u;
69 i |= (mant << 0) & 0x000fffffffffffffull;
70 i |= (expo << 52) & 0x7ff0000000000000ull;
71 i |= (sign << 63) & 0x8000000000000000ull;
72 return i;
73 } else {
74 uint64 expo = static_cast<int64>(e) + 1023 - 1;
75 uint64 sign = std::signbit(m) ? 0x01u : 0x00u;
76 uint64 mant = static_cast<uint64>(std::fabs(std::ldexp(m, 53)));
77 uint64 i = 0u;
78 i |= (mant << 0) & 0x000fffffffffffffull;
79 i |= (expo << 52) & 0x7ff0000000000000ull;
80 i |= (sign << 63) & 0x8000000000000000ull;
81 return i;
82 }
83 }
84 }
85
86 // 0x3f800000u --> 1.0f
87 MPT_FORCEINLINE somefloat32 DecodeIEEE754binary32(uint32 i) {
88 if constexpr (mpt::float_traits<somefloat32>::is_float32 && mpt::float_traits<somefloat32>::is_ieee754_binary && mpt::float_traits<somefloat32>::is_native_endian) {
89 return mpt::bit_cast<somefloat32>(i);
90 } else {
91 uint32 mant = (i & 0x007fffffu) >> 0;
92 uint32 expo = (i & 0x7f800000u) >> 23;
93 uint32 sign = (i & 0x80000000u) >> 31;
94 if (expo == 0) {
95 float m = sign ? -static_cast<float>(mant) : static_cast<float>(mant);
96 int e = static_cast<int>(expo) - 127 + 1 - 24;
97 float f = std::ldexp(m, e);
98 return static_cast<somefloat32>(f);
99 } else {
100 mant |= 0x00800000u;
101 float m = sign ? -static_cast<float>(mant) : static_cast<float>(mant);
102 int e = static_cast<int>(expo) - 127 + 1 - 24;
103 float f = std::ldexp(m, e);
104 return static_cast<somefloat32>(f);
105 }
106 }
107 }
108
109 MPT_FORCEINLINE somefloat64 DecodeIEEE754binary64(uint64 i) {
110 if constexpr (mpt::float_traits<somefloat64>::is_float64 && mpt::float_traits<somefloat64>::is_ieee754_binary && mpt::float_traits<somefloat64>::is_native_endian) {
111 return mpt::bit_cast<somefloat64>(i);
112 } else {
113 uint64 mant = (i & 0x000fffffffffffffull) >> 0;
114 uint64 expo = (i & 0x7ff0000000000000ull) >> 52;
115 uint64 sign = (i & 0x8000000000000000ull) >> 63;
116 if (expo == 0) {
117 double m = sign ? -static_cast<double>(mant) : static_cast<double>(mant);
118 int e = static_cast<int>(expo) - 1023 + 1 - 53;
119 double f = std::ldexp(m, e);
120 return static_cast<somefloat64>(f);
121 } else {
122 mant |= 0x0010000000000000ull;
123 double m = sign ? -static_cast<double>(mant) : static_cast<double>(mant);
124 int e = static_cast<int>(expo) - 1023 + 1 - 53;
125 double f = std::ldexp(m, e);
126 return static_cast<somefloat64>(f);
127 }
128 }
129 }
130
131
132 // template parameters are byte indices corresponding to the individual bytes of iee754 in memory
133 template <std::size_t hihi, std::size_t hilo, std::size_t lohi, std::size_t lolo>
134 struct IEEE754binary32Emulated {
135 public:
136 using self_t = IEEE754binary32Emulated<hihi, hilo, lohi, lolo>;
137 std::byte bytes[4];
138
139 public:
140 MPT_FORCEINLINE std::byte GetByte(std::size_t i) const {
141 return bytes[i];
142 }
143 IEEE754binary32Emulated() = default;
144 MPT_FORCEINLINE explicit IEEE754binary32Emulated(somefloat32 f) {
145 SetInt32(EncodeIEEE754binary32(f));
146 }
147 MPT_FORCEINLINE IEEE754binary32Emulated & operator=(somefloat32 f) {
148 SetInt32(EncodeIEEE754binary32(f));
149 return *this;
150 }
151 // b0...b3 are in memory order, i.e. depend on the endianness of this type
152 // little endian: (0x00,0x00,0x80,0x3f)
153 // big endian: (0x3f,0x80,0x00,0x00)
154 MPT_FORCEINLINE explicit IEEE754binary32Emulated(std::byte b0, std::byte b1, std::byte b2, std::byte b3) {
155 bytes[0] = b0;
156 bytes[1] = b1;
157 bytes[2] = b2;
158 bytes[3] = b3;
159 }
160 MPT_FORCEINLINE operator somefloat32() const {
161 return DecodeIEEE754binary32(GetInt32());
162 }
163 MPT_FORCEINLINE self_t & set(somefloat32 f) {
164 SetInt32(EncodeIEEE754binary32(f));
165 return *this;
166 }
167 MPT_FORCEINLINE somefloat32 get() const {
168 return DecodeIEEE754binary32(GetInt32());
169 }
170 MPT_FORCEINLINE self_t & SetInt32(uint32 i) {
171 bytes[hihi] = static_cast<std::byte>(i >> 24);
172 bytes[hilo] = static_cast<std::byte>(i >> 16);
173 bytes[lohi] = static_cast<std::byte>(i >> 8);
174 bytes[lolo] = static_cast<std::byte>(i >> 0);
175 return *this;
176 }
177 MPT_FORCEINLINE uint32 GetInt32() const {
178 return 0u
179 | (static_cast<uint32>(bytes[hihi]) << 24)
180 | (static_cast<uint32>(bytes[hilo]) << 16)
181 | (static_cast<uint32>(bytes[lohi]) << 8)
182 | (static_cast<uint32>(bytes[lolo]) << 0);
183 }
184 MPT_FORCEINLINE bool operator==(const self_t & cmp) const {
185 return true
186 && bytes[0] == cmp.bytes[0]
187 && bytes[1] == cmp.bytes[1]
188 && bytes[2] == cmp.bytes[2]
189 && bytes[3] == cmp.bytes[3];
190 }
191 MPT_FORCEINLINE bool operator!=(const self_t & cmp) const {
192 return !(*this == cmp);
193 }
194 };
195 template <std::size_t hihihi, std::size_t hihilo, std::size_t hilohi, std::size_t hilolo, std::size_t lohihi, std::size_t lohilo, std::size_t lolohi, std::size_t lololo>
196 struct IEEE754binary64Emulated {
197 public:
198 using self_t = IEEE754binary64Emulated<hihihi, hihilo, hilohi, hilolo, lohihi, lohilo, lolohi, lololo>;
199 std::byte bytes[8];
200
201 public:
202 MPT_FORCEINLINE std::byte GetByte(std::size_t i) const {
203 return bytes[i];
204 }
205 IEEE754binary64Emulated() = default;
206 MPT_FORCEINLINE explicit IEEE754binary64Emulated(somefloat64 f) {
207 SetInt64(EncodeIEEE754binary64(f));
208 }
209 MPT_FORCEINLINE IEEE754binary64Emulated & operator=(somefloat64 f) {
210 SetInt64(EncodeIEEE754binary64(f));
211 return *this;
212 }
213 MPT_FORCEINLINE explicit IEEE754binary64Emulated(std::byte b0, std::byte b1, std::byte b2, std::byte b3, std::byte b4, std::byte b5, std::byte b6, std::byte b7) {
214 bytes[0] = b0;
215 bytes[1] = b1;
216 bytes[2] = b2;
217 bytes[3] = b3;
218 bytes[4] = b4;
219 bytes[5] = b5;
220 bytes[6] = b6;
221 bytes[7] = b7;
222 }
223 MPT_FORCEINLINE operator somefloat64() const {
224 return DecodeIEEE754binary64(GetInt64());
225 }
226 MPT_FORCEINLINE self_t & set(somefloat64 f) {
227 SetInt64(EncodeIEEE754binary64(f));
228 return *this;
229 }
230 MPT_FORCEINLINE somefloat64 get() const {
231 return DecodeIEEE754binary64(GetInt64());
232 }
233 MPT_FORCEINLINE self_t & SetInt64(uint64 i) {
234 bytes[hihihi] = static_cast<std::byte>(i >> 56);
235 bytes[hihilo] = static_cast<std::byte>(i >> 48);
236 bytes[hilohi] = static_cast<std::byte>(i >> 40);
237 bytes[hilolo] = static_cast<std::byte>(i >> 32);
238 bytes[lohihi] = static_cast<std::byte>(i >> 24);
239 bytes[lohilo] = static_cast<std::byte>(i >> 16);
240 bytes[lolohi] = static_cast<std::byte>(i >> 8);
241 bytes[lololo] = static_cast<std::byte>(i >> 0);
242 return *this;
243 }
244 MPT_FORCEINLINE uint64 GetInt64() const {
245 return 0u
246 | (static_cast<uint64>(bytes[hihihi]) << 56)
247 | (static_cast<uint64>(bytes[hihilo]) << 48)
248 | (static_cast<uint64>(bytes[hilohi]) << 40)
249 | (static_cast<uint64>(bytes[hilolo]) << 32)
250 | (static_cast<uint64>(bytes[lohihi]) << 24)
251 | (static_cast<uint64>(bytes[lohilo]) << 16)
252 | (static_cast<uint64>(bytes[lolohi]) << 8)
253 | (static_cast<uint64>(bytes[lololo]) << 0);
254 }
255 MPT_FORCEINLINE bool operator==(const self_t & cmp) const {
256 return true
257 && bytes[0] == cmp.bytes[0]
258 && bytes[1] == cmp.bytes[1]
259 && bytes[2] == cmp.bytes[2]
260 && bytes[3] == cmp.bytes[3]
261 && bytes[4] == cmp.bytes[4]
262 && bytes[5] == cmp.bytes[5]
263 && bytes[6] == cmp.bytes[6]
264 && bytes[7] == cmp.bytes[7];
265 }
266 MPT_FORCEINLINE bool operator!=(const self_t & cmp) const {
267 return !(*this == cmp);
268 }
269 };
270
271 using IEEE754binary32EmulatedBE = IEEE754binary32Emulated<0, 1, 2, 3>;
272 using IEEE754binary32EmulatedLE = IEEE754binary32Emulated<3, 2, 1, 0>;
273 using IEEE754binary64EmulatedBE = IEEE754binary64Emulated<0, 1, 2, 3, 4, 5, 6, 7>;
274 using IEEE754binary64EmulatedLE = IEEE754binary64Emulated<7, 6, 5, 4, 3, 2, 1, 0>;
275
276 constexpr bool declare_binary_safe(const IEEE754binary32EmulatedBE &) {
277 return true;
278 }
279 constexpr bool declare_binary_safe(const IEEE754binary32EmulatedLE &) {
280 return true;
281 }
282 constexpr bool declare_binary_safe(const IEEE754binary64EmulatedBE &) {
283 return true;
284 }
285 constexpr bool declare_binary_safe(const IEEE754binary64EmulatedLE &) {
286 return true;
287 }
288
289 static_assert(mpt::check_binary_size<IEEE754binary32EmulatedBE>(4));
290 static_assert(mpt::check_binary_size<IEEE754binary32EmulatedLE>(4));
291 static_assert(mpt::check_binary_size<IEEE754binary64EmulatedBE>(8));
292 static_assert(mpt::check_binary_size<IEEE754binary64EmulatedLE>(8));
293
294 template <mpt::endian endian = mpt::endian::native>
295 struct IEEE754binary32Native {
296 public:
297 somefloat32 value;
298
299 public:
300 MPT_FORCEINLINE std::byte GetByte(std::size_t i) const {
301 static_assert(endian == mpt::endian::little || endian == mpt::endian::big);
302 if constexpr (endian == mpt::endian::little) {
303 return static_cast<std::byte>(EncodeIEEE754binary32(value) >> (i * 8));
304 }
305 if constexpr (endian == mpt::endian::big) {
306 return static_cast<std::byte>(EncodeIEEE754binary32(value) >> ((4 - 1 - i) * 8));
307 }
308 }
309 IEEE754binary32Native() = default;
310 MPT_FORCEINLINE explicit IEEE754binary32Native(somefloat32 f) {
311 value = f;
312 }
313 MPT_FORCEINLINE IEEE754binary32Native & operator=(somefloat32 f) {
314 value = f;
315 return *this;
316 }
317 // b0...b3 are in memory order, i.e. depend on the endianness of this type
318 // little endian: (0x00,0x00,0x80,0x3f)
319 // big endian: (0x3f,0x80,0x00,0x00)
320 MPT_FORCEINLINE explicit IEEE754binary32Native(std::byte b0, std::byte b1, std::byte b2, std::byte b3) {
321 static_assert(endian == mpt::endian::little || endian == mpt::endian::big);
322 if constexpr (endian == mpt::endian::little) {
323 value = DecodeIEEE754binary32(0u | (static_cast<uint32>(b0) << 0) | (static_cast<uint32>(b1) << 8) | (static_cast<uint32>(b2) << 16) | (static_cast<uint32>(b3) << 24));
324 }
325 if constexpr (endian == mpt::endian::big) {
326 value = DecodeIEEE754binary32(0u | (static_cast<uint32>(b0) << 24) | (static_cast<uint32>(b1) << 16) | (static_cast<uint32>(b2) << 8) | (static_cast<uint32>(b3) << 0));
327 }
328 }
329 MPT_FORCEINLINE operator somefloat32() const {
330 return value;
331 }
332 MPT_FORCEINLINE IEEE754binary32Native & set(somefloat32 f) {
333 value = f;
334 return *this;
335 }
336 MPT_FORCEINLINE somefloat32 get() const {
337 return value;
338 }
339 MPT_FORCEINLINE IEEE754binary32Native & SetInt32(uint32 i) {
340 value = DecodeIEEE754binary32(i);
341 return *this;
342 }
343 MPT_FORCEINLINE uint32 GetInt32() const {
344 return EncodeIEEE754binary32(value);
345 }
346 MPT_FORCEINLINE bool operator==(const IEEE754binary32Native & cmp) const {
347 return value == cmp.value;
348 }
349 MPT_FORCEINLINE bool operator!=(const IEEE754binary32Native & cmp) const {
350 return value != cmp.value;
351 }
352 };
353
354 template <mpt::endian endian = mpt::endian::native>
355 struct IEEE754binary64Native {
356 public:
357 somefloat64 value;
358
359 public:
360 MPT_FORCEINLINE std::byte GetByte(std::size_t i) const {
361 static_assert(endian == mpt::endian::little || endian == mpt::endian::big);
362 if constexpr (endian == mpt::endian::little) {
363 return mpt::byte_cast<std::byte>(static_cast<uint8>(EncodeIEEE754binary64(value) >> (i * 8)));
364 }
365 if constexpr (endian == mpt::endian::big) {
366 return mpt::byte_cast<std::byte>(static_cast<uint8>(EncodeIEEE754binary64(value) >> ((8 - 1 - i) * 8)));
367 }
368 }
369 IEEE754binary64Native() = default;
370 MPT_FORCEINLINE explicit IEEE754binary64Native(somefloat64 f) {
371 value = f;
372 }
373 MPT_FORCEINLINE IEEE754binary64Native & operator=(somefloat64 f) {
374 value = f;
375 return *this;
376 }
377 MPT_FORCEINLINE explicit IEEE754binary64Native(std::byte b0, std::byte b1, std::byte b2, std::byte b3, std::byte b4, std::byte b5, std::byte b6, std::byte b7) {
378 static_assert(endian == mpt::endian::little || endian == mpt::endian::big);
379 if constexpr (endian == mpt::endian::little) {
380 value = DecodeIEEE754binary64(0ull | (static_cast<uint64>(b0) << 0) | (static_cast<uint64>(b1) << 8) | (static_cast<uint64>(b2) << 16) | (static_cast<uint64>(b3) << 24) | (static_cast<uint64>(b4) << 32) | (static_cast<uint64>(b5) << 40) | (static_cast<uint64>(b6) << 48) | (static_cast<uint64>(b7) << 56));
381 }
382 if constexpr (endian == mpt::endian::big) {
383 value = DecodeIEEE754binary64(0ull | (static_cast<uint64>(b0) << 56) | (static_cast<uint64>(b1) << 48) | (static_cast<uint64>(b2) << 40) | (static_cast<uint64>(b3) << 32) | (static_cast<uint64>(b4) << 24) | (static_cast<uint64>(b5) << 16) | (static_cast<uint64>(b6) << 8) | (static_cast<uint64>(b7) << 0));
384 }
385 }
386 MPT_FORCEINLINE operator somefloat64() const {
387 return value;
388 }
389 MPT_FORCEINLINE IEEE754binary64Native & set(somefloat64 f) {
390 value = f;
391 return *this;
392 }
393 MPT_FORCEINLINE somefloat64 get() const {
394 return value;
395 }
396 MPT_FORCEINLINE IEEE754binary64Native & SetInt64(uint64 i) {
397 value = DecodeIEEE754binary64(i);
398 return *this;
399 }
400 MPT_FORCEINLINE uint64 GetInt64() const {
401 return EncodeIEEE754binary64(value);
402 }
403 MPT_FORCEINLINE bool operator==(const IEEE754binary64Native & cmp) const {
404 return value == cmp.value;
405 }
406 MPT_FORCEINLINE bool operator!=(const IEEE754binary64Native & cmp) const {
407 return value != cmp.value;
408 }
409 };
410
411 static_assert((sizeof(IEEE754binary32Native<>) == 4));
412 static_assert((sizeof(IEEE754binary64Native<>) == 8));
413
414 constexpr bool declare_binary_safe(const IEEE754binary32Native<> &) noexcept {
415 return true;
416 }
417 constexpr bool declare_binary_safe(const IEEE754binary64Native<> &) noexcept {
418 return true;
419 }
420
421 template <bool is_ieee754, mpt::endian endian = mpt::endian::native>
422 struct IEEE754binary_types {
423 using IEEE754binary32LE = IEEE754binary32EmulatedLE;
424 using IEEE754binary32BE = IEEE754binary32EmulatedBE;
425 using IEEE754binary64LE = IEEE754binary64EmulatedLE;
426 using IEEE754binary64BE = IEEE754binary64EmulatedBE;
427 };
428 template <>
429 struct IEEE754binary_types<true, mpt::endian::little> {
430 using IEEE754binary32LE = IEEE754binary32Native<>;
431 using IEEE754binary32BE = IEEE754binary32EmulatedBE;
432 using IEEE754binary64LE = IEEE754binary64Native<>;
433 using IEEE754binary64BE = IEEE754binary64EmulatedBE;
434 };
435 template <>
436 struct IEEE754binary_types<true, mpt::endian::big> {
437 using IEEE754binary32LE = IEEE754binary32EmulatedLE;
438 using IEEE754binary32BE = IEEE754binary32Native<>;
439 using IEEE754binary64LE = IEEE754binary64EmulatedLE;
440 using IEEE754binary64BE = IEEE754binary64Native<>;
441 };
442
443 using IEEE754binary32LE = IEEE754binary_types<mpt::float_traits<somefloat32>::is_float32 && mpt::float_traits<somefloat32>::is_ieee754_binary && mpt::float_traits<somefloat32>::is_native_endian, mpt::endian::native>::IEEE754binary32LE;
444 using IEEE754binary32BE = IEEE754binary_types<mpt::float_traits<somefloat32>::is_float32 && mpt::float_traits<somefloat32>::is_ieee754_binary && mpt::float_traits<somefloat32>::is_native_endian, mpt::endian::native>::IEEE754binary32BE;
445 using IEEE754binary64LE = IEEE754binary_types<mpt::float_traits<somefloat32>::is_float64 && mpt::float_traits<somefloat32>::is_ieee754_binary && mpt::float_traits<somefloat32>::is_native_endian, mpt::endian::native>::IEEE754binary64LE;
446 using IEEE754binary64BE = IEEE754binary_types<mpt::float_traits<somefloat32>::is_float64 && mpt::float_traits<somefloat32>::is_ieee754_binary && mpt::float_traits<somefloat32>::is_native_endian, mpt::endian::native>::IEEE754binary64BE;
447
448 static_assert(sizeof(IEEE754binary32LE) == 4);
449 static_assert(sizeof(IEEE754binary32BE) == 4);
450 static_assert(sizeof(IEEE754binary64LE) == 8);
451 static_assert(sizeof(IEEE754binary64BE) == 8);
452
453
454 // unaligned
455
456 using float32le = IEEE754binary32EmulatedLE;
457 using float32be = IEEE754binary32EmulatedBE;
458 using float64le = IEEE754binary64EmulatedLE;
459 using float64be = IEEE754binary64EmulatedBE;
460
461 static_assert(sizeof(float32le) == 4);
462 static_assert(sizeof(float32be) == 4);
463 static_assert(sizeof(float64le) == 8);
464 static_assert(sizeof(float64be) == 8);
465
466
467 // potentially aligned
468
469 using float32le_fast = IEEE754binary32LE;
470 using float32be_fast = IEEE754binary32BE;
471 using float64le_fast = IEEE754binary64LE;
472 using float64be_fast = IEEE754binary64BE;
473
474 static_assert(sizeof(float32le_fast) == 4);
475 static_assert(sizeof(float32be_fast) == 4);
476 static_assert(sizeof(float64le_fast) == 8);
477 static_assert(sizeof(float64be_fast) == 8);
478
479
480
481 template <>
482 struct make_endian<mpt::endian::little, float> {
483 using type = IEEE754binary32LE;
484 };
485
486 template <>
487 struct make_endian<mpt::endian::big, float> {
488 using type = IEEE754binary32BE;
489 };
490
491 template <>
492 struct make_endian<mpt::endian::little, double> {
493 using type = IEEE754binary64LE;
494 };
495
496 template <>
497 struct make_endian<mpt::endian::big, double> {
498 using type = IEEE754binary64BE;
499 };
500
501
502
503 } // namespace MPT_INLINE_NS
504 } // namespace mpt
505
506
507
508 #endif // MPT_ENDIAN_FLOATINGPOINT_HPP