Integer Arithmetic CS 347 Lecture 03 January 20, 1998 Topics Numeric Encodings Unsigned & Two s complement Programming Implications C promotion rules Consequences of overflow Basic operations Addition, negation, multiplication
Word Size w Integers 32 for most machines Notation 64 for Alpha and next generation of high end machines 8 or 16 for many signal processing applications Lower case E.g., x, y, z Bit Vectors Upper Case E.g., X, Y, Z Write individual bits as integers with value 0 or 1 E.g., X = x w 1, x w 2, x 0 Big-Endian 2
Encodings Unsigned B2U(X) = w 1 i= 0 x i Two s Complement w 2 i = 0 2 i B2T(X) = x i 2 i x w 1 2 w 1 Ranges UMin = 0 000 0 0x00 0 UMax = 2 w 1 111 1 0xFF F 3 Ranges TMin = 2 w 1 100 0 0x80 0 TMax = 2 w 1 1 011 1 0x7F F Other Values Zero 000 0 Minus 1 111 1 0xFF F Sign Bit
W = 32 Real-Life Values UMax +4,294,967,295 TMax + 2,147,483,647 TMin 2,147,483,648 W = 64 UMax +18,446,744,073,709,551,615 TMax +9,223,372,036,854,775,807 TMin 9,223,372,036,854,775,808 C Programming #include <limits.h> Declares constants: ULONG_MAX LONG_MAX LONG_MIN 4
Encoding Example X B2U(X) B2T(X) 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 7 1010 10 6 1011 11 5 1100 12 4 1101 13 3 1110 14 2 1111 15 1 5 Observe Same encodings for nonnegative values Assymetric range TMax Tmin Each Encoding isbijection Every bit pattern represents unique integer value Each representable integer has unique bit encoding ==> Can Invert Mappings U2B(x) = B2U -1 (x) Bit pattern for unsigned integer T2B(x) = B2T -1 (x) Bit pattern for two s comp integer
Relation Between Unsigned & Two s Comp. Unsigned Two s Complement U2T x U2B B2T x X Maintain Same Bit Pattern w 2 B2T(X) = x i = 0 i 2 i x w 1 2 w 1 w 2 = i =0 x i 2 i +x w 1 2 w 1 x w 1 2 w w 1 = i =0 x i 2 i x w 1 2 w = B2U(X) x w 1 2 w x' = x x 2 w x < 2 w 1 x 2 w 1 6
From Unsigned to Two s Complement U2T(x) = B2T(U2B(x)) = x x w 1 2 w 2 w 1 Identity U2T(x) What you get in C: int u2t(unsigned x) { return (int) x; } 2 w 1 2 w X 1010 B2U(X) 10 B2T(X) 6 2 w 1 0001 0011 0101 0111 1001 1011 1101 1111 16 0000 0010 0100 0110 1000 1010 1100 1110 7
From Two s Complement to Unsigned T2U(x) = B2U(T2B(x)) = x + x w 1 2 w T2U(x) 2 w What you get in C: int t2u(int x) { return (unsigned) x; } 2 w 1 Identity X 1010 B2U(X) B2T(X) 10 6 + 16 1001 1011 1101 1111 0001 0011 0101 1000 1010 1100 1110 0000 0010 0100 0110 0 2 w 1 0 2 w 1 0111 8
Constants Signed vs. Unsigned in C By default are considered to be signed integers Unsigned if have U as suffix 0U, 4294967259U Casting Explicit casting between signed & unsigned same as U2T and T2U int tx, ty; unsigned ux, uy; tx = (int) ux; uy = (unsigned) ty; Implicit casting also occurs via assignments and procedure calls tx = ux; uy = ty; 9
Expression Evaluation Casting Surprises If mix unsigned & signed in single expression, signed values implicitly cast to unsigned Including comparison operations <, >, ==, <=, >= Constant 1 Constant 2 Relation Evaluation 0 0U == unsigned -1 0 < signed -1 0U? 2147483647-2147483648? 2147483647U -2147483648? -1-2? (unsigned) -1-2? 2147483647 2147483648U? 2147483647 (int) 2147483648U? 10
Adder Circuit Adder Structure Full Adder u 3 u 2 u 1 u 0 v 3 v 2 v 1 v 0 Carry = (u+v+w) / 2 FA FA FA FA FA Cin = 0 Sum = (u+v+w) mod 2 Cout (Ignore) s 3 s 2 s 1 s 0 Assume Carry Input = 0 Ignore Carry Output Observation Output at bit position i depends only on inputs at positions 0 to i 11
Unsigned Addition u v UAdd w u2b u2b Standard Addition Function Ignores carry output Implements Modular Arithmetic s = UAdd w (u, v) = u + v mod 2 w w-bit Adder b2u UAdd w (u,v) = u + v u + v 2 w u + v < 2 w u + v 2 w s 12
Visualizing Integer Addition Integer Addition 4-bit integers u and v Compute sum Add 4 (u, v) Values increase linearly with u and v Forms planar surface Add 4 (u, v) 30 25 20 15 10 10 12 14 5 0 0 1 2 3 4 5 6 7 8 9 u 10 11 12 13 14 15 0 2 4 6 8 v 13
Visualizing Unsigned Addition Wraps Around If true sum 2 w At most once UAdd 4 (u, v) 16 True Sum 2 w+1 14 12 10 2 w 8 6 14 12 0 Modular Sum 4 2 0 0 1 2 3 4 5 6 7 8 9 u 10 11 12 13 14 15 0 2 4 6 8 10 v 14
Mathematical Properties Modular Addition Forms an Abelian Group Closed under addition 0 UAdd w (u, v) 2 w 1 Commutative UAdd w (u, v) = UAdd w (v, u) Associative UAdd w (t, UAdd w (u, v)) = UAdd w (UAdd w (t, u ), v) 0 is additive identity UAdd w (u, 0) = u Every element has additive inverse Let UComp w (u ) = 2 w u UAdd w (u, UComp w (u )) = 0 15
Detecting Unsigned Overflow Task Given s = UAdd w (u, v) Determine if s = u + v Application unsigned s, u, v; s = u + v; Did addition overflow? Claim Overflow iff s < u ovf = (s < u) Or symmetrically iff s < v Proof No Overflow u s 2 w Overflow u v v 2 w s Know that 0 v < 2 w No overflow ==> s = u + v u + 0 = u Overflow ==> s = u + v 2 w < u + 0 = u 16
Two s Complement Addition u t2b TAdd w w-bit Adder v t2b Add Two s Complement Numbers with Conventional Adder E.g., signed addition in C: int s, u, v; s = u + v; Ignores carry output s = TAdd w (u, v) What is the behavior of this operation? b2t s 17
Characterizing TAdd Functionality True sum requires w+1 bits Drop off MSB Treat remaining bits as 2 s comp. integer 0 111 1 0 100 0 0 000 0 True Sum 2 w 1 2 w 1 0 TAdd Result 011 1 000 0 1 100 0 2 w 1 100 0 1 000 0 2 w TAdd w (u,v) = u + v + 2 w 1 u + v u + v 2 w 1 u + v < TMin w TMin w u + v TMax w TMax w < u + v 18
Visualizing 2 s Comp. Addition Values 4-bit two s comp. Range from -8 to +7 TAdd 4 (u, v) 8 Wraps Around 6 If sum 2 w 1 4 At most once 2 Becomes negative If sum < 2 w 1 0-2 -4 0 2 4 6 At most once Becomes positive -6-8 -8-7 -6-5 -4-3 -2-1 0 1 u 2 3 4 5 6 7-8 -6-4 -2 v 19
Detecting 2 s Comp. Overflow Task Given s = TAdd w (u, v) 2 w 1 Determine if s = u + v Application Claim int s, u, v; s = u + v; 0 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 2 w 1 PosOver NegOver Easy to see that if u 0 and v < 0, then TMin w u +v TMax w Symmetrically if u < 0 and v 0 Other cases from analysis of TAdd 20
Mathematical Properties of TAdd Isomorphic Algebra to UAdd TAdd w (u, v) = U2T(UAdd w (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 TComp w (u ) = U2T(UComp w (T2U(u )) TAdd w (u, TComp w (u )) = 0 TComp w (u) = u u TMin w?? u = TMin w 21
Two s Complement Negation Mostly like Integer Negation TComp(u) = u 2 w 1 TMin is Special Case TComp(TMin) = TMin Negation in C is NOT True Negation Tcomp(u ) mx = -x 2 w 1 2 w 1 Really mx = TComp(x) But is a complement x + -x == 0 2 w 1 1001 1011 1101 1111 0001 0011 0101 0111 1000 1010 1100 1110 0000 0010 0100 0110 u 22
Negating with Complement & Increment In C ~x + 1 == -x Complement Derive using property that for bit value x : x = 1 x w 2 B2T w ( X ) = (1 x i=0 i ) 2 i (1 x w 1 ) 2 w 1 = ( 2 w 1 1)+ 2 w 1 B2T w ( X) = B2T w ( X) 1 Increment TAdd w ( B2T w ( X ),1) = B2T w ( X) X 100 0 TMin w X = 100 0 = TComp w B2T w ( X) ( ) 23
Comp. & Incr. Example X B2T(X) 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 1001 1010 1011 1100 1101 1110 1111 8 7 6 5 4 3 2 1 X 1111 1110 1101 1100 1011 1010 1001 1000 0111 0110 0101 0100 0011 0010 0001 0000 B2T(X) 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0 Add(X, 1) 0000 1111 1110 1101 1100 1011 1010 1001 1000 0111 0110 0101 0100 0011 0010 0001 B2T( ) 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 24
Task Complement Upper Bits Given bit vector X Negate it without using addition Rule Let k be minimum value such that x k = 1 I.e., X = x w 1, x w 2, x k+1,1, 0,,0 Compute Negate(X ) = Justification Effect of complement: X = x w 1, x w 2, x k+1, 0,1,,1 Effect of increment: Observation Low order bit of X and X identical x w 1, x w 2, x k+1,1, 0,,0 Add(X, 1) = x w 1, x w 2, x k+1, 1,0,,0 25
Upper Bits Complement Example X B2T(X) 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 1001 1010 1011 1100 1101 1110 1111 8 7 6 5 4 3 2 1 UC(X) 0000 1111 1110 1101 1100 1011 1010 1001 1000 0111 0110 0101 0100 0011 0010 0001 B2T( ) 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 26
Task: Sign Extension Given w-bit signed integer x Convert it to w+k-bit integer with same value Rule: Make k copies of sign bit: X = x w 1,, x w 1, x w 1, x w 2,, x 0 k copies of MSB Correctness Argument Induct on k, i.e., that extending by single bit maintains value Key observation: 2 w 1 = 2 w +2 w 1 w 2 i =0 w 2 i=0 w 2 i=0 B2T( X ) = x w 1 2 w + x w 1 2 w 1 + x i 2 i = B2T( X) ( ) + x i 2 i ( ) + x i 2 i = x w 1 2 w + 2 w 1 = x w 1 2 w 1 27
Multiplication Computing Exact Product of w-bit numbers x, y Either signed or unsigned Ranges Unsigned: 0 x * y (2 w 1) 2 = 2 2w 2 w+1 + 1 Up to 2w bits Two s complement min: x * y ( 2 w 1 )*(2 w 1 1) = 2 2w 2 + 2 w 1 Up to 2w 1 bits Two s complement max: x * y ( 2 w 1 ) 2 = 2 2w 2 Up to 2w bits, but only for TMin 2 w 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 28
Operation Multiplication In C int x, y; int p = x * y; Compute exact product of two w-bit numbers x, y Truncate result tow-bit number p = TMult w (x, y) Unsigned Counterpart unsigned ux = (unsigned) x; unsigned uy = (unsigned) y; unsigned up = ux * uy Truncates product to w-bit number up = UMult w (ux, uy) Simply modular arithmetic up = ux uy mod 2 w Relation Claim that up == (unsigned) p 29
Multiplication Analysis B2T(X) = w 2 i = 0 x i 2 i x w 1 2 w 1 ax x = ax 2 w 1 sx sx ux = ax + 2 w 1 sx x y = ax ay 2 w 1 (sx ay + sy ax ) + 2 2w 2 sx sy ux uy = ax ay + 2 w 1 (sx ay + sy ax ) + 2 2w 2 sx sy α (2w 2 bits) β (w bits) γ (1 bit) 30
Multiplication Analysis (cont.) Signed (w = 5) Unsigned α β + γ α + β + γ p up Observe Only LSB of β affects values of p and up LSB of β and β are the same p and up have matching bit patterns! 31
Algebraic Properties of Unsigned Unsigned Multiplication with Addition Forms Commutative Ring Addition is commutative group Closed under multiplication 0 UMult w (u, v) 2 w 1 Multiplication Commutative UMult w (u, v) = UMult w (v, u) Multiplication is Associative UMult w (t, UMult w (u, v)) = UMult w (UMult w (t, u ), v) 1 is multiplicative identity UMult w (u, 1) = u Multiplication distributes over addtion UMult w (t, UAdd w (u, v)) = UAdd w (UMult w (t, u ), UMult w (t, v)) 32
Algebraic Properties of Two s Comp. Isomorphic Algebras Unsigned multiplication and addition Truncating to w bits Two s complement multiplication and addition Truncating to w bits Both Form Rings Isomorphic to ring of integers mod 2 w NOT isomorphic to ring of integers 33
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 Initialization int x = foo(); int y = bar(); unsigned ux = x; unsigned uy = y; 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 34