FPU

From OSDev.wiki
Revision as of 03:05, 23 January 2009 by osdev>Thepowersgang (Forgot to preview)
Jump to navigation Jump to search
This page is a stub.
You can help the wiki by accurately adding more contents to it.

x87 Floating-Point Unit

Programming

Initialize the FPU

In order to utilize the FPU from within the 32-bit protected mode environment, one must first initialize it before use. If used before being setup properly would result in an exception. The following code shows how to init the FPU so that datatypes such as float, double, and long double can be used without generating an exception.

ex:

void setup_x87_fpu()
{
   uint32 cr4; // backup of CR4
   uint16 mode = 0x33f;  // our control word mode variable
   
   // place CR4 into our variable
   __asm__ __volatile__("mov %%cr4, %0;" : "=r" (cr4));
   
   // set the OSFXSR bit
   cr4 |= 0x200;
   
   // reload CR4 and INIT the FPU (FINIT)
   __asm__ __volatile__("mov %0, %%cr4; finit;" : : "r"(cr4));
   
   // load the FPU control word with our 'mode' variable
   __asm__ __volatile__("fldcw %0" : : "m" (mode));
}

Setting the 9th bit (OSFXSR) in the CR4 tells the CPU that we intend on using the FXSAVE, FXRSTOR, and SSEx instructions. If this bit is not set, a #UD exception will be generated on use of the FPU or any SSE instructions.

The 'mode' variable is set to 0x33f, but what exactly does this mean when loading the FPU control word? Bits 0-5 of the control word are various interrupt masks, when set they mask the interrupt from reaching the CPU and (most likely) causing a fault if proper handling doesn't exist. Bit 7 is the IEM, which stands for the Interrupt Enable Mask, and should only be set if there are interrupts that will be UNMASKED. Bits 8-11 are for two things, the PC (Precision Control) and RC (Rounding Control). In our case, we have the PC fully set (default) which corresponds to 64-bit precision. The RC is set to the default of 0 and will round to the nearest and to the even-most value if split.


References

http://www.website.masmforum.com/tutorials/fptute/fpuchap1.htm