1 Multiplication
Total Page:16
File Type:pdf, Size:1020Kb
CS 140 Class Notes 1 1 Multiplication Consider two unsigned binary numb ers X and Y . Wewanttomultiply these numb ers. The basic algorithm is similar to the one used in multiplying the numb ers on p encil and pap er. The main op erations involved are shift and add. Recall that the `p encil-and-pap er' algorithm is inecient in that each pro duct term obtained bymultiplying each bit of the multiplier to the multiplicand has to b e saved till all such pro duct terms are obtained. In machine implementations, it is desirable to add all such pro duct terms to form the partial product. Also, instead of shifting the pro duct terms to the left, the partial pro duct is shifted to the right b efore the addition takes place. In other words, if P is the partial pro duct i after i steps and if Y is the multiplicand and X is the multiplier, then P P + x Y i i j and 1 P P 2 i+1 i and the pro cess rep eats. Note that the multiplication of signed magnitude numb ers require a straight forward extension of the unsigned case. The magnitude part of the pro duct can b e computed just as in the unsigned magnitude case. The sign p of the pro duct P is computed from the signs of X and Y as 0 p x y 0 0 0 1.1 Two's complement Multiplication - Rob ertson's Algorithm Consider the case that we want to multiply two 8 bit numb ers X = x x :::x and Y = y y :::y . 0 1 7 0 1 7 Dep ending on the sign of the two op erands X and Y , there are 4 cases to b e considered : x = y =0, that is, b oth X and Y are p ositive. Hence, multiplication of these numb ers is 0 0 similar to the multiplication of unsigned numb ers. In other words, the pro duct P is computed in a series of add-and-shift steps of the form P P + x Y i i j 1 P P 2 i+1 i Note that all the partial pro duct are non-negative. Hence, leading 0s are intro duced during right shift of the partial pro duct. x =1;y =0, that is, X is negative and Y is p ositive. In this case, the partial pro duct is 0 0 always p ositive till the sign bit x is used. In the nal step, a subtraction is p erformed. 0 That is, P P Y x = 0;y = 1, that is, X is p ositive and Y is negative. In this case, the partial pro duct 0 0 is p ositive and hence leading 0s are shifted into the partial pro duct until the rst 1 in X is encountered. Multiplication of Y by this 1, and addition to the result causes the partial pro duct to b e negative, from which p oint on leading 1s are shifted in rather than 0s. x =1;y =1, that is, b oth X and Y are negative. Once again, leading 1s are shifted into 0 0 the partial pro duct whenever the partial pro duct is negative. Also, since X is negative, the correction step subtraction as the last step is also p erformed. Imp ortant : Recall the di erence in the correction steps b etween multiplication of two integers and two fractions. In the case of twointegers, the correction step involves subtraction and shift right. Whereas, in the case of fractions, the correction step involves subtraction and setting Q7 0. 1.2 Bo oth's Algorithm Recall that the preceding multiplication algorithms Rob ertson's algorithm involves scanning the multiplier from right to left and using the current multiplier bit x to determine whether the i multiplicand Y be added, subtracted or add 0 do nothing to the partial pro duct. In Bo oth's algorithm, two adjacent bits x x are examined in each step. If x x = 01, then Y is added to i i+1 i i+1 the partial pro duct, while if x x = 10, Y is subtracted from P partial pro duct. If x x =00 i i+1 i i i+1 or 11, then neither addition nor subtraction is p erformed. Thus, b o oth's algorithm e ectively skips over sequences of 1s and 0s in X . As a result, the total number of addition/subtraction steps required to multiply twonumb ers decrease however, at the cost of extra hardware. The pro cess of insp ecting the multiplier bits required by b o oth's algorithm can be viewed as 1, where 0 means shift the partial pro duct to enco ding the multiplier using three digits 0, 1 and the right that is, no addition or subtraction is p erformed, while 1 means add multiplicand b efore shifting and 1 means subtract multiplicand from the partial pro duct b efore shifting. The number thus pro duced is called a signed digit number and this pro cess of converting a multiplier X into a signed digit form is called as multiplier recoding. To generate X from X , app end the number X with a 0 to the right that is, start with X = x x :::x 0. Then use the following table to 0 1 n1 generate X from X : x x x i i+1 i 0 0 0 0 1 1 1 0 1 1 1 0 Bo oth's algorithm results in reduction in the numb er of add/subtract steps needed as compared to the Rob ertson's algorithm if the multiplier contains runs or sequences of 1s or 0s. The worst case scenario o ccurs in b o oth's algorithm if X = 010101::01, where there are n=2 isolated 1s, which forces n=2 subtractions and n=2 additions. This is worse than the standard multiplication algorithm - which contains only n=2 additions. The basic b o oth's algorithm can b e improved by detecting an isolated 1 in the multiplier and just p erforming addition at the corresp onding p oint in the multiplication. Similarly, an isolated 0 corresp onds to a subtraction. This is called as modi ed booth's algorithm, which always requires fewer addition/subtractions as compared to other multiplication algorithms. 2 Division In a xed p oint division of two numb ers, a divisor V and a dividend D are given. The goal of division is to compute quotient Q and remainder R such that D = Q V + R One of the simplest metho ds of division is the sequential digit-by-digit algorithm. Here, in step i i, the quotient bit q is determined by comparing the value of 2 V , which represents the divisor i i shifted i bits to the right, to the partial remainder R . The quotient bit q is set to 1 is 2 is less i i than R , else it is set to 0. Thus, the partial remainder is computed using the expression: i i R = R q 2 V i+1 i i This is equivalentto R =2R q V i+1 i i As it is clear from the basic approach describ ed here, the value of the quotient bit is determined by p erforming a trial subtraction of the form 2R V . If this is p ositive, then q =1, otherwise i i q =0. Note that when q =0, the result of the trial subtraction is 2R V , but the new partial i i i remainder should b e R =2R. Based on how this discrepancy is handled in the computations i+1 i of q and R , the division algorithm can b e divided into two parts: i i+1 2.1 Restoring Division Here, at every step, the op eration R =2R V i+1 i is p erformed. When the result of the subtraction is negative, a restoring addition is p erformed as follows: R = R + V i+1 i+1 In other words, the partial remainder is restored to the correct value is the appropriate quotient bit is 0. 2.2 Non-Restoring Division It is based on the observation that a restoring step of the form R = R + V i i followed by the next partial remainder calculation step R =2R V i+1 i can b e merged into a single op eration R =2R +V i+1 i Thus, when the quotient bit q =1, then the next partial remainder is computed by p erforming a i subtraction. However, when the quotient bit q =0, rather than restoring the partial remainder, i the next step is the addition of the divisor to the partial remainder, and not a subtraction. : In the case of non-restoring division, note that when the last quotient bit is 0 Important Note that is, q = 1, then as a result of the trial subtraction, the partial remainder is negative. Hence, 0 a correction step is necessary to restore the remainder value. Note that this is necessary only for the last step. The restoring and non-restoring division techniques describ ed here are applicable to unsigned integers as well as sign-magnitude numb ers..