NDS/DOCfixed point
From Dev-Scene
[edit] Introduction To Fixed Point Numbers
The DS has no floating point unit but it does support fixed point math. What are fixed point numbers? They are integers with some digits interpreted as the fraction. For example we could interpret 1001 as 10.01 (last two digits are the fraction). We can add two fixed point numbers the same way, as we would add integers:
Fixed Point: 10.01 +13.99 =24.00 Integer: 1001 +1399 =2400
This makes fixed point easier to implement than floating point, because we already have an integer ALU in our CPU. A multiplication of two fixed point numbers is a bit more complicated, but not that much:
12.13 * 2.05 = 24.86 (we only have 2 digits fraction) as an integer, we get: 1213 * 205 = 248665
So, we can use integer multiplication, but be have to shift the result 2 digits to the right. Because a computer works with a binary system, we have to work with n binary digits for the integer part of the number and m binary digits for the fraction. In most cases we want negative numbers, too, so we need one bit sign. If, for example, we have a 1.15.16 fixed point number, this means, we have one bit sign, 15 bits for the integer part and 16 bit fraction. This number would fit into a 32 bit signed integer.
[edit] What We Have On The DS
For the DS we need these fixed point numbers:
- v16 = 1.3.12 fixed point number (used for 3D)
- t16 = 1.11.4 fixed point number
- f32 = 1.19.12 fixed point number (used for matrices)
- v10 = 1.0.9 fixed point number (Whoops! 10 bits do not fit into a normal integer but 3 v10 numbers fit into a 32 bit integer, so this format is used for normals in 3D. It is also OK for normals to be between -1 and 1. This is why these fixed point numbers have a long fraction but no integer part!)
- 0.8.8 fixed point number: this format is used for the scaling the extended rotation backgrounds and does not have a typedef in the libnds.