Re: How to use floating point in a module?

From: Linus Torvalds
Date: Sun May 30 2004 - 23:29:29 EST

On Mon, 31 May 2004, Måns Rullgård wrote:
> Floating point is forbidden in kernel code since the floating point
> registers (and other floating point context) is not saved/restored
> during system calls, for efficiency. I'm speculating here, but it
> might be possible to manually save the floating point context while
> doing some floating point operations. The problem arises if this code
> is interrupted midway. Using a preemptive 2.6 kernel would easily
> break here.

You can do it "safely" on x86 using


and make sure that _all_ the FP stuff is in between those two things, and
that you don't do _anything_ that might fault or sleep.

The kernel_fpu_xxx() macros make sure that preemption is turned off etc,
so the above should always be safe.

Even then, of course, using FP in the kernel assumes that you actually
_have_ an FPU, of course. The in-kernel FP emulation package is _not_
supposed to work with kernel FP instructions.

Oh, and since the kernel doesn't link with libc, you can't use anything
even remotely fancy. It all has to be stuff that gcc can do in-line,
without any function calls.

In other words: the rule is that you really shouldn't use FP in the
kernel. There are ways to do it, but they tend to be for some _real_
special cases, notably for doing MMX/XMM work. Ie the only "proper" FPU
user is actually the RAID checksumming MMX stuff.

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at
Please read the FAQ at