Comp Integers Spring 2015 Topics Numeric Encodings

Slides:



Advertisements
Similar presentations
When NOT to use Unsigned? Don’t Use Just Because Number Nonzero – C compilers on some machines generate less efficient code unsigned i; for (i = 1; i
Advertisements

Fabián E. Bustamante, Spring 2007 Integers Today Numeric Encodings Programming Implications Basic operations Programming Implications Next time Floats.
Princess Sumaya Univ. Computer Engineering Dept. Chapter 3:
Princess Sumaya Univ. Computer Engineering Dept. Chapter 3: IT Students.
CS213 Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication.
Bits, Bytes, and Integers August 29, 2007 Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C Representations.
“The course that gives CMU its Zip!” Topics Numeric Encodings –Unsigned & Two’s complement Programming Implications –C promotion rules Basic operations.
CS213 Integers Apr 3, 2006 Topics Numeric Encodings
“The course that gives CMU its Zip!” Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations.
February 4, 2003 CSCE 212 Computer Architecture Lecture 3 Representing Integers.
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
1 Arithmetic and Logical Operations - Part II. Unsigned Numbers Addition in unsigned numbers is the same regardless of the base. Given a pair of bit sequences.
Carnegie Mellon 1 This Week: Integers Integers  Representation: unsigned and signed  Conversion, casting  Expanding, truncating  Addition, negation,
Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication Programming.
Int’s and Integers CENG331: Introduction to Computer Systems 3 rd Lecture Instructor: Erol Sahin Acknowledgement: Most of the slides are adapted from the.
Comp Integers Spring 2015 Topics Numeric Encodings
“The course that gives CMU its Zip!” Topics Basic operations –Addition, negation, multiplication Programming Implications –Consequences of overflow.
Carnegie Mellon 1 Bits, Bytes, and Integers : Introduction to Computer Systems 3 rd Lectures, May 27th, 2014 Instructors: Greg Kesden.
Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication Programming.
1 Saint Louis University Arithmetic and Bitwise Operations on Binary Data CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides.
CS 105 “Tour of the Black Holes of Computing” Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations.
Carnegie Mellon Introduction to Computer Systems /18-243, spring rd Lecture, Jan. 20 th Instructors: Gregory Kesden and Markus Püschel.
CS 105 “Tour of the Black Holes of Computing” Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations.
Int’s and Integers CENG331: Introduction to Computer Systems 3 rd Lecture Instructor: Erol Sahin Acknowledgement: Most of the slides are adapted from the.
“The course that gives CMU its Zip!” Topics Basic operations –Addition, negation, multiplication Programming Implications –Consequences of overflow.
Integers Topics Representations of Integers Basic properties and operations Implications for C.
Integer Operations Computer Organization and Assembly Language: Module 5.
Bits, Bytes, and Integers September 1, 2006 Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C Representations.
Topics Numeric Encodings –Unsigned & Two’s complement Programming Implications –C promotion rules Basic operations –Addition, negation, multiplication.
Instructors: Greg Kesden
This Week: Integers Integers Summary
Integers Topics Numeric Encodings (2.2) Programming Implications
CS 367 Integers Topics (Ch. 2.2, 2.3) Numeric Encodings
Topics IEEE Floating Point Standard Rounding Floating Point Operations
CS 105 “Tour of the Black Holes of Computing!”
Bits, Bytes, and Integers CSE 238/2038/2138: Systems Programming
Bits, Bytes, and Integers
University of Washington
Instructor: David Ferry
Integer Representations and Arithmetic
CS 367 Floating Point Topics (Ch 2.4) IEEE Floating Point Standard
“The course that gives CMU its Zip!”
CS213 Integers Topics Numeric Encodings Programming Implications
CS 105 “Tour of the Black Holes of Computing”
CS 105 “Tour of the Black Holes of Computing”
C Puzzles Taken from old exams
CS140 Lecture 08: Data Representation: Bits and Ints
Machine-Level Programming: Control Flow
Representing Information (2)
CS 105 “Tour of the Black Holes of Computing!”
Bits, Bytes, and Integers January 16, 2008
Integers Topics Numeric Encodings Programming Implications
1 The Hardware/Software Interface CSE351 Spring 2011 Module 3: Integers Monday, April 4, 2011.
CS 105 “Tour of the Black Holes of Computing!”
Arithmetic Topics Basic operations Programming Implications
Bits, Bytes, and Integers
Representing Information (2)
Computer Organization COMP 210
Bits, Bytes, and Integers Part 2 3rd Lectures
Integer Representations Jan. 23, 2001
Computer Organization COMP 210
Computer Organization COMP 210
Operations and Arithmetic
CS 105 “Tour of the Black Holes of Computing!”
CS213 Floating Point Topics IEEE Floating Point Standard Rounding
Bits, Bytes, and Integers
Computer Systems Introduction
CS 105 “Tour of the Black Holes of Computing!”
GCSE COMPUTER SCIENCE Topic 3 - Data 3.3 Logical and Arithmetic Shifts.
CS 105 “Tour of the Black Holes of Computing!”
Presentation transcript:

Comp 21000 Integers Spring 2015 Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication Consequences of overflow Using shifts to perform power-of-2 multiply/divide

Multiplication Computing Exact Product of w-bit numbers x, y Ranges Either signed or unsigned Ranges Unsigned: 0 ≤ x * y ≤ (2w – 1) 2 = 22w – 2w+1 + 1 Up to 2w bits Two’s complement min: x * y ≥ (–2w–1)*(2w–1–1) = –22w–2 + 2w–1 Up to 2w–1 bits Two’s complement max: x * y ≤ (–2w–1) 2 = 22w–2 Up to 2w 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

Unsigned Multiplication in C • • • Operands: w bits * v • • • True Product: 2*w bits u · v • • • • • • UMultw(u , v) • • • Discard w bits: w bits Standard Multiplication Function Ignores high order w bits Implements Modular Arithmetic UMultw(u , v) = u · v mod 2w

Unsigned vs. Signed Multiplication Unsigned Multiplication unsigned ux = (unsigned) x; unsigned uy = (unsigned) y; unsigned up = ux * uy Truncates product to w-bit number up = UMultw(ux, uy) Modular arithmetic: up = ux  uy mod 2w Two’s Complement Multiplication int x, y; int p = x * y; Compute exact product of two w-bit numbers x, y Truncate result to w-bit number p = TMultw(x, y)

Unsigned vs. Signed Multiplication Unsigned Multiplication unsigned ux = (unsigned) x; unsigned uy = (unsigned) y; unsigned up = ux * uy Two’s Complement Multiplication int x, y; int p = x * y; Relation Signed multiplication gives same bit-level result as unsigned up == (unsigned) p

Power-of-2 Multiply with Shift Operation u << k gives u * 2k Both signed and unsigned Examples u << 3 == u * 8 u << 5 - u << 3 == u * 24 Most machines shift and add faster than multiply Compiler generates this code automatically (when mult by constant) k u • • • Operands: w bits * 2k ••• 1 ••• True Product: w+k bits u · 2k • • • ••• UMultw(u , 2k) ••• ••• Discard k bits: w bits TMultw(u , 2k)

Compiled Multiplication Code C Function int mul12(int x) { return x*12; } x*12 = x*(3 * 4) = (x*3) * 4 = (x + x*2) * 4 Compiled Arithmetic Operations Explanation leal (%eax,%eax,2), %eax sall $2, %eax t <- x+x*2 return t << 2; C compiler automatically generates shift/add code when multiplying by constant

Unsigned Power-of-2 Divide with Shift Quotient of Unsigned by Power of 2 u >> k gives  u / 2k  Uses logical shift k u Binary Point ••• ••• Operands: / 2k ••• 1 ••• Division: u / 2k . ••• ••• ••• Result:  u / 2k  ••• •••

Compiled Unsigned Division Code C Function unsigned udiv8(unsigned x) { return x/8; } Compiled Arithmetic Operations Explanation shrl $3, %eax # Logical shift return x >> 3; Uses logical shift for unsigned For Java Users Logical shift written as >>>

Signed Power-of-2 Divide with Shift Quotient of Signed by Power of 2 x >> k gives  x / 2k  Uses arithmetic shift Rounds wrong direction when u < 0 1 ••• x 2k / x / 2k Division: Operands: k RoundDown(x / 2k) Result: . Binary Point

Correct Power-of-2 Divide Quotient of Negative Number by Power of 2 Want  x / 2k  (Round Toward 0) Compute as  (x+2k-1)/ 2k  In C: (x + (1<<k)-1) >> k Biases dividend toward 0 Case 1: No rounding Adding 2k-1 is adding k 1’s Last k bits must be zero if there is no rounding! k Dividend: u 1 ••• ••• +2k +–1 ••• 1 ••• 1 1 Binary Point 1 ••• 1 ••• 1 1 Divisor: / 2k ••• 1 •••  u / 2k  . 1 ••• 1 1 1 ••• 1 ••• 1 1 All the added 1’s are shifted out! Biasing has no effect

Correct Power-of-2 Divide (Cont.) Case 2: Rounding k Dividend: x 1 ••• ••• +2k +–1 ••• 1 ••• 1 1 1 ••• ••• Incremented by 1 Binary Point Divisor: / 2k ••• 1 •••  x / 2k  . 1 ••• 1 1 1 ••• ••• Biasing adds 1 to final result Incremented by 1

Correct Power-of-2 Divide (Cont.) Case 2: Rounding Proof If there would be rounding in k shifts, then at least one of the last k bits must be 1. Adding k 1’s will thus cause a 1 to be carried into bit position k+1 This will have the effect of adding 1 to the final answer.

Compiled Signed Division Code C Function int idiv8(int x) { return x/8; } Compiled Arithmetic Operations Explanation testl %eax, %eax js L4 L3: sarl $3, %eax ret L4: addl $7, %eax jmp L3 if x < 0 x += 7; // why? # Arithmetic shift return x >> 3; Uses arithmetic shift for int For Java Users Arith. shift written as >>

Conversions: see slide 17 When to use unsigned: see slide 26 C Puzzle Answers Assume machine with 32 bit word size, two’s comp. integers TMin makes a good counterexample in many cases 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 x < 0  ((x*2) < 0) False: TMin ux >= 0 True: 0 = UMin x & 7 == 7  (x<<30) < 0 True: x1 = 1 ux > -1 False: 0 x > y  -x < -y False: -1, TMin x * x >= 0 False: 30426 x > 0 && y > 0  x + y > 0 False: TMax, TMax x >= 0  -x <= 0 True: –TMax < 0 x <= 0  -x >= 0 False: TMin Conversions: see slide 17 Casting: see slide 20, 21 When to use unsigned: see slide 26