본문 바로가기

SW

컴퓨터 구조 4

#Arithmetic for Computers - ALU

-operands on Integers 

컴퓨터 하드웨어의 한계로 overflow가 나오면 무시된다. (ex> 두 64bit의 수를 더했을 때, 결과는 64 bit 고정) 

 

Integer의 ADD와 SUB 계산시, overflow가 나올 수 있는 condition

  A B Result
A + B + + -
A + B - - +
A - B
(negation을 이용하여 계산)
+ - -
A - B
(negation을 이용하여 계산)
- + +

ADD의 두 integer가 다른 부호를 지니면거나, SUB의 두 integer가 같은 부호를 지니면 overflow는 발생하지 않는다. 

-> overflow는 carry-in의 MSB와 carry-out의 MSB가 다를 때 발생한다. 

 

-ALU

CPU내의 연산기관인 ALU에서는 arithmetic operation인 add, sub와 logical operation인 AND, OR, NOT등의 연산을 수행한다. (logic gate도 ALU의 일부분이다)

 

기본적으로 1-bit의 ALU를 병렬적으로 이어서 32-bit, 64-bit ALU를 만든다. 

한개의 ALU는 4종류의 hard ware block으로 구성되어 있다. (AND and Or gate, inverters, and multiplexors(mux))

 

1-bit ALU (AND/OR/ADD/NOT)

각각 bit flipper/ ADD, OR, ADD. operation에 따라 AND OR ADD의 결과값을 낸다.

Suntraction의 경우, bit flip을 해주고 ADD연산을 통해 값을 구한다. 

subtraction part

32-bit ALU  -> Array of 1-bit ALUs

xi means i th bit of x

1-bit ALU를 연결하여 Result를 낸다. (LSB부터 MSB까지)

 

overflow detection

ALU에서 overflow발생 시 overflow를 detect할 수 있는 overflow detection 회로를 달아준다.

Conditional branch

특정한 조건인 true일때만 작동되는 branch를 통해 계산값을 낼 수도 있다. 

예를 들어, 모든 bit result의 값이 0이면 모든 bit를 더해서 invert했을 때, 0이 나온다. 

conditional branch를 ALU에 달면 equality를 판단할 수 있다. 

 

-Multiplication

 

 

요소는 Multiplier, 곱하는 값 Multiplicand, 결과 Product 가 있다.

0. 처음에 Product와 Multiplicand를 비트수에 맞춰 0으로 채운다.  

1. Mulitilpier 0 이 1이면, Muliplicand와 Product를 더한다. 

    Multiplier 0 이 0이면, 아무것도 하지 않는다. 

2. Multiplicand를 left shift한다. (자릿수 맞춤)

3. Multiplier를 right shift함 (다음 multiplier0 을 처리하기 위해)

 

이 과정을 bit수만큼 반복한다. 

 

이 과정은 한번 cycle당 operation을 3번씩 반복하므로 clock를 많이 소모한다. 

 

 

 

ex>

mulipication ALU

Mulipliler Multiplicand Product
0011 0000 0010 0000 0000
0011 0000 0010 0000 0010
0011 0000 0100  0000 0010
0001 0000 0100 0000 0010
0001 0000 0100 0000 0110
  .........  

 

Optimized Multiplier

clock을 줄이기 위해 최적화 된 multiplier

한 스텝에 한 clock sycle을 in parallel로 갖도록 만들어졌다 (1/3으로 줄일 수 있다)

그 외에 multiplier를 빠르게 만드려면 adder의 수를 늘리면 된다. (수가 많을수록 가격은 비싸지지만 속도는 빨라진다. )

 

LEGv8 Multiplication Instructions: MUL, SMULH, UMULH

 

-Division

 

요소는 값 Quotient,  나눠지는 값 Dividend, 나누는 값 Divisor,  나머지 Remainder 가 있다.

0. 처음에 Product와 Multiplicand를 비트수에 맞춰 0으로 채운다.  

1. Dividend - divisor를 해보고 결과가 0보다 큰지 작은지에 따라서,

2. 0보다 크면, Quotient를 left shift하고 LSB에 1을 넣는다. 

0보다 작으면, 원래 대로 remainder를 돌리기 위해 divisor를 더해서 restore하고, Quotient를 left shift 하고 LSB에 0을 넣는다.

3. divisor를 right shift 한다. 

 

이 과정을 bit수만큼 반복한다. optimized전 multiplier와 유사하게 동작한다고 생각하면 된다. 

 

이 과정도 마찬가지로 한번 cycle당 operation을 3번씩 반복하므로 clock를 많이 소모한다. 

 

ex>

Quotient Divisor Remainder
0000 0010 0000 0000 0111
0000 0010 0000 1110 0111
0000 0010 0000  0000 0111
0000 0001 0000 1111 0111 
0000 0001 0000 0000 0111
  .........  

 

Optimized Divider

Remainder 레지스터의 오른쪽 64bit는 처음에 dividend로 초기화 하고, 하나씩 left shift한다.

LSB 자리에 subtraction 결과에 따라 0 또 는 1을 채운다. (이것이 최종 quotient 가 된다).

최종적으로, Remainder의 왼쪽 64bit은 remainder, 오른쪽 64bit는 quotient결과이다.

LEGv8 Division Instructions: SDIV (signed), UDIV (unsigned)

 

-Floating-point real numbers

Floating Point (부동소수점)(0이 아닌수로 시작하는 표기. 소수점 앞이 0이 아닌, 한자리의 정수여야 한다.)

Floating point standard: define by IEEE [ Single precision(32-bit), Double Precision(64-bit) ]

IEEE의 floating-point 형식은 [ S, Exponent(single: 8bit/ double: 11bit), Fraction(single: 23bit/ double: 52bit) ]로 되어있다. 

    S는 sign bit로 0이면 non-negative고 1이면 negative이다.

    Normalized significand: 1.0 ≤ |significand| < 2.0

    Exponent: excess representation: actual exponent + Bias, exponent 자체는 항상 positive number이다. 

    Single: Bias = 127; Double: Bias = 1023

 

따라서 32-bit word, sigle-precision 경우,

Smallest value:

 Exponent: 00000001 , actual exponent = 1 – 127 = –126

 Fraction: 000…00 , significand = 1.0

 

Largest value:

 exponent: 11111110 , actual exponent = 254 – 127 = +127

 Fraction: 111…11, significand ≈ 2.0

 

64-bit doubleword, Double-Precision 경우, 

Smallest value:

 Exponent: 00000000001 , actual exponent = 1 – 1023 = –1022

 Fraction: 000…00 , significand = 1.0

 

Largest value:

 Exponent: 11111111110 , actual exponent = 2046 – 1023 = +1023

 Fraction: 111…11 , significand ≈ 2.0

 

-Floating-Point Addition

1. align decimal/binary point(작은 것을 큰 것에 맞춘다)를 맞춘다. 

2.  significant를 더한다.

3. 소수점 앞에 무조건 0이 아닌 한자리수가 오는지 확인한다.

4. 마지막 자리수를 반올림한다. 4자리수가 될 떄까지 반올림해준다. 

 

FP Adder Hardware

addition과 같은 step으로 작동한다. 

 

-Floating-Point Multiplication

1. exponent(지수)를 더한다. 

2. sinificands를 곱한다. 

3. 결과를 정규화하고 over/under flow가 발생하는지 확인한다. 

4. 자릿수를 변경한다. 

5. sign을 결정한다. 

 

-FP Instructions in LEGv8

Single-precision arithmetic : FADDS, FSUBS, FMULS, FDIVS

Double-precision arithmetic : FADDD, FSUBD, FMULD, FDIVD

Single- and double-precision comparison : FCMPS, FCMPD

Branch on FP condition code true or false : B.cond

'SW' 카테고리의 다른 글

컴퓨터 구조 3  (0) 2022.10.04
컴퓨터 구조 2  (1) 2022.10.04
컴퓨터 구조 1  (0) 2022.10.04
CG4  (0) 2022.10.03
CG3  (0) 2022.10.02