CS429: Computer Organization and Architecture - Integers, Schemes and Mind Maps of Computer Architecture and Organization

CS429: Computer Organization and Architecture. Integers. Dr. Bill Young. Department of Computer Science. University of Texas at Austin.

Typology: Schemes and Mind Maps

2022/2023

Uploaded on 05/11/2023

sheela_98
sheela_98 🇺🇸

4.2

(12)

234 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS429: Computer Organization and Architecture
Integers
Dr. Bill Young
Department of Computer Science
University of Texas at Austin
Last updated: June 10, 2019 at 14:22
CS429 Slideset 3: 1 Integers
Topics of this Slideset
Numeric Encodings: Unsigned and 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
CS429 Slideset 3: 2 Integers
C Puzzles
i n t x = f o o ( ) ;
i n t y = b a r () ;
u n s i g n e d ux = x ;
u n s i g n e d uy = y ;
Assume a machine with 32-bit, two’s complement integers.
For each of the following, either:
Argue that is true for all argument values;
Give an example where it’s not true.
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
CS429 Slideset 3: 3 Integers
Encoding Integers: Unsigned
For unsigned integers, we treat all values as non-negative and use
positional notation as with non-negative decimal numbers.
Assume we have a wlength bit string X.
Unsigned: B2Uw(X) = Pw1
i=0 Xi×2i
CS429 Slideset 3: 4 Integers
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CS429: Computer Organization and Architecture - Integers and more Schemes and Mind Maps Computer Architecture and Organization in PDF only on Docsity!

CS429: Computer Organization and Architecture

Integers

Dr. Bill Young

Department of Computer Science

University of Texas at Austin

Last updated: June 10, 2019 at 14:

CS429 Slideset 3: 1

Integers

Topics of this Slideset

Numeric Encodings: Unsigned and 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

CS429 Slideset 3: 2

Integers

C Puzzles

i n t

x =

f o o ( ) ;

i n t

y =

b a r ( ) ;

u n s i g n e d

ux

= x ;

u n s i g n e d

uy

= y ;

Assume a machine with 32-bit, two’s complement integers. For each of the following, either:

Argue that is true for all argument values; Give an example where it’s not true. x < 0

((x*2) < 0

ux >= 0(x & 7) == 7

(x<<30) < 0

ux > -1x > y

-x < -y

x * x >= 0x > 0 && y > 0

x + y > 0

x >= 0

-x <= 0

x <= 0

-x >= 0

CS429 Slideset 3: 3

Integers

Encoding Integers: Unsigned

For unsigned integers, we treat all values as non-negative and use positional notation

as with non-negative decimal numbers.

Assume we have a

w

length bit string X.

Unsigned:

B2U

w

X

w

1

i =

X

i^

×

i

CS429 Slideset 3: 4

Integers

Unsigned Integers: 4-bit System

CS429 Slideset 3: 5

Integers

Encoding Integers: Two’s Complement

Two’s complement is a way of encoding integers, including somepositive and negative values. It’s exactly like unsigned except

the

high order bit is given negative weight

Two’s complement:

B2T

w

X

X

w

1

×

w

1

w

2

i =

X

i^

×

i

Decimal

Hex

Binary

3B 6D

C4 93

Sign Bit: For 2’s complement, the most significant bit indicates the sign.

0 for nonnegative 1 for negative

CS429 Slideset 3: 6

Integers

Encoding Example

x =

1 5 2 1 3 :

00111011

01101101

y =

15213:

11000100

10010011

Weight

15213

-

1

1

1

1

1

2

0

0

1

2

4

1

4

0

0

8

1

8

0

0

16

0

0

1

16

32

1

32

0

0

64

1

64

0

0

128

0

0

1

128

256

1

256

0

0

512

1

512

0

0

1024

0

0

1

1024

2048

1

2048

0

0

4096

1

4096

0

0

8192

1

8192

0

0

16384

0

0

1

16384

0

0

1

Sum

15213

-

CS429 Slideset 3: 7

Integers

Signed Integers: 4-bit System

CS429 Slideset 3: 8

Integers

Signed vs Unsigned in C

Constants

By default, constants are considered to be signed integers. They are unsigned if they have “U” as a suffix:

0U

4294967259U

Casting

Explicit casting between signed and unsigned is the same asU2T and T2U:^ i n t

tx ,

t y ;

u n s i g n e d

ux ,

uy ;

t x

=

( i n t

)

ux ;

uy

=

( u n s i g n e d

)

t y ;

Implicit casting also occurs via assignments and procedurecalls.^ t x

=

ux ;

uy

=

t y ;

CS429 Slideset 3: 13

Integers

Casting Surprises

Expression Evaluation

If you mix unsigned and signed in a single expression, signedvalues implicitly cast to unsigned. This includes when you compare using

Const 1

Const 2

Rel.

Evaluation

0U

unsigned

signed

0U

unsigned

signed

2147483647U

unsigned

signed

(unsigned) 1

unsigned

2147483648U

unsigned

(int) 2147483648U

signed

CS429 Slideset 3: 14

Integers

Sign Extension

Task:

Given a w-bit signed integer x, convert it to a w+k-bit

integer with the

same value

Rule:

Make k copies of the sign bit :

x

x

w

1

x

w

1

,^

x

w

2

w

0

Why does this work?

CS429 Slideset 3: 15

Integers

Sign Extension Example

s h o r t

i n t

x =

1 5 2 1 3 ;

i n t

i x

=

( i n t

)

x ;

s h o r t

i n t

y =

15213;

i n t

i y

=

( i n t

)

y ;

Decimal

Hex

Binary

x

15213

3B 6D

00111011 01101101

ix

15213

00 00 3B 6D

00000000 00000000 00111011 01101101

y

C4 93

11000100 10010011

iy

FF FF C4 93

11111111 11111111 11000100 10010011

In converting from smaller to larger signed integer data types, Cautomatically performs sign extension.

CS429 Slideset 3: 16

Integers

Why Use Unsigned?

Don’t use just to ensure numbers are nonzero.

Some C compilers generate less efficient code for unsigned.^ u n s i g n e d

i ;

f o r

( i =1;

i^

<

c n t ;

i ++)

a [ i ]

+= a [ i

1]

It’s easy to make mistakes.^ f o r

( i

=

cnt

2;

i^

=

0 ;

i^

−−

)

a [ i ]

+= a [ i +1]

Do use when performing modular arithmetic.

multiprecision arithmetic other esoteric stuff

Do use when you need extra bits of range.

CS429 Slideset 3: 17

Integers

Negating Two’s Complement

To find the negative of a number in two’s complement form:complement the bit pattern and add 1:

x

x

Example:

= 0x9C =

10

complement:

= 0x62 = 98

10

add 1:

= 0x63 = 99

10

Try it with:

and

CS429 Slideset 3: 18

Integers

Complement and Increment Examples

Decimal

Hex

Binary

x

3B 6D

˜x

C4 92

˜x+

C4 93

FF FF

CS429 Slideset 3: 19

Integers

Unsigned Addition

Given two w-bit unsigned quantities u, v, the true sum may be aw+1-bit quantity. Discard the carry bit

and treat the result as an unsigned integer.

Thus, unsigned addition implements

modular addition

UAdd

w

u

,^

v

u

v

) mod 2

w

UAdd

w

u

,^

v

{

u

v

u

v

^2

w

u

v

w

u

v

w

CS429 Slideset 3: 20

Integers

Detecting 2’s Complement Overflow

Task: Determine if

s

= TAdd

w

u

v

u

v

Claim:

We have overflow iff either:

u

,^

v

0 but

s

0 (NegOver)

u

,^

v

0 but

s

0 (PosOver)

Can compute this as:

ovf = (u<0 == v<0) && (u<0 != s<0);

On the machine, this causes the

overflow flag

to be set.

Why don’t we have to worry about the case where one input ispositive and one negative?

CS429 Slideset 3: 25

Integers

Properties of TAdd

TAdd is Isomorphic to UAdd. This is clear since they have identical bit patterns.

Tadd

w

u

,^

v

) = U2T(UAdd

w

(T2U(

u

,^

T2U(

v

Two’s Complement under TAdd forms a group.

Closed, commutative, associative, 0 is additive identity. Every element has an additive inverse:Let TComp

w

u

) = U2T(UComp

w

(T2U(

u

)), then

TAdd

w

u

,^

UComp

w

u

TComp

w

u

{

u

u

= TMin

w

TMin

w

u

= TMin

w

CS429 Slideset 3: 26

Integers

Multiplication

Computing the exact product of two w-bit numbers x, y.

This

is the same for both signed and unsigned. Ranges:

Unsigned: 0

x

y

w

2

2

w

w

  • 1, requires

up to 2

w

bits.

Two’s comp. min: x

y

w

1

w

1

2

w

2

w

1

, requires up

to 2

w

1 bits.

Two’s comp. max:

x

y

w

1

2

2

w

2

, requires up to

w

(but only for TMin

2 w

Maintaining the exact result

Would need to keep expanding the word size with eachproduct computed. Can be done in software with “arbitrary precision” arithmeticpackages.

CS429 Slideset 3: 27

Integers

Unsigned Multiplication in C

Given two w-bit unsigned quantities u, v, the true sum may be a2w-bit quantity. We just discard the most significant w bits,

treat the result as

an unsigned number.Thus, unsigned multiplication implements

modular

multiplication

UMult

w

u

,^

v

u

×

v

) mod 2

w

CS429 Slideset 3: 28

Integers

Unsigned vs. Signed Multiplication

Unsigned Multiplication^ u n s i g n e d

ux

=

( u n s i g n e d

)

x ;

u n s i g n e d

uy

=

( u n s i g n e d

)

y ;

u n s i g n e d

up =

ux

uy ;

Truncates product to w-bit number:

up

= UMult

w

ux

uy

Modular arithmetic:

up

ux

·^

uy

) mod 2

w

Two’s Complement Multiplication^ i n t

x ,

y ;

i n t

p = x

y ;

Compute exact product of two w-bit numbers x, y. Truncate result to w-bit number:

p

= TMult

w

x

y

CS429 Slideset 3: 29

Integers

Unsigned vs. Signed Multiplication

Unsigned Multiplication^ u n s i g n e d

ux

=

( u n s i g n e d

)

x ;

u n s i g n e d

uy

=

( u n s i g n e d

)

y ;

u n s i g n e d

up =

ux

uy ;

Two’s Complement Multiplication^ i n t

x ,

y ;

i n t

p = x

y ;

Relation

Signed multiplication gives same bit-level result as unsigned. up == (unsigned) p

CS429 Slideset 3: 30

Integers

Multiply with Shift

A left shift by

k

, is equivalent to multiplying by 2

k

. This is true for

both signed and unsigned values.

u << 1

u

×

u << 2

u

×

u << 3

u

×

u << 4

u

×

u << 5

u

×

u << 6

u

×

Compilers often use shifting for multiplication, since shift and addis much faster than multiply (on most machines).

u << 5 - u << 3

u * 24

CS429 Slideset 3: 31

Integers

Aside: Floor and Ceiling Functions

Two useful functions on real numbers are the

floor

and

ceiling

functions. Definition:

The floor function

r

, is the greatest integer less than

or equal to

r

.^14

.^14

Definition:

The ceiling function

r

, is the smallest integer greater

than or equal to

r

.^14

.^14

CS429 Slideset 3: 32

Integers

Properties of Two’s Complement Arithmetic

Isomorphic Algebras

Unsigned multiplication and addition: truncate to w bits Two’s complement multiplication and addition: truncate to wbits

Both form rings isomorphic to ring of integers mod

w

Comparison to Integer Arithmetic

Both are rings Integers obey ordering properties, e.g.

u

u

v

v

u

^0

v

u

v

These properties are not obeyed by two’s complementarithmetic.

TMax + 1

TMin

(for 16-bit words)

CS429 Slideset 3: 37

Integers

C Puzzle Answers

Assume a machine with 32-bit word size, two’s complementintegers.^ i n t

x =

f o o ( ) ;

i n t

y =

b a r ( ) ;

u n s i g n e d

ux

= x ;

u n s i g n e d

uy

= y ;

x < 0

((x*2) < 0

False:

TMin

ux >= 0

True:

0 = UMin

(x & 7) == 7

(x<<30) < 0

True:

x 1

= 1

ux > -

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

CS429 Slideset 3: 38

Integers