Convert Hex Integer to 32 bit Float (ASM)
#1
Here's a little snippet of PPC ASM I made that will convert your Hex integer to a IEEE-754 32 bit standard Float

The integer can only be a byte or halfword, and it must NOT be negative.

Implementing negatives (neg. halfs and bytes) shouldn't be that difficult. However, adding the ability to do words too, may be a bit of a pain.

This is written to be as a subroutine (via a function call), you will need to add LR storage plus stack pushing and popping.

Args: r3 = Integer to convert
Return Value: r3 = the Float, or -1 for error



Raw ASM:
5464001F 40A20030
38A0001F 38C0007F
7C640034 7CE42850
7D263A14 5529B810
20870017 7C632030
5463027E 7D231B78
48000008 3860FFFF
4E800020

Source:
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# Check For Negative Value, and Check if Halfword/Byte wasn't Used #
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

clrrwi. r4, r3, 16
bne+ skip_conversion #Not doing words or negative values

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# Set Static Register Values #
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

li r5, 31 #Used for formula to calculate amount to add to the Bias
li r6, 127 #This is the IEEE 754 Bias Standard

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# Start Conversion, Count Leading Zeros, Give Variable of Bias Equation #
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

cntlzw r4, r3 #Count the leading zeros til first 1
subf r7, r4, r5 #This will give us the amount to add to the Bias

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# Calculate Final Bias, Store Bias to Bits 1 thru 8 #
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

add r9, r6, r7 #Add to bias
slwi r9, r9, 23 #Shift the Bias over to bits 1 thru 8, bit 0 is reserved

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# Calculate where to Store Fraction Bits at Beginning of Mantissa #
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

subfic r4, r7, 23 #This will give amount of left-hand shift to place fraction bits at beginning of mantissa
slw r3, r3, r4 #Execute the shift
clrlwi r3, r3, 9 #Get rid of possible '1' bit that is left in bit 8's spot

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# Final Step, Insert the Mantissa Bits #
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

or r3, r9, r3 #Place Mantissa bits with all other bits now. Use logical OR to not erase all of our progress
b the_end

skip_conversion:
li r3, -1

the_end:
blr
Reply


Messages In This Thread
Convert Hex Integer to 32 bit Float (ASM) - by Vega - 08-22-2019, 09:35 PM

Forum Jump:


Users browsing this thread: 2 Guest(s)