7/28/2019 Coping With Fixed Point-Bry
1/22
Copyright Khronos Group, 2004 - Page 1
Coping with Fixed PointCoping with Fixed Point
Mik BRY CEO
7/28/2019 Coping With Fixed Point-Bry
2/22
Copyright Khronos Group, 2004 - Page 2
Overviewverview 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
7/28/2019 Coping With Fixed Point-Bry
3/22
Copyright Khronos Group, 2004 - Page 3
Fixed Point theory and historyixed Point theory and history Widely used in software 3D
Fixed Form
Fast and simple to implement
7/28/2019 Coping With Fixed Point-Bry
4/22
Copyright Khronos Group, 2004 - Page 4
Widely used in software 3Didely used in software 3DFixed Point number is represented by a real number in an integer formatwith an imaginary radix point separating the integer and fractional part.
Prior to HW 3D, fixed point was widely used since the beginning of realtime3D.
But in small handheld devices, we need to go back to a constrained worldwhere we have to take care of limited memory, tiny screen. And also oncertain targets avoids floating computings.
7/28/2019 Coping With Fixed Point-Bry
5/22
Copyright Khronos Group, 2004 - Page 5
Fixed Form used inixed Form used in OpenGL ESpenGL ES Signed format : s15.16 used in
OpenGL ES
GLFixed = number*2^16
GLFixed = number
7/28/2019 Coping With Fixed Point-Bry
6/22
Copyright Khronos Group, 2004 - Page 6
Fast and simple to implementast and simple to implementBasic 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/28/2019 Coping With Fixed Point-Bry
7/22
Copyright Khronos Group, 2004 - Page 7
Floating Point to Fixed Pointloating Point to Fixed Point Floats format : standardized
Maths errors support
Convert Fixed and Float numbers
7/28/2019 Coping With Fixed Point-Bry
8/22
7/28/2019 Coping With Fixed Point-Bry
9/22
Copyright Khronos Group, 2004 - Page 9
So :
Float = mantissa*2^(exponent-127)
Fixed = number*2^16
7/28/2019 Coping With Fixed Point-Bry
10/22
Copyright Khronos Group, 2004 - Page 10
Representation errors supportepresentation errors support Floats IEEE Standard has a full range of exception handling
Floats support for NaN and infinite
Overflow and Underflow are checked
7/28/2019 Coping With Fixed Point-Bry
11/22
Copyright Khronos Group, 2004 - Page 11
Convert Fixed and float numbersonvert Fixed and float numbersIn 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
7/28/2019 Coping With Fixed Point-Bry
12/22
Copyright Khronos Group, 2004 - Page 12
Effective use of Fixed Point inffective use of Fixed Point in OpenGL ESpenGL 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 profi les
7/28/2019 Coping With Fixed Point-Bry
13/22
Copyright Khronos Group, 2004 - Page 13
ARM architectureRM architectureDesigned 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
7/28/2019 Coping With Fixed Point-Bry
14/22
Copyright Khronos Group, 2004 - Page 14
Easy to implementsasy to implements So
Fixed Points fi t perfectly to ARM cores
No needs for DSP or FPU
Caution of Divide support
And cache memory
7/28/2019 Coping With Fixed Point-Bry
15/22
Copyright Khronos Group, 2004 - Page 15
OpenGL ESpenGL ES support for Fixed Mathupport for Fixed MathCommon Lite Profile is a strict f ixed point implementation
GLFixed : a 32 bits integerClampx :
GL Commands mapping using fixed math: x instead of f
glClearColorx(0, 0, 0, 0);
OES_Fixed_Point extension
7/28/2019 Coping With Fixed Point-Bry
16/22
Copyright Khronos Group, 2004 - Page 16
Maintain accuracy and overflows andaintain accuracy and overflows andoptimization tricksptimization tricksAccuracy and range
Overflow underflow
Avoiding pitfalls
Optimization tricks
7/28/2019 Coping With Fixed Point-Bry
17/22
Copyright Khronos Group, 2004 - Page 17
Accuracy and Rangeccuracy and RangeA 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.
7/28/2019 Coping With Fixed Point-Bry
18/22
Copyright Khronos Group, 2004 - Page 18
Overflow underflowverflow underflowOverflow
- An "overflow" wi ll occur when the result of a arithmetic operation is too large to f it intothe fixed representation of a fixed point number
a = 0x7FFF
7/28/2019 Coping With Fixed Point-Bry
19/22
Copyright Khronos Group, 2004 - Page 19
Optimization tricksptimization tricks Trigonometric operations
Using LookupTable best with 1024 bytes size first quadrant and s8:24 foravoiding underflow.
Square operationsUsing 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 higherinteger range
If (Fixed24_8Num&0x7F000000 != 0) error // to big number
Else glFixedNum = Fixed24_8Num
7/28/2019 Coping With Fixed Point-Bry
20/22
Copyright Khronos Group, 2004 - Page 20
Avoiding pitfallsvoiding pitfallsOverflow
Integer Range
Also divide errors (try to not use it)
7/28/2019 Coping With Fixed Point-Bry
21/22
Copyright Khronos Group, 2004 - Page 21
Fixed Point inixed Point in OpenGL ESpenGL ESIn all Profi le
Easy to implements in using same methods as floating ones and usingGLFixed
Take care of pitfalls and use small numbers
Perfect for limited devices with small memory and screen and a simpleARM processor.
7/28/2019 Coping With Fixed Point-Bry
22/22
Copyright Khronos Group, 2004 - Page 22
Questions?uestions?
Mik BRY [email protected]