Optimal Int to Float Function
#1
Lightbulb 
This code is the most optimal way to convert an integer of any value to float, supporting negative values and being self contained:

Code:
# 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
blr

NOTE: This is meant to be called as a function
Super Mario Eclipse, what Super Mario Sunshine could've been.
Reply
#2
How does this differ from way back when Salmon01 posted a version of this he found in the PPC Compiler Writer's Guide? https://mkwii.com/showthread.php?tid=1252

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

Code:
#Prologue
stwu sp, -0x30 (sp)
stmw r30, 0x8 (sp)
stfd f31, 0x10 (sp)

#Conversion
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

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

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


Forum Jump:


Users browsing this thread: 2 Guest(s)