Convert an integer (r12) to float (f12) in a sane, efficent way?
lets say r12=0000 0006
and i would want to convert it to 0x40c0 0000 (float value of 6)
how would i do that?
i read the Conversion from integer to float in the Working with floats thread, but i didnt get it
#r3 holds your integer value to convert, result will be placed into f1.
#This code assumes r3, r4, f1, and f2 are all safe. Adjust choosing which registers to use accordingly for your code's safety requirements
lis r4, 0x4330
stw r4, -0x8 (sp)
lis r4, 0x8000
stw r4, -0x4 (sp)
lfd f2, -0x8 (sp) # load magic double into f2
xoris r3, r3, 0x8000 # flip sign bit
stw r3, -0x4 (sp) # store lower half (upper half already stored)
lfd f1, -0x8 (sp)
fsub f1, f1, f2 # complete conversion

Since you're using r12 as your 'input' you need to substitute all instances of r3 in the above source to r12. Then you need to select another safe register to use to substitute for using r4.

Not only that, you need to use two safe float registers to substitute for f1 and f2. f12 and f13 should be good to go. Replace f1 with f12, replace f2 with f13.

Thus, f12 will contain the resulting float value. One issue I do see is that the result is in double precision, no point for that since a whole hex integer number will NEVER need to be in double precision when converted to float value. Plus if you happen to store your float result as single precision with the above snippet of code operated in double precision, this will cause issues on Real Hardware. So let's convert the float result to single precision (32-bit). Then finally store it to memory and load it back to r12.

frsp f12, f12
stfs f12, -0x4 (sp)
lwz r12, -0x4 (sp)

Try that out. This should make r12 be 0x40C00000

Forum Jump:

Users browsing this thread: 1 Guest(s)