Download presentation
Presentation is loading. Please wait.
Published byDjaja Kusnadi Modified over 6 years ago
1
Comp 21000 Integers Spring 2015 Topics Numeric Encodings
Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication Consequences of overflow Using shifts to perform power-of-2 multiply/divide
2
Multiplication Computing Exact Product of w-bit numbers x, y Ranges
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 (TMinw)2 Maintaining Exact Results Would need to keep expanding word size with each product computed Done in software by “arbitrary precision” arithmetic packages
3
Unsigned Multiplication in C
• • • Operands: w bits * v • • • True Product: 2*w bits u · v • • • • • • UMultw(u , v) • • • Discard w bits: w bits Standard Multiplication Function Ignores high order w bits Implements Modular Arithmetic UMultw(u , v) = u · v mod 2w
4
Unsigned vs. Signed Multiplication
Unsigned Multiplication unsigned ux = (unsigned) x; unsigned uy = (unsigned) y; unsigned up = ux * uy Truncates product to w-bit number up = UMultw(ux, uy) Modular arithmetic: up = ux uy mod 2w Two’s Complement Multiplication int x, y; int p = x * y; Compute exact product of two w-bit numbers x, y Truncate result to w-bit number p = TMultw(x, y)
5
Unsigned vs. Signed Multiplication
Unsigned Multiplication unsigned ux = (unsigned) x; unsigned uy = (unsigned) y; unsigned up = ux * uy Two’s Complement Multiplication int x, y; int p = x * y; Relation Signed multiplication gives same bit-level result as unsigned up == (unsigned) p
6
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 faster than multiply Compiler generates this code automatically (when mult by constant) k u • • • Operands: w bits * 2k ••• 1 ••• True Product: w+k bits u · 2k • • • ••• UMultw(u , 2k) ••• ••• Discard k bits: w bits TMultw(u , 2k)
7
Compiled Multiplication Code
C Function int mul12(int x) { return x*12; } x*12 = x*(3 * 4) = (x*3) * 4 = (x + x*2) * 4 Compiled Arithmetic Operations Explanation leal (%eax,%eax,2), %eax sall $2, %eax t <- x+x*2 return t << 2; C compiler automatically generates shift/add code when multiplying by constant
8
Unsigned Power-of-2 Divide with Shift
Quotient of Unsigned by Power of 2 u >> k gives u / 2k Uses logical shift k u Binary Point ••• ••• Operands: / 2k ••• 1 ••• Division: u / 2k . ••• ••• ••• Result: u / 2k ••• •••
9
Compiled Unsigned Division Code
C Function unsigned udiv8(unsigned x) { return x/8; } Compiled Arithmetic Operations Explanation shrl $3, %eax # Logical shift return x >> 3; Uses logical shift for unsigned For Java Users Logical shift written as >>>
10
Signed Power-of-2 Divide with Shift
Quotient of Signed by Power of 2 x >> k gives x / 2k Uses arithmetic shift Rounds wrong direction when u < 0 1 ••• x 2k / x / 2k Division: Operands: k RoundDown(x / 2k) Result: . Binary Point
11
Correct Power-of-2 Divide
Quotient of Negative Number by Power of 2 Want x / 2k (Round Toward 0) Compute as (x+2k-1)/ 2k In C: (x + (1<<k)-1) >> k Biases dividend toward 0 Case 1: No rounding Adding 2k-1 is adding k 1’s Last k bits must be zero if there is no rounding! k Dividend: u 1 ••• ••• +2k +–1 ••• 1 ••• 1 1 Binary Point 1 ••• 1 ••• 1 1 Divisor: / 2k ••• 1 ••• u / 2k . 1 ••• 1 1 1 ••• 1 ••• 1 1 All the added 1’s are shifted out! Biasing has no effect
12
Correct Power-of-2 Divide (Cont.)
Case 2: Rounding k Dividend: x 1 ••• ••• +2k +–1 ••• 1 ••• 1 1 1 ••• ••• Incremented by 1 Binary Point Divisor: / 2k ••• 1 ••• x / 2k . 1 ••• 1 1 1 ••• ••• Biasing adds 1 to final result Incremented by 1
13
Correct Power-of-2 Divide (Cont.)
Case 2: Rounding Proof If there would be rounding in k shifts, then at least one of the last k bits must be 1. Adding k 1’s will thus cause a 1 to be carried into bit position k+1 This will have the effect of adding 1 to the final answer.
14
Compiled Signed Division Code
C Function int idiv8(int x) { return x/8; } Compiled Arithmetic Operations Explanation testl %eax, %eax js L4 L3: sarl $3, %eax ret L4: addl $7, %eax jmp L3 if x < 0 x += 7; // why? # Arithmetic shift return x >> 3; Uses arithmetic shift for int For Java Users Arith. shift written as >>
15
Conversions: see slide 17 When to use unsigned: see slide 26
C Puzzle Answers Assume machine with 32 bit word size, two’s comp. integers TMin makes a good counterexample in many cases x < 0 ((x*2) < 0) ux >= 0 x & 7 == 7 (x<<30) < 0 ux > -1 x > y -x < -y x * x >= 0 x > 0 && y > 0 x + y > 0 x >= 0 -x <= 0 x <= 0 -x >= 0 x < 0 ((x*2) < 0) False: TMin ux >= 0 True: 0 = UMin x & 7 == 7 (x<<30) < 0 True: x1 = 1 ux > -1 False: 0 x > y -x < -y False: -1, TMin x * x >= 0 False: x > 0 && y > 0 x + y > 0 False: TMax, TMax x >= 0 -x <= 0 True: –TMax < 0 x <= 0 -x >= 0 False: TMin Conversions: see slide 17 Casting: see slide 20, 21 When to use unsigned: see slide 26
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.