Load immediate for floats/doubles?

(Benjamin Ou) #1

Is there a way to load arbitrary floating-point numbers into the float registers fx1-fx31?

(Jim Wilson) #2

Try asking the compiler.

rohan:2072$ cat tmp.c
float sub0 (void) { return 0.0; }
float sub1 (void) { return 1.0; }
rohan:2073$ riscv32-unknown-elf-gcc -O2 -S tmp.c
rohan:2074$ cat tmp.s
.file “tmp.c”
.option nopic
.align 1
.globl sub0
.type sub0, @function
fmv.s.x fa0,zero
.size sub0, .-sub0
.align 1
.globl sub1
.type sub1, @function
lui a5,%hi(.LC0)
flw fa0,%lo(.LC0)(a5)
.size sub1, .-sub1
.section .srodata.cst4,“aM”,@progbits,4
.align 2
.word 1065353216
.ident “GCC: (GNU) 8.3.0”

It is easy for 0 if x-regs are the same size as f-regs. For other values we put them in memory and load them. The compiler uses decimal because host float isn’t necessarily the same as target float and the assembler won’t necessarily get this right. But you can always use “.float 1.0” instead if you aren’t worried about obscure issues, this should work fine also.