Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using software floating point on x86 linux

Is it (easily) possible to use software floating point on i386 linux without incurring the expense of trapping into the kernel on each call? I've tried -msoft-float, but it seems the normal (ubuntu) C libraries don't have a FP library included:

$ gcc -m32 -msoft-float -lm -o test test.c
/tmp/cc8RXn8F.o: In function `main':
test.c:(.text+0x39): undefined reference to `__muldf3'
collect2: ld returned 1 exit status
like image 794
bdonlan Avatar asked Jun 19 '09 15:06

bdonlan


1 Answers

It is surprising that gcc doesn't support this natively as the code is clearly available in the source within a directory called soft-fp. It's possible to compile that library manually:

$ svn co svn://gcc.gnu.org/svn/gcc/trunk/libgcc/ libgcc
$ cd libgcc/soft-fp/
$ gcc -c -O2 -msoft-float -m32  -I../config/arm/ -I..  *.c
$ ar -crv libsoft-fp.a *.o

There are a few c files which don't compile due to errors but the majority does compile. After copying libsoft-fp.a into the directory with our source files they now compile fine with -msoft-float:

$ gcc -g -m32  -msoft-float test.c -lsoft-fp -L. 

A quick inspection using

$ objdump -D --disassembler-options=intel a.out  | less

shows that as expected no x87 floating point instructions are called and the code runs considerably slower as well, by a factor of 8 in my example which uses lots of division.

Note: I would've preferred to compile the soft-float library with

$ gcc -c -O2 -msoft-float -m32  -I../config/i386/ -I..  *.c

but that results in loads of error messages like

adddf3.c: In function '__adddf3':
adddf3.c:46: error: unknown register name 'st(1)' in 'asm'

Seems like the i386 version is not well maintained as st(1) points to one of the x87 registers which are obviously not available when using -msoft-float. Strangely or luckily the arm version compiles fine on an i386 and seems to work just fine.

like image 110
user1059432 Avatar answered Nov 10 '22 07:11

user1059432