Download presentation
Presentation is loading. Please wait.
Published byIra Watts Modified over 6 years ago
1
Arithmetic Topics Basic operations Programming Implications
Addition, negation, multiplication Programming Implications Consequences of overflow Using shifts to perform power-of-2 multiply/divide
2
Unsigned Addition Standard Addition Function
• • • Operands: w bits + v • • • True Sum: w+1 bits u + v • • • Discard Carry: w bits UAddw(u , v) • • • Standard Addition Function Ignores carry output Implements Modular Arithmetic s = UAddw(u , v) = u + v mod 2w
3
Visualizing Integer Addition
4-bit integers u, v Compute true sum Values increase linearly with u and v Forms planar surface But, our sum must fit in a 4-bit register... Add4(u , v) v u
4
Visualizing Unsigned Addition
Wraps Around At most once if true sum ≥ 2w Overflow UAdd4(u , v) True Sum 2w 2w+1 Overflow v Modular Sum u
5
Two’s Complement Addition
u • • • Operands: w bits + v • • • True Sum: w+1 bits u + v • • • Discard Carry: w bits TAddw(u , v) • • • TAdd and UAdd have Identical Bit-Level Behavior Signed vs. unsigned addition in C: int s, t, u, v; s = (int)((unsigned)u + (unsigned)v); t = u + v Will give s == t
6
Visualizing 2’s Comp. Addition
NegOver Values 4-bit two’s comp. Range from -8 to +7 Wraps Around If sum 2w–1 Becomes negative At most once If sum < –2w–1 Becomes positive TAdd4(u , v) v u PosOver
7
Detecting 2’s Comp. Overflow
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) Example int s, u, v; s = u + v; ovf = (u<0 == v<0) && (u<0 != s<0); 2w –1 2w–1 PosOver NegOver
8
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
9
Unsigned vs. Signed Multiplication
Unsigned Multiplication unsigned ux, uy; 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)
10
Unsigned vs. Signed Multiplication
Unsigned Multiplication unsigned ux, uy; 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
11
Power-of-2 Multiply with Shift
Operation u << k gives u * 2k Both signed and unsigned Examples u << 3 == u * 8 (u << 3) + u == u * 9 Most machines shift and add much faster than multiply Compiler generates this code automatically 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)
12
Unsigned Power-of-2 Divide w/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 ••• •••
13
Arithmetic Surprises Assume 32-bit integers, signed x and y, unsigned ux Are the following expressions always true? 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: x2 = 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.