15-213 Integer Arithmetic Operations Jan. 25, 2001 C Puzzles
15-213 Integer Arithmetic Operations Jan. 25, 2001 C Puzzles
Integer Arithmetic Operations – Argue that is true for all argument values
– Give example where not true
Jan. 25, 2001 Initialization
• x < 0 ⇒ ((x*2) < 0)
• ux >= 0
int x = foo();
Topics • x & 7 == 7 ⇒ (x<<30) < 0
• Basic operations int y = bar();
• ux > -1
– Addition, negation, multiplication unsigned ux = x;
• Programming Implications • x > y ⇒ -x < -y
unsigned uy = y;
– Consequences of overflow • x * x >= 0
– Using shifts to perform power-of-2 • x > 0 && y > 0 ⇒ x + y > 0
multiply/divide
• x >= 0 ⇒ -x <= 0
• x <= 0 ⇒ -x >= 0
class04.ppt CS 213 S’01 class04.ppt –2– CS 213 S’01
10
s = UAddw(u , v) = u + v mod 2w 10
8
5
6 v
u+ v u +v < 2
w 4
UAdd w (u,v) = 0
î u+v −2 u +v ≥ 2w
w 0 1 2
3 4
2
5 6 7 8 0
u 9 10 11
12 13 14
15
Page 1
Visualizing Unsigned Addition Mathematical Properties
Wraps Around Modular Addition Forms an Abelian Group
w Overflow
• If true sum • Closed under addition
• At most once 0 ≤ UAddw(u , v) ≤ 2w –1
UAdd4(u , v) • Commutative
UAddw(u , v) = UAddw(v , u)
16
• Associative
True Sum 14
UAddw(t, UAddw(u , v)) = UAddw(UAddw(t, u ), v)
2w+1 12
• 0 is additive identity
Overflow
10
UAddw(u , 0) = u
2w 8
14 • Every element has additive inverse
6 12
10 – Let UCompw (u ) = 2w – u
4
0 8
UAddw(u , UComp w (u )) = 0
6
Modular Sum 2
4
v
0
2
0 1 2
3 4 5 6
7 8 9 10 11 0
u 12 13 14
15
Page 2
Characterizing TAdd Visualizing 2’s Comp. Addition
Functionality True Sum
NegOver
• True sum requires w+1 0 111…1 2w–1 Values
bits PosOver
• 4-bit two’s
TAdd Result TAdd4(u , v)
• Drop off MSB 0 100…0 comp.
2w –1 011…1
• Treat remaining bits as • Range from -8
2’s comp. integer to +7 8
0 000…0 0 000…0
PosOver
Wraps Around 6
• If sum ≥ 2w–1
4
TAdd(u , v) 1 100…0 –2w –1 100…0 2
– Becomes
>0 negative 0
6
1 000…0 –2w NegOver
v – At most once -2 4
2
<0 • If sum < –2w–1 -4
u + v + 2 w−1 u + v < TMin w (NegOver)
0
– Becomes -6 -2
<0
u
>0 TAddw (u,v) = u + v TMinw ≤ u + v ≤ TMax w positive -8
-4
Page 3
Two’s Complement Negation Negating with Complement & Increment
Mostly like Integer Negation In C
• TComp(u) = –u ~x + 1 == -x
2w–1
TMin is Special Case Complement
• TComp(TMin) = TMin
• Observation: ~x + x == 1111…112 == -1
Negation in C is Actually
TComp x 1 001 110 1
Tcomp(u )
mx = -x
• mx = TComp(x) + ~x 0 110 001 0
• Computes additive inverse –2w–1 2w–1
TMin >0
Decimal Hex Binary v u > v
TMin -32768 80 00 10000000 00000000 <0
~TMin 32767 7F FF 01111111 11111111
~TMin+1 -32768 80 00 10000000 00000000 <0 >0
u
0 Bad Approach
Decimal Hex Binary • Test (u–v) > 0
0 0 00 00 00000000 00000000 – TSub(u,v) = TAdd(u, TComp(v))
~0 -1 FF FF 11111111 11111111 • Problem: Thrown off by either Negative or Positive Overflow
~0+1 0 00 00 00000000 00000000
Page 4
Comparing with TSub Working Around Overflow Problems
Will Get Wrong Results Partition into Three Regions
NegOver TSub4(u , v) • u < 0, v ≥ 0 ⇒ u<v • u ≥ 0, v < 0 ⇒ u>v
• NegOver: u < 0, v > 0
– but u-v > 0 u<0
≥0 ≥0
• PosOver: u > 0, v < 0 v v v
– but u-v < 0 <0 <0 u
8 v<0
NegOver
6
<0 u ≥0 <0 u ≥0
4
u-v 2
• u, v same sign ⇒ u-v does not overflow
– Can safely use test (u–v) > 0
>0 + - 0
- + 6
-2 u-v
v 4
2
<0 - + ≥0 u ≥0 -
-4
0
+ - v +
-2 -6
v v v
-4
<0 >0 -6
-8
<0 u<0 <0 -
u -1 0 1
2 3 4 5
6 7
+
-8
-8 -7 -6
-5 -4 -3 -2
u v<0
PosOver
<0 u ≥0 <0 ≥0
u
PosOver
class04.ppt – 17 – CS 213 S’01 class04.ppt – 18 – CS 213 S’01
• Unsigned: 0 x * y
– Up to 2w bits UMultw(u , v) • • •
Discard w bits: w bits
–2w–1)*(2w–1–1) = –22w–2 + 2w–1
Page 5
Unsigned vs. Signed Multiplication Multiplication Examples
short int x = 15213;
Unsigned Multiplication
int txx = ((int) x) * x;
unsigned ux = (unsigned) x; int xx = (int) (x * x);
unsigned uy = (unsigned) y; int xx2 = (int) (2 * x * x);
unsigned up = ux * uy
• Truncates product to w-bit number up = UMultw(ux, uy) x = 15213: 00111011 01101101
txx = 231435369: 00001101 11001011 01101100 01101001
• Simply modular arithmetic
xx = 27753: 00000000 00000000 01101100 01101001
up = ux ⋅ uy mod 2w xx2 = -10030: 11111111 11111111 11011000 11010010
Two’s Complement Multiplication
int x, y; Observations
int p = x * y; • Casting order important
• Compute exact product of two w-bit numbers x, y – If either operand int, will perform int multiplication
• Truncate result tow-bit number p = TMultw(x, y) – If both operands short int, will perform short int multiplication
• Really is modular arithmetic
Relation – Computes for xx: 152132 mod 65536 = 27753
• Signed multiplication gives same bit-level result as unsigned
– Computes for xx2: (int) 55506U = -10030
• up == (unsigned) p
• Note that xx2 == (xx << 1)
class04.ppt – 21 – CS 213 S’01 class04.ppt – 22 – CS 213 S’01
Page 6
2’s Comp Power-of-2 Divide with Shift Correct Power-of-2 Divide
Quotient of Signed by Power of 2 Quotient of Negative Number by Power of 2
• u >> k gives u / 2k • Want u / 2k (Round Toward 0)
• Uses arithmetic shift • Compute as (u+2k-1)/ 2k
• Rounds wrong direction when u < 0 – In C: (u + (1<<k)-1) >> k
k
– Biases dividend toward 0
u ••• ••• Binary Point
Operands:
/ 2k 0 ••• 0 1 0 ••• 0 0 Case 1: No rounding
k
Division: u / 2k 0 ••• ••• . ••• Dividend: u 1 ••• 0 ••• 0 0
+2k +–1 0 ••• 0 0 1 ••• 1 1
Result: RoundDown(u / 2k) 0 ••• •••
1 ••• 1 ••• 1 1 Binary Point
Division Computed Hex Binary
Divisor: / 2k 0 ••• 0 1 0 ••• 0 0
y -15213 -15213 C4 93 11000100 10010011
y >> 1 -7606.5 -7607 E2 49 11100010 01001001 u / 2k 0 ••• 1 1 1
1 ••• . 1 ••• 1 1
y >> 4 -950.8125 -951 FC 49 11111100 01001001
y >> 8 -59.4257813 -60 FF C4 11111111 11000100
Biasing has no effect
Page 7
Properties of Unsigned Arithmetic Properties of Two’s Comp. Arithmetic
Unsigned Multiplication with Addition Forms Isomorphic Algebras
Commutative Ring • Unsigned multiplication and addition
• Addition is commutative group – Truncating to w bits
• Closed under multiplication • Two’s complement multiplication and addition
0 ≤ UMultw(u , v) ≤ 2w –1 – Truncating to w bits
• Multiplication Commutative Both Form Rings
UMultw(u , v) = UMultw(v , u) • Isomorphic to ring of integers mod 2w
• Multiplication is Associative
UMultw(t, UMultw(u , v)) = UMultw(UMultw(t, u ), v)
Comparison to Integer Arithmetic
• 1 is multiplicative identity • Both are rings
• Integers obey ordering properties, e.g.,
UMultw(u , 1) = u
• Multiplication distributes over addtion u>0 ⇒ u+v>v
UMultw(t, UAddw(u , v)) = UAddw(UMultw(t, u ), UMultw(t, v)) u > 0, v > 0 ⇒ u·v>0
• These properties are not obeyed by two’s complement arithmetic
TMax + 1 == TMin
15213 * 30426 == -10030 (16-bit words)
C Puzzle Answers
• Assume machine with 32 bit word size, two’s complement integers
• TMin makes a good counterexample in many cases
Page 8