Introduction to Floating Point Programming
By Lord Lucifer
January 6, 1999

Introduction



This article will give a general overview of the Intel Architcture Floating
Point Unit (FPU).  It will cover the FPU's architecture and instruction set.
 

 FPU Data Formats



                  Length   Precision  Range (Approx)
  Single Real     32 bit   24 bit     1.18e-38 to 3.40e38
  Double Real     64 bit   53 bit     2.23e-308 to 1.79e308
  Extended Real   80 bit   64 bit     3.37e-4932 to 1.18e4932
  Word Integer    16 bit   15 bit     -32768 to 32768
  Short Integer   32 bit   31 bit     -2.14e9 to 2.14e9
  Long Integer    64 bit   63 bit     -9.22e18 to 9.22e18
  Packed BCD      80 bit   18         (-1e18 + 1) to (1e18 - 1)
 

 FPU Data Registers


Data Registers:
      _ _________ _______________________________________
  R7 |_|_________|_______________________________________|
  R6 |_|_________|_______________________________________|
  R5 |_|_________|_______________________________________|
  R4 |_|_________|_______________________________________|
  R3 |_|_________|_______________________________________|
  R2 |_|_________|_______________________________________|
  R1 |_|_________|_______________________________________|
  R0 |_|_________|_______________________________________|
    80 79        63                                      0
    Sign  Exponent                             Significand

                           ______________________________
  FPU Instruction Pointer |______________________________|
  FPU Operand Pointer     |______________________________|
                         47                              0
                                          _______________
  Control Register                       |_______________|
  Status Register                        |_______________|
  Tag Register                           |_______________|
  Opcode                                     |___________|
                                        15   10          0
 

FPU Data Register Stack:
     ___________________________
  7 |___________________________|                    Stack
  6 |___________________________| ST(2)              Growth
  5 |___________________________| ST(1)                |
  4 |___________________________| ST(0)  TOP=100b      |
  3 |___________________________|                     \|/
  2 |___________________________|                      `
  1 |___________________________|
  0 |___________________________|
 

Status Register:
   ___________________________________________________________
  |___|___|___________|___|___|___|___|___|___|___|___|___|___|
    B   C3     TOP      C2  C1  C0  ES  SF  PE  UE  OE  ZE  IE

  B     - FPU Busy
  C0-C3 - Condition Codes
  TOP   - Top of Stack Pointer
  ES    - Error Summary Status
  SF    - Stack Fault
  PE    - Precision Exception
  UE    - Underflow Exception
  OE    - Overflow Exception
  ZE    - Zero Divide Exception
  DE    - Denormailzed Operand Exception
  IE    - Invalid Operation Exception
 

FPU Instruction Set


Data Transfer Instructions:

  FLD    src = [st(), mem32, mem64]
         Load Real
         Pushes real operand (from memory or FPU data register) onto
         the top of the FPU stack.  If the operand is a single or double real
         number, it is automatically converted to extended real formt.

  FST    dest = [mem32, mem64, mem80, st()]
         Store Real
         Copies the value on top of the FPU stack to the destination operand.

  FSTP   dest = [mem32, mem64, mem80, st()]
         Store Real and Pop
         Copies the top of the FPU stack to the destination operand and pops
         the value off of the stack.

  FXCH   [st()]
         Exchange Register Contents
         Exchanges the top of the FPU stack with another FPU register.

  FILD   src = [mem32, mem64, mem80]
         Load Integer
         Converts integer operand to extended real format, the pushes it onto
         the top of the FPU stack.

  FIST   dest = [mem32, mem64, mem80]
         Store Integer
         Converts value at the top of the FPU stack to an integer value, and
         stores it in the destination.

  FISTP  dest = [mem32, mem64, mem80]
         Store Integer and Pop
         Converts the top of the FPU stack to an integer value, stores it in
         the destination, and pops the value off the stack.

  FBLD   src = [bcd80]
         Load Packed Decimal
         Converts BCD source to extended real format and pushes it to the top
         of the FPU stack.

  FBSTP  dest = [mem80]
         Store Packed Decimal and Pop
         Converts top of FPU stack to BCD format, stores it in destination,
         and pops value off of the stack.
 

Constant Load Instructions:

  These instructions push commonly used constants on to the top of the FPU
  stack (ST(0)).  These constants have extended real precision and are accurate
  to approximately 19 digits.

  FLDZ      Load +0.0
  FLD1      Load +1.0
  FLDPI     Load PI
  FLDL2T    Load log(2) 10  [log(base 2) of 10]
  FLDL2E    Load log(2) e
  FLDLG2    Load log(10) 2
  FLDLN2    Load log(e) 2
 

Arithmetic Instructions:

  The following instructions operate on the following types of operands:
  - 2 FPU register values
  - A Register value and a real or integer value in memory

  FADD(P)   Add Real [FADDP = Add real and pop]
  FIADD     Add Integer
  FSUB(P)   Subtract Real
  FISUB     Subtract Integer from Real
  FSUBR(P)  Reverse Subtract Real
  FISUBR    Reverse Subtract Real from Integer
  FMUL(P)   Multiply Real
  FIMUL     Multiply Integer by Real
  FDIV(P)   Divide Real
  FIDIV     Divide Integer by Real
  FDIVR(P)  Reverse Divide Real
  FIDIVR    Reverse Divide Integer by Real
 

Comparison Instructions:

  FCOM      Compare real and set FPU condition code flags
  FUCOM     Unordered compare real and set FPU condition code flags
  FICOM     Compare Integer
  FCOMI     Compare real and set EFLAGS status flags
  FUCOMI    Unordered compare real and set EFLAGS status flags
  FTST      Test (Compare real with 0.0)
  FXAM      Examine

Trigonometric Instructions:

  FSIN      Sine
  FCOS      Cosine
  FSINCOS   Sine and Cosine
  FPTAN     Tangent
  FPATAN    ArcTangent

  These instructions operate on the top one or two FPU Stack registers.
  They return their results to the stack.
  The operands must be given in radians.

Logarithm, Exponential, and Scale:

  FYL2X     Compute log (y * log[2] x)
  FYL2XPI   Compute log epsilon (y * log[2] x)
  F2XMI     Compute exponential (2^x - 1)
  FSCALE    Scale - multiplies source operand by power of 2

FPU Control Instructions:

  FINIT/FNINIT   Initialize FPU
  FLDCW          Load FPU control word
  FSTCW/FNSTCW   Store FPU control word
  FSTSW/FNSTSW   Store FPU status word
  FCLEX/FNCLEX   Clear FPU exception flags
  FLDENV         Load FPU environment
  FSTENV/FNSTENV Store FPU environment
  FRSTOR         Restore FPU state
  FSAVE/FNSAVE   Save FPU state
  FINCSTP        Increment FPU register stack pointer
  FDECSTP        Decrement FPU register stack pointer
  FFREE          Free FPU register
  FNOP           No operation
  WAIT/FWAIT     Check for and handle pending unmasked FPU exceptions
 

For a plaintext copy: fpu1.txt



Copyright (C) 1999
Lord Lucifer lord-lucifer@usa.net