The X87 Floating-Point Unit Robb T
Total Page:16
File Type:pdf, Size:1020Kb
The x87 Floating-Point Unit Robb T. Koether The x87 FPU The x87 Floating-Point Unit Architecture Lecture 18 The FPU Status Intel Manual, vol. 1, Chapter 8 Register and Rounding The FPU Registers and Stack Robb T. Koether The x87 Instruction Set Hampden-Sydney College Assignment Wed, Mar 25, 2009 Outline The x87 Floating-Point Unit Robb T. Koether 1 The x87 FPU Architecture The x87 FPU Architecture 2 The FPU The FPU Status Register and Rounding Status Register and Rounding 3 The FPU Registers and Stack The FPU Registers and Stack The x87 4 The x87 Instruction Set Instruction Set Assignment 5 Assignment Floating-Point Data Types The x87 Floating-Point Unit Robb T. Koether The x87 FPU The x87 Floating Point Unit (FPU) recognizes three Architecture floating-point types. The FPU float Status - single precision, 32 bits. Register and double - double precision, 64 bits. Rounding long double - double extended precision, 80 bits. The FPU Registers and double Stack We will use the type in our compiler. The x87 However, in the FPU, all calculations will be as long Instruction Set doubles. Assignment The FPU Status Register The x87 Floating-Point Unit Robb T. Koether The 16-bit status register contains a number of bit fields The x87 FPU Architecture that are set by floating-point instructions, including a The FPU Status 3-bit field TOP that points to the top of the FPU stack. Register and Rounding The size of the stack is 8; TOP holds a value from 0 to The FPU 7. Registers and Stack We will have use later for the bit fields C0, C1, C2, and The x87 C3 Instruction Set , which are condition codes containing information Assignment about the most recent floating-point operation. The FPU Control Word The x87 Floating-Point Unit Robb T. Koether The x87 FPU Architecture The FPU The 16-bit control word contains a number of bit fields, Status Register and including Rounding A 2-bit field PC that controls precision. The FPU Registers and A 2-bit field RC that controls rounding. Stack The x87 Instruction Set Assignment The PC Field The x87 Floating-Point Unit Robb T. Koether The x87 FPU Architecture The PC settings The FPU Status 00 = single precision. Register and Rounding 10 = double precision. The FPU 11 = double extended-precision. Registers and Stack The default is double extended-precision. The x87 Instruction Set Assignment The RC Field The x87 Floating-Point Unit Robb T. Koether The x87 FPU The RC settings Architecture 00 = Round to nearest. The FPU 01 = Round down (towards −∞). Status Register and 10 = Round up (towards +1). Rounding 11 = Round towards zero. The FPU Registers and Stack The default is round to nearest. The x87 Therefore, when we convert a double to an int, the Instruction Set value will be rounded, not truncated. Assignment The x87 FPU Architecture The x87 Floating-Point Unit Robb T. Koether The FPU has 8 general purpose 80-bit (double The x87 FPU Architecture extended-precision) registers. The FPU Status They are labeled st(0), st(1),..., st(7). Register and Rounding They are organized as a stack, with st(0) on top. The FPU Registers and Typically, floating-point operations pop values off the Stack stack and push results onto the stack. The x87 Instruction Set However, many instructions allow us to access any Assignment position in the stack. The FPU Stack The x87 Floating-Point Unit st(0) Robb T. Register is always on top of the stack. Koether The x87 FPU 79 0 Architecture st(4) 1.234 The FPU Status st(3) 5.678 Register and Rounding st(2) 9.876 The FPU Registers and st(1) 5.432 Stack Top st(0) 100.0 The x87 Instruction Set Assignment The FPU Stack The x87 Floating-Point Unit st(0) Robb T. When we push, moves to the next register. Koether The x87 FPU 79 0 Architecture st(5) 1.234 The FPU Status st(4) 5.678 Register and Rounding st(3) 9.876 Grows The FPU Downward Registers and st(2) 5.432 Stack st(1) 100.0 The x87 Instruction Set Top st(0) 999.9 Assignment The FPU Stack The x87 Floating-Point Unit st(0) Robb T. When we pop, moves to the previous register. Koether The x87 FPU 79 0 Architecture st(4) 1.234 The FPU Status st(3) 5.678 Register and Rounding st(2) 9.876 Shrinks The FPU Upward Registers and st(1) 5.432 Stack Top st(0) 100.0 The x87 Instruction Set 999.9 Assignment The x87 Instruction Set The x87 Floating-Point Unit Robb T. Koether The x87 FPU We will be interested in three categories of instruction. Architecture Data transfer. The FPU Status Basic arithmetic. Register and Comparisons. Rounding The FPU Other categories are Registers and Stack Transcendental instructions (trig, exponential, and The x87 logarithmic functions). Instruction Set Loading constants (0, 1, π, log2 10, etc.) Assignment Data Transfer - Load The x87 Floating-Point Unit Robb T. Koether The fld Load Instruction The x87 FPU fld src Architecture The FPU Status Pushes the floating-poing value at src onto the FPU Register and Rounding stack. The FPU Registers and The operand src may be a memory address or an Stack FPU register st(i). It cannot be a non-FPU register. The x87 Instruction Set Examples Assignment fld avg fld (%esp) Data Transfer - Load The x87 Floating-Point Unit Robb T. Koether The fild Integer Load Instruction The x87 FPU Architecture fild src The FPU Status Register and Converts the integer at src to double Rounding extended-precision and pushes it onto the FPU stack. The FPU Registers and The operand src is a memory address. Stack The x87 Examples Instruction Set fild count Assignment fild (%esp) The x87 Instruction Set The x87 Floating-Point Unit Robb T. Koether The x87 FPU Architecture The FPU Many FPU instructions come in two versions. Status Register and The basic instruction: Fxxx. Rounding The same instruction, followed by popping the FPU The FPU Registers and stack: FxxxP. Stack The x87 Instruction Set Assignment Data Transfer - Store The x87 Floating-Point Unit Robb T. The fild Integer Load Instruction Koether fst dst The x87 FPU fstp dst Architecture The FPU Status Register and fst transfers the value at st(0) to dst. The operand Rounding dst may be a memory address or an FPU register The FPU Registers and st(i). Stack fstp is the same, except that it also pops the value off The x87 Instruction Set the FPU stack. Assignment Examples fst avg fstp avg Data Transfer - Store The x87 Floating-Point Unit Robb T. fist Koether The Integer Store Instruction fist dst The x87 FPU Architecture fistp dst The FPU Status Register and fist st(0) dst Rounding transfers the value at to and converts The FPU it to an integer. The operand dst is a memory address. Registers and Stack fistp is the same, except that it also pops the value off The x87 the FPU stack. Instruction Set Assignment Examples fist (%esp) fistp (%esp) Arithmetic - Add The x87 Floating-Point Unit Robb T. Koether The faddp Add Instruction faddp The x87 FPU Architecture The FPU Status Adds st(0) to st(1) and stores the result in st(1). Register and Rounding The FPU st(1) st(1) + st(0) Registers and Stack Pops the FPU stack, thereby removing st(0) and The x87 Instruction Set bringing st(1) to the top of the stack (st(0)). Assignment There are several other versions of fadd - see the manual. Arithmetic - Multiply The x87 Floating-Point Unit Robb T. Koether The fmulp Multiply Instruction The x87 FPU fmulp Architecture The FPU Status Register and Multiplies st(1) by st(0) and stores the result in Rounding st(1). The FPU Registers and st(1) st(1) × st(0) Stack The x87 Pops the FPU stack. Instruction Set Assignment There are several other versions of fmul - see the manual. Arithmetic - Subtract The x87 Floating-Point Unit Robb T. Koether The fsubrp Subtract Instruction The x87 FPU fsubrp Architecture The FPU Status Register and Subtracts st(0) from st(1) and stores the result in Rounding st(1). The FPU Registers and st(1) st(1) − st(0) Stack The x87 Pops the FPU stack. Instruction Set Assignment There are several other versions of fsub - see the manual. Arithmetic - Subtract The x87 Floating-Point Unit Robb T. Koether The Intel manual describes fsubp and fsubrp as The x87 FPU follows. Architecture fsubp The FPU Status st(1) st(1) − st(0); Pop stack. Register and Rounding Machine code DEE9. The FPU fsubrp Registers and Stack st(1) st(0) − st(1); Pop stack. The x87 Machine code DEE1. Instruction Set Assignment However, the gnu assembler will reverse their meanings. Arithmetic - Divide The x87 Floating-Point Unit Robb T. Koether The fdivrp Divide Instruction The x87 FPU fdivrp Architecture The FPU Status Register and Divides st(1) by st(0) and stores the result in Rounding st(1). The FPU Registers and st(1) st(1)=st(0) Stack The x87 Pops the FPU stack. Instruction Set Assignment There are several other versions of fdiv - see the manual. Arithmetic - Divide The x87 Floating-Point Unit Robb T. Koether The Intel manual describes fdivp and fdivr as The x87 FPU Architecture fdivp The FPU st(1) st(1)=st(0); Pop stack. Status Register and Machine code DEF9. Rounding fdivrp The FPU Registers and st(1) st(0)=st(1); Pop stack.