PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Floating Point Numbers - Looking for critique

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

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()));
}
Have you considered using 32.32 fixed numbers (or even 48.16) instead of floating point numbers?

32.32 fixed numbers have a huge range and are probably faster then any floating point implementations.
Kzinti wrote:Have you considered using 32.32 fixed numbers (or even 48.16) instead of floating point numbers?

32.32 fixed numbers have a huge range and are probably faster then any floating point implementations.

What's the best way to do, say, a multiplication for those numbers? Do you know a good implementation for comparison?

- HM
hm wrote:What's the best way to do, say, a multiplication for those numbers? Do you know a good implementation for comparison?


I do not have any implementation. For some reason I assumed you needed bigger ranges to handle world positions. You usually only need substractions/additions on these and can do the more complex maths using local space coordinates. If this doesn't apply to you, well, yeah... 32.32 multiplications are probably nasty. But still easier to implement then floating point, and also faster. Just break it down into multiple 32 bits multiplies and add the resulting factors together.

Good luck if you need divisions by non-power of two =)
If I were doing this I'd just use IEEE floating point and be done with it. For the people who want to continue to use fixed point and handle the problems associated with it, let them perform the TnL stuff themselves and submit fixed point values directly to the rasterizer.

Until you're running on a device with hardware rasterization capabilities, a software rasterizer will always be the long pole in the tent. Even then, the overwhelming perf win you get from offloading the rasterization duties to dedicated hardware should free up the CPU to handle the additional workload in dealing with floating point data.

From what I recall the floating point library in the ARM development kit is much faster than the MS implementation provided with Windows CE. I think even the Intel Compiler has a faster fp lib than Microsoft. Intel might have an eval version you could download and try for comparison purposes.
to my knowledge, the Intel floating-point library just comes with the compiler's run-time and is automatically linked against when using that compiler. i don't think one can get it extra.

especially problematic since HM probably wants it in open source...

yet, i can confirm that it is (together with the Intel compiler) several times faster than Microsoft's implementation.

i'm not sure how what this exactly means, but the documentation (NDA) of a device we will be using soon states that the SDK includes a 'logarithmic FPU'. whatever it is, they recommend using it over the IEEE floating-point emulation. i was not able to find anything related to this in the SDK though. does anybody know more about this?

btw: HM, if you want i can time your implementation against the Intel Compiler. i'm sure you have some test code...

Daniel
drgoldie wrote:HM, if you want i can time your implementation against the Intel Compiler. i'm sure you have some test code...


Thanks, Daniel. But not necessary, I have the Intel compiler myself.

- HM
What does the right shift on the mantissa in the Add do?

Float a,b;
a = Float::fromF(2.0f);
b = Float::fromF(0);
a += b;
a += b;
...

a gets smaller each time

Personal preference, having default constructors for the basic types rather than using fromX

Float a(2.0f),b(0.0f);
hm · 28 Feb 2006, 17:58 UTC — Questions
pappaxray wrote:What does the right shift on the mantissa in the Add do?

It's basically static error analysis, which tells you that you loose a bit of precision with every add. Alternatively you could normalize after each addition, but that's what makes IEEE floats so expensive, and which is what I tried to avoid here.

Constructors vs. fromX: Well the issue is compiler-generated type conversions, especially when you use integer and integeres with fixed point semantics values along the Float type. Finding those bugs is totally time consuming and nasty, that's why I have the fromX syntax. But yes, it's a matter of preference.

- HM

PS: I ended up using regular floats because of the precision requirements I had to meet...