The x 87 FPU Lecture 19 Fri Mar

  • Slides: 25
Download presentation
The x 87 FPU Lecture 19 Fri, Mar 26, 2004

The x 87 FPU Lecture 19 Fri, Mar 26, 2004

Reading l Read Chapter 8: Programming with the x 87 Floating-Point Unit of the

Reading l Read Chapter 8: Programming with the x 87 Floating-Point Unit of the Intel Developer’s Manual, Vol. 1.

Floating-Point Data Types l The x 87 Floating Point Unit (FPU) recognizes three floating-point

Floating-Point Data Types l The x 87 Floating Point Unit (FPU) recognizes three floating-point types. l l float – single precision, 32 bits. double – double precision, 64 bits. long double – double extended precision, 80 bits. We will use the type double in our compiler.

The x 87 FPU Architecture l l l The FPU has 8 general purpose

The x 87 FPU Architecture l l l The FPU has 8 general purpose 80 -bit (double extended-precision) registers. They are labeled st(0), st(1), …, st(7). They are organized as a stack, with st(0) on top. Typically, floating-point operations pop values off the stack and push results onto the stack. Many instructions allow us to access any position in the stack.

The FPU Stack l Register st(0) is always on top of the stack. 79

The FPU Stack l Register st(0) is always on top of the stack. 79 Top st(4) st(3) st(2) st(1) st(0) 0 1. 234 5. 678 9. 876 5. 432 100. 0

The FPU Stack l When we push, it refers to the next register. 79

The FPU Stack l When we push, it refers to the next register. 79 Top st(5) st(4) st(3) st(2) st(1) st(0) 0 1. 234 5. 678 9. 876 5. 432 100. 0 999. 9 Grows Downward

The FPU Stack l When we pop, it refers to the previous register. 79

The FPU Stack l When we pop, it refers to the previous register. 79 Top st(4) st(3) st(2) st(1) st(0) 0 1. 234 5. 678 9. 876 5. 432 100. 0 Shrinks Upward

The FPU Status Register l l l The 16 -bit status register contains a

The FPU Status Register l l l The 16 -bit status register contains a number of bit fields that are set by floating-point instructions, including a 3 -bit field TOP that points to the top of the FPU stack. TOP holds a value from 0 to 7. We will have use later for the bit fields C 0, C 1, C 2, and C 3, which are condition codes containing information about the most recent floating-point comparison.

The FPU Control Word l The 16 -bit control word contains a number of

The FPU Control Word l The 16 -bit control word contains a number of bit fields, including l l A 2 -bit field PC that controls precision. A 2 -bit field RC that controls rounding.

The PC field l The PC settings l l 00 = single precision. 10

The PC field l The PC settings l l 00 = single precision. 10 = double precision. 11 = double extended-precision. The default is double extended-precision.

The RC Field l The RC settings l l l 00 = Round to

The RC Field l The RC settings l l l 00 = Round to nearest. 01 = Round down (towards – ). 10 = Round up (towards + ). 11 = Round towards zero. The default is round to nearest. Therefore, when we convert a double to an int, the value will be rounded, not truncated.

The x 87 Instruction Set l We will be interested in three categories of

The x 87 Instruction Set l We will be interested in three categories of instruction. l l Data transfer. Basic arithmetic. Comparisons. Other categories are l l Transcendental instructions (trig, exponential, and logarithmic functions). Loading constants (0, 1, , log 2 10, etc. )

The x 87 Instruction Set l Many FPU instructions come in two versions. l

The x 87 Instruction Set l Many FPU instructions come in two versions. l l The basic instruction: Fxxx. The same instruction, followed by popping the FPU stack: Fxxx. P.

Data Transfer – Load l fld src l l Push the floating-poing value at

Data Transfer – Load l fld src l l Push the floating-poing value at src onto the FPU stack. The operand src may be l l A memory address. An FPU register st(i). Note that it cannot be a (non-FPU) register. Examples l l fld avg fld (%esp)

Data Transfer – Load l fild src l l l Convert the integer at

Data Transfer – Load l fild src l l l Convert the integer at src to double extendedprecision and push it onto the FPU stack. The operand src is a memory address. Examples l l fild count fild (%esp)

Data Transfer – Store l fst dst l Transfer the value at st(0) to

Data Transfer – Store l fst dst l Transfer the value at st(0) to dst. l The operand dst may be l l l Example l l A memory address. An FPU register st(i). fst avg The instruction fstp is the same, except that it also pops the value off of the FPU stack.

Data Transfer – Store l fist dst l l l Example l l Transfer

Data Transfer – Store l fist dst l l l Example l l Transfer the value at st(0) to dst and convert it to an integer. The operand dst is a memory address. fist (%esp) The instruction fistp is the same, except that it also pops the value off of the FPU stack.

Arithmetic – Add l l To add two floating-point numbers, we will use the

Arithmetic – Add l l To add two floating-point numbers, we will use the faddp instruction. faddp will l Add st(0) to st(1) and store the result in st(1) + st(0) Pop the FPU stack, thereby removing st(0) and bringing st(1) to the top of the stack (st(0)). There are several other versions of fadd – see the manual.

Arithmetic – Multiply l l l To multiply two floating-point numbers, we will use

Arithmetic – Multiply l l l To multiply two floating-point numbers, we will use the fmulp instruction. fmulp will l Multiply st(1) by st(0) and store the result in st(1) st(0) l Pop the FPU stack. There are several other versions of fmul – see the manual.

Arithmetic – Subtract l l l To subtract two floating-point numbers, we will use

Arithmetic – Subtract l l l To subtract two floating-point numbers, we will use the fsubrp instruction. fsubrp will l Subtract st(0) from st(1) and store the result in st(1) – st(0) l Pop the FPU stack. There are several other versions of fsub – see the manual.

Arithmetic – Subtract l The Intel manual describes fsubp and fsubrp as follows. l

Arithmetic – Subtract l The Intel manual describes fsubp and fsubrp as follows. l fsubp l l l fsubrp l l l st(1) – st(0); Pop stack. Machine code DEE 9. st(1) st(0) – st(1); Pop stack. Machine code DEE 1. However, the gnu assembler will reverse their meanings.

Arithmetic – Divide l l l To divide two floating-point numbers, we will use

Arithmetic – Divide l l l To divide two floating-point numbers, we will use the fdivrp instruction. fdivrp will l Divide st(1) by st(0) and store the result in st(1) / st(0) l Pop the FPU stack. There are several other versions of fdiv – see the manual.

Arithmetic – Divide l The Intel manual describes fdivp and fdivr as l fdivp

Arithmetic – Divide l The Intel manual describes fdivp and fdivr as l fdivp l l l fdivrp l l l st(1) / st(0); Pop stack. Machine code DEF 9. st(1) st(0) / st(1); Pop stack. Machine code DEF 1. However, the gnu assembler will reverse their meanings.

Arithmetic – Square Root l There is a square-root instruction fsqrt. l If we

Arithmetic – Square Root l There is a square-root instruction fsqrt. l If we want to, we can create a special squareroot operator, say #sqrt#. l Then the source code a = #sqrt# b; would be interpreted as “assign to a the square root of b. ” l No function call would be required.

Transcendental Functions l The same is true of the following transcendental functions. l l

Transcendental Functions l The same is true of the following transcendental functions. l l l fsin: st(0) sin(st(0)). fcos: st(0) cos(st(0)). fptan: st(0) 1. 0, st(1) tan(st(0)). fpatan: st(0) arctan(st(1)/st(0)). fsincos: st(0) cos(st(0)), st(1) sin(st(0)).