Software FPU emulation in linux kernel

Thorsten Otto
2025-03-03T16:22:21Z

Hi,

i'm currently trying to port the software FPU emulation of linux-m68k to
FreeMiNT, and got some questions about it.

As seen in https://github.com/torvalds/linux/blob/
7eb172143d5508b4da468ed59ee857c6e5e01da6/arch/m68k/kernel/vectors.c#L96 and
https://github.com/torvalds/linux/blob/
7eb172143d5508b4da468ed59ee857c6e5e01da6/arch/m68k/kernel/vectors.c#L119

the FPSP for 040/060 are not installed when the software emulation is used.
However, almost all trigonometric functions are not implemented: https://
github.com/torvalds/linux/blob/7eb172143d5508b4da468ed59ee857c6e5e01da6/arch/
m68k/math-emu/fp_trig.c#L21-L28
What sense does it then make to use an emulation, when only basic instructions
like fadd/fmul etc. are implemented? Programs are not even aborted when using
one of those functions, uprint just prints an error message, and the functions
just return with their input arguments as result instead of calculating the
expected value.

Then i also noticed, that there seem to be some quirks. Eg. the IS_ZERO macro
is defined as
#define IS_ZERO(a) ((a)->mant.m64 == 0)
But that condition is also true for infinities, and there are several places
where IS_ZERO is used before checking for infinities. Eg. in the emulation of
fmul https://github.com/torvalds/linux/blob/
7eb172143d5508b4da468ed59ee857c6e5e01da6/arch/m68k/math-emu/fp_arith.c#L157
+inf * +inf will return a NAN instead of +inf.

Next problem is the interaction between assembler/C. On entry, a2 is loaded
with a pointer to the emulated FPU register set in the thread struct. However,
all the arithmetic functions call out to C functions, which in turn callback
some assembler functions like fp_long_ext2ext. At that time, a2 may contain
any value. Won't that just crash, or did i miss something?

Another problem: the fsqrt function seems to be broken when using arguments <
1.0. Eg. fsqrt(0.75) yields 1.7320508075. Looks like the returned exponent is
off by one there.

So essentially: is that emulation actually used anywhere?