Re: Software FPU emulation in linux kernel

Michael Schmitz
2025-03-03T23:39:01Z

Thorsten,

On 4/03/25 05:22, Thorsten Otto wrote:
> 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.

That is correct - the FPSP and IFPSP packages are only meant to handle
unimplemented instructions on the 040 and 060 processors, respectively.
These packages were part of Motorola's developer support for 040/060,
and are used unmodified (aside from kernel specific asm glue) by the
Linux kernel.

The generic math emulation support was added much later, and is largely
incomplete (many functions are just stubs, as you found out). The
Kconfig help text for the M68KFPU_EMU option does note that BTW.

> 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.
As I recall it, the main purpose was to keep kernel and a few important
system utilities from blowing up on 030 machines with no FPU. User space
programs that need to use trig functions or other FPU implemented
functions can be compiled with -m soft_float (or something along those
lines) on those systems.
>
>
> 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.

I'm sure the FPU emulation code can be improved a great deal. In this
particular instance however, both operands are checked for infinity
first. Only if the mantissa of (one of) those operands is zero will the
NAN condition be raised.

I suspect this is mandated by some sort of standard - zero mantissa at
infinite exponent makes no sense as a number.

> 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?

a0 - a2 and d0 - d5 are saved on the stack on entry in fpu_emu, and
restored in ret_from_exception. What happens in between to these
registers is up to assembly or C code. These registers are never
expected to be preserved, so code can't rely on them having any specific
content.

I cannot locate the specific assembly function you mention
(fp_long_ext2ext). Can you post code or disassembly to illustrate the
problem?

> 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.
That might be a genuine error in the sqrt algorithm there - can't see
how it does arise though.
> So essentially: is that emulation actually used anywhere?

Not beyond the very basic stuff that did get implemented, no. You may
have to look elsewhere for a feature complete FPU emulation (does netbsd
have that?).

Cheers,

Michael

>
>
>
>
>