gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > 1.19.12 fixed point?

#47062 - LOst? - Mon Jul 04, 2005 6:36 pm

What is 1.19.12 fixed point? I don't get it. I only know 16:16 and 8:8 fixed point. I have no idea how to create a 1.19.12 fixed point value or edit the decimal point value alone, or the integer value alone.

Another question: When rotating sprites, do you need to use a special amount of LUT Sine values? Some people use 360, but I want to use 256 or 512 because i can wrap it with AND. Can that be arranged?

#47063 - tepples - Mon Jul 04, 2005 7:00 pm

Is 1.19.12 the same as signed 20:12?

And when rotating things, you can arrange your trig tables however you want. Yes, a lot of people use a 256- or 512-entry trig table.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#47085 - sajiimori - Mon Jul 04, 2005 11:34 pm

Yeah, it's more like signed 20.12 than 1.19.12 because it's two's complement. This is as opposed to standard floating point where you can negate a value just by flipping the sign bit.
Code:

typedef s32 fx32;  // 20.12 fixed point


fx32 iToFx32(s32 val)
{
  return val << 12;
}


int fx32ToInt(fx32 val)
{
  return val >> 12;
}


fx32 fractionalPart(fx32 val)
{
  return val & 0xFFF;
}


fx32 replaceIntegerPart(fx32 val, int newInt)
{
  return iToFx32(newInt) + fractionalPart(val);
}


fx32 replaceFractionalPart(fx32 val, fx32 newFract)
{
  return intToFx32(fx32ToInt(val)) + newFract;
}


Last edited by sajiimori on Mon Jul 04, 2005 11:42 pm; edited 1 time in total

#47086 - duencil - Mon Jul 04, 2005 11:39 pm

tepples wrote:
Is 1.19.12 the same as signed 20:12?.


Yes, its the same - the term 1.19.12 is misleading. The leftmost bit indicates the sign. If its a 1, the bits contain a negative number in two's complement form.

As for rotations, there are 512 entry lookup tables in ndslib for SIN and COS already.

#47096 - rize - Tue Jul 05, 2005 4:00 am

there are ndslib/libnds functions for converting various formats to f32 (floatof32 for example).

to convert an f32 to integer just shift right 12 (you throw away all the decimal precision of course)

and converting an int is the opposite (shift left 12).

basically, with this fixed point format, there are 12 bits for the decimal meaning that the fractional part can hold 4096 values. These values are roughtly equivalent to 0/4096 .. 1/4096 .. 2/4096 ... 4095/4096 etc.

it's kind of like having 4 decimal places but having to count by 2.5 intead of 1:

ala

0.0002
0.0004
0.0007
0.0009
etc.

if you just do floatof32(0.000254) or something, it'll essentially round to the nearest f32 value.

Look at the f32 defines in the library and experiment to get a better idea of what's going on.

excuse the rambling nature of hte previous comments

#47099 - LOst? - Tue Jul 05, 2005 4:59 am

Thank you for your answers. This will help me to understand fixed point more!