Mario Kart Wii Gecko Codes, Cheats, & Hacks

Full Version: Optimal Int to Float Function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This code is the most optimal way to convert an integer of any value to float, supporting negative values and being self contained:

# Args:    r3 = Integer to convert to float
# Returns: f1 = Float representation of the input integer

lis r4, 0x5980
ori r4, r4, 0x0004
stw r4, -0x4 (sp)
lis r4, 0x4330

lfs f1, -0x4 (sp)

xoris r3, r3, 0x8000
stw r3, -0x4 (sp)
stw r4, -0x8 (sp)

lfd f0, -0x8 (sp)
fsubs f1, f0, f1

NOTE: This is meant to be called as a function
How does this differ from way back when Salmon01 posted a version of this he found in the PPC Compiler Writer's Guide?

When taking what Salmon provided and changing it to be self-contained, I ended up with this (untested) ---

stwu sp, -0x30 (sp)
stmw r30, 0x8 (sp)
stfd f31, 0x10 (sp)

lis r30, 0x4330
lis r31, 0x8000
stmw r30, 0x18 (sp)
lfd f31, 0x18 (sp)
xoris r3, r3, 0x8000
stw r3, 0x1C (sp)
lfd f1, 0x18 (sp)
fsub f1, f1, f31

lfd f31, 0x10 (sp)
lmw r30, 0x8 (sp)
addi sp, sp, 0x30

Also what's the purpose of the lis r31, 0x5980 instruction? Floats are by far my weakest point of my ASM knowledge.