Download Integer Arithmetic, Lecture Slide - Computer Science and more Slides Software Engineering in PDF only on Docsity!
“The course that gives CMU its Zip!”
Topics
- Basic operations
- Addition, negation, multiplication
- Programming Implications
- Consequences of overflow
- Using shifts to perform power-of- multiply/divide
class04.ppt CS 213 F’
Integer Arithmetic Operations
Sept. 3, 1998
C Puzzles
- Taken from Exam #2, CS 347, Spring ‘
- 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 ⇒ ((x2) < 0)*
- ux >= 0
- x & 7 == 7 ⇒ (x<<30) < 0
- ux > -
- 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
(^0 1 2 3 4 5 6 ) (^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 Addition
Integer Addition
- 4-bit integers u and v
- Compute true sum Add 4 ( u , v )
- Values increase linearly with u and v
- Forms planar surface
Add 4 ( u , v )
u
v
(^0 1 2 3 4 5 6 ) (^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 Addition
Wraps Around
- If true sum ≥ 2 w
- At most once
2 w
2 w +
UAdd 4 ( u , v )
u
v
True Sum
Modular Sum
Overflow
Overflow
Detecting Unsigned Overflow
Task
- Given s = UAdd w ( u , v )
- Determine if s = u + v
Application
unsigned s, u, v; s = u + v;
Claim
- Overflow iff s < u ovf = (s < u)
- Or symmetrically iff s < v
Proof
- Know that 0 ≤ v < 2 w
- No overflow ==> s = u + v ≥ u + 0 = u
- Overflow ==> s = u + v – 2 w^ < u + 0 = u
u v
2 w
s
u v
2 w
s
No Overflow
Overflow
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 TAdd w ( u , v )
-8 (^) -7 (^) -6 (^) -5 (^) -4 (^) -3 (^) -2 (^) - (^0 1 2 3 4 5 ) 7 -
0
2
4 6
0
2
4
6
8
Visualizing 2’s Comp. Addition
Values
- 4-bit two’s comp.
- Range from - to +
Wraps Around
- If sum ≥ 2 w –
- Becomes negative
- At most once
- If sum < –2 w –
- Becomes positive
- At most once
TAdd 4 ( u , v )
u
v PosOver
NegOver
Detecting 2’s Comp. Overflow
Task
- Given s = TAdd w ( u , v )
- Determine if s = Add w ( 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
2 w^ –
2 w – PosOver
NegOver
Two’s Complement Negation
Mostly like Integer Negation
T Min is Special Case
Negation in C is Actually
TComp
mx = -x
- mx = TComp( x )
- Computes additive inverse for TAdd x + -x == 0
1000
1001 1010
1011 1100
1101 1110
1111 0000
0001 0010
0011 0100
0101 0110
0111
–2 w –
2 w –
–2 w –
2 w –
u
Tcomp( u )
Negating with Complement & Increment
In C
~x + 1 == -x
Complement
- Observation: ~x + x == 1111…11 2 == -
Increment
- ~x + x + (-x + 1) == -1 + (-x + 1)
- ~x + 1 == -x
Warning: Be cautious treating int’s as integers
- OK here: We are using group properties of TAdd and TComp
x^ 1 0 0^ 1 1 1 0 1
+ ~x^ 0 1 1^ 0 0 0 1 0
-1^ 1 1 1^ 1 1 1 1 1
Comparing Two’s Complement Numbers
Task
- Given signed numbers u , v
- Determine whether or not u > v
- Return 1 for numbers in shaded region below
Bad Approach
- Test (u–v) > 0
- TSub( u , v ) = TAdd( u , TComp( v ))
- Problem: Thrown off by either Negative or Positive Overflow
u
v
u < v
u > v
u==v
-8 -7^ -6^ -5^ -4^ -3^ -2^ -1^0 -8^1 2 3 4 5 6
0 2 4 6
0
2
4
6
8
Comparing with TSub
Will Get Wrong Results
- NegOver: u < 0 , v > 0
- PosOver: u > 0 , v < 0
u
v
u-v
NegOver
PosOver
TSub 4 ( u , v )
u
v
PosOver
NegOver
Multiplication
Computing Exact Product of w -bit numbers x , y
- Either signed or unsigned
Ranges
- Unsigned: 0 ≤ x * y ≤ (2 w^ – 1) 2 = 2^2 w^ – 2 w +1^ + 1
- Two’s complement min: x * y ≥ (–2 w –1)*(2 w –1–1) = –2^2 w –2^ + 2 w –
- Two’s complement max: x * y ≤ (–2 w –1) 2 = 2^2 w –
- Up to 2 w 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
- Also implemented in Lisp, ML, and other “advanced” languages
Unsigned Multiplication in C
Standard Multiplication Function
- Ignores high order w bits
Implements Modular Arithmetic
UMult w ( u , v ) = u · v mod 2 w
u ***** v u · v • • •
True Product: 2* w bits
Operands: w bits
Discard w bits: w bits UMult w ( u^ ,^ v )