]> git.tdb.fi Git - libs/datafile.git/blob - source/binfloat.h
Remove the loaded flag from PackSource files
[libs/datafile.git] / source / binfloat.h
1 #ifndef MSP_DATAFILE_BINFLOAT_H_
2 #define MSP_DATAFILE_BINFLOAT_H_
3
4 #include "type.h"
5
6 namespace Msp {
7 namespace DataFile {
8
9 /**
10 Facilitates splitting floating-point numbers into parts and putting them back
11 together.  Supports arbitary sizes up to 64 bits.  The 16, 32 and 64 bit
12 formats exactly match those defined by ISO/IEC 60559:2011.
13
14 The exponent is stored in an unbiased form.  The mantissa is stored with the
15 integer part included, aligned to the high bits of a 64-bit integer.
16 */
17 struct BinFloat
18 {
19         struct Bits
20         {
21                 unsigned exponent;
22                 unsigned mantissa;
23
24                 Bits(unsigned);
25         };
26
27         template<typename T>
28         union Conversion
29         {
30                 T f;
31                 typename MatchingInt<T>::UnsignedType i;
32         };
33
34         bool sign;
35         bool infinity;
36         int exponent;
37         UInt64 mantissa;
38
39         static BinFloat explode(UInt64, const Bits &);
40
41         template<typename T>
42         static BinFloat explode_iec559(T v)
43         {
44                 Conversion<T> c;
45                 c.f = v;
46                 return explode(c.i, sizeof(T)*CHAR_BIT);
47         }
48
49         UInt64 compose(const Bits &);
50
51         template<typename T>
52         T compose_iec559()
53         {
54                 Conversion<T> c;
55                 c.i = compose(sizeof(T)*CHAR_BIT);
56                 return c.f;
57         }
58 };
59
60 } // namespace DataFile
61 } // namespace Msp
62
63 #endif