Converting Floaters to Integers for ASM Codes

This is a mini guide to demonstrate how to change a floating point value to an integer. There are two methods available.

Let's say f1 has our floating point value that we want to convert to integer. Once converted to an integer, we will want the value at memory address 0x80001554.

f1 has the value of 0x403EDF5840000000

f1 has the value of 0x403EDF5840000000

Method #1 (Using stfiwx)

fctiw f13, f1

f1 is converted to an integer, the result is stored in f13. f13's value is now 0xFFF800000000001F

Btw, here is a good converter to use to know beforehand what your converted result will be (in decimal) - https://babbage.cs.qc.cuny.edu/IEEE-754.old/64bit.html

The 1F represents the decimal value 31. The actual exact decimal amount from the conversion was 30.872440338134766. Standard rounding was applied after the conversion which is why the result is 31 (0x1F)

We want the 0x0000001F (our hex integer value) from f13. To accomplish this we will use the stfiwx instruction. The instruction stores the converted float in the same manner as a stwx type instruction. Thus we need to set the values of two registers to store to 0x80001554.

lis r12, 0x8000

li r11, 0x1554

stfiwx f13, rX, rY

The stfiwx instruction takes the combined values of rX and rY as the memory address where the value will be stored to.

stfiwx f13, r12, r11

The stfiwx instruction will take just the second word value of f13 and store it to 0x80001554

Word at address 0x80001554 is now 0x0000001F (our integer value)

Method #2 (Using stfd)

fctiw f13, f1

f1 is converted to an integer, the result is stored in f13. f13's value is now 0xFFF80000 0000001F

We want the 1F (our integer value) from f13. To accomplish this we must store the whole floating point double to 0x80001550 so the word at 0x80001554 will have our integer value.

lis r12, 0x8000

stfd f13, 0x1550 (r12)

Word at address 0x80001550 is now 0xFFF80000

Word at address 0x80001554 is now 0x0000001F (our integer value)

Side Note: fctiw rounds the integer (if needed) to its nearest whole number; aka standard rounding. Use fctiwz instead if you want to round to the lowest whole number. If fctiwz was done for the steps above, the hex integer result would have been 0x1E (decimal value of 30.8~ rounded down to 30)