15-213 “The course that gives CMU its Zip!” Topics • Basic operations – Addition, negation, multiplication • Programming Implications – Consequences of overflow – Using shifts to perform power- of-2 multiply/divide CS 213 F’98 class04.ppt Integer Arithmetic Operations Sept. 3, 1998
28
Embed
15-213 “The course that gives CMU its Zip!” Topics Basic operations –Addition, negation, multiplication Programming Implications –Consequences of overflow.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
15-213“The course that gives CMU its
Zip!”
Topics• Basic operations
– Addition, negation, multiplication
• Programming Implications
– Consequences of overflow
– Using shifts to perform power-of-2 multiply/divide
CS 213 F’98class04.ppt
Integer Arithmetic OperationsSept. 3, 1998
CS 213 F’98– 2 –class04.ppt
C Puzzles• Taken from Exam #2, CS 347, Spring ‘97
• Assume machine with 32 bit word size, two’s complement integers
• For each of the following C expressions, either:
– Argue that is true for all argument values
– Give example where not 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
int x = foo();
int y = bar();
unsigned ux = x;
unsigned uy = y;
Initialization
CS 213 F’98– 3 –class04.ppt
Unsigned Addition
Standard Addition Function• Ignores carry output
Implements Modular Arithmetics = UAddw(u , v) = u + v mod
2w
• • •
• • •
u
v+
• • •u + v
• • •
True Sum: w+1 bits
Operands: w bits
Discard Carry: w bits UAddw(u , v)
CS 213 F’98– 4 –class04.ppt
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0
2
4
6
8
10
12
14
0
5
10
15
20
25
30
Visualizing Integer AdditionInteger Addition
• 4-bit integers u and v
• Compute true sum Add4(u , v)
• Values increase linearly with u and v
• Forms planar surface
Add4(u , v)
u
v
CS 213 F’98– 5 –class04.ppt
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0
2
4
6
8
10
12
14
0
2
4
6
8
10
12
14
16
Visualizing Unsigned AdditionWraps Around
• If true sum ≥ 2w
• At most once
0
2w
2w+1
UAdd4(u , v)
u
v
True Sum
Modular Sum
Overflow
Overflow
CS 213 F’98– 6 –class04.ppt
Mathematical PropertiesModular Addition Forms an Abelian Group
• 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)
• 0 is additive identity
UAddw(u , 0) = u
• Every element has additive inverse
– Let UCompw (u ) = 2w – u
UAddw(u , UCompw (u )) = 0
CS 213 F’98– 7 –class04.ppt
Detecting Unsigned OverflowTask
• Given s = UAddw(u , v)
• Determine if s = u + v
Applicationunsigned s, u, v;
s = u + v;• Did addition overflow?
Claim• Overflow iff s < u
ovf = (s < u)• Or symmetrically iff s < v
Proof• Know that 0 ≤ v < 2w
• No overflow ==> s = u + v ≥ u + 0 = u
• Overflow ==> s = u + v – 2w < u + 0 = u
u v
2w
s
u v
2w
s
No Overflow
Overflow
CS 213 F’98– 8 –class04.ppt
Two’s Complement Addition
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
• • •
• • •
u
v+
• • •u + v
• • •
True Sum: w+1 bits
Operands: w bits
Discard Carry: w bits TAddw(u , v)
CS 213 F’98– 9 –class04.ppt
Characterizing TAddFunctionality
• True sum requires w+1 bits
• Drop off MSB
• Treat remaining bits as 2’s comp. integer
–2w –1
–2w
0
2w –1
2w–1
True Sum
TAdd Result
1 000…0
1 100…0
0 000…0
0 100…0
0 111…1
100…0
000…0
011…1
PosOver
NegOver
(NegOver)
(PosOver)
u
v
< 0 > 0
< 0
> 0
NegOver
PosOverTAdd(u , v)
CS 213 F’98– 10 –class04.ppt
-8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
-8
-6
-4
-2
0
2
4
6
-8
-6
-4
-2
0
2
4
6
8
Visualizing 2’s Comp. Addition
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
– At most once
TAdd4(u , v)
uv
PosOver
NegOver
CS 213 F’98– 11 –class04.ppt
Detecting 2’s Comp. OverflowTask
• Given s = TAddw(u , v)
• Determine if s = Addw(u , v)
• Example
int s, u, v;
s = 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);
Proof• Easy to see that if u ≥ 0 and v < 0, then TMinw ≤ u +v ≤ TMaxw
• Symmetrically if u < 0 and v ≥ 0
• Other cases from analysis of TAdd
0
2w –1
2w–1PosOver
NegOver
CS 213 F’98– 12 –class04.ppt
Mathematical Properties of TAddIsomorphic Algebra to UAdd
• TAddw(u , v) = U2T(UAddw(T2U(u ), T2U(v)))
– Since both have identical bit patterns
Two’s Complement Under TAdd Forms a Group• Closed, Commutative, Associative, 0 is additive identity
• Every element has additive inverse
– Let TCompw (u ) = U2T(UCompw(T2U(u ))
TAddw(u , TCompw (u )) = 0
TCompw(u) =−u u≠TMinwTMinw u=TMinw
⎧⎨⎩
CS 213 F’98– 13 –class04.ppt
Two’s Complement NegationMostly like Integer Negation