In working through the OpenGL ES stuff I am doing, there are certain areas where the dynamic range of fixed point 16.16 simply is not enough. I tried having different code paths for different numeric ranges, but I am running into limitations that simply are no longer worth it. So I am about to bite the bullet and switch to floating point numbers.
However, I do not want to simply use IEEE floats out of the emulation library, for all the well-known performance reasons. Rather, I started out working on a small floating point class that is a little bit more sloppy than IEEE (e.g. not always nromalized mantissa's, no checks for over/underflow etc.).
I am posting the current first iteration below, and I would like to invite everyone to constructive criticism.
Thanks,
HM
and
However, I do not want to simply use IEEE floats out of the emulation library, for all the well-known performance reasons. Rather, I started out working on a small floating point class that is a little bit more sloppy than IEEE (e.g. not always nromalized mantissa's, no checks for over/underflow etc.).
I am posting the current first iteration below, and I would like to invite everyone to constructive criticism.
Thanks,
HM
- Code:
// ----------------------------------------------------------------------
// A non-IEEE floating point type
//
// The floating point number is represented as a signed 32-bit word,
// where the upper 24 bits form a signed mantissa, and the lower 8 bits
// contain an unsigned exponent.
//
// The exponent is adjusted such that a value of 2^30 corresponds to
// the pattern, e.g. the integer value is truncated and the exponent
// is 128.
//
// 24-bit signed mantissa exponent
// ------------------------------- ---------
// 0100 0000 0000 0000 0000 0000 1000 0000
//
// A value of zero is represented as an all zero word.
// ----------------------------------------------------------------------
class Float {
private:
Float(I32 value) {
m_value = value;
}
Float(I32 mantissa, U8 exponent) {
m_value = mantissa ? (mantissa << 8) | exponent : 0;
}
I32 Mantissa() const {
return m_value >> 8;
}
U8 Exponent() const {
return static_cast<U8>(m_value & 0xffu);
}
public:
Float() {
m_value = 0;
}
Float(const Float& other) {
m_value = other.m_value;
}
Float& operator=(const Float& other) {
m_value = other.m_value;
return *this;
}
Float& operator+=(const Float& other) {
*this = *this + other;
return *this;
}
Float& operator-=(const Float& other) {
*this = *this - other;
return *this;
}
Float& operator*=(const Float& other) {
*this = *this * other;
return *this;
}
Float& operator/=(const Float& other) {
*this = *this / other;
return *this;
}
Float operator+(const Float& other) const {
I32 mant1 = Mantissa() >> 1;
I32 mant2 = other.Mantissa() >> 1;
I32 diff = static_cast<I32>(Exponent()) - static_cast<I32>(other.Exponent());
if (diff >= 0) {
return Float(mant1 + (mant2 >> diff), Exponent() + 1);
} else {
return Float((mant1 >> -diff) + mant2, other.Exponent() + 1);
}
}
Float operator-(const Float& other) const {
I32 mant1 = Mantissa() >> 1;
I32 mant2 = -(other.Mantissa() >> 1);
I32 diff = static_cast<I32>(Exponent()) - static_cast<I32>(other.Exponent());
if (diff >= 0) {
return Float(mant1 + (mant2 >> diff), Exponent() + 1);
} else {
return Float((mant1 >> -diff) + mant2, other.Exponent() + 1);
}
}
Float operator*(const Float& other) const {
if (m_value == 0 || other.m_value == 0)
return Float(0);
else {
I32 result = static_cast<I32>(static_cast<I64>(Mantissa()) * static_cast<I64>(other.Mantissa()) >> 24);
U8 exp = static_cast<I32>(Exponent()) + static_cast<I32>(other.Exponent()) - (128 - 24);
return Float(result, exp);
}
}
Float operator/(const Float& other) const {
return *this * other.Inverse();
}
Float operator-() const {
return Float(-Mantissa(), Exponent());
}
Float operator+() const {
return *this;
}
bool operator==(const Float& other) const {
return Normalized() == other.Normalized();
}
bool operator!=(const Float& other) const {
return Normalized() != other.Normalized();
}
bool operator<(const Float& other) const {
return (*this - other).Mantissa() < 0;
}
bool operator<=(const Float& other) const {
return (*this - other).Mantissa() <= 0;
}
bool operator>(const Float& other) const {
return (*this - other).Mantissa() > 0;
}
bool operator>=(const Float& other) const {
return (*this - other).Mantissa() >= 0;
}
static Float fromF(GLfloat value) {
int exp;
double mant = frexp(value, &exp);
return Float(static_cast<I32>(mant * (1 << 23)), exp + 128 - 23).Normalized();
}
static Float fromX(GLfixed value) {
return Float(value, 128 - 16).Normalized();
}
static Float fromI(GLint value) {
return Float(value, 128).Normalized();
}
GLfloat toF() const {
return static_cast<GLfloat>(ldexp(static_cast<float>(Mantissa()), static_cast<I32>(Exponent()) - 128));
}
GLfixed toX() const {
I32 diff = static_cast<I32>(Exponent()) - 128 - 16;
if (diff < 0)
return Mantissa() >> -diff;
else
return Mantissa() << diff;
}
GLint toI() const {
I32 diff = static_cast<I32>(Exponent()) - 128;
if (diff < 0)
return Mantissa() >> -diff;
else
return Mantissa() << diff;
}
Float Normalized() const;
void Normalize() {
*this = Normalized();
}
Float Inverse() const;
private:
I32 m_value; // the lower 8 bits contain the exponent
// for 0, all bits are 0
};
and
- Code:
namespace {
inline I32 CountLeadingZeros(I32 x) {
#ifdef EGL_USE_CLZ
return _CountLeadingZeros(x);
#else
U8 zeros = 31;
if (x & 0xffff0000) { zeros -= 16; x >>= 16; }
if (x & 0xff00) { zeros -= 8; x >>= 8; }
if (x & 0xf0) { zeros -= 4; x >>= 4; }
if (x & 0xc) { zeros -= 2; x >>= 2; }
if (x & 0x2) { zeros -= 1; }
return zeros;
#endif
}
inline I32 Mul24(I32 a, I32 b) {
I64 product = static_cast<I64>(a) * static_cast<I64>(b);
return static_cast<I32>(product >> 22);
}
}
Float Float::Normalized() const {
U8 exp = Exponent();
I32 mant = Mantissa();
if (mant) {
I32 zeros = CountLeadingZeros(mant < 0 ? -mant : mant);
U8 shift = zeros - 9;
return Float(mant << shift, exp - shift);
} else {
return Float(0);
}
}
Float Float::Inverse() const {
I32 a = Mantissa();
I32 x;
/* 1/(4x) */
static const I32 __gl_rcp_tab[] = { /* domain 0.5 .. 1.0-1/16 */
0x8000, 0x71c7, 0x6666, 0x5d17, 0x5555, 0x4ec4, 0x4924, 0x4444
};
if (a == 0) return Float(0); // or infinity?
bool sign = false;
if (a < 0) {
sign = true;
a = -a;
}
x = __gl_rcp_tab[(a >> 20)&0x7] << 7;
/* four iterations of newton-raphson x = x(2-ax) */
x = Mul24(x,(0x800000 - Mul24(a,x)));
x = Mul24(x,(0x800000 - Mul24(a,x)));
x = Mul24(x,(0x800000 - Mul24(a,x)));
x = Mul24(x,(0x800000 - Mul24(a,x)));
if (sign)
x = -x;
return Float(x, (256 - 44) - static_cast<I32>(Exponent()));
}