// HACKER NEWS — CYBERSECURITY
Reverse-engineering the Intel 8087's tangent algorithm: more than CORDIC
Computer history, restoring vintage computers, IC reverse engineering, and whatever
I hope you're not tired of the 8087, because I have another article about
Intel's floating-point chip.1
In 1980, Intel introduced the 8087, making floating-point operations much faster
in the IBM PC and other systems.
In this article, I look at the algorithm behind the chip's tangent instruction.
One popular approach for trigonometric functions is an algorithm called CORDIC.
Another approach is a polynomial approximation. The 8087 combined the two
to obtain both high accuracy and high performance.
The 8087 provided an enormous speedup over the 8086 microprocessor, computing a tangent in
90 microseconds rather than 13,000 microseconds.2
By examining the circuitry and microcode of the 8087,
I can explain the algorithm behind the tangent instruction, called FPTAN.
To explore the 8087's circuitry, I popped the lid off a chip with a chisel
and created a high-resolution image with a microscope.
The microcode ROM is the large rectangular region in the center of the die, holding the 1648 micro-instructions that control the chip.
The bottom half of the chip (red box) is the datapath, the circuitry that performs floating-point calculations on 80-bit values.3
Zooming in on the datapath shows the relevant functional units.
The exponent ROM holds fixed exponent values that the algorithms need.
The constant ROM holds constants, including the constants used by the CORDIC algorithm.
The shifter is a large component; it shifts a 64-bit value left or right by arbitrary amounts.
The adder is the heart of the 8087's calculations; as well as providing addition and subtraction, it is used in a loop for multiplication, division, and square roots.
The B register holds one input to the adder, while multiple sources can provide the other input. The sum register holds the adder's output.
The eight stack registers and the temporary registers hold floating-point numbers.
Finally, the shift register holds 16 status bits for the CORDIC calculations.
CORDIC is a clever algorithm for quickly computing transcendental functions
with simple hardware: it uses shift and add instructions along with table lookups,
but doesn't need multiplication or division.
This algorithm dates back to 1956, when it was developed for the B-58 Hustler,
the first bomber capable of flying at Mach 2.
The aircraft had an analog navigation computer, but analog components provided limited accuracy.
Engineer Jack Volder was given the task of designing a digital computer to replace
the analog computer.7
One key problem was that an analog computer can easily generate sines and cosines
with an electromechanical device called a resolver.
But trigonometric functions are difficult to produce digitally, especially with the
slow transistors of that era.
Jack Volder came up with a fast way to calculate trigonometric functions
with simple hardware.
He called the algorithm—and the computer that implemented it—CORDIC: "COordinate Rotation DIgital Computer".
CORDIC converts an angle to a vector, where
the vector's coordinates provide the necessary trig functions.
The trick is to break down the angle into a sequence of special angles, angles that
make vector rotation easy.
These special angles are precomputed and stored in a table, so the CORDIC calculation
can be performed quickly, even on 1950s hardware.
Each CORDIC iteration provides an additional bit of accuracy, so the algorithm converges
rapidly. CORDIC became popular, including in scientific calculators, which used decimal CORDIC instead of binary.
I'll try to keep the math to a minimum, but in this section I'll give a quick explanation
of how CORDIC works.
The diagram below reviews how trig functions are related to the coordinates of a point.
Suppose you have an angle θ; it specifies a point (X, Y) on the unit circle.
The basic formulas are X=cos θ, Y=sin θ, and Y/X = tan θ.
Thus, if you can determine the coordinate (X, Y), then you can determine the value