Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cFloat.h
Go to the documentation of this file.
1 //
5 //
6 
7 #ifndef _INC_cFloat_H
8 #define _INC_cFloat_H
9 #ifndef NO_PRAGMA_ONCE
10 #pragma once
11 #endif
12 
13 #include "cTypes.h"
14 #include "cBits.h"
15 #include "cUnitTestDecl.h"
16 #include "cDebugAssert.h"
17 
18 namespace Gray
19 {
21  {
25  public:
27 
28  static const UINT32 k_uOne = 0x3f800000;
29  static const UINT32 k_uTwo = 0x40000000;
30 
31  static const UINT32 k_SIGN_MASK = 0x80000000;
32 
33  static const UINT32 k_EXP_MASK = 0x7f800000;
34 
35  static const UINT32 k_MANT_MASK = 0x007fffff;
36  static const UINT32 k_MANT_BITS = 23;
37 
38  public:
39  cFloat32() noexcept
40  {
41  // undefined.
42  }
43  cFloat32(float f) noexcept
44  {
45  m_v.u_f = f;
46  }
47 
48  void put_Bits(UINT32 dw) noexcept
49  {
50  m_v.u_dw = dw;
51  }
52 
53  static inline UINT32 toBits(float src) noexcept
54  {
57  UINT32 dst = 0;
58  STATIC_ASSERT(sizeof(src) == sizeof(dst), toBits);
59  ::memcpy(&dst, &src, sizeof(dst));
60  return dst;
61  }
62  static inline float fromBits(UINT32 src) noexcept
63  {
66  float dst = 0;
67  STATIC_ASSERT(sizeof(src) == sizeof(dst), fromBits);
68  ::memcpy(&dst, &src, sizeof(dst));
69  return dst;
70  }
71 
72  // bool get_Negative() const
73  // short get_Exponent() const
74  UINT32 get_Mantissa() const noexcept
75  {
76  return (m_v.u_dw & k_MANT_MASK);
77  }
78 
79  UNITTEST_FRIEND(cFloat);
80  };
81 
83  {
87 
88  public:
90 
91  static const UINT64 k_SIGN_MASK = CUINT64(80000000, 00000000);
92 
93  static const UINT64 k_EXP_MASK = CUINT64(7FF00000, 00000000);
94 
95  static const UINT64 k_MANT_MASK = CUINT64(000FFFFF, FFFFFFFF);
96  static const UINT32 k_MANT_BITS = 52;
97 
98  public:
99  cFloat64() noexcept
100  {
101  // undefined value.
102  }
103  cFloat64(double d) noexcept
104  {
105  m_v.u_d = d;
106  }
107 
108  void put_Bits(UINT64 qw) noexcept
109  {
110  m_v.u_qw = qw;
111  }
112 
113  static inline UINT64 toBits(double src) noexcept
114  {
117  UINT64 dst;
118  STATIC_ASSERT(sizeof(src) == sizeof(dst), toBits);
119  ::memcpy(&dst, &src, sizeof(dst));
120  return dst;
121  }
122  static inline double fromBits(UINT64 src) noexcept
123  {
126  double dst;
127  STATIC_ASSERT(sizeof(src) == sizeof(dst), fromBits);
128  ::memcpy(&dst, &src, sizeof(dst));
129  return dst;
130  }
131 
132 #if 0
133  // bool get_Negative() const
134  short get_Exponent() const noexcept
135  {
136  // TODO FIX SIGN
137  return (short)((m_v.u_qw & k_EXP_MASK) >> sdf);
138  }
139 #endif
140  UINT64 get_Mantissa() const noexcept
141  {
142  return (m_v.u_qw & k_MANT_MASK);
143  }
144  };
145 
146  // class cFloat80 // long double or "double double" NOT in M$?
147 };
148 
149 #endif // _INC_cFloat_H
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define CUINT64(h, l)
Definition: cBits.h:39
#define STATIC_ASSERT(exp, name)
Definition: cDebugAssert.h:24
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
Definition: cFloat.h:21
cUnion32 m_v
holds the float32.
Definition: cFloat.h:26
cFloat32() noexcept
Definition: cFloat.h:39
void put_Bits(UINT32 dw) noexcept
Definition: cFloat.h:48
UINT32 get_Mantissa() const noexcept
Definition: cFloat.h:74
cFloat32(float f) noexcept
Definition: cFloat.h:43
static float fromBits(UINT32 src) noexcept
Definition: cFloat.h:62
static UINT32 toBits(float src) noexcept
Definition: cFloat.h:53
Definition: cFloat.h:83
static UINT64 toBits(double src) noexcept
Definition: cFloat.h:113
cUnion64 m_v
holds the float64.
Definition: cFloat.h:89
cFloat64(double d) noexcept
Definition: cFloat.h:103
UINT64 get_Mantissa() const noexcept
Definition: cFloat.h:140
static double fromBits(UINT64 src) noexcept
Definition: cFloat.h:122
cFloat64() noexcept
Definition: cFloat.h:99
void put_Bits(UINT64 qw) noexcept
Definition: cFloat.h:108
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
Definition: cDebugAssert.h:29
Definition: cTypes.h:66
float u_f
32 bit float.
Definition: cTypes.h:79
UINT32 u_dw
32 bit unsigned
Definition: cTypes.h:78
double u_d
assumed to be 64 bits.
Definition: cTypes.h:119
UINT64 u_qw
64 bits = QuadPart = ULONGLONG.
Definition: cTypes.h:122