Coping with Fixed Point

Coping with Fixed Point
Mik BRY CEO
[email protected]
© Copyright Khronos Group, 2004 - Page 1
Overview
• Fixed Point theory and history
• Floating Point to Fixed Point
• Maintain accuracy avoid overflows and optimization tricks
• Effective use of Fixed Point in OpenGL ES
© Copyright Khronos Group, 2004 - Page 2
Fixed Point theory and history
• Widely used in software 3D
• Fixed Form
• Fast and simple to implement
© Copyright Khronos Group, 2004 - Page 3
Widely used in software 3D
Fixed Point number is represented by a real number in an integer format
with an imaginary radix point separating the integer and fractional part.
Prior to HW 3D, fixed point was widely used since the beginning of realtime
3D.
But in small handheld devices, we need to go back to a constrained world
where we have to take care of limited memory, tiny screen. And also on
certain targets avoids floating computings.
© Copyright Khronos Group, 2004 - Page 4
Fixed Form used in OpenGL ES
• Signed format : s15.16 used in
OpenGL ES
GLFixed = number*2^16
GLFixed = number<<16
#define FNUM int
// Convert from int to fixed number
#define INT2FNUM(x) x<<16
// Convert from fixed math to int
#define FNUM2INT(x) x>>16
© Copyright Khronos Group, 2004 - Page 5
Fast and simple to implement
Basic math operations
Adding/Sub : same as int opts
FNUM a = INT2FNUM(1);
FNUM b = INT2FNUM(2);
a += b;
Multiply/div (avoid it see later)
#define FMUL(x,y) (x*y) >>16
#define FDIV(x,y)(x/y)>>16
Comparisons same as int excepting zero compare
© Copyright Khronos Group, 2004 - Page 6
Floating Point to Fixed Point
• Floats format : standardized
• Maths errors support
• Convert Fixed and Float numbers
© Copyright Khronos Group, 2004 - Page 7
Floats format
Floating format is now the commonly
used number format for 3D operations.
Instead of always multiply by a fixed
exponent as in Fixed Point, It uses
Exponent:
Float a = mantissa*2^(exponent-127)
© Copyright Khronos Group, 2004 - Page 8
So :
Float = mantissa*2^(exponent-127)
Fixed = number*2^16
© Copyright Khronos Group, 2004 - Page 9
Representation errors support
• Floats IEEE Standard has a full range of exception handling
• Floats support for NaN and infinite
• Overflow and Underflow are checked
© Copyright Khronos Group, 2004 - Page 10
Convert Fixed and float numbers
In preprocessing and constants vars it is transparent
#define FNUM_PI (int)(3.14f*65536) // 2^16=65536
Simple implementation, not fast at all:
Float to Fixed:
fixedNum = (int)(floatNum*65536
Fixed to Float:
floatNum = ((float)fixedNum)/65536
Without Floating support at all in C compiler it is a little bit more tricky
© Copyright Khronos Group, 2004 - Page 11
Effective use of Fixed Point in OpenGL ES
• Why Fixed Points in OpenGL ES CL?
• ARM chipset : no floating point support
• Easy to implements in embedded systems
• But not as perfect as Floating math so 2 OpenGL ES profiles
© Copyright Khronos Group, 2004 - Page 12
ARM architecture
Designed for low power consumption so :
• No floating point support in mainstream ARM7 – ARM9 cores
• No divide support in ARM7
• Small cache memory L1 and no L2
© Copyright Khronos Group, 2004 - Page 13
Easy to implements
• So
Fixed Points fit perfectly to ARM cores…
No needs for DSP or FPU
Caution of Divide support
And cache memory
© Copyright Khronos Group, 2004 - Page 14
OpenGL ES support for Fixed Math
Common Lite Profile is a strict fixed point implementation
GLFixed : a 32 bits integer
Clampx :
GL Commands mapping using fixed math: x instead of f
glClearColorx(0, 0, 0, 0);
OES_Fixed_Point extension
© Copyright Khronos Group, 2004 - Page 15
Maintain accuracy and overflows and
optimization tricks
• Accuracy and range
• Overflow underflow
• Avoiding pitfalls
• Optimization tricks
© Copyright Khronos Group, 2004 - Page 16
Accuracy and Range
A fixed point number has a limited integer range. It is not possible to
represent very large and very small numbers.
integer range in GLFixed : -32768 < integer part < 32768 (2^15)
Fractionnal accuracy : 0,0000152587890625 // 1/(2^16)
A fixed point number has limited accuracy.
You must choose small numbers and inputs
a normalize data as possible.
© Copyright Khronos Group, 2004 - Page 17
Overflow underflow
Overflow
- An "overflow" will occur when the result of a arithmetic operation is too large to fit into
the fixed representation of a fixed point number
a = 0x7FFF << 16
b = 0x20 << 16
a += b // An overflow
Underflow
When you used a not enough accurate value for fraction, like in trigo maths
But in fixed point no exception handling
Using 64 intermediate numbers but speed consuming
© Copyright Khronos Group, 2004 - Page 18
Optimization tricks
• Trigonometric operations
Using LookupTable best with 1024 bytes size first quadrant and s8:24 for
avoiding underflow.
• Square operations
Using logarithm functions LUT are too big for small cache
• Use other fixed formats range s24:8 for larger numbers and convert to
GLFixed:
Fixed24_8Num = glFixedNum>>8 // You lost some precision but higher
integer range
If (Fixed24_8Num&0x7F000000 != 0) error // to big number
Else glFixedNum = Fixed24_8Num<<8 // convert to GLFixed
© Copyright Khronos Group, 2004 - Page 19
Avoiding pitfalls
Overflow
Integer Range
Also divide errors (try to not use it)
© Copyright Khronos Group, 2004 - Page 20
Fixed Point in OpenGL ES
In all Profile
• Easy to implements in using same methods as floating ones and using
GLFixed
• Take care of pitfalls and use small numbers
• Perfect for limited devices with small memory and screen and a simple
ARM processor.
© Copyright Khronos Group, 2004 - Page 21
Questions?
Mik BRY [email protected]
© Copyright Khronos Group, 2004 - Page 22