jin-soo kim ([email protected]) systems software &...

48
Integers Jin-Soo Kim ([email protected]) Systems Software & Architecture Lab. Seoul National University Fall 2019

Upload: others

Post on 15-Mar-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

Integers

Jin-Soo Kim([email protected])

Systems Software &Architecture Lab.

Seoul National University

Fall 2019

Page 2: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 2

Page 3: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 3

A computer is a machine.

Page 4: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 4

▪ By John von Neumann, 1945

CPU

Processingunit

Memory

Storageunit

Byte addressable arrayCode + data (user program, OS)Stack to support procedures

Address

Data

Instruction

Data movementArithmetic & logical opsControl transfer

Page 5: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 5

▪ Analog vs. digital?

▪ Compact Disc (CD)

• 44.1 KHz, 16-bit, 2-channel

▪ MP3

• A digital audio encoding with lossy data compression

Page 6: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 6

Source: http://www.soundsetal.com/blog-how-do-vinyl-records-work/http://www.explainthatstuff.com/cdplayers.html

Page 7: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 7

A/D D/A

CPU

Memory

Analog(sensors)

Analog

Digital

...

Page 8: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 8

▪ Information = Bits + Context

• Computers manipulate representations of things

• Things are represented as binary digits

• What can you represent with N bits?

– 2N things

– Numbers, characters, pixels, positions, source code, executable files, machine instructions, …

– Depends on what operations you do on them

01010011 01001110 01010101 01000001 01000010 01000011 01001000 01001001

‘S’ ‘N’ ‘U’ ‘A’ ‘R’ ‘C’ ‘H’ ‘I’(char)

1096109651 1229472594(int)

1.08216479583800794… x 1045(double)

Page 9: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 9

▪ Why not base 10 representation?

• Easy to store with bistable elements

• Straightforward implementation of arithmetic functions

• Reliably transmitted on noisy and inaccurate wires

▪ Electronic implementation

0.0V

0.5V

2.8V

3.3V

0 1 0

Page 10: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 10

▪ Binary: 000000002 to 111111112

▪ Octal: 0008 to 3778• An integer constant that begins with 0 is an octal number in C

▪ Decimal: 010 to 25510• First digit must not be 0 in C

▪ Hexadecimal: 0016 to FF16• Base 16 number representation

• Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’

• Write FA1D37B16 in C as 0xFA1D37B or 0xfa1d37b

0 0 00001 1 00012 2 00103 3 00114 4 01005 5 01016 6 01107 7 01118 8 10009 9 1001A 10 1010B 11 1011C 12 1100D 13 1101E 14 1110F 15 1111

Page 11: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

Representing Integers

Chap. 2.4

Page 12: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 12

▪ Encoding unsigned integers

▪ What is the range for unsigned values with w bits?

▪ Using 64 bits: 0 to +18,446,774,073,709,551,615

𝑩 = [𝒃𝒘−𝟏, 𝒃𝒘−𝟐, . . . , 𝒃𝟎]

𝑫(𝑩) =

𝒊=𝟎

𝒘−𝟏

𝒃𝒊 ⋅ 𝟐𝒊

x = 0001 0000 0101 11102

D(x) = 212 + 26 + 24 + 23 + 22 + 21

= 4096 + 64 + 16 + 8 + 4 + 2= 4190

Page 13: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 13

▪ Encoding positive numbers

• Same as unsigned numbers

▪ Encoding negative numbers

• Sign-magnitude representation

• Ones’ complement representation

• Two’s complement representation

Page 14: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 14

▪ Two zeros

• [000…00], [100..00]

▪ Used for floating-point numbers 0000 0001

0010

0011

0100

0101

0110

011110001001

1010

1011

1100

1101

1110

1111

0 12

3

45

67

-4

0-1-2

-3

-5-6

-7

bw-1 bw-2 … … … … b1 b0

Sign bit

𝑺(𝑩) = (−𝟏)𝒃𝒘−𝟏 ⋅

𝒊=𝟎

𝒘−𝟐

𝒃𝒊 ⋅ 𝟐𝒊

Magnitude

Page 15: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 15

▪ Easy to find –n

▪ Two zeros

• [000..00], [111..11]

▪ No longer used

0000 0001

0010

0011

0100

0101

0110

011110001001

1010

1011

1100

1101

1110

1111

0 12

3

45

67-7-6

-5-4

-3

-2-1

0

bw-1 bw-2 … … … … b1 b0

Sign bit

𝑶(𝑩) = −𝒃𝒘−𝟏(𝟐𝒘−𝟏 − 𝟏) +

𝒊=𝟎

𝒘−𝟐

𝒃𝒊 ⋅ 𝟐𝒊

Page 16: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 16

▪ Unique zero

▪ Easy for hardware

• leading 0 ≥ 0

• leading 1 < 0

▪ Used by almost all modern machines

0000 0001

0010

0011

0100

0101

0110

011110001001

1010

1011

1100

1101

1110

1111

0 12

3

45

67-8-7

-6-5

-4

-3-2

-1

bw-1 bw-2 … … … … b1 b0

Sign bit

𝑶(𝑩) = −𝒃𝒘−𝟏 ⋅ 𝟐𝒘−𝟏 +

𝒊=𝟎

𝒘−𝟐

𝒃𝒊 ⋅ 𝟐𝒊

Asymmetric range:

−𝟐𝒏−𝟏 ~ 𝟐𝒏−𝟏 − 𝟏

Page 17: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 17

▪ Following holds for two’s complement

▪ Complement

• Observation: ~ x + x == 1111…112 == – 1

▪ Increment~ x + x == – 1~ x + x + (– x + 1) == – 1 + (– x + 1)~ x + 1 == – x

~ x + 1 == – x

Page 18: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 18

▪ Signed: w bits → w+k bits

• Given w-bit signed integer x

• Convert it to w+k-bit integer with same value

▪ Replicate the sign bit to the left

• +2: 0000 0010 → 0000 0000 0000 0010

• -2: 1111 1110 → 1111 1111 1111 1110

▪ In RISC-V instruction set

• lb: sign-extend loaded byte

• lbu: zero-extend loaded byte

• • •X

X’ • • • • • •

• • •

w

wk

Sign bit

Page 19: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

Manipulating Integers

Chap. 2.4, 3.1 – 3.4, 3.6

Page 20: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 20

▪ Operations &, |, ~, ^ available in C

• Apply to any “integral” data type– (unsigned) long, int, short, char

• View arguments as bit vectors

• Arguments applied bit-wise

▪ Examples (char data type)

~0x41 → 0xBE ~010000012 → 101111102

~0x00 → 0xFF ~000000002 → 111111112

0x69 & 0x55 → 0x41 011010012 & 010101012 → 010000012

0x69 | 0x55 → 0x7D 011010012 & 010101012 → 011111012

0x69 ^ 0x55 → 0x3C 011010012 & 010101012 → 001111002

Page 21: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 21

▪ &&, ||, !

• View 0 as “False”, anything nonzero as “True”

• Always return 0 or 1

• Early termination

▪ Examples (char data type)

!0x41 → 0x00

!0x00 → 0x01

!!0x41 → 0x01

0x69 && 0x55 → 0x01

0x69 || 0x55 → 0x01

if (p && *p) … // avoids null pointer access

Page 22: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 22

▪ Left shift: x << y

• Shift bit-vector x left y positions

– Throw away extra bits on left

– Fill with 0’s on right

▪ Right shift: x >> y

• Shift bit-vector x right y positions

– Throw away extra bits on right

• Logical shift: fill with 0’s on left

• Arithmetic shift: replicate MSB on right

– Useful with two’s complement integer

representation

▪ Undefined if y < 0 or y word size

01100010Argument x

00010000<< 3

00011000Log. >> 2

00011000Arith. >> 2

10100010Argument x

00010000<< 3

00101000Log. >> 2

11101000Arith. >> 2

0001000000010000

0001100000011000

0001100000011000

00010000

00101000

11101000

00010000

00101000

11101000

Page 23: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 23

▪ Example: 7 + 6

▪ Overflow if result out of range

• Adding +ve and –ve operands: No overflow

• Adding two +ve operands: Overflow if result sign is 1

• Adding two –ve operands: Overflow if result sign is 0

Page 24: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 24

▪ Signed addition in C

• Ignores carry output

• The low-order w bits are identical to unsigned addition

Mode x y x + y Truncated x + y

Unsigned

Two’s comp.

4 [100]

-4 [100]

3 [011]

3 [011]

7 [0111]

-1 [1111]

7 [111]

-1 [111]

Unsigned

Two’s comp.

4 [100]

-4 [100]

7 [111]

-1 [111]

11 [1011]

-5 [1011]

3 [011]

3 [011]

Unsigned

Two’s comp.

3 [011]

3 [011]

3 [011]

3 [011]

6 [0110]

6 [0110]

6 [110]

-2 [110]

Examples for 3-bit integer

Page 25: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 25

▪ Add negation of second operand

▪ Example: 7 – 6 = 7 + (–6)

▪ Overflow if result out of range

• Subtracting two +ve or two –ve operands: No overflow

• Subtracting +ve from –ve operand: Overflow if result sign is 0

• Subtracting –ve from +ve operand: Overflow if result sign is 1

+7: 0000 0000 … 0000 0111-6: 1111 1111 … 1111 1010

+1: 0000 0000 … 0000 0001

Page 26: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 26

▪ Graphics and media processing operates on vectors of 8-bit/16-bit data

• Use 64-bit adder,

with partitioned carry chain

• Operate on 8x8-bit, 4x16-bit,

or 2x32-bit vectors

• SIMD (Single-Instruction,

Multiple-Data)

▪ Saturating operations

• On overflow, result is largest representable value

• e.g., clipping in audio, saturation in video

Source: https://software.intel.com/en-us/blogs/2012/01/31/vectorization-find-out-what-it-is-find-out-more

Page 27: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 27

100010011000

0000 0000 1000 1001000

×

▪ Long-multiplication approach

Length of product is the sum of operand lengths

multiplicand

multiplier

product

Page 28: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 28

64th

Initially 0

Page 29: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 29

▪ Perform steps in parallel: add / shift

▪ One cycle per partial-product addition

• That’s ok, if frequency of multiplication is low

Multiplier

Page 30: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 30

▪ 310 x 510 = 000000112 x 000001012

00000011

0000010100000000

MC

MPP

Page 31: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 31

▪ MP0 = 1: Add MC to P, shift right P and MP by 1 bit

00000011

0000010100000011

MC

MPP

Page 32: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 32

▪ MP0 = 0: Shift right P and MP by 1 bit

00000011

1000001000000001

MC

MPP

Page 33: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 33

▪ MP0 = 1: Add MC to P, shift right P and MP by 1 bit

00000011

1100000100000011

MC

MPP

Page 34: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 34

▪ MP0 = 0 (5 times): Shift right P and MP by 1 bit (5 times)

00000011

1110000000000001

MC

MPP

Page 35: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 35

▪ Final product available in P + MP

00000011

0000111100000000

MC

MPP

Page 36: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 36

▪ Uses multiple adders

• Cost/performance tradeoff

▪ Can be pipelined

• Several multiplication performed in parallel

Page 37: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 37

▪ Signed multiplication in C

• Ignores high order w bits

• The low-order w bits are identical to unsigned multiplication

Mode x y x · y Truncated x · y

Unsigned

Two’s comp.

5 [101]

-3 [101]

3 [011]

3 [011]

15 [001111]

-9 [110111]

7 [111]

-1 [111]

Unsigned

Two’s comp.

4 [100]

-4 [100]

7 [111]

-1 [111]

28 [011100]

4 [000100]

4 [100]

-4 [100]

Unsigned

Two’s comp.

3 [011]

3 [011]

3 [011]

3 [011]

9 [001001]

9 [001001]

1 [001]

1 [001]

Examples for 3-bit integer

Page 38: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 38

▪ Check for 0 divisor

▪ If divisor ≤ dividend bits:

1 bit in quotient, subtract

▪ Otherwise: 0 bit in quotient,

bring down next dividend bit

▪ Restoring division

• Do the subtract, and if remainder

goes < 0, add divisor back

10011000 1001010

-100010101 1010

-100010

n-bit operands yield n-bitquotient and remainder

quotient

dividend

remainder

divisor

Page 39: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 39

Initially dividend

Initially divisor in left half

Page 40: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 40

▪ One cycle per partial-remainder subtraction

▪ Looks a lot like a multiplier!• Same hardware can be used for both

Quotient

Page 41: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 41

▪ 1510 / 410 = 000011112 / 000001002

00000100

00000000 00001111

D

R

Page 42: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 42

▪ R – D < 0: Shift left R and Q by 1 bit, Q0 = 0

00000100

00000000 0001111 0

D

R Q

Page 43: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 43

▪ R – D < 0: Shift left R and Q by 1 bit, Q0 = 0 (6 times)

00000100

00000111 1 0000000

D

R Q

Page 44: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 44

▪ R – D >= 0: R = R – D, shift left R and Q by 1 bit, Q0 = 1

00000100

00000111 0000001

D

R Q

Page 45: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 45

▪ R – D >= 0: R = R – D, shift left Q by 1 bit, Q0 = 1

00000100

00000011 00000011

D

R Q

Page 46: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 46

▪ Signed division

• Divide using absolute values

• Adjust sign of quotient and remainder

as required

– (e.g.) 7 / 2: Q = 3, R = 1

(-7) / 2: Q = -3, R = -1

7 / (-2): Q = -3, R = 1

(-7) / (-2): Q = 3, R = -1

▪ Faster division

• Can’t use parallel hardware as in multiplier – Subtraction is conditional on sign of

remainder

• Faster dividers (e.g. SRT division) generate multiple quotient bits per step, but still

require multiple steps

𝑫𝒊𝒗𝒊𝒅𝒆𝒏𝒅 = 𝑸𝒖𝒐𝒕𝒊𝒆𝒏𝒕 × 𝑫𝒊𝒗𝒊𝒔𝒐𝒓 + 𝑹𝒆𝒎𝒂𝒊𝒏𝒅𝒆𝒓

−𝒙

𝒚==

(−𝒙)

𝒚

Page 47: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 47

▪ Power-of-2 multiplication with left shift

• u << k gives u * 2k

– e.g. u << 3 == u * 8, (u << 5) – (u << 3) == u * 24

• Both signed and unsigned

• Faster than multiply

▪ Power-of-2 division with right shift

• u >> k gives u / 2k

• Only for unsigned integers

– e.g. 1010 / 410 = 210, 000010102 >> 2 = 000000102 = 210

-1010 / 410 = -210, 111101102 >> 2 = 111111012 = -310

• For negative numbers, should be computed as (u + (1 << k) – 1) >> k

– e.g. 111101102 + 000000112 = 111110012, 111110012 >> 2 = 111111102 = -210

0 0 1 0 0 0•••

x

2k/

x / 2k

•••

k1 ••• •••

1 •••1 1••• .

Binary Point

0 0 0 1 1 1•••+2k –1 •••

•••

1 ••• •••

1

Incremented by 1

Incremented by 1

Page 48: Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Integerscsl.snu.ac.kr/courses/4190.308/2019-2/2-int.pdf · 2 to 11111111 2 Octal: 000 8 to 377 8 •An integer constant that

4190.308: Computer Architecture | Fall 2019 | Jin-Soo Kim ([email protected]) 48

▪ Two’s complement for representing negative numbers

▪ Sign extension preserves the value

▪ Same hardware can be used for both signed and unsigned addition. This

is also true for multiplication. But their overflow conditions are different.

▪ Remember?

▪ Using shift right for power-of-2 division requires correction for negative

numbers

int x = 50000;printf (“%s\n”, (x*x >= 0)? “Yes” : “No”);