Convert Hex Integer to 32 bit Float (ASM)
#4
The PowerPC Compiler Writer's Guide contains a number of helpful algorithms, including a conversion for 32-bit signed integer to float, so any integer between 0x80000000 and 0x7FFFFFFF is supported. I modified that code to work by itself and then turned it into a proper function:

Code:
s32_to_float:
lis r4, 0x4330
stw r4, -0x8 (r1)
lis r4, 0x8000
stw r4, -0x4 (r1)
lfd f2, -0x8 (r1) # load magic double into f2
xoris r3, r3, 0x8000 # flip sign bit
stw r3, -0x4 (r1) # store lower half (upper half already stored)
lfd f1, -0x8 (r1)
fsub f1, f1, f2 # complete conversion
blr

The parameter for this function (passed via r3) would be the integer you wish to convert, and the return value (placed in f1) would be the converted float.

Also note that I used the red zone (area beneath the stack) to store/load values temporarily, this is safe to do in this case as no function calls exist between the store and load operations.

To be fair, your code is definitely more clever than mine, as you actually bothered to learn how floats work instead of just copying code from the web. However, since this one works for negative values and words, I thought it would be useful too.
Reply


Messages In This Thread
RE: Convert Hex Integer to 32 bit Float (ASM) - by salmon01 - 08-22-2019, 11:26 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)