230 likes | 455 Views
Coping with Fixed Point. Mik BRY CEO mbry@apoje.com. 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. Fixed Point theory and history. Widely used in software 3D
E N D
Coping with Fixed Point Mik BRY CEO mbry@apoje.com
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
Fixed Point theory and history • Widely used in software 3D • Fixed Form • Fast and simple to implement
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.
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
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
Floating Point to Fixed Point • Floats format : standardized • Maths errors support • Convert Fixed and Float numbers
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)
So : Float = mantissa*2^(exponent-127) Fixed = number*2^16
Representation errors support • Floats IEEE Standard has a full range of exception handling • Floats support for NaN and infinite • Overflow and Underflow are checked
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
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
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
Easy to implements • So Fixed Points fit perfectly to ARM cores… No needs for DSP or FPU Caution of Divide support And cache memory
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
Maintain accuracy and overflows and optimization tricks • Accuracy and range • Overflow underflow • Avoiding pitfalls • Optimization tricks
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.
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
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
Avoiding pitfalls Overflow Integer Range Also divide errors (try to not use it)
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.
Questions? Mik BRY mbry@apoje.com