Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RISC-V RV32I soft float lib calls MUL and MULHU instructions in __muldf3

Tags:

riscv

I'm using current riscv-tools to build a firmware image for the PicoRV32 core. The firmware requires floating point, so I'm using -msoft-float. This are the compiler/linker options I am using:

-Os -m32 -march=RV32I -msoft-float -ffreestanding -nostdlib -lgcc

in this configuration, __muldf3 is provided by (according to the linkers -Map output):

/opt/riscv/lib/gcc/riscv64-unknown-elf/4.9.2/soft-float/32/libgcc.a(dp-bit.o)

But this code is not compatible with the RV32I ISA: It uses the MUL and MULHU instructions!

How do I get soft-float for the plain RV32I ISA? Do I need to compile my own version of libgcc.a? Are there instructions somewhere on how to do this?

like image 680
CliffordVienna Avatar asked Oct 19 '22 08:10

CliffordVienna


1 Answers

As you've noticed, the "-march=" flag only affects the current translation unit, not the libraries which get generated at toolchain build time.

Although there exist "disable-atomics"/"disable-float" configuration flags for building the toolchain, there is no multilib option for multiply/divide because they don't affect the ABI; the assumption is that the execution environment can emulate those instructions.

To the last point, the latest Privileged ISA v1.7 is designed such that you can run mul/div code and then trap into the machine-mode to emulate the mul/div instructions (you can even trap into M-mode while running in M-mode!). You'd have to provide your own mul trap handler in M-mode (probably located in your own crt0 file and linked in at compile time).

I instead recommend you try the "--with-arch" flag. A recent patch supports the --with-arch flag, so it's possible to build a gcc that by default won't generate multiply/divide. This will prevent libgcc from containing those instructions. You can try adding --with-arch=RV32I to the gcc configure line (to do so, you'll have to modify Makefile.in in the riscv-gnu-toolchain).

like image 195
Chris Avatar answered Oct 22 '22 23:10

Chris