본문 바로가기

카테고리 없음

[컴퓨터구조] 3. Arithmetic for Computers

반응형

[요약]

1. Integer Addition and Subtraction

2. Integer Multiplication

- 기존 : Prod = Prod + Mcand --> SL Multiplicand --> SR Multiplier

- Improved : (Multiplier를 product 하위 32비트에 초기화) --> Prod(상위 32비트)에 multiplicand 더하기 --> SR Product

3. Integer Division

- (Dividend를 Remainder 하위 32비트에 초기화) --> SL Remainder --> Remainder(상위 32비트)에서 Divisor 빼기 --> Remainder의 MSB가 1: Remainder LSB에 +1 한 후, Remainder(상위 32비트)에 Divisor 더하기 / 0: Remainder LSB에 1 

4. Floating Point

- x = (-1)^s * (1+Fraction) * 2^(Exponent-Bias)

- 1 + Fraction : significand / Exponent-Bias : actual exponent

5. Floating Point Arithmetic

 

 

 

 

 

1. Integer Addition and Subtraction

Integer Addition

+ve와 -ve:  오버플로우 없음 / two +ve: result sign이 1이면 오버플로우 / two -ve: result sign이 0이면 오버플로우

 

Integer Subtractioon

second operand의 부정을 더함.

two +ve or two -ve : 오버플로우 없음 / (-ve) - (+ve) = -ve : result sign 0일 때(+ve) 오버플로우 / (+ve) - (-ve) = +ve : result sign 1일 때(-ve) 오버플로우

Dealing with Overflow

C 등은 오버플로우 무시. 그외 fortran 등은 exception.

 

 

2. Integer Multiplication

Multiplication HW (개선전 곱셈)

Multiplicand: shift left / Product: doesn't move

2*3이면 2가 multiplicant, 3이 multiplier

1) Test Multiplier0 : multiplier의 최하위비트가 1이면 multiplicand를 곱레지스터에 더한다. 0이면ㄴ 더하지 않고 대신 0을 더한다.

2) Shift the Multiplicand register left 1 bit : Multiplicand를 왼쪽으로 이동

3) Shift the Multiplier register right 1 bit : 다음번 반복에서 검사할 Multiplier의 다음 비트를 준비하기 위해 오른쪽으로 이동

4) 32번째 반복이면 끝. 아니면 다시 1)로 간다.

 

Improved Multiplication HW

Multiplicand: doesn't move / Product: shift right / Multiplier is saved at product reg. initially(Multiplier는 product에 초기화됨)

1) Test Product0(Multiplier0) : Product의 최하위비트(LSB)(multiplier의 최하위비트)가 1이면 multiplicand를 Product의 상위 32비트에 더한다. 0이면 더하지 않음.

2) Shift the Product register right 1 bit : 다음번 반복에서 검사할 Multiplier의 다음 비트를 준비하기 위해 오른쪽으로 이동

3) 32번째 반복이면 끝. 아니면 다시 1)로 간다.

 

차이점 : 원래는 Md 64 bits, 64-bit ALU, Product 64 bits, Mr 32bits / Improved는 Md 32 bits, 32-bit ALU, Product 64 bits (Multiplier는 product에 초기화 되어 따로 공간 필요 없다.)

 

Signed Multiplication

Negate negative number --> Perform unsigned multiplication --> If multiplier and multiplicand have different sign, negate the result

부정처리해주고 그냥 multiplication해주고, multiplier랑 multiplicand랑 부호 다르면 negate해준다.

 

 

MIPS Multiplication

product 위한 두개의 32-bit register있다. HI는 most-significant 32 bits / LO는 least-significant 32 bits. 

mult rs, rt / multu rs, rt : 64-bit product in HI/LO

mfhi rd / mflo rd : Move from HI/LO to rd. HI 값을 product가 32비트를 오버플로우 하는지 테스트하는 데에 사용 가능.

mul rd, rs, rt : Least-significant 32 bits of product --> rd

 

 

 

 

 

3. Integer Division

Integer Division

 

Improved Division HW

add or sub만을 위한 ALU 필요. 32-bit Divisor reg, 32-bit ALU, 64-bit Remainder reg(32-bit Quotient reg)

Dividend is saved at Remainder reg. initially(Dividend는 remainder에 초기화됨)

Low 32bit of remainder is for quotient. Hi 32 bit of remainder is for remainder.

1) Shift the Remainder reg. left 1bit

2) Subtract the Divisor from the upper bits of Remainder reg

3) Test Remainder's MSB : Remainder의 MSB가 1이면 Remainder reg의 LSB에 0 넣어서 원래의 Remainder reg를 저장하고, Divisor를 Remainder의 상위 절반에 더해줌. 0이면 Remainder reg.의 LSB에 1 넣어줌.

4) 32번째 반복이면 끝. 아니면 다시 1)로 간다.

 

Division of Signed Number

Q(몫)은 Divisor(2), Dividend(7) 의 부호가 다르면 마이너스. R(나머지)는 Divident(7)의 부호에 따라 결정.

 

MIPS Division

HI/LO register를 결과 위해 사용. HI : 32-bit remainder / LO : 32-bit quotient

div rs, rt / divu rs, rt

mfhi, mflo : to access result. Move from hi / Move from lo

 

 

 

4. Floating Point

Floating Point

coefficient * (base number)^exponent

coefficient는 single digit이어야 함. 1 <= coefficient < 10. 2340 --> 2.34 * 10^3. binary일 때는 +-1.xxxxxx * 2^yyyy. 이 형태일 때 normalized. 아니면 not normalized.

 

Normalized Scientific Notation

(-1)^sign * significand * 2^exponent

sign이 1이면 negative number

significand 많을 수록 accuracy 올라감.

exponent 많을 수록 range 증가.

 

Floating Point Standard

IEEE Std 754-1985

Single precision : 32-bit (C에서 float) / Double precision : 64-bit(C에서 double)

 

IEEE Floating-Point Format

S : Sign bit. 0이면 non-negative. 1이면 negative.

Normalize significand : 1.0 <= |significand| < 2.0. 항상 leading pre-binary-point는 1bit이다. 따라서 꼭 표시할 필요 없음. 따라서 Significand는 Fractiond에 1 붙인 것(더한 것).

Fraction : coefficient(1) 빼고, 소수점 아래만.

Exponent : actual exponent  + bias. Single에서 Bias = 127. Double에서는 1203. 

                   싱글에서 모두 0이면 0-127 = -127로 smallest exponent. 모두 1이면 255-127 = 128로 largest.

 

 

 

Single-Precision Range

smallest value

- exponent는 00000001. actual exponent는 1-127 = -126.

- fraction은 000...00. --> significand = 1.0

- +- 1.0 * 2^-126 ~~ +-1.2 * 10^-38

largest value

- exponent는 11111110 --> actual exponent는 254-127 = 127

- fraction은 111...11 --> significand = 2.0 (1.999999.....)

= += 2.0 * 2^+127 ~~ +-3.4 * 10^+38

 

Double-Precision Range

추후 추가예정

 

Floating Point Precision

추후 추가예정

 

Denormalized Numbers

 

5. Floating Point Arithmetic

1) Align decimal points : 더 작은 exponent를 가진 애를 옮겨서 10^n의 n을 맞춰준다.

2) Add significands : 더해준다. exponent는 바뀌지 않는다.

3) Normalize result & check for over/underflow : Normalize해주고, flow 발생 체크해준다.

4) Round and renormalize if necessary : 처음에 4-digit이었다면 4-digit으로 반올림해준다. 반올림해서 다시 노멀라이즈 필요하면 해준다.

 

728x90
반응형