StuBS
|
On x86, floating-point operations require a bit of compiler and OS support. This is primarily due to the CPU using extra registers for floating-point operations. These registers have to be saved and restored for context switches. We focus on the x87 Floating Point Unit, as it has a relatively small register file (108 byte). This FPU is not entirely accurate and some operations (srqt, sin, cos) might be approximated.
The following flags tell the compiler that we want to use the x87 FPU.
C++: tools/build.mk
Additionally, we have to ensure that we do not lose any FPU registers during a context switch. Similar to the non-scratch registers, we have to manually save and restore them.
Reserving enough space for the x87 FPU.
machine/context.asm
Just add the
finit
,fsave
, andfrstor
instructions to the context switch functions.
Now, all basic floating-point operations should work as expected (add, sub, mul, div). However, some special operations, like sqare-root or sine, are still missing. Luckily, the x87 FPU has special instructions for most of them. Still, we have to implement them manually.
Of course, you can also use the Fast Inverse Sqrt from Quake III.