Coping with Fixed Point

# Coping with Fixed Point

## Coping with Fixed Point

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Coping with Fixed Point Mik BRY CEO mbry@apoje.com

2. 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

3. Fixed Point theory and history • Widely used in software 3D • Fixed Form • Fast and simple to implement

4. 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.

5. 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

6. 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

7. Floating Point to Fixed Point • Floats format : standardized • Maths errors support • Convert Fixed and Float numbers

8. 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)

9. So : Float = mantissa*2^(exponent-127) Fixed = number*2^16

10. Representation errors support • Floats IEEE Standard has a full range of exception handling • Floats support for NaN and infinite • Overflow and Underflow are checked

11. 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

12. 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

13. 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

14. Easy to implements • So Fixed Points fit perfectly to ARM cores… No needs for DSP or FPU Caution of Divide support And cache memory

15. 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

16. Maintain accuracy and overflows and optimization tricks • Accuracy and range • Overflow underflow • Avoiding pitfalls • Optimization tricks

17. 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.

18. 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

19. 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

20. Avoiding pitfalls Overflow Integer Range Also divide errors (try to not use it)

21. 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.

22. Questions? Mik BRY mbry@apoje.com