FLOATING-POINT DIVISION WITH OPTIONAL CHECKING TO ENSURE FULL RESULT 
PRECISION

Summary Document
_______________________________________________________________________________________________________

INTRODUCTION
Certain early steppings of the Intel Pentium(TM) processor contain a flaw 
in the floating-point unit which may result in a rare loss of result 
precision from division operations. To address the flaw from a software 
vantage point, a unique collaboration between Intel and industry experts 
was formed. Utilizing expert input from Tim Coe, Cleve Moler, Terje 
Mathisen, and Peter Tang, a software workaround for the FDIV flaw has been 
devised at Intel.

A number of software patches have been proposed that may be suited to 
avoiding a potential division flaw. Note that Intel's proposed software 
workaround consists of hand coded and optimized assembly routines that 
continue to utilize the hardware floating-point division operation with 
additional operations executed only in the rare case that a given division 
is known to be susceptible to the floating-point division flaw.

The software patch presented is intended to be implemented at the 
compilation and software development levels. End-users should use 
recompiled code where available. Recompiled code with a patch such as the 
one that Intel is providing will allow the fastest patched executable 
speed. Patches that interrupt executables to override floating-point 
division operations with alternate solutions incur the added expense 
interrupts. The disabling of the floating-point unit as a patch for the 
floating-point division flaw will slow all floating-point calculations, 
including those such as FADD that are unaffected by the FDIV flaw.

The utilization of other patches, such as those that disable the 
floating-point unit, can be of use to those who must execute code that has 
not been coded or recompiled with corporation of a software patch.

ALGORITHM
In order to avoid a possible precision reduction, a software patch for 
FDIV instructions should be incorporated into codes that may be executed 
on flawed Pentium processors. Intel's recommended software workaround 
involves three basic steps as listed below.

1. Identify the executing processor. A global variable is initialized that 
identifies the executing processor. Subsequent iterations of this step 
involve only testing the global variable. On Pentium  processors 
exhibiting the floating-point division flaw, a process is followed to 
ensure that full precision is returned in the result of a division. On 
other processors, only the original hardware division instruction is 
executed.

2. On a flawed processor, a test is performed to determine if the operands 
of an FDIV instruction may be susceptible to the floating-point division 
flaw. If the division operands are found not to be susceptible to the 
flaw, a result is returned from a simple hardware division.

3. Once susceptible operands are identified, they are scaled to avoid 
problematic numeric ranges. The adjusted operands are used in a hardware 
division and the full precision result is then returned.

IMPLEMENTATION
The optimal software solution to the FDIV flaw involves recompilation of 
software with compilers modified to generate a software patch sequence 
around each initially generated FDIV instruction. Optionally, assembly 
codes may be modified to incorporate a software patch at each 
floating-point division instruction. The replacement of each FDIV-type 
instruction with the Intel software workaround results in code resembling 
the pseudo-code that follows.


/* Original sequence */

 FDIV (original_operands)


/* New sequence */

 processor = identify_processor

 if (flawed (processor))

     problematic_divisor = check_for_problematic_divisor_value

     if (problematic_divisor)

         new_operands = scale_the_operands (original_operands)

         FDIV (new_operands)

     else

         FDIV (original_operands)

 else

     FDIV (original_operands)


Routines corresponding to identify_processor, 
check_for_problematic_divisor_value, and scale_the_operand are summarized 
below.

NOTE: The following files may be updated periodically. For a revision 
history and further details on the algorithms, proofs, exceptions, and 
implementation of the Intel software patch, refer to the complete text of 
DIVISION WITH OPTIONAL CHECKING TO ENSURE FULL RESULT PRECISION. Questions 
pertaining to documents and code related to the Intel FDIV software patch 
can be directed to software-support@intel.com or FAXed to (408)765-5165 
attention FDIV PATCH.


ROUTINES SUMMARY
fdiv_detect: performs a check for a reduction in precision and initializes 
a global variable depending upon the result. A value of 1 or -1 is also 
returned to indicate the result as described in the PATCH IMPLEMENTATION 
section.

fdiv_chk (x,y), frem_chk (x,y), frem1_chk (x,y), fatan_chk (x,y): fdiv_chk 
operates in all three (single, double, and extended) precisions. The other 
functions are always extended-precision operations. They leave a function 
result on the user stack.

fdiv_r: performs register-register division - the routine is parameterized 
using the six lowest bits of eax.

fdiv_m32, fdiv_m63, fdiv_m16i, fdiv_m32i: perform divisions with stack and 
memory operands. The memory operand must be copied to the user stack. The 
copy alleviates the overhead of handling prefixes in the run-time 
routines. The function name following the underscore indicates the size of 
the memory operand.

fdivr_m32, fdivr_m64, fdivr_m16i, fdivr_m32i: fdivr_ functions are similar 
to the fdiv_ functions but perform reverse divisions.


FILES SUMMARY
The following UNIX assembly files containing code to implement division 
with checking are available. Corresponding files are available for DOS 
operating systems under the names *16.asm and *32.asm in addition to the 
UNIX *u.s files listed. These files can be downloaded from the following 
ZIP files: UNIX.TAR.Z , MASM16.ZIP and MASM32.ZIP.

patchu.s: FDIV/FDIVR instruction replacement routines to handle all 
flavors of division. For example, register-register operand division and 
register-memory operand division.

fchku.s: User routines to do single, double, and extended precision 
divisions.

fpremu.s: FPREM/FPREM1 instruction replacement code. Also user routines to 
do single, double, and extended-precision remainder operations.

wrapu.s: FPATAN instruction replacement code. After saving of the 
floating-point environment, calls fatan_chk.

fpatanu.s: FPATAN replacement code to be used in library routines. 
Contains the core fatan_chk routine.

fptanu.s: FPTAN instruction replacement code.


