15 #ifndef MOTIVE_MATH_FLOAT_H_
16 #define MOTIVE_MATH_FLOAT_H_
21 #include "motive/math/range.h"
27 static const uint32_t kExponentMask = 0x000000FF;
28 static const int kExponentShift = 23;
29 static const int kExponentOffset = 127;
36 static const int kInfinityExponent = 128;
37 static const int kMaxFloatExponent = 127;
38 static const int kMaxInvertableExponent = 126;
39 static const int kMinInvertableExponent = -126;
40 static const int kMinFloatExponent = -126;
41 static const int kZeroExponent = -127;
44 extern const float kMinInvertablePowerOf2;
45 extern const float kMaxInvertablePowerOf2;
46 extern const Range kInvertablePowerOf2Range;
60 inline int ExponentAsInt(
const float f) {
63 return ((u.i >> kExponentShift) & kExponentMask) - kExponentOffset;
71 inline float ExponentFromInt(
const int i) {
73 u.i = ((i + kExponentOffset) & kExponentMask) << kExponentShift;
86 inline float ReciprocalExponent(
const float f) {
87 return ExponentFromInt(-ExponentAsInt(f));
96 inline float SqrtReciprocalExponent(
const float f) {
97 return ExponentFromInt(-ExponentAsInt(f) / 2);
102 inline float MaxPowerOf2Scale(
const float f,
const int max_exponent) {
103 return ExponentFromInt(
104 std::min(kMaxFloatExponent, max_exponent - ExponentAsInt(f)));
111 inline float ClampNearZero(
const float x,
const float epsilon) {
112 const bool is_near_zero = std::fabs(x) <= epsilon;
113 return is_near_zero ? 0.0f : x;
118 #endif // MOTIVE_MATH_FLOAT_H_