1 / 72

Integer Operations

Integer Operations. Outline. Arithmetic Operations overflow Unsigned addition, multiplication Signed addition, negation, multiplication Using Shift to perform power-of-2 multiply/divide Suggested reading Chap 2.3. Negation :取反. • • •. • • •. u. Operands: w bits. • • •. • • •. +.

Download Presentation

Integer Operations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Integer Operations

  2. Outline • Arithmetic Operations • overflow • Unsigned addition, multiplication • Signed addition, negation, multiplication • Using Shift to perform power-of-2 multiply/divide • Suggested reading • Chap 2.3 Negation:取反

  3. • • • • • • u Operands: w bits • • • • • • + v True Sum: w+1 bits u + v UAddw(u , v) Discard Carry: w bits Unsigned Addition

  4. Unsigned Addition • Standard Addition Function • Ignores carry output • Implements Modular Arithmetic • s = UAddw(u , v) = (u + v) mod 2w P67 (2.9)

  5. Overflow UAdd4(u , v) True Sum 2w+1 Overflow 2w v 0 Modular Sum u Visualizing Unsigned Addition P68 Figure 2.16 • Wraps Around • If true sum ≥ 2w • At most once • Module: 取模

  6. Unsigned Addition Forms an Abelian Group P68 • Closed under addition • 0   UAddw(u , v)     2w –1 • Commutative (交换律) • UAddw(u , v) = UAddw(v , u) • Associative (结合律) • UAddw(t, UAddw (u,v)) = UAddw(UAddw (t, u ), v)

  7. Unsigned Addition Forms an Abelian Group • 0 is additive identity • UAddw(u , 0)  =  u • Every element has additive inverse • Let UCompw(u )  = 2w – u • UAddw(u , UCompw(u ))  =  0 P68 (2.10)

  8. Signed Addition • Functionality • True sum requires w+1 bits • Drop off MSB • Treat remaining bits as 2’s comp. integer P70 (2.12) PosOver:Positive Overflow NegOver:Negative Overflow

  9. True Sum 0 111…1 2w–1 PosOver TAdd Result 0 100…0 2w –1 011…1 0 000…0 0 000…0 PosOver TAdd(u , v) 1 100…0 –2w –1 100…0 > 0 –2w NegOver 1 000…0 v < 0 < 0 > 0 u NegOver Signed Addition P70 Figure 2.17

  10. Visualizing 2’s Comp. Addition • Values • 4-bit two’s comp. • Range from -8 to +7 • Wraps Around • If sum  2w-1 • Becomes negative • If sum < –2w–1 • Becomes positive

  11. NegOver TAdd4(u , v) v u PosOver Visualizing 2’s Comp. Addition P72 Figure 2.19

  12. 2w–1 PosOver 2w –1 0 NegOver Detecting Tadd Overflow P71 • Task • Given s = TAddw(u , v) • Determine if s =Addw(u , v) • Claim • Overflow iff either: • u, v < 0, s 0 (NegOver) • u, v 0, s < 0 (PosOver) • ovf = (u<0 == v<0) && (u<0 != s<0);

  13. Mathematical Properties of TAdd • Two’s Complement Under TAdd Forms a Group • Closed, Commutative, Associative, 0 is additive identity • Every element has additive inverse • Let • TAddw(u , TCompw(u ))  =  0 P73 (2.13)

  14. Mathematical Properties of TAdd • Isomorphic Algebra to UAdd • TAddw (u , v) = U2T (UAddw(T2U(u ), T2U(v))) • Since both have identical bit patterns • T2U(TAddw (u , v)) = UAddw(T2U(u ), T2U(v)) Isomorphic:同构

  15. x -1 1 1 0 1 0 1 1 1 1 1 1 1 0 1 1 1 + ~x 0 1 1 0 0 0 1 0 Negating with Complement & Increment P73 • In C • ~x + 1 == -x • Complement • Observation: ~x + x == 1111…111 == -1 ~x:Complement

  16. Signed Addition • Increment • ~x + 1 = ~x +[x + (-x)] +1 • (~x + x) + -x + 1 == -1 + (-x + 1) == -x • So, • ~x + 1 == -x

  17. Multiplication P75 • Computing Exact Product of w-bit numbers x, y • Either signed or unsigned • Ranges • Unsigned: 0 ≤ x * y ≤ (2w – 1) 2 = 22w – 2w+1 + 1 • Up to 2w bits • Two’s complement min: x *y ≥–2w–1*(2w–1–1) = –22w–2 + 2w–1 • Up to 2w–1 bits • Two’s complement max: x * y ≤ (–2w–1) 2 = 22w–2 • Up to 2w bits, but only for TMinw2

  18. Multiplication • Maintaining Exact Results • Would need to keep expanding word size with each product computed • Done in software by “arbitrary precision” arithmetic packages

  19. • • • • • • k u Operands: w bits * 2k 0 ••• 0 1 0 ••• 0 0 True Product: w+k bits u · 2k 0 ••• 0 0 UMultw(u , 2k) ••• 0 ••• 0 0 Discard k bits: w bits TMultw(u , 2k) Power-of-2 Multiply with Shift

  20. Power-of-2 Multiply with Shift • Operation • u << k gives u * 2k • Both signed and unsigned • Examples • u << 3 == u * 8 • u << 5 - u << 3 == u * 24 • Most machines shift and add much faster than multiply • Compiler will generate this code automatically

  21. k u Binary Point ••• ••• ••• Operands: / 2k 0 ••• 0 1 0 ••• 0 0 Division: u / 2k . 0 ••• 0 0 ••• Quotient: u / 2k 0 ••• 0 0 ••• Unsigned Power-of-2 Divide with Shift • Quotient of Unsigned by Power of 2 • u >> k gives  u / 2k • Uses logical shift

  22. k ••• u Binary Point ••• Operands: / 2k 0 ••• 0 1 0 ••• 0 0 Division: u / 2k . 0 ••• ••• ••• Result: RoundDown(u / 2k) 0 ••• ••• 2’s Comp Power-of-2 Divide with Shift P77 • Quotient of Signed by Power of 2 • u >> k gives  u / 2k • Uses arithmetic shift • Rounds wrong direction when u < 0

  23. Correct Power-of-2 Divide • Quotient of Negative Number by Power of 2 • Want  u / 2k (Round Toward 0) • Compute as  (u+2k-1)/ 2k • In C: (u + (1<<k)-1) >> k • Biases divided toward 0 Quotient:商

  24. Case 1: No rounding k Dividend: u 1 ••• 0 ••• 0 0 +2k +–1 0 ••• 0 0 1 ••• 1 1 Binary Point 1 ••• 1 ••• 1 1 Divisor: / 2k 0 ••• 0 1 0 ••• 0 0  u / 2k  . 0 1 ••• 1 1 1 ••• 1 ••• 1 1 • Biasing has no effect Correct Power-of-2 Divide

  25. Case 2: Rounding k Dividend: u 1 ••• ••• +2k +–1 0 ••• 0 0 1 ••• 1 1 1 ••• ••• Incremented by 1 Binary Point Divisor: / 2k 0 ••• 0 1 0 ••• 0 0  u / 2k  . 1 0 ••• 1 1 1 ••• ••• Incremented by 1 • Biasing adds 1 to final result Correct Power-of-2 Divide

  26. Floating Point

  27. Topics • Fractional Binary Numbers • IEEE 754 Standard • Rounding Mode • FP Operations • Floating Point in C • Suggested Reading: Chap 2.4

  28. Encoding Rational Numbers P80 • Form V = • Very useful when >> 0 or <<1 • An Approximation to real arithmetic • From programmer’s perspective • Uninteresting • Arcane and incomprehensive * Arcane:神秘的 * Incomprehensive: 不可理解的

  29. Encoding Rational Numbers • Until 1980s • Many idiosyncratic formats, fast speed, easy implementation, less accuracy • IEEE 754 • Designed by W. Kahan for Intel processors • Based on a small and consistent set of principles, elegant, understandable, hard to make go fast Idiosyncratic: 特殊的 Elegant:雅致的

  30. 2m 2m–1 4 • • • 2 1 bm bm–1 • • • b2 b1 b0 . b–1 b–2 b–3 • • • b–n 1/2 • • • 1/4 1/8 2–n Fractional Binary Numbers

  31. Fractional Binary Numbers • Bits to right of “binary point” represent fractional powers of 2 • Represents rational number: 2i P81 (2.17)

  32. Fractional Numbers to Binary Bits unsigned result_bits=0, current_bit=0x80000000 for (i=0;i<32;i++) { x *= 2 if ( x>= 1 ) { result_bits |= current_bit ; if ( x == 1) break ; x -= 1 ; } current_bit >> 1 ; }

  33. Fraction Binary Number Examples Value Binary Fraction 0.2 0.00110011[0011] • Observations: • The form 0.11111…11 represent numbers just below 1.0 which is noted as 1.0- • Binary Fractions can only exactly represent x/2k • Others have repeated bit patterns

  34. IEEE Floating-Point Representation P83 • Numeric form • V=(-1)sM  2E • Sign bit s determines whether number is negative or positive • Significand Mnormally a fractional value in range [1.0,2.0). • Exponent E weights value by power of two

  35. s exp frac IEEE Floating-Point Representation • Encoding • s is sign bit • exp field encodes E • frac field encodes M • Sizes • Single precision (32 bits): 8 exp bits, 23 frac bits • Double precision (64 bits): 11 exp bits, 52 frac bits

  36. Normalize Values P84 • Condition • exp000…0 and exp111…1 • Exponent coded as biased value • E = Exp – Bias • Exp : unsigned value denoted by exp • Bias : Bias value • Single precision: 127 (Exp: 1…254, E : -126…127) • Double precision: 1023 (Exp: 1…2046, E : -1022 …1023) • In general: Bias = 2m-1 - 1, where m is the number of exponent bits

  37. Normalize Values • Significand coded with implied leading 1 • m =1.xxx…x2 • xxx…x: bits of frac • Minimum when 000…0 (M = 1.0) • Maximum when 111…1 (M = 2.0 – ) • Get extra leading bit for “free”

  38. Normalized Encoding Examples • Value: 12345 (Hex: 0x3039) • Binary bits: 11000000111001 • Fraction representation: 1.1000000111001*213 • M: 10000001110010000000000 • E: 10001100 (140) • Binary Encoding • 0100 0110 0100 0000 1110 0100 0000 0000 • 4640E400

  39. Denormalized Values P84 • Condition • exp=000…0 • Values • Exponent Value: E = 1 – Bias • Significant Value m =0.xxx…x2 • xxx…x: bits of frac

  40. Denormalized Values • Cases • exp = 000…0, frac = 000…0 • Represents value 0 • Note that have distinct values +0 and –0 • exp = 000…0, frac000…0 • Numbers very close to 0.0 • Lose precision as get smaller • “Gradual underflow”

  41. Special Values P85 • Condition • exp = 111…1

  42. Special Values • exp = 111…1, frac = 000…0 • Represents value(infinity) • Operation that overflows • Both positive and negative • E.g., 1.0/0.0 = 1.0/0.0 = +, 1.0/0.0 = 

  43. Special Values • exp = 111…1, frac000…0 • Not-a-Number (NaN) • Represents case when no numeric value can be determined • E.g., sqrt(–1), 

  44.  + -Normalized +Denorm +Normalized -Denorm NaN NaN 0 +0 Summary of Real Number Encodings P85 Figure 2.22

  45. 0 7 6 3 2 s exp frac 8-bit Floating-Point Representations

  46. 8-bit Floating-Point Representations • Exp exp E 2E • 0 0000 -6 1/64 (denorms) • 1 0001 -6 1/64 • 2 0010 -5 1/32 • 3 0011 -4 1/16 • 4 0100 -3 1/8 • 5 0101 -2 1/4 • 6 0110 -1 1/2 • 7 0111 0 1 • 8 1000 +1 2 • 9 1001 +2 4 • 10 1010 +3 8 • 11 1011 +4 16 • 12 1100 +5 32 • 13 1101 +6 64 • 14 1110 +7 128 • 15 1111 n/a (inf, NaN)

  47. Dynamic Range (Denormalized numbers) P86Figure 2.23 • s exp frac E Value • 0 0000 000 -6 0 • 0 0000 001 -6 1/8*1/64 = 1/512 • 0 0000 010 -6 2/8*1/64 = 2/512 • … • 0 0000 110 -6 6/8*1/64 = 6/512 • 0 0000 111 -6 7/8*1/64 = 7/512

  48. Dynamic Range • s exp frac E Value • 0 0001 000 -6 8/8*1/64 = 8/512 • 0 0001 001 -6 9/8*1/64 = 9/512 • … • 0 0110 110 -1 14/8*1/2 = 14/16 • 0 0110 111 -1 15/8*1/2 = 15/16 • 0 0111 000 0 8/8*1 = 1 • 0 0111 001 0 9/8*1 = 9/8

  49. Dynamic Range (Denormalized numbers) • s exp frac E Value • 0 0111 010 0 10/8*1 = 10/8 • … • 0 1110 110 7 14/8*128 = 224 • 0 1110 111 7 15/8*128 = 240 • 0 1111 000 n/a inf

  50. Distribution of Representable Values • 6-bit IEEE-like format • K = 3 exponent bits • n = 2 significand bits • Bias is 3 • Notice how the distribution gets denser toward zero.

More Related