Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cFloatEst.h
Go to the documentation of this file.
1 //
5 //
6 
7 #ifndef _INC_cFloatEst_H
8 #define _INC_cFloatEst_H
9 #ifndef NO_PRAGMA_ONCE
10 #pragma once
11 #endif
12 #if _MSC_VER >= 1000
13 // #pragma warning(disable:4275) // stupid linkage warning.
14 #endif // _MSC_VER >= 1000
15 
16 #include "Calc.h"
19 
20 namespace GrayLib
21 {
23 
24 #ifdef _MSC_VER
25 #pragma check_stack(off)
26 #if _MSC_VER >= 1300
27 #pragma runtime_checks( "", off )
28 #endif
29 #endif
30 
31  class cFloatEst : public cFloat32
32  {
36 
37  public:
38 
39 #ifdef USE_FLOAT_IEEE
40  static inline float Sqrt(float n) noexcept
41  {
44  UINT32 val = toBits(n);
45  val >>= 1;
46  val += (k_uOne >> 1);
47  return fromBits(val);
48  }
49  static inline float pow2(int iPow)
50  {
53  ASSERT((iPow >= -127) && (iPow <= FLT_MAX_EXP)); // FLT_MIN_EXP ??
54  UINT32 xd = (iPow + 127) << k_MANT_BITS;
55  return fromBits(xd);
56  }
57  static inline int ifloor_log2(float x)
58  {
59  // Get the integer exponent.
60  // Same as (int)floorf(log2f(x)) its very precise and fast
61  UINT32 xd = toBits(x);
62  int nExp = (xd & k_EXP_MASK) >> k_MANT_BITS;
63  nExp -= 127;
64  return nExp;
65  }
66  static inline int iceil_log2(float x)
67  {
68  // Get the integer exponent.
69  // Same as (int)ceilf(log2f(x)) its very precise and fast
70  UINT32 xd = toBits(x);
71  int nExp = (xd & k_EXP_MASK) >> k_MANT_BITS;
72  if (xd & k_MANT_MASK)
73  nExp -= 126;
74  else
75  nExp -= 127;
76  return nExp;
77  }
78 
79 #else // USE_FLOAT_IEEE
80 
81  static inline float Sqrt(float n)
82  {
83  return ::sqrtf(n);
84  }
85  static inline float pow2(int iPow)
86  {
87  return Calc::Pow<float>(2.0f, (float)iPow);
88  }
89  static inline int ifloor_log2(float x)
90  {
91  return (int)::ceilf(Calc::Log(x) * cTypeF<float>::k_Log2e);
92  }
93  static inline int iceil_log2(float x)
94  {
95  return (int)::floorf(Calc::Log(x) * cTypeF<float>::k_Log2e);
96  }
97 #endif // USE_FLOAT_IEEE
98 
99  static inline int ifloor(float f)
100  {
101  // Round down. just cast to int ???
102  return (int) ::floorf(f); // FIX THIS ?
103  }
104  static inline int iceil(float f)
105  {
106  // Round up
107  return (int) ::ceilf(f);
108  }
109  static inline bool IsInteger(float f)
110  {
111  return f == (int)f;
112  }
113 
115  };
116 
117 #ifdef _MSC_VER
118 #pragma check_stack
119 #if _MSC_VER >= 1300
120 #pragma runtime_checks( "", restore )
121 #endif
122 #endif // _MSC_VER
123 
124  class cFloat32SinTable // cSingletonStatic ? cValueCurve
125  {
130 
131  public:
133 
134  public:
135  void CreateSinTable(int iTicks);
136  float Sin(RADIANf_t fVal);
137  // float Cos( RADIANf_t fVal );
138  // float Tan( RADIANf_t fVal );
139  };
140 
141 };
142 
143 #endif // _INC_cFloat_H
#define ASSERT(exp)
Definition: cDebugAssert.h:87
Definition: cFloatEst.h:125
void CreateSinTable(int iTicks)
Definition: cFloatEst.cpp:14
cArrayVal< float > m_Values
sine wave table values.
Definition: cFloatEst.h:132
float Sin(RADIANf_t fVal)
Definition: cFloatEst.cpp:22
Definition: cFloatEst.h:32
UNITTEST_FRIEND(cFloatEst)
static int ifloor_log2(float x)
Definition: cFloatEst.h:57
static int ifloor(float f)
Definition: cFloatEst.h:99
static float pow2(int iPow)
Definition: cFloatEst.h:49
static bool IsInteger(float f)
Definition: cFloatEst.h:109
static int iceil_log2(float x)
Definition: cFloatEst.h:66
static int iceil(float f)
Definition: cFloatEst.h:104
static float Sqrt(float n) noexcept
Definition: cFloatEst.h:40
Definition: cFloat.h:21
static const UINT32 k_MANT_MASK
23 bits = fractional mantissa = FLT_MANT_DIG
Definition: cFloat.h:35
static const UINT32 k_uOne
(UINT32) = float 1.0f (8 bit exponent)
Definition: cFloat.h:28
static const UINT32 k_EXP_MASK
8 bits = signed exponent (base 2)
Definition: cFloat.h:33
static const UINT32 k_MANT_BITS
23 bits = fractional mantissa = FLT_MANT_DIG
Definition: cFloat.h:36
static float fromBits(UINT32 src) noexcept
Definition: cFloat.h:62
static UINT32 toBits(float src) noexcept
Definition: cFloat.h:53
Definition: cMesh.h:22
UNITTEST2_PREDEF(cQuadtree)
float RADIANf_t
type is float radians
Definition: Calc.h:27
static TYPE Log(TYPE a)
Natural (e) log. // negative will throw ?
static const TYPE k_Log2e
M_LOG2E = log2(e) = 1/ln(2)
Definition: Calc.h:57