1 IKI20210 Pengantar Organisasi Komputer Kuliah No. 24: Aritmatika 3 Januari 2003 Bobby Nazief Johny Moningka

Slides:



Advertisements
Similar presentations
Fixed Point Numbers The binary integer arithmetic you are used to is known by the more general term of Fixed Point arithmetic. Fixed Point means that we.
Advertisements

1 IKI10230 Pengantar Organisasi Komputer Bab 6: Aritmatika 7 & 14 Mei 2003 Bobby Nazief Qonita Shahab bahan kuliah:
Princess Sumaya Univ. Computer Engineering Dept. Chapter 3:
Princess Sumaya Univ. Computer Engineering Dept. Chapter 3: IT Students.
Floating Point Numbers
1 CS61C L9 Fl. Pt. © UC Regents CS61C - Machine Structures Lecture 9 - Floating Point, Part I September 27, 2000 David Patterson
Integer Multiplication (1/3)
Chapter 3 Arithmetic for Computers. Multiplication More complicated than addition accomplished via shifting and addition More time and more area Let's.
CS 61C L15 Floating Point I (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C :
1  2004 Morgan Kaufmann Publishers Chapter Three.
Chapter 6 Arithmetic. Addition Carry in Carry out
1 EECS Components and Design Techniques for Digital Systems Lec 19 – Fixed Point & Floating Point Arithmetic 10/23/2007 David Culler Electrical Engineering.
CS 61C L15 Floating Point I (1) Krause, Spring 2005 © UCB This day in history… TA Danny Krause inst.eecs.berkeley.edu/~cs61c-td inst.eecs.berkeley.edu/~cs61c.
Chapter Four Arithmetic and Logic Unit
CS 61C L15 Floating Point I (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
CS 61C L10 Floating Point (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #10: Floating Point
CPSC 321 Computer Architecture ALU Design – Integer Addition, Multiplication & Division Copyright 2002 David H. Albonesi and the University of Rochester.
Computer ArchitectureFall 2008 © August 27, CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)
IT 251 Computer Organization and Architecture Introduction to Floating Point Numbers Chia-Chi Teng.
Computer Organization and Architecture Computer Arithmetic Chapter 9.
Computer Arithmetic Nizamettin AYDIN
Computer Arithmetic. Instruction Formats Layout of bits in an instruction Includes opcode Includes (implicit or explicit) operand(s) Usually more than.
Computer Arithmetic.
Csci 136 Computer Architecture II – Floating-Point Arithmetic
CEN 316 Computer Organization and Design Computer Arithmetic Floating Point Dr. Mansour AL Zuair.
Computer Architecture ALU Design : Division and Floating Point
IT253: Computer Organization
Computing Systems Basic arithmetic for computers.
ECE232: Hardware Organization and Design
Data Representation in Computer Systems
Floating Point (a brief look) We need a way to represent –numbers with fractions, e.g., –very small numbers, e.g., –very large numbers,
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.
Computer Arithmetic II Instructor: Mozafar Bag-Mohammadi Spring 2006 University of Ilam.
Lecture 6: Multiply, Shift, and Divide
Fixed and Floating Point Numbers Lesson 3 Ioan Despi.
Computer Arithmetic II Instructor: Mozafar Bag-Mohammadi Ilam University.
CSC 221 Computer Organization and Assembly Language
Princess Sumaya Univ. Computer Engineering Dept. Chapter 3:
Lecture notes Reading: Section 3.4, 3.5, 3.6 Multiplication
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
FLOATING POINT ARITHMETIC P&H Slides ECE 411 – Spring 2005.
CS 61C L3.2.1 Floating Point 1 (1) K. Meinz, Summer 2004 © UCB CS61C : Machine Structures Lecture Floating Point Kurt Meinz inst.eecs.berkeley.edu/~cs61c.
Computer Arithmetic See Stallings Chapter 9 Sep 10, 2009
CHAPTER 3 Floating Point.
CDA 3101 Fall 2013 Introduction to Computer Organization
Chapter 3 Arithmetic for Computers. Chapter 3 — Arithmetic for Computers — 2 Arithmetic for Computers Operations on integers Addition and subtraction.
Floating Point Numbers Representation, Operations, and Accuracy CS223 Digital Design.
1 IKI20210 Pengantar Organisasi Komputer Kuliah No. 23: Aritmatika 18 Desember 2002 Bobby Nazief Johny Moningka
William Stallings Computer Organization and Architecture 8th Edition
Integer Division.
CS/COE0447 Computer Organization & Assembly Language
April 2006 Saeid Nooshabadi
Topic 3d Representation of Real Numbers
CSCE 350 Computer Architecture
CS 61C: Great Ideas in Computer Architecture Floating Point Arithmetic
ECEG-3202 Computer Architecture and Organization
Floating Point Numbers
Topic 3d Representation of Real Numbers
Number Representation
Presentation transcript:

1 IKI20210 Pengantar Organisasi Komputer Kuliah No. 24: Aritmatika 3 Januari 2003 Bobby Nazief Johny Moningka bahan kuliah: Sumber: 1. Hamacher. Computer Organization, ed Materi kuliah CS61C/2000 & CS152/1997, UCB.

2 Two’s Complement Number line °2 N-1 non-negatives °2 N-1 negatives °one zero °how many positives? °comparison? °overflow?

3 Two’s Complement Formula °Can represent positive and negative numbers in terms of the bit value times a power of 2: d 31 x d 30 x d 2 x d 1 x d 0 x 2 0 °Example two = 1x x x x2 2 +0x2 1 +0x2 0 = = -2,147,483,648 ten + 2,147,483,644 ten = -4 ten °Note: need to specify width: we use 32 bits

4 Two’s complement shortcut: Negation °Invert every 0 to 1 and every 1 to 0, then add 1 to the result Sum of number and its one’s complement must be two two = -1 ten Let x’ mean the inverted representation of x Then x + x’ = -1  x + x’ + 1 = 0  x’ + 1 = -x °Example: -4 to +4 to -4 x : two x’: two +1: two ()’: two +1: two

5 Addition & Subtraction Operations °Addition: Just add the two numbers Ignore the Carry-out from MSB Result will be correct, provided there’s no overflow (+5) (+2) (+7) (+5) (-6) (-1) (-5) (-2) (-7) (+7) (-3) (+4) (+2)  (+4) (-4) (-2) (-2)  (-5) (+5) (+3) °Subtraction: Form 2’s complement of the subtrahend Add the two numbers as in Addition

6 Overflow Detection °Overflow: the result is too large (or too small) to represent properly Example: - 8 < = 4-bit binary number <= 7 °When adding operands with different signs, overflow cannot occur! °Overflow occurs when adding: 2 positive numbers and the sum is negative 2 negative numbers and the sum is positive °On your own: Prove you can detect overflow by: Carry into MSB ° Carry out of MSB – 6 –4 – 5 7 0

7 Arithmetic & Branching Conditions

8 Condition Codes °CC Flags will be set/cleared by arithmetic operations: N (negative): 1 if result is negative (MSB = 1), otherwise 0 C (carry): 1 if carry-out(borrow) is generated, otherwise 0 V (overflow): 1 if overflow occurs, otherwise 0 Z (zero): 1 if result is zero, otherwise (+5) (-6) (-1) (+7) (-3) (+4) (+5) (+4) (-7?) (+3) (-3) (0)

9 Multiplication of Positive Numbers

10 Unsigned Multiplication °Paper and pencil example (unsigned): Multiplicand 1101(13) Multiplier 1011(11) Product (143) °m bits x n bits = m+n bit product °Binary makes it easy: 0 => place 0 ( 0 x multiplicand) 1 => place a copy ( 1 x multiplicand)

11 Unsigned Combinational Multiplier °Stage i accumulates A * 2 i if B i == 1 B0B0 A0A0 A1A1 A2A2 A3A3 A0A0 A1A1 A2A2 A3A3 A0A0 A1A1 A2A2 A3A3 A0A0 A1A1 A2A2 A3A3 B1B1 B2B2 B3B3 P0P0 P1P1 P2P2 P3P3 P4P4 P5P5 P6P6 P7P7 0000

12 How does it work? °at each stage shift A left ( x 2) °use next bit of B to determine whether to add in shifted multiplicand °accumulate 2n bit partial product at each stage B0B0 A0A0 A1A1 A2A2 A3A3 A0A0 A1A1 A2A2 A3A3 A0A0 A1A1 A2A2 A3A3 A0A0 A1A1 A2A2 A3A3 B1B1 B2B2 B3B3 P0P0 P1P1 P2P2 P3P3 P4P4 P5P5 P6P6 P7P

13 Multiplier Circuit Product (Multiplier) Multiplicand 32-bit FA Add/NoAdd Control 32 bits 64 bits Shift Right MUX 0 32 bits C Multiplicand 1101 C ProductMultiplier Add Shift Add Shift NoAdd Shift Add Shift

14 Signed-Operand & Multiplication

15 Signed Multiplication °Negative Multiplicand: Multiplicand 10011(-13) Multiplier 01011(+11) Product (-143) °Negative Multiplier: Form 2’s complement of both multiplier and multiplicand Proceed as above

16 Motivation for Booth’s Algorithm °Works well for both Negative & Positive Multipliers °Example 2 x 6 = 0010 x 0110: 0010 x shift (0 in multiplier) add (1 in multiplier) add (1 in multiplier) shift (0 in multiplier) °FA with add or subtract gets same result in more than one way: 6= – = – = °For example ° 0010 x shift (0 in multiplier) sub (first 1 in multpl.) shift (mid string of 1s) add (prior step had last 1)

17 Booth’s Algorithm Current BitBit to the RightExplanationExampleOp 1 0Begins run of 1s sub 1 1Middle of run of 1s none 0 1End of run of 1s add 0 0Middle of run of 0s none Originally for Speed (when shift was faster than add) Small number of additions needed when multiplier has a few large blocks of 1s

18 Booths Example (2 x 7) 1a. P = P - m shift P (sign ext) 1b > nop, shift > nop, shift > add 4a shift 4b done OperationMultiplicandProductnext? 0. initial value > sub

19 Booths Example (2 x -3) 1a. P = P - m shift P (sign ext) 1b > add a shift P 2b > sub a shift 3b > nop 4a shift 4b done OperationMultiplicandProductnext? 0. initial value > sub

20 Fast Multiplication (read yourself!)

21 Integer Division

22 Divide: Paper & Pencil 1001 Quotient Divisor Dividend – – Remainder (or Modulo result) °See how big a number can be subtracted, creating quotient bit on each step Binary => 1 * divisor or 0 * divisor °Dividend = Quotient x Divisor + Remainder => | Dividend | = | Quotient | + | Divisor |

23 Division Circuit Remainder (Quotient) Divisor 33-bit FA Control 33 bits 65 bits Shift Left 33 bits Q Setting Sign-bit Checking

24 RemainderQuotient Initially Shift _ Sub(-11)11101 Set q Restore Shift _ Sub(-11) Set q Restore Shift _ Sub(-11) Set q Shift _ Sub(-11) _ Set q Restore Restoring Division Algorithm 0010 Quotient Divisor Dividend 11 10Remainder

25 Tugas Lab No. 4: I/O & Interupsi °Tugas Kelompok Ukuran kelompok  ditentukan pada saat tutorial (bergantung jumlah STK200 yang masih berfungsi) °Tutorial oleh Asisten Lab 8 – 9 Januari 2003 °Batas Waktu Pengumpulan Tugas (termasuk Demo) 22 – 23 Januari 2003

26 Floating-point Numbers & Operations

27 Review of Numbers °Computers are made to deal with numbers °What can we represent in N bits? Unsigned integers: 0to2 N - 1 Signed Integers (Two’s Complement) -2 (N-1) to 2 (N-1) - 1

28 Other Numbers °What about other numbers? Very large numbers? (seconds/century) 3,155,760, ( x 10 9 ) Very small numbers? (atomic diameter) ( x ) Rationals (repeating pattern) 2/3 ( ) Irrationals 2 1/2 ( ) Transcendentals e ( ),  ( ) °All represented in scientific notation

29 Scientific Notation Review 6.02 x radix (base) decimal pointmantissa exponent °Normalized form: no leadings 0s (exactly one digit to left of decimal point) °Alternatives to representing 1/1,000,000,000 Normalized: 1.0 x Not normalized: 0.1 x 10 -8,10.0 x

30 Scientific Notation for Binary Numbers 1.0 two x 2 -1 radix (base) “binary point”Mantissa exponent °Computer arithmetic that supports it called floating point, because it represents numbers where binary point is not fixed, as it is for integers Declare such variable in C as float

31 Floating Point Representation (1/2) °Normal format: +1.xxxxxxxxxx two *2 yyyy two °Multiple of Word Size (32 bits) 0 31 SExponent Significand 1 bit8 bits23 bits °S represents Sign Exponent represents y’s Significand represents x’s °Represent numbers as small as 2.0 x to as large as 2.0 x 10 38

32 Floating Point Representation (2/2) °What if result too large? (> 2.0x10 38 ) Overflow! Overflow => Exponent larger than represented in 8-bit Exponent field °What if result too small? (>0, < 2.0x ) Underflow! Underflow => Negative exponent larger than represented in 8-bit Exponent field °How to reduce chances of overflow or underflow?

33 Double Precision Fl. Pt. Representation °Next Multiple of Word Size (64 bits) °Double Precision (vs. Single Precision) C variable declared as double Represent numbers almost as small as 2.0 x to almost as large as 2.0 x But primary advantage is greater accuracy due to larger significand 0 31 SExponent Significand 1 bit11 bits20 bits Significand (cont’d) 32 bits

34 IEEE 754 Floating Point Standard (1/4) °Single Precision, DP similar °Sign bit:1 means negative 0 means positive °Significand: To pack more bits, leading 1 implicit for normalized numbers bits single, bits double always true: 0 < Significand < 1(for normalized numbers) °Note: 0 has no leading 1, so reserve exponent value 0 just for number 0

35 IEEE 754 Floating Point Standard (2/4) °Kahan wanted FP numbers to be used even if no FP hardware; e.g., sort records with FP numbers using integer compares °Could break FP number into 3 parts: compare signs, then compare exponents, then compare significands °Wanted it to be faster, single compare if possible, especially if positive numbers °Then want order: Highest order bit is sign ( negative < positive) Exponent next, so big exponent => bigger # Significand last: exponents same => bigger #

36 IEEE 754 Floating Point Standard (3/4) °Negative Exponent? 2’s comp? 1.0 x 2 -1 v. 1.0 x2 +1 (1/2 v. 2) / This notation using integer compare of 1/2 v. 2 makes 1/2 > 2! °Instead, pick notation is most negative, and is most positive 1.0 x 2 -1 v. 1.0 x2 +1 (1/2 v. 2) 1/

37 IEEE 754 Floating Point Standard (4/4) °Called Biased Notation, where bias is number subtract to get real number IEEE 754 uses bias of 127 for single prec. Subtract 127 from Exponent field to get actual value for exponent 1023 is bias for double precision °Summary (single precision): 0 31 SExponent Significand 1 bit8 bits23 bits °(-1) S x (1 + Significand) x 2 (Exponent-127) Double precision identical, except with exponent bias of 1023

38 Special Numbers °What have we defined so far? (Single Precision) ExponentSignificandObject 000 0nonzero??? 1-254anything+/- fl. pt. # 2550+/- infinity 255nonzeroNaN

39 Infinity and NaNs result of operation overflows, i.e., is larger than the largest number that can be represented overflow is not the same as divide by zero (raises a different exception) +/- infinity S It may make sense to do further computations with infinity e.g., X/0 > Y may be a valid comparison Not a number, but not infinity (e.q. sqrt(-4)) invalid operation exception (unless operation is = or =) NaN S non-zero NaNs propagate: f(NaN) = NaN HW decides what goes here

40 FP Addition °Much more difficult than with integers °Can’t just add significands °How do we do it? De-normalize to match exponents Add significands to get resulting one Keep the same exponent Normalize (possibly changing exponent) °Note: If signs differ, just perform a subtract instead.

41 FP Subtraction °Similar to addition °How do we do it? De-normalize to match exponents Subtract significands Keep the same exponent Normalize (possibly changing exponent)

42 Extra Bits for rounding "Floating Point numbers are like piles of sand; every time you move one you lose a little sand, but you pick up a little dirt." How many extra bits? IEEE: As if computed the result exactly and rounded. Addition: 1.xxxxx1.xxxxx1.xxxxx +1.xxxxx0.001xxxxx0.01xxxxx 1x.xxxxy 1.xxxxxyyy 1x.xxxxyyy post-normalization pre-normalization pre and post °Guard Digits: digits to the right of the first p digits of significand to guard against loss of digits – can later be shifted left into first P places during normalization. °Addition: carry-out shifted in °Subtraction: borrow digit and guard °Multiplication: carry and guard, Division requires guard

43 Rounding Digits normalized result, but some non-zero digits to the right of the significand --> the number should be rounded E.g., B = 10, p = 3: = * 10 = * 10 = * 10 2-bias - one round digit must be carried to the right of the guard digit so that after a normalizing left shift, the result can be rounded, according to the value of the round digit IEEE Standard: four rounding modes: round to nearest (default) round towards plus infinity round towards minus infinity round towards 0 round to nearest: round digit < B/2 then truncate > B/2 then round up (add 1 to ULP: unit in last place) = B/2 then round to nearest even digit it can be shown that this strategy minimizes the mean error introduced by rounding

44 Sticky Bit Additional bit to the right of the round digit to better fine tune rounding d0. d1 d2 d3... dp X... X X X S X X S + Sticky bit: set to 1 if any 1 bits fall off the end of the round digit d0. d1 d2 d3... dp X... X X X 0 X X 0 - d0. d1 d2 d3... dp X... X X X 1 - generates a borrow Rounding Summary: Radix 2 minimizes wobble in precision Normal operations in +,-,*,/ require one carry/borrow bit + one guard digit One round digit needed for correct rounding Sticky bit needed when round digit is B/2 for max accuracy Rounding to nearest has mean error = 0 if uniform distribution of digits are assumed

45 Denormalized Numbers bias 1-bias 2-bias B = 2, p = 4 normal numbers with hidden bit --> denorm gap The gap between 0 and the next representable number is much larger than the gaps between nearby representable numbers. IEEE standard uses denormalized numbers to fill in the gap, making the distances between numbers near 0 more alike bias 1-bias 2-bias p bits of precision p-1 bits of precision same spacing, half as many values! NOTE: PDP-11, VAX cannot represent subnormal numbers. These machines underflow to zero instead.