310 likes | 327 Views
This guide provides an in-depth exploration of overflow, addition, subtraction, and multiplication in computer arithmetic. Learn how to identify overflow conditions and optimize multiplication, covering topics such as floating-point representation and IEEE standards. Discover key concepts and practical exercises to enhance your understanding of computational arithmetic techniques.
E N D
Foundation of Systems Xiang Lian The University of Texas-Pan American
Addition Overflow • Occurs when the result is out the range for a given number of bits. Ex: 1011 (-5) + 1100 (-4) 10111 (7×) 0111 (7) + 0100 (4) 11011 (-5×) 0011 (3) + 0100 (4) 10111 (7) 1111 (-1) + 1100 (-4) 11011 (-5)
Checking of Addition Overflow When Both Operands of the Same Sign • Occurs when the sign bit differs to those of the two operands Ex: 1011 (-5) + 1100 (-4) 10111 (7×) 0111 (7) + 0100 (4) 11011 (-5×) 0011 (3) + 0100 (4) 10111 (7) 1111 (-1) + 1100 (-4) 11011 (-5)
Checking of Addition Overflow When Two Operands Differ in Sign • Does not occurs Ex: 1011 (-5) + 0100 (4) 11111 (-1) 1111 (-1) + 0100 (4) 10011 (3)
Subtraction Overflow Convert subtraction to addition. It is easy to check overflow by applying the rules for addition. Ex. 10-5=10+(-5)
Exercise • Checking whether -5+(-4) has overflow or not. • Using 4 bits • Using 5 bits • Checking whether -5-4 has overflow or not.
Dealing with Overflow Depends on languages/users. • Ignore overflow—Using MIPS instruction: addu, addui, subu. • Raise an exception to handle—using MIPS instruction: add, addi, sub.
1000 × 1001 1000 0000 0000 1000 1001000 Multiplication §3.3 Multiplication • Start with long-multiplication approach multiplicand multiplier product Length of product is the sum of operand lengths Chapter 3 — Arithmetic for Computers — 9
Multiplication Hardware Initially 0 Chapter 3 — Arithmetic for Computers — 10
1000 × 1001 1000 0000 0000 1000 1001000 1000×1001 (unsigned integers) §3.3 Multiplication • Start with long-multiplication approach Counter: 1 multiplicand 00001000 multiplier 00001000 00001001 product 00000000 Length of product is the sum of operand lengths Chapter 3 — Arithmetic for Computers — 11
1000 × 1001 1000 0000 0000 1000 1001000 1000×1001 (unsigned integers) §3.3 Multiplication • Start with long-multiplication approach 00001000 Counter: 2 multiplicand 00010000 multiplier 00001000 00001001 product 00001000 Length of product is the sum of operand lengths Chapter 3 — Arithmetic for Computers — 12
1000 × 1001 1000 0000 0000 1000 1001000 1000×1001 (unsigned integers) §3.3 Multiplication • Start with long-multiplication approach 00010000 Counter: 3 multiplicand 00100000 multiplier 00001000 00001001 product 00001000 Length of product is the sum of operand lengths Chapter 3 — Arithmetic for Computers — 13
1000 × 1001 1000 0000 0000 1000 1001000 1000×1001 (unsigned integers) §3.3 Multiplication • Start with long-multiplication approach 00100000 Counter: 4 multiplicand 01000000 multiplier 01001000 000001001 product 01001000 Length of product is the sum of operand lengths Chapter 3 — Arithmetic for Computers — 14
Exercise Calculate 1111×1111 using the algorithm. Is the product correct when both are • positive integers? • negative integers?
Optimized Multiplier • Perform steps in parallel: add/shift • One cycle per partial-product addition • That’s ok, if frequency of multiplications is low Chapter 3 — Arithmetic for Computers — 16
MIPS Multiplication • Two 32-bit registers for product • HI: most-significant 32 bits • LO: least-significant 32-bits • Instructions • mult rs, rt / multu rs, rt • 64-bit product in HI/LO • mfhi rd / mflo rd • Move from HI/LO to rd • Can test HI value to see if product overflows 32 bits • mul rd, rs, rt • Least-significant 32 bits of product –> rd Chapter 3 — Arithmetic for Computers — 17
Floating Point §3.5 Floating Point • Representation for non-integral numbers • Including very small and very large numbers • Like scientific notation • –2.34 × 1056 • +0.002 × 10–4 • +987.02 × 109 • In binary • ±1.xxxxxxx2 × 2yyyy • Types float and double in C normalized not normalized Chapter 3 — Arithmetic for Computers — 18
Floating Point Standard • Defined by IEEE Std 754-1985 • Developed in response to divergence of representations • Portability issues for scientific code • Now almost universally adopted • Two representations • Single precision (32-bit) • Double precision (64-bit) Chapter 3 — Arithmetic for Computers — 19
IEEE Floating-Point Format single: 8 bitsdouble: 11 bits single: 23 bitsdouble: 52 bits • S: sign bit (0 non-negative, 1 negative) • Normalize significand: 1.0 ≤ |significand| < 2.0 • Always has a leading pre-binary-point 1 bit, so no need to represent it explicitly (hidden bit) • Significand is Fraction with the “1.” restored • Exponent: excess representation: actual exponent + Bias • Ensures exponent is unsigned • Single: Bias = 127; Double: Bias = 1203 S Exponent Fraction Chapter 3 — Arithmetic for Computers — 20
Single-Precision Range • Exponents 00000000 and 11111111 reserved • Smallest value • Exponent: 00000001 actual exponent = 1 – 127 = –126 • Fraction: 000…00 significand = 1.0 • ±1.0 × 2–126 ≈ ±1.2 × 10–38 • Largest value • exponent: 11111110 actual exponent = 254 – 127 = +127 • Fraction: 111…11 significand ≈ 2.0 • ±2.0 × 2+127 ≈ ±3.4 × 10+38 Chapter 3 — Arithmetic for Computers — 21
Double-Precision Range • Exponents 0000…00 and 1111…11 reserved • Smallest value • Exponent: 00000000001 actual exponent = 1 – 1023 = –1022 • Fraction: 000…00 significand = 1.0 • ±1.0 × 2–1022 ≈ ±2.2 × 10–308 • Largest value • Exponent: 11111111110 actual exponent = 2046 – 1023 = +1023 • Fraction: 111…11 significand ≈ 2.0 • ±2.0 × 2+1023 ≈ ±1.8 × 10+308 Chapter 3 — Arithmetic for Computers — 22
Floating-Point Precision • Relative precision • all fraction bits are significant • Single: approx 2–23 • Equivalent to 23 × log102 ≈ 23 × 0.3 ≈ 6 decimal digits of precision • Double: approx 2–52 • Equivalent to 52 × log102 ≈ 52 × 0.3 ≈ 16 decimal digits of precision Chapter 3 — Arithmetic for Computers — 23
Floating-Point Example • What number is represented by the single-precision float 11000000101000…00 • S = 1 • Fraction = 01000…002 • Fxponent = 100000012 = 129 • x = (–1)1 × (1 + .012) × 2(129 – 127) = (–1) × 1.25 × 22 = –5.0 Chapter 3 — Arithmetic for Computers — 24
Converting Floating Point Numbers Ex. Convert 5.1875 to its binary representation (single precision). 5.1875 101. .??????
Converting Floating Point Numbers Ex. Convert 5.1875 to its binary representation (single precision). 5.1875 0.1875×2 =0.375 0 0 0.375×2 =0.75 101. .?????? 0.75×2 =1.5 1 0.5×2 =1 1
Converting Floating Point Numbers Ex. Convert 5.1875 to its binary representation (single precision). 5.1875 0.1875×2 =0.375 0 0 0.375×2 =0.75 101. .0011 0.75×2 =1.5 1 0.5×2 =1 1
Converting Floating Point Numbers Ex. Convert 5.1875 to its binary representation (single precision). 5.1875 0.1875×2 =0.375 0 0 0.375×2 =0.75 101. .0011 0.75×2 =1.5 1 101.0011 0.5×2 =1 1 1.010011×22 0 10000001 0100110…0(23 bits)
Exercise Convert the following numbes to its binary representation (single precision). • -13.8125 • 0.6
Associativity • Parallel programs may interleave operations in unexpected orders • Assumptions of associativity may fail §3.6 Parallelism and Computer Arithmetic: Associativity • Need to validate parallel programs under varying degrees of parallelism Chapter 3 — Arithmetic for Computers — 30
FP Instructions in MIPS • Single-precision arithmetic • add.s, sub.s, mul.s, div.s • e.g., add.s $f0, $f1, $f6 • $f0 = $f1 + $f6 • Double-precision arithmetic • add.d, sub.d, mul.d, div.d • e.g., mul.d $f4, $f4, $f6 • Single- and double-precision comparison • c.xx.s, c.xx.d (xx is eq, lt, le, …) • Sets or clears FP condition-code bit • e.g. c.lt.s $f3, $f4 • Branch on FP condition code true or false • bc1t, bc1f • e.g., bc1t TargetLabel Chapter 3 — Arithmetic for Computers — 31