204 ARITHMETIC 4.2.1 The rather long section of (Remote web server)
204 ARITHMETIC 4.2.1 The rather long section of code from lines 25 to 37 is needed because MIX has only a 5-byte accumulator for adding signed numbers while in general 2p+ 1 = 9 places of accuracy are required by Algorithm A. The program could be shortened to about half its present length if we were willing to sacrifice a little bit of its accuracy, but we shall see in the next section that full accuracy is important. Line 55 uses a nonstandard MIX instruction defined in Section 4.5.2. The running time for floating point addition and subtraction depends on several factors that are analyzed in Section 4.2.4. Now let us consider multiplication and division, which are simpler than addition, and which are somewhat similar to each other. Algorithm M (Floating point multiplication or division). Given base b, excess 9, pdigit, normalized floating point numbers u = (eu, fiL) and v = (e,, fv), this algorithm forms the product w = u @ v or the quotient w = u @ v. Ml. [Unpack.] Separate the exponent and fraction parts of the representations of LL and V. (Sometimes it is convenient, but not necessary, to test the operands for zero during this step.) M2. [Operate.] Set . . . e, + e, + ev - 4, fw + fu fv for multiplication; (g) ew +- e, -e, + q + 1, fW c (F1fu)/fV for division. (Since the input numbers are assumed to be normalized, it follows that either fW = 0, or l/b* 5 If,,,] < 1, or a division-by-zero error has occurred.) If necessary, the representation of fW may be reduced to p + 2 or p + 3 digits at this point, as in exercise 5. M3. [Normalize.] Perform Algorithm N on (e,, fW) to normalize, round, and pack the result. (Note: Normalization is simpler in this case, since scaling left occurs at most once, and since rounding overflow cannot occur after division.) 1 The following MIX subroutines, which are intended to be used in connection with Program A, illustrate the machine considerations necessary in connection with Algorithm M. Program M (Floating point multiplication and division). 01 Q EQU BYTE/2 q is half the byte size 02 FMUL STJ EXITF Floating point multiplication subroutine: 03 JOV OFLO Ensure overflow is off. 04 STA TEMP TEMP + w. 0.5 LDX ACC rx +-u. 06 STX FU(O:4) FU+ztffffO. 07 LDI TEMP (EXP) 08 LD2 ACC@XP) 09 INC2 -&,I r12 +- e, + ev -q. 10 SLA 1 11 MUL FU Multiply fu times fv. 12 JMP NORM Normalize, round, and exit.