Topics CISC3 Integers Sep 11, 28 Nmeric Encodings Unsigned & Twoʼs complement Programming Implications C promotion rles Basic operations Addition, negation, mltiplication Programming Implications Conseqences of overflow Using shifts to perform power-of-2 mltiply/divide Powerpoint Lectre Notes for Compter Systems: A Programmer's Perspective, R. Bryant and D. O'Hallaron, Prentice Hall, 23 15-213 Fʼ2 Encoding Integers Unsigned Twoʼs Complement B2U(X) = w"1 # x i!2 i B2T(X) =!x w!1 "2 w!1 w!2 + # x i "2 i i= i= short int x = 15213; short int y = -15213; C short 2 bytes long Sign Bit Sign Bit Decimal Hex Binary x 15213 3B D 11111 11111 y -15213 C4 93 111 1111 For 2ʼs complement, most significant bit indicates sign for nonnegative 1 for negative 2 CISC3 Fa8 Encoding Example (Cont.) x = 15213: 11111 11111 y = -15213: 111 1111 Weight 15213-15213 1 1 1 1 1 2 1 2 4 1 4 8 1 8 1 1 1 32 1 32 4 1 4 128 1 128 25 1 25 512 1 512 124 1 124 248 1 248 49 1 49 8192 1 8192 1384 1 1384-3278 1-3278 3 Sm 15213-15213 CISC3 Fa8
Nmeric Ranges Unsigned Vales UMin = UMax = 2 w 1 111 1 Vales for W = 1 Twoʼs s Complement Vales TMin = 2 w 1 1 TMax = 2 w 1 1 11 1 Other Vales Mins 1 111 1 Decimal Hex Binary UMax 5535 FF FF 11111111 11111111 TMax 3277 7F FF 1111111 11111111 TMin -3278 8 1-1 -1 FF FF 11111111 11111111 4 CISC3 Fa8 Vales for Different Word Sizes W 8 1 32 4 UMax 255 5,535 4,294,97,295 18,44,744,73,79,551,15 TMax 127 32,77 2,147,483,47 9,223,372,3,854,775,87 TMin -128-32,78-2,147,483,48-9,223,372,3,854,775,88 Observations TMin = TMax + 1 Asymmetric range UMax = 2 * TMax + 1 C Programming #inclde <limits.h> K&R App. B11 Declares constants, e.g., ULONG_MAX LONG_MAX LONG_MIN Vales platform-specific 5 CISC3 Fa8 Unsigned & Signed Nmeric Vales X B2U(X) B2T(X) Eqivalence Same encodings for 1 1 1 nonnegative vales 1 2 2 11 3 3 Uniqeness 1 4 4 Every bit pattern represents 11 5 5 niqe integer vale 11 111 7 7 Each representable integer 1 8 8 has niqe bit encoding 11 9 7 11 1 Can Invert Mappings 111 11 5 U2B(x) = B2U -1 (x) 11 12 4 Bit pattern for nsigned 111 13 3 integer 111 14 2 T2B(x) = B2T -1 (x) 1111 15 1 Bit pattern for twoʼs comp integer CISC3 Fa8
Casting Signed to Unsigned C Allows Conversions from Signed to Unsigned short int x = 15213; nsigned short int x = (nsigned short) x; short int y = -15213; nsigned short int y = (nsigned short) y; Reslting Vale No change in bit representation Nonnegative vales nchanged x = 15213 Negative vales change into (large) positive vales y = 5323 7 CISC3 Fa8 Relation between Signed & Unsigned Twoʼs Complement Unsigned T2U x T2B B2U x X Maintain Same Bit Pattern - w 1 x + + + + + + x - + + + + + +2 w 1 2 w 1 = 2*2 w 1 = 2 w x = " x x! # $ x + 2 w x < 8 CISC3 Fa8 Relation Between Signed & Unsigned Weight -15213 5323 1 1 1 1 1 2 1 2 1 2 4 8 1 1 1 1 1 32 4 128 1 128 1 128 25 512 124 1 124 1 124 248 49 8192 1384 1 1384 1 1384 3278 1-3278 1 3278 Sm -15213 5323 y = y + 2 * 3278 = y + 553 9 CISC3 Fa8
Signed vs. Unsigned in C Constants By defalt are considered to be signed integers Unsigned if have U as sffix U, 429497259U Casting Explicit casting between signed & nsigned same as U2T and T2U int tx, ty; nsigned x, y; tx = (int) x; y = (nsigned) ty; Implicit casting also occrs via assignments and procedre calls tx = x; y = ty; 1 CISC3 Fa8 Casting Srprises Expression Evalation If mix nsigned and signed in single expression, signed vales implicitly cast to nsigned Inclding comparison operations <, >, ==, <=, >= Examples for W = 32 Constant 1 Constant 2 Relation Evalation U == nsigned -1 < signed -1 U > nsigned 214748347-214748348 > signed 214748347U -214748348 < nsigned -1-2 > signed (nsigned) -1-2 > nsigned 214748347 214748348U < nsigned 11 214748347 (int) 214748348U > CISC3 signedfa8 Explanation of Casting Srprises 2ʼs s Comp. Unsigned Ordering Inversion Negative Big Positive UMax UMax 1 TMax TMax + 1 TMax Unsigned Range 2ʼs Comp. Range 1 2 TMin 12 CISC3 Fa8
Sign Extension Task: Rle: Given w-bit signed integer x Convert it to w+k-bit integer with same vale Make k copies of sign bit: X = x w 1,, x w 1, x w 1, x w 2,, x k copies of MSB X w X 13 k w CISC3 Fa8 Sign Extension Example short int x = 15213; int ix = (int) x; short int y = -15213; int iy = (int) y; Decimal Hex Binary x 15213 3B D 11111 11111 ix 15213 3B D 11111 11111 y -15213 C4 93 111 1111 iy -15213 FF FF C4 93 11111111 11111111 111 1111 Converting from smaller to larger integer data type C atomatically performs sign extension 14 CISC3 Fa8 Jstification For Sign Extension Prove Correctness by Indction on k Indction Step: extending by single bit maintains vale X w - X - + w+1 Key observation: 2 w 1 = 2 w +2 w 1 Look at weight of pper bits: X 2 w 1 x w 1 X 2 w x w 1 + 2 w 1 x w 1 = 2 w 1 x w 1 15 CISC3 Fa8
Why Shold I Use Unsigned? Donʼt Use Jst Becase Nmber Nonzero C compilers on some machines generate less efficient code nsigned i; for (i = 1; i < cnt; i++) a[i] += a[i-1]; Easy to make mistakes for (i = cnt-2; i >= ; i--) a[i] += a[i+1]; Do Use When Performing Modlar Arithmetic Mltiprecision arithmetic Other esoteric stff Do Use When Need Extra Bitʼs s Worth of Range Working right p to limit of word size 1 CISC3 Fa8 Negating with Complement & Increment Claim: Following Holds for 2ʼs 2 s Complement ~x + 1 == -x Complement Observation: ~x + x == 1111 11 2 == -1 Increment ~x + x + (-x + 1) == -1 + (-x + 1) ~x + 1 == -x + x ~x -1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Warning: Be catios treating intʼs as integers 17 OK here CISC3 Fa8 Comp. & Incr. Examples x = 15213 Decimal Hex Binary x 15213 3B D 11111 11111 ~x -15214 C4 92 111 111 ~x+1-15213 C4 93 111 1111 y -15213 C4 93 111 1111 Decimal Hex Binary ~ -1 FF FF 11111111 11111111 ~+1 18 CISC3 Fa8
Unsigned Addition Operands: w bits + v Tre Sm: w+1 bits + v Discard Carry: w bits UAdd w (, v) Standard Addition Fnction Ignores carry otpt Implements Modlar Arithmetic s = UAdd w (, v) = + v mod 2 w UAdd w (,v) = # + v $ % + v! 2 w + v < 2 w + v " 2 w 19 CISC3 Fa8 Visalizing Integer Addition Integer Addition 4-bit integers, v Compte tre sm Add 4 (, v) Vales increase linearly with and v Forms planar srface 32 28 24 2 1 12 Add 4 (, v) Integer Addition 14 12 8 4 2 4 8 1 12 14 2 4 1 8 v 2 CISC3 Fa8 Visalizing Unsigned Addition Wraps Arond If tre sm 2 w At most once UAdd 4 (, v) Overflow Tre Sm 2 w+1 Overflow 1 14 12 2 w Modlar Sm 1 8 4 2 2 4 8 1 12 14 2 4 1 8 14 12 v 21 CISC3 Fa8
Mathematical Properties Modlar Addition Forms an Abelian Grop Closed nder addition UAdd w (, v) 2 w 1 Commtative UAdd w (, v) = UAdd w (v, ) Associative UAdd w (t, UAdd w (, v)) = UAdd w (UAdd w (t, ), v) is additive identity UAdd w (, ) = Every element has additive inverse Let UComp w ( ) = 2 w UAdd w (, UComp w ( )) = 22 CISC3 Fa8 Twoʼs Complement Addition Operands: w bits + v Tre Sm: w+1 bits + v Discard Carry: w bits TAdd w (, v) TAdd and UAdd have Identical Bit-Level Behavior Signed vs. nsigned addition in C: int s, t,, v; s = (int) ((nsigned) + (nsigned) v); t = + v Will give s == t 23 CISC3 Fa8 Characterizing TAdd Fnctionality Tre sm reqires w+1 bits Drop off MSB Treat remaining bits as 2ʼs comp. integer PosOver TAdd(, v) > v < < > NegOver 111 1 1 1 1 2 w 1 2 w 1 2 w 1 1 2 w Tre Sm 24 CISC3 Fa8 PosOver NegOver TAdd Reslt 11 1 1 # + v + 2 w!1 % + v < TMin (NegOver) w TAdd w (,v) = $ + v TMin w " + v " TMax w % & + v! 2 w!1 TMax w < + v (PosOver)
Visalizing 2ʼs Comp. Addition Vales 4-bit twoʼs comp. Range from -8 to +7 Wraps Arond If sm 2 w 1 Becomes negative At most once If sm < 2 w 1 Becomes positive At most once NegOver 8 4 2-2 -4 - -8-8 - -4-2 2 4 TAdd 4 (, v) -8 - -4-2 v 2 4 PosOver 25 CISC3 Fa8 Detecting 2ʼs Comp. Overflow Task Claim Given s = TAdd w (, v) Determine if s = Add w (, v) Example int s,, v; s = + v; Overflow iff either:, v <, s (NegOver), v, s < (PosOver) 2 w 1 2 w 1 ovf = (< == v<) && (<!= s<); PosOver NegOver 2 CISC3 Fa8 Mathematical Properties of TAdd Isomorphic Algebra to UAdd TAdd w (, v) = U2T(UAdd w (T2U( ), T2U(v))) Since both have identical bit patterns Twoʼs s Complement Under TAdd Forms a Grop Closed, Commtative, Associative, is additive identity Every element has additive inverse Let TComp w ( ) = U2T(UComp w (T2U( )) TAdd w (, TComp w ( )) = #! " TMin w TComp w () = $ % TMin w = TMin w 27 CISC3 Fa8
Mltiplication Compting Exact Prodct of w-bit nmbers x, y Either signed or nsigned Ranges Unsigned: 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, bt only for (TMin w ) 2 Maintaining Exact Reslts Wold need to keep expanding word size with each prodct compted Done in software by arbitrary precision arithmetic packages 28 CISC3 Fa8 Unsigned Mltiplication in C Operands: w bits * v Tre Prodct: 2*w bits v Discard w bits: w bits UMlt w (, v) Standard Mltiplication Fnction Ignores high order w bits Implements Modlar Arithmetic UMlt w (, v) = v mod 2 w 29 CISC3 Fa8 Unsigned vs. Signed Mltiplication Unsigned Mltiplication nsigned x = (nsigned) x; nsigned y = (nsigned) y; nsigned p = x * y Trncates prodct to w-bit nmber p = UMlt w (x, y) Modlar arithmetic: p = x y mod 2 w Twoʼs s Complement Mltiplication int x, y; int p = x * y; Compte exact prodct of two w-bit nmbers x, y Trncate reslt to w-bit nmber p = TMlt w (x, y) 3 CISC3 Fa8
Unsigned vs. Signed Mltiplication Unsigned Mltiplication nsigned x = (nsigned) x; nsigned y = (nsigned) y; nsigned p = x * y Twoʼs s Complement Mltiplication int x, y; Relation int p = x * y; Signed mltiplication gives same bit-level reslt as nsigned p == (nsigned) p 31 CISC3 Fa8 Power-of-2 Mltiply with Shift Operation << k gives * 2 k Both signed and nsigned Operands: w bits Tre Prodct: w+k bits Examples 2 k Discard k bits: w bits UMlt w (, 2 k ) << 3 == * 8 << 5 - << 3 == * 24 k 1 Most machines shift and add mch faster than mltiply Compiler generates this code atomatically 32 CISC3 Fa8 * 2 k TMlt w (, 2 k ) Unsigned Power-of-2 Divide with Shift Qotient of Unsigned by Power of 2 >> k gives / 2 k Uses logical shift Operands: Division: / 2 k / 2 k k 1 Binary Point. Reslt: / 2 k Division Compted Hex Binary x 15213 15213 3B D 11111 11111 x >> 1 7.5 7 1D B 1111 11111 x >> 4 95.8125 95 3 B 11 11111 x >> 8 59.4257813 59 3B 11111 33 CISC3 Fa8
Signed Power-of-2 Divide with Shift Qotient of Signed by Power of 2 x >> k gives x / 2 k Uses arithmetic shift Ronds wrong direction when < k x Operands: / 2 k 1 Division: x / 2 k Binary Point. Reslt: RondDown(x / 2 k ) Division Compted Hex Binary y -15213-15213 C4 93 111 1111 y >> 1-7.5-77 E2 49 1111 111 y >> 4-95.8125-951 FC 49 111111 111 y >> 8-59.4257813 - FF C4 11111111 111 34 CISC3 Fa8 Correct Power-of-2 Divide Qotient of Negative Nmber by Power of 2 Want x / 2 k (Rond Toward ) Compte as (x+2 k -1)/ 2 k In C: (x + (1<<k)-1) >> k Biases dividend toward Case 1: No ronding Dividend: Divisor: / 2 k / 2 k k 1 +2 k + 1 1 1 1 1 1 1 1 1 1 1 1 1. Binary Point 1 1 1 Biasing has no effect 35 CISC3 Fa8 Correct Power-of-2 Divide (Cont.) Case 2: Ronding Dividend: x k 1 +2 k + 1 1 1 1 1 Incremented by 1 Binary Point Divisor: / 2 k 1 x / 2 k 1 1 1 1. Biasing adds 1 to final reslt Incremented by 1 3 CISC3 Fa8
Properties of Unsigned Arithmetic Unsigned Mltiplication with Addition Forms Commtative Ring Addition is commtative grop Closed nder mltiplication UMlt w (, v) 2 w 1 Mltiplication Commtative UMlt w (, v) = UMlt w (v, ) Mltiplication is Associative UMlt w (t, UMlt w (, v)) = UMlt w (UMlt w (t, ), v) 1 is mltiplicative identity UMlt w (, 1) = Mltiplication distribtes over addtion UMlt w (t, UAdd w (, v)) = UAdd w (UMlt w (t, ), UMlt w (t, v)) 37 CISC3 Fa8 Properties of Twoʼs Comp. Arithmetic Isomorphic Algebras Unsigned mltiplication and addition Trncating to w bits Twoʼs complement mltiplication and addition Trncating to w bits Both Form Rings Isomorphic to ring of integers mod 2 w Comparison to Integer Arithmetic Both are rings Integers obey ordering properties, e.g., > + v > v >, v > v > These properties are not obeyed by twoʼs comp. arithmetic TMax + 1 == TMin 38 15213 * 342 == -13 (1-bit words) CISC3 Fa8 Work in Grop: C Pzzles Work in grop of two to solve these C pzzles Taken from old exams Assme machine with 32 bit word size, twoʼs complement integers For each of the following C expressions, either: Arge that is tre for all argment vales Give example where not tre x < ((x*2) < ) Initialization int x = foo(); int y = bar(); nsigned x = x; nsigned y = y; x >= x & 7 == 7 (x<<3) < x > -1 x > y -x < -y x * x >= x > && y > x + y > x >= -x <= 39 x <= CISC3 Fa8 -x >=
Schedle Lec3 - Integers (Sep 11) Lec4 - Floating point (Sep 1) o Reading assignment: chap 3.1-3.7 o Qiz chap 2 Lec5 - Machine-Level Programming I - Introdction (Sep 18) Lec - Machine-Level Programming II - Introdction (Sep 25) o Lab 1 de 4 CISC3 Fa8