WiiRd forum

Wii & Gamecube Hacking => Wii Game hacking help => Topic started by: paprika_killer on June 08, 2010, 12:04:33 PM

Title: Converting Floats to Ints (or Words)
Post by: paprika_killer on June 08, 2010, 12:04:33 PM
So I have this Floating point loaded into a free FP register, and now I want to convert it to an Integer, or preferably, a Word, and then store it in a normal register.
How do I do this?

(converting it back isn't necessary, but for future reference it would be nice to know how as well)
Title: Re: Converting Floats to Ints (or Words)
Post by: dcx2 on June 08, 2010, 06:26:42 PM
I know there's a way to cast floats to ints.  It involves a certain instruction...I think it's fctwiz.  (Float Convert To Integer Word, rounded toward Zero)  I believe you also need some stack space; push the FP register onto the stack, and then pop the value off into a integer register.

If no one answers you by the end of the day, when I get home I'll see if I can find the place in my notes where I've dealt with this before.
Title: Re: Converting Floats to Ints (or Words)
Post by: paprika_killer on June 10, 2010, 03:25:50 PM
So anyone got any cool ideas how to do this?
I looked up fctiwz and it takes 2 floating point registers as parameters:

Parameters

FRT    Specifies the floating-point register where the integer result is placed.
FRB    Specifies the source floating-point register for the floating-point operand.

So I'm not really sure how to get the actual integer to a normal register.
Title: Re: Converting Floats to Ints (or Words)
Post by: dcx2 on June 10, 2010, 04:27:47 PM
After you fctiwz, push the new float register onto the stack, and then pop it off into a normal register.  There may or may not be a 32-/64-bit issue...

Here's an example, from the end of the following website:
http://www.lightsoft.co.uk/PD/rob/ppcfloat.html


          fctiwz   f0,f1                  ; convert
          stfd    f0,(r8)                ; store
          lwz     r3,4(r8)               ; load into integer register

They are using stfd instead of stfs.  d = double precision = 64 bits (hence the 4(r8)), s = single precision = 32 bits.  So the following might work, although there might be a reason why they're using stfd...

          fctiwz   f0,f1                  ; convert
          stfs    f0,(r8)                ; store
          lwz     r3,0(r8)               ; load into integer register

EDIT: note that they aren't using the stack here. =(  You need some unused memory, and usually the stack is the safest way to make unused memory.  You can pretend r8 is pointing at a Gecko Register for the purposes of the example
Title: Re: Converting Floats to Ints (or Words)
Post by: paprika_killer on June 10, 2010, 04:57:30 PM
I never worked with this "stack" before, is there anything about it here on wiird?
Title: Re: Converting Floats to Ints (or Words)
Post by: dcx2 on June 10, 2010, 05:15:03 PM
The stack is just a portion of memory like any other portion of memory, but it follows certain rules.  It's used to make room for local variables that are created in a function, to store the previous stack frame pointer and LR, to temporarily store registers that you don't want to over-write, and for passing arguments to and from functions.

We use stwu to make room on the stack (stwu stores the old stack pointer with stw part and also modifies the value of the stack pointer so that there is more room with the u part).  When you're done, you addi to recover the room you made on the stack.

Here's a short example on using the stack and float registers.
http://wiird.l0nk.org/forum/index.php/topic,5561.0.html

Here's a lengthier discussion related to C0 codes, with a link to some documentation regarding the stack
http://wiird.l0nk.org/forum/index.php/topic,4810.0.html

Here's an in-depth tutorial on walking the stack that I wrote.  About half-way through, I explain what a function prologue and epilogue are; they use the stack to save and restore the values of registers that will be over-written.
http://wiird.l0nk.org/forum/index.php/topic,5080.0.html
Title: Re: Converting Floats to Ints (or Words)
Post by: paprika_killer on June 10, 2010, 06:10:24 PM
wow, that last one was an excellent tutorial.