First Commit of my working state
[simh.git] / HP2100 / hp2100_fp1.c
1 /* hp2100_fp1.c: HP 1000 multiple-precision floating point routines
2
3 Copyright (c) 2005-2008, J. David Bryan
4
5 Permission is hereby granted, free of charge, to any person obtaining a
6 copy of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 and/or sell copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the name of the author shall not be used
23 in advertising or otherwise to promote the sale, use or other dealings in
24 this Software without prior written authorization from the author.
25
26 10-May-08 JDB Fixed uninitialized return in fp_accum when setting
27 19-Mar-08 JDB Reworked "complement" to avoid inlining bug in gcc-4.x
28 01-Dec-06 JDB Reworked into generalized multiple-precision ops for FPP
29 12-Oct-06 JDB Altered x_trun for F-Series FFP compatibility
30 Added F-Series ..TCM FFP helpers
31
32 Primary references:
33 - HP 1000 M/E/F-Series Computers Engineering and Reference Documentation
34 (92851-90001, Mar-1981)
35 - HP 1000 M/E/F-Series Computers Technical Reference Handbook
36 (5955-0282, Mar-1980)
37 - DOS/RTE Relocatable Library Reference Manual
38 (24998-90001, Oct-1981)
39
40
41 This module implements multiple-precision floating-point operations to
42 support the 1000 F-Series hardware Floating Point Processor. It employs
43 64-bit integer arithmetic for speed and simplicity of implementation. The
44 host compiler must support 64-bit integers, and the HAVE_INT64 symbol must be
45 defined during compilation. If this symbol is not defined, then FPP support
46 is not available.
47
48 HP 2100/1000 computers used a proprietary floating-point format. The 2100
49 had optional firmware that provided two-word floating-point add, subtract,
50 multiply, and divide, as well as single-integer fix and float. The 1000-M/E
51 provided the same two-word firmware operations as standard equipment.
52 Three-word extended-precision instructions for the 2100 and 1000-M/E were
53 provided by the optional Fast FORTRAN Processor firmware.
54
55 The 1000-F substituted a hardware floating point processor for the firmware
56 in previous machines. In addition to the two- and three-word formats, the
57 F-Series introduced a four-word double-precision format. A five-word format
58 that provided extra range in the exponent by unpacking it from the mantissa
59 was also provided, although this capability was not documented in the user
60 manual. In addition, the FPP improved the accuracy of floating-point
61 calculations, as the firmware versions sacrificed a few bits of precision to
62 gain speed. Consequently, operations on the F-Series may return results that
63 differ slightly from the same operations on the M/E-Series or the 2100.
64
65 F-Series units after date code 1920 also provided two-word double-integer
66 instructions in firmware, as well as double-integer fix and float operations.
67
68 The original 32-bit floating-point format is as follows:
69
70 15 14 0
71 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
72 |MS| mantissa high | : M
73 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
74 | mantissa low | exponent |XS| : M + 1
75 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
76 15 8 7 1 0
77
78 Both 23-bit mantissa and 7-bit exponent are in twos-complement form. The
79 exponent sign bit has been rotated into the LSB of the second word.
80
81 The extended-precision floating-point format is a 48-bit extension of the
82 32-bit format used for single precision. A packed extended-precision value
83 consists of a 39-bit mantissa and a 7-bit exponent. The format is as
84 follows:
85
86 15 14 0
87 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
88 |MS| mantissa high | : M
89 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
90 | mantissa middle | : M + 1
91 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
92 | mantissa low | exponent |XS| : M + 2
93 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
94 15 8 7 1 0
95
96 The double-precision floating-point format is similar to the 48-bit
97 extended-precision format, although with a 55-bit mantissa:
98
99 15 14 0
100 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
101 |MS| mantissa high | : M
102 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
103 | mantissa middle high | : M + 1
104 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
105 | mantissa middle low | : M + 2
106 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
107 | mantissa low | exponent |XS| : M + 3
108 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
109 15 8 7 1 0
110
111 The FPP also supports a special five-word expanded-exponent format:
112
113 15 14 0
114 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
115 |MS| mantissa high | : M
116 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
117 | mantissa middle high | : M + 1
118 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
119 | mantissa middle low | : M + 2
120 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
121 | mantissa low | : M + 3
122 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
123 | exponent |XS| : M + 4
124 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
125 15 8 7 1 0
126
127 The exponent is a full 16-bit twos-complement value, but the allowed range is
128 only 10 bits, i.e., -512 to +511.
129
130 In a normalized value, the sign and MSB of the mantissa differ. Zero is
131 represented by all words = 0.
132
133 Internally, unpacked floating-point values are contained in a structure
134 having a signed 64-bit mantissa and a signed 32-bit exponent. Mantissas are
135 left-justified with the unused bits masked to zero. Exponents are
136 right-justified. The precision is indicated by the value of a structure
137 field.
138
139 HP terminology for the three-word floating-point format is confused. Some
140 documents refer to it as "double precision," while others use "extended
141 precision." The instruction mnemonics begin with "X" (e.g., .XADD),
142 suggesting the extended-precision term.
143
144 HP apparently intended that the four-word double-precision format would be
145 called "triple-precision," as the instruction mnemonics begin with "T" (e.g.,
146 ".TADD" for the four-word add instruction). The source files for the
147 software simulations of these instructions for the M/E-Series also explicitly
148 refer to "triple precision math." However, the engineering documentation and
149 the F-Series reference manual both use the double-precision term.
150
151 This module adopts the single/extended/double terminology and uses the
152 initial letters of the instructions (F/X/T) to indicate the precision used.
153
154 The FPP hardware consisted of two circuit boards that interfaced to the main
155 CPU via the Microprogammable Processor Port (MPP) that had been introduced
156 with the 1000 E-Series. One board contained argument registers and ALUs,
157 split into separate mantissa and exponent parts. The other contained a state
158 machine sequencer. FPP results were copied automatically to the argument
159 registers in addition to being available over the MPP, so that chained
160 operations could be executed from these "accumulators" without reloading.
161
162 The FPP operated independently of the CPU. An opcode, specifying one of the
163 six operations (add, subtract, multiply, divide, fix, or float) was sent to
164 the FPP, and a start command was given. Operands of appropriate precision
165 were then supplied to the FPP. Once the operands were received, the FPP
166 would execute and set a flag when the operation was complete. The result
167 would then be retrieved from the FPP. The floating-point instruction
168 firmware in the CPU initiated the desired FPP operation and handled operand
169 reads from and result writes to main memory.
170
171 Under simulation, "fp_exec" provides the six arithmetic operations analogous
172 to FPP execution. The remainder of the functions are helpers that were
173 provided by firmware in the 1000-F but that can reuse code needed to simulate
174 the FPP hardware. As with the hardware, "fp_exec" retains the last result
175 in an internal accumulator that may be referenced in subsequent operations.
176
177 NOTE: this module also provides the floating-point support for the firmware
178 single-precision 1000-M/E base set and extended-precision FFP instructions.
179 Because the firmware and hardware implementations returned slightly different
180 results, particularly with respect to round-off, conditional checks are
181 implemented in the arithmetic routines. In some cases, entirely different
182 algorithms are used to ensure fidelity with the real machines. Functionally,
183 this means that the 2100/1000-M/E and 1000-F floating-point diagnostics are
184 not interchangeable, and failures are to be expected if a diagnostic is run
185 on the wrong machine.
186 */
187
188 #include "hp2100_defs.h"
189 #include "hp2100_cpu.h"
190 #include "hp2100_cpu1.h"
191 #include "hp2100_fp1.h"
192
193
194 #if defined (HAVE_INT64) /* we need int64 support */
195
196 /* Field widths. */
197
198 #define IN_W_SIGN 1
199 #define IN_W_SMAGN 15
200 #define IN_W_DMAGN 31
201
202 #define FP_W_MSIGN 1
203 #define FP_W_FMANT 23
204 #define FP_W_XMANT 39
205 #define FP_W_TMANT 55
206 #define FP_W_EMANT 55
207 #define FP_W_EXPANDEXP 9
208 #define FP_W_EXP 7
209 #define FP_W_ESIGN 1
210
211 /* Starting bit numbers. */
212
213 #define IN_V_SIGN (64 - IN_W_SIGN)
214 #define IN_V_SNUM (64 - IN_W_SIGN - IN_W_SMAGN)
215 #define IN_V_DNUM (64 - IN_W_SIGN - IN_W_DMAGN)
216
217 #define FP_V_FNUM (64 - FP_W_MSIGN - FP_W_FMANT - FP_W_EXP - FP_W_ESIGN)
218 #define FP_V_XNUM (64 - FP_W_MSIGN - FP_W_XMANT - FP_W_EXP - FP_W_ESIGN)
219 #define FP_V_TNUM (64 - FP_W_MSIGN - FP_W_TMANT - FP_W_EXP - FP_W_ESIGN)
220 #define FP_V_ENUM (64 - FP_W_MSIGN - FP_W_EMANT - FP_W_EXP - FP_W_ESIGN)
221
222 #define FP_V_MSIGN (64 - FP_W_MSIGN)
223 #define FP_V_FMANT (64 - FP_W_MSIGN - FP_W_FMANT)
224 #define FP_V_XMANT (64 - FP_W_MSIGN - FP_W_XMANT)
225 #define FP_V_TMANT (64 - FP_W_MSIGN - FP_W_TMANT)
226 #define FP_V_EMANT (64 - FP_W_MSIGN - FP_W_EMANT)
227 #define FP_V_EXP 1
228 #define FP_V_ESIGN 0
229
230 /* Right-aligned field masks. */
231
232 #define IN_M_SIGN (((t_uint64) 1 << IN_W_SIGN) - 1)
233 #define IN_M_SMAGN (((t_uint64) 1 << IN_W_SMAGN) - 1)
234 #define IN_M_DMAGN (((t_uint64) 1 << IN_W_DMAGN) - 1)
235
236 #define FP_M_MSIGN (((t_uint64) 1 << FP_W_MSIGN) - 1)
237 #define FP_M_FMANT (((t_uint64) 1 << FP_W_FMANT) - 1)
238 #define FP_M_XMANT (((t_uint64) 1 << FP_W_XMANT) - 1)
239 #define FP_M_TMANT (((t_uint64) 1 << FP_W_TMANT) - 1)
240 #define FP_M_EMANT (((t_uint64) 1 << FP_W_EMANT) - 1)
241
242 #define FP_M_EXPANDEXP ((1 << FP_W_EXPANDEXP) - 1)
243 #define FP_M_EXP ((1 << FP_W_EXP) - 1)
244 #define FP_M_ESIGN ((1 << FP_W_ESIGN) - 1)
245
246 /* In-place field masks. */
247
248 #define IN_SIGN (IN_M_SIGN << IN_V_SIGN)
249 #define IN_SMAGN (IN_M_SMAGN << IN_V_SNUM)
250 #define IN_DMAGN (IN_M_DMAGN << IN_V_DNUM)
251
252 #define FP_MSIGN (FP_M_MSIGN << FP_V_MSIGN)
253 #define FP_FMANT (FP_M_FMANT << FP_V_FMANT)
254 #define FP_XMANT (FP_M_XMANT << FP_V_XMANT)
255 #define FP_TMANT (FP_M_TMANT << FP_V_TMANT)
256 #define FP_EMANT (FP_M_EMANT << FP_V_EMANT)
257 #define FP_EXP (FP_M_EXP << FP_V_EXP)
258 #define FP_ESIGN (FP_M_ESIGN << FP_V_ESIGN)
259
260 /* In-place record masks. */
261
262 #define IN_SSMAGN (IN_SIGN | IN_SMAGN)
263 #define IN_SDMAGN (IN_SIGN | IN_DMAGN)
264
265 #define FP_SFMANT (FP_MSIGN | FP_FMANT)
266 #define FP_SXMANT (FP_MSIGN | FP_XMANT)
267 #define FP_STMANT (FP_MSIGN | FP_TMANT)
268 #define FP_SEMANT (FP_MSIGN | FP_EMANT)
269 #define FP_SEXP (FP_ESIGN | FP_EXP)
270
271 /* Minima and maxima. */
272
273 #define FP_ONEHALF ((t_int64) 1 << (FP_V_MSIGN - 1)) /* mantissa = 0.5 */
274 #define FP_MAXPMANT ((t_int64) FP_EMANT) /* maximum pos mantissa */
275 #define FP_MAXNMANT ((t_int64) FP_MSIGN) /* maximum neg mantissa */
276 #define FP_MAXPEXP (FP_M_EXPANDEXP) /* maximum pos expanded exponent */
277 #define FP_MAXNEXP (-(FP_MAXPEXP + 1)) /* maximum neg expanded exponent */
278
279 /* Floating-point helpers. */
280
281 #define DENORM(x) ((((x) ^ (x) << 1) & FP_MSIGN) == 0)
282
283 #define TO_EXP(e) (int8) ((e >> FP_V_EXP & FP_M_EXP) | \
284 (e & FP_M_ESIGN ? ~FP_M_EXP : 0))
285
286 /* Property constants. */
287
288 static const t_int64 p_half_lsb[6] = { ((t_int64) 1 << IN_V_SNUM) - 1, /* different than FP! */
289 ((t_int64) 1 << IN_V_DNUM) - 1, /* different than FP! */
290 (t_int64) 1 << (FP_V_FMANT - 1),
291 (t_int64) 1 << (FP_V_XMANT - 1),
292 (t_int64) 1 << (FP_V_TMANT - 1),
293 (t_int64) 1 << (FP_V_EMANT - 1) };
294
295 static const t_int64 n_half_lsb[6] = { 0,
296 0,
297 ((t_int64) 1 << (FP_V_FMANT - 1)) - 1,
298 ((t_int64) 1 << (FP_V_XMANT - 1)) - 1,
299 ((t_int64) 1 << (FP_V_TMANT - 1)) - 1,
300 ((t_int64) 1 << (FP_V_EMANT - 1)) - 1 };
301
302 static const uint32 op_start[6] = { IN_V_SNUM,
303 IN_V_DNUM,
304 FP_V_FMANT,
305 FP_V_XMANT,
306 FP_V_TMANT,
307 FP_V_EMANT };
308
309 static const t_int64 mant_mask[6] = { IN_SSMAGN,
310 IN_SDMAGN,
311 FP_SFMANT,
312 FP_SXMANT,
313 FP_STMANT,
314 FP_SEMANT };
315
316 static const uint32 op_bits[6] = { IN_W_SMAGN,
317 IN_W_DMAGN,
318 FP_W_FMANT + FP_W_MSIGN,
319 FP_W_XMANT + FP_W_MSIGN,
320 FP_W_TMANT + FP_W_MSIGN,
321 FP_W_EMANT + FP_W_MSIGN };
322
323 static const t_int64 op_mask[6] = { ~(t_int64) 0 << IN_V_SNUM,
324 ~(t_int64) 0 << IN_V_DNUM,
325 ~(t_int64) 0 << FP_V_FNUM,
326 ~(t_int64) 0 << FP_V_XNUM,
327 ~(t_int64) 0 << FP_V_TNUM,
328 ~(t_int64) 0 << FP_V_ENUM };
329
330 static const uint32 int_p_max[2] = { IN_M_SMAGN,
331 IN_M_DMAGN };
332
333
334 /* Internal unpacked floating-point representation. */
335
336 typedef struct {
337 t_int64 mantissa;
338 int32 exponent;
339 OPSIZE precision;
340 } FPU;
341
342
343
344 /* Low-level helper routines. */
345
346
347 /* Arithmetic shift right for mantissa only.
348
349 Returns TRUE if any one-bits are shifted out (for F-series only).
350 */
351
352 static t_bool asr (FPU *operand, int32 shift)
353 {
354 t_uint64 mask;
355 t_bool bits_lost;
356
357 if (UNIT_CPU_MODEL == UNIT_1000_F) { /* F-series? */
358 mask = ((t_uint64) 1 << shift) - 1; /* mask for lost bits */
359 bits_lost = ((operand->mantissa & mask) != 0); /* flag if any lost */
360 }
361 else
362 bits_lost = FALSE;
363
364 operand->mantissa = operand->mantissa >> shift; /* mantissa is int, so ASR */
365 return bits_lost;
366 }
367
368
369 /* Logical shift right for mantissa and exponent.
370
371 Shifts mantissa and corrects exponent for mantissa overflow.
372 Returns TRUE if any one-bits are shifted out (for F-series only).
373 */
374
375 static t_bool lsrx (FPU *operand, int32 shift)
376 {
377 t_uint64 mask;
378 t_bool bits_lost;
379
380 if (UNIT_CPU_MODEL == UNIT_1000_F) { /* F-series? */
381 mask = ((t_uint64) 1 << shift) - 1; /* mask for lost bits */
382 bits_lost = ((operand->mantissa & mask) != 0); /* flag if any lost */
383 }
384 else
385 bits_lost = FALSE;
386
387 operand->mantissa = (t_uint64) operand->mantissa >> shift; /* uint, so LSR */
388 operand->exponent = operand->exponent + shift; /* correct exponent */
389 return bits_lost;
390 }
391
392
393 /* Unpack an operand into a long integer.
394
395 Returns a left-aligned integer or mantissa. Does not mask to precision; this
396 should be done subsequently if desired.
397 */
398
399 static t_int64 unpack_int (OP packed, OPSIZE precision)
400 {
401 uint32 i;
402 t_uint64 unpacked = 0;
403
404 if (precision == in_s)
405 unpacked = (t_uint64) packed.word << 48; /* unpack single integer */
406
407 else if (precision == in_d)
408 unpacked = (t_uint64) packed.dword << 32; /* unpack double integer */
409
410 else {
411 if (precision == fp_e) /* five word operand? */
412 precision = fp_t; /* only four mantissa words */
413
414 for (i = 0; i < 4; i++) /* unpack fp 2 to 4 words */
415 if (i < TO_COUNT (precision))
416 unpacked = unpacked << 16 | packed.fpk[i];
417 else
418 unpacked = unpacked << 16;
419 }
420
421 return (t_int64) unpacked;
422 }
423
424
425 /* Unpack a packed operand.
426
427 The packed value is split into separate mantissa and exponent variables. The
428 multiple words of the mantissa are concatenated into a single 64-bit signed
429 value, and the exponent is shifted with recovery of the sign.
430 */
431
432 static FPU unpack (OP packed, OPSIZE precision)
433 {
434 FPU unpacked;
435
436 unpacked.precision = precision; /* set value's precision */
437
438 unpacked.mantissa = /* unpack and mask mantissa */
439 unpack_int (packed, precision) & mant_mask[precision];
440
441 switch (precision) {
442
443 case fp_f:
444 case fp_x:
445 case fp_t:
446 unpacked.exponent = /* unpack exponent from correct word */
447 TO_EXP (packed.fpk[(uint32) precision - 1]);
448 break;
449
450 case fp_e:
451 unpacked.exponent = /* unpack expanded exponent */
452 (int16) (packed.fpk[4] >> FP_V_EXP | /* rotate sign into place */
453 (packed.fpk[4] & 1 ? SIGN : 0));
454 break;
455
456 case fp_a: /* no action for value in accum */
457 case in_s: /* integers don't use exponent */
458 case in_d: /* integers don't use exponent */
459 default:
460 unpacked.exponent = 0;
461 break;
462 }
463
464 return unpacked;
465 }
466
467
468 /* Pack a long integer into an operand. */
469
470 static OP pack_int (t_int64 unpacked, OPSIZE precision)
471 {
472 int32 i;
473 OP packed;
474
475 if (precision == in_s)
476 packed.word = (uint16) (unpacked >> 48) & DMASK; /* pack single integer */
477
478 else if (precision == in_d)
479 packed.dword = (uint32) (unpacked >> 32) & DMASK32; /* pack double integer */
480
481 else {
482 if (precision == fp_e) /* five word operand? */
483 precision = fp_t; /* only four mantissa words */
484
485 for (i = 3; i >= 0; i--) { /* pack fp 2 to 4 words */
486 packed.fpk[i] = (uint16) unpacked & DMASK;
487 unpacked = unpacked >> 16;
488 }
489 }
490
491 return packed;
492 }
493
494
495 /* Pack an unpacked floating-point number.
496
497 The 64-bit mantissa is split into the appropriate number of 16-bit words.
498 The exponent is rotated to incorporate the sign bit and merged into the
499 appropriate word.
500 */
501
502 static OP pack (FPU unpacked)
503 {
504 OP packed;
505 uint8 exp;
506
507 packed = pack_int (unpacked.mantissa, unpacked.precision); /* pack mantissa */
508
509 exp = ((uint8) unpacked.exponent << FP_V_EXP) | /* rotate exponent */
510 ((unpacked.exponent < 0) << FP_V_ESIGN);
511
512 switch (unpacked.precision) { /* merge exponent into correct word */
513
514 case in_s: /* no action for integers */
515 case in_d:
516 break;
517
518 case fp_f: /* merge into last word */
519 case fp_x:
520 case fp_t:
521 packed.fpk[(uint32) unpacked.precision - 1] =
522 (packed.fpk[(uint32) unpacked.precision - 1] & ~FP_SEXP) | exp;
523 break;
524
525 case fp_e: /* place in separate word */
526 packed.fpk[4] = ((uint16) unpacked.exponent << FP_V_EXP) |
527 ((unpacked.exponent < 0) << FP_V_ESIGN);
528 break;
529
530 case fp_a: /* no action for value in accum */
531 break;
532 }
533
534 return packed;
535 }
536
537
538 /* Normalize an unpacked floating-point number.
539
540 Floating-point numbers are in normal form if the sign bit and the MSB of the
541 mantissa differ. Unnormalized numbers are shifted as needed with appropriate
542 exponent modification.
543 */
544
545 static void normalize (FPU *unpacked)
546 {
547
548 if (unpacked->mantissa) /* non-zero? */
549 while (DENORM (unpacked->mantissa)) { /* normal form? */
550 unpacked->exponent = unpacked->exponent - 1; /* no, so left shift */
551 unpacked->mantissa = unpacked->mantissa << 1;
552 }
553 else
554 unpacked->exponent = 0; /* clean for zero */
555 return;
556 }
557
558
559 /* Round an unpacked floating-point number and check for overflow.
560
561 An unpacked floating-point number is rounded by adding one-half of the LSB
562 value, maintaining symmetry around zero. If rounding resulted in a mantissa
563 overflow, the result logically is shifted to the right with an appropriate
564 exponent modification. Finally, the result is checked for exponent underflow
565 or overflow, and the appropriate approximation (zero or infinity) is
566 returned.
567
568 Rounding in hardware involves a special mantissa extension register that
569 holds three "guard" bits and one "sticky" bit. These represent the value of
570 bits right-shifted out the mantissa register. Under simulation, we track
571 such right-shifts and utilize the lower eight bits of the 64-bit mantissa
572 value to simulate the extension register.
573
574 Overflow depends on whether the FPP expanded-exponent form is being used
575 (this expands the exponent range by two bits). If overflow is detected, the
576 value representing infinity is dependent on whether the operation is on
577 behalf of the Fast FORTRAN Processor. The F-Series FPP returns positive
578 infinity on both positive and negative overflow for all precisions. The 2100
579 and M/E-Series FFPs return negative infinity on negative overflow of
580 extended-precision values. Single-precision overflows on these machines
581 always return positive infinity.
582
583 The number to be rounded must be normalized upon entry.
584 */
585
586 static uint32 roundovf (FPU *unpacked, t_bool expand)
587 {
588 uint32 overflow;
589 t_bool sign;
590
591 sign = (unpacked->mantissa < 0); /* save mantissa sign */
592
593 if (sign) /* round and mask the number */
594 unpacked->mantissa =
595 (unpacked->mantissa + n_half_lsb[unpacked->precision]) &
596 mant_mask[unpacked->precision];
597 else
598 unpacked->mantissa =
599 (unpacked->mantissa + p_half_lsb[unpacked->precision]) &
600 mant_mask[unpacked->precision];
601
602 if (sign != (unpacked->mantissa < 0)) /* mantissa overflow? */
603 lsrx (unpacked, 1); /* correct by shifting */
604 else
605 normalize (unpacked); /* renorm may be needed */
606
607 if (unpacked->mantissa == 0) { /* result zero? */
608 unpacked->mantissa = 0; /* return zero */
609 unpacked->exponent = 0;
610 overflow = 0; /* with overflow clear */
611 }
612 else if (unpacked->exponent < /* result underflow? */
613 (FP_MAXNEXP >> (expand ? 0 : 2))) {
614 unpacked->mantissa = 0; /* return zero */
615 unpacked->exponent = 0;
616 overflow = 1; /* and set overflow */
617 }
618 else if (unpacked->exponent > /* result overflow? */
619 (FP_MAXPEXP >> (expand ? 0 : 2))) {
620 if (sign && /* negative value? */
621 (unpacked->precision == fp_x) && /* extended precision? */
622 (UNIT_CPU_MODEL != UNIT_1000_F)) { /* not F-series? */
623 unpacked->mantissa = FP_MAXNMANT; /* return negative infinity */
624 unpacked->exponent = FP_MAXPEXP & FP_M_EXP;
625 }
626 else {
627 unpacked->mantissa = FP_MAXPMANT; /* return positive infinity */
628 unpacked->exponent = FP_MAXPEXP & FP_M_EXP;
629 }
630 overflow = 1; /* and set overflow */
631 }
632 else
633 overflow = 0; /* value is in range */
634
635 return overflow;
636 }
637
638
639 /* Normalize, round, and pack an unpacked floating-point number. */
640
641 static uint32 nrpack (OP *packed, FPU unpacked, t_bool expand)
642 {
643 uint32 overflow;
644
645 normalize (&unpacked); /* normalize for rounding */
646 overflow = roundovf (&unpacked, expand); /* round and check for overflow */
647 *packed = pack (unpacked); /* pack result */
648
649 return overflow;
650 }
651
652
653
654 /* Low-level arithmetic routines. */
655
656
657 /* Complement an unpacked number. */
658
659 static void complement (FPU *result)
660 {
661 if (result->mantissa == FP_MAXNMANT) { /* maximum negative? */
662 result->mantissa = FP_ONEHALF; /* complement of -1.0 * 2 ^ n */
663 result->exponent = result->exponent + 1; /* is 0.5 * 2 ^ (n + 1) */
664 }
665 else
666 result->mantissa = -result->mantissa; /* negate mantissa */
667 return;
668 }
669
670
671 /* Add two unpacked numbers.
672
673 The mantissas are first aligned if necessary by scaling the smaller of the
674 two operands. If the magnitude of the difference between the exponents is
675 greater than the number of significant bits, then the smaller number has been
676 scaled to zero (swamped), and so the sum is simply the larger operand.
677 Otherwise, the sum is computed and checked for overflow, which has occured if
678 the signs of the operands are the same but differ from that of the result.
679 Scaling and renormalization is perfomed if overflow occurred.
680 */
681
682 static void add (FPU *sum, FPU augend, FPU addend)
683 {
684 int32 magn;
685 t_bool bits_lost;
686
687 if (augend.mantissa == 0)
688 *sum = addend; /* X + 0 = X */
689
690 else if (addend.mantissa == 0)
691 *sum = augend; /* 0 + X = X */
692
693 else {
694 magn = augend.exponent - addend.exponent; /* difference exponents */
695
696 if (magn > 0) { /* addend smaller? */
697 *sum = augend; /* preset augend */
698 bits_lost = asr (&addend, magn); /* align addend */
699 }
700 else { /* augend smaller? */
701 *sum = addend; /* preset addend */
702 magn = -magn; /* make difference positive */
703 bits_lost = asr (&augend, magn); /* align augend */
704 }
705
706 if (magn <= (int32) op_bits[augend.precision]) { /* value swamped? */
707 sum->mantissa = /* no, add mantissas */
708 addend.mantissa + augend.mantissa;
709
710 if (((addend.mantissa < 0) == (augend.mantissa < 0)) && /* mantissa overflow? */
711 ((addend.mantissa < 0) != (sum->mantissa < 0))) {
712 bits_lost = bits_lost | lsrx (sum, 1); /* restore value */
713 sum->mantissa = /* restore sign */
714 sum-> mantissa | (addend.mantissa & FP_MSIGN);
715 }
716
717 if (bits_lost) /* any bits lost? */
718 sum->mantissa = sum->mantissa | 1; /* include one for rounding */
719 }
720 }
721 return;
722 }
723
724
725 /* Multiply two unpacked numbers.
726
727 The single-precision firmware (FMP) operates differently from the firmware
728 extended-precision (.XMPY) and the hardware multiplies of any precision.
729 Firmware implementations form 16-bit x 16-bit = 32-bit partial products and
730 sum them to form the result. The hardware uses a series of shifts and adds.
731 This means that firmware FMP and hardware FMP return slightly different
732 values, as may be seen by attempting to run the firmware FMP diagnostic on
733 the FPP.
734
735 The FMP microcode calls a signed multiply routine to calculate three partial
736 products (all but LSB * LSB). Because the LSBs are unsigned, i.e., all bits
737 significant, the two MSB * LSB products are calculated using LSB/2. The
738 unsigned right-shift ensures a positive LSB with no significant bits lost,
739 because the lower eight bits are unused (they held the vacated exponent). In
740 order to sum the partial products, the LSB of the result of MSB * MSB is also
741 right-shifted before addition. Note, though, that this loses a significant
742 bit. After summation, the result is left-shifted to correct for the original
743 right shifts.
744
745 The .XMPY microcode negates both operands as necessary to produce positive
746 values and then forms six of the nine 16-bit x 16-bit = 32-bit unsigned
747 multiplications required for a full 96-bit product. Given a 48-bit
748 multiplicand "a1a2a3" and a 48-bit multiplier "b1b2b3", the firmware performs
749 these calculations to develop a 48-bit product:
750
751 a1 a2 a3
752 +-------+-------+-------+
753 b1 b2 b3
754 +-------+-------+-------+
755 _________________________
756
757 a1 * b3 [p1]
758 +-------+-------+
759 a2 * b2 [p2]
760 +-------+-------+
761 a1 * b2 [p3]
762 +-------+-------+
763 a3 * b1 [p4]
764 +-------+-------+
765 a2 * b1 [p5]
766 +-------+-------+
767 a1 * b1 [p6]
768 +-------+-------+
769 _________________________________
770
771 product
772 +-------+-------+-------+
773
774 The least-significant words of partial products [p1], [p2], and [p4] are used
775 only to develop a carry bit into the 48-bit sum. The product is complemented
776 as necessary to restore the sign.
777
778 The basic FPP hardware algorithm scans the multiplier and adds a shifted copy
779 of the multiplicand whenever a one-bit is detected. To avoid successive adds
780 when a string of ones is encountered (because adds are more expensive than
781 shifts), the hardware instead adds the multiplicand shifted by N+1+P and
782 subtracts the multiplicand shifted by P to obtain the equivalent value with a
783 maximum of two operations.
784
785 Instead of implementing either the .XMPY firmware algorithm or the hardware
786 shift-and-add algorithm directly, it is more efficient under simulation to
787 use 32 x 32 = 64-bit multiplications, thereby reducing the number required
788 from six to four (64-bit "c1c2" x 64-bit "d1d2"):
789
790 ah al
791 +-------+-------+
792 bh bl
793 +-------+-------+
794 _________________
795
796 al * bl [ll]
797 +-------+-------+
798 ah * bl [hl]
799 +-------+-------+
800 al * bh [lh]
801 +-------+-------+
802 ah * bh [hh]
803 +-------+-------+
804 _________________________________
805
806 product
807 +-------+-------+
808
809 However, the FMP algorithm is implemented directly from the microcode to
810 preserve the fidelity of the simulation, i.e., to lose the same amount
811 of precision.
812 */
813
814 static void multiply (FPU *product, FPU multiplicand, FPU multiplier)
815 {
816 uint32 ah, al, bh, bl, sign = 0;
817 t_uint64 hh, hl, lh, ll, carry;
818 int16 ch, cl, dh, dl;
819 t_bool firmware;
820
821 product->precision = multiplicand.precision; /* set precision */
822
823 if ((multiplicand.mantissa == 0) || /* 0 * X = 0 */
824 (multiplier.mantissa == 0)) /* X * 0 = 0 */
825 product->mantissa = product->exponent = 0;
826
827 else {
828 firmware = (UNIT_CPU_MODEL != UNIT_1000_F); /* set firmware flag */
829
830 if (!firmware || (product->precision != fp_f)) { /* hardware? */
831 if (multiplicand.mantissa < 0) { /* negative? */
832 complement (&multiplicand); /* complement operand */
833 sign = ~sign; /* track sign */
834 }
835 if (multiplier.mantissa < 0) { /* negative? */
836 complement (&multiplier); /* complement operand */
837 sign = ~sign; /* track sign */
838 }
839 }
840
841 product->exponent = /* compute exponent */
842 multiplicand.exponent + multiplier.exponent + 1;
843
844 ah = (uint32) (multiplicand.mantissa >> 32); /* split multiplicand */
845 al = (uint32) (multiplicand.mantissa & DMASK32); /* into high and low parts */
846 bh = (uint32) (multiplier.mantissa >> 32); /* split multiplier */
847 bl = (uint32) (multiplier.mantissa & DMASK32); /* into high and low parts */
848
849 if (firmware && (product->precision == fp_f)) { /* single-precision firmware? */
850 ch = (int16) (ah >> 16) & DMASK; /* split 32-bit multiplicand */
851 cl = (int16) (ah & 0xfffe); /* into high and low parts */
852 dh = (int16) (bh >> 16) & DMASK; /* split 32-bit multiplier */
853 dl = (int16) (bh & 0xfffe); /* into high and low parts */
854
855 hh = (t_uint64) (((int32) ch * dh) & ~1); /* form cross products */
856 hl = (t_uint64) (((t_int64) ch * (t_int64) (uint16) dl +
857 (t_int64) dh * (t_int64) (uint16) cl) &
858 0xfffffffffffe0000);
859
860 product->mantissa = (t_uint64) (((t_int64) hh << 32) + /* sum partials */
861 ((t_int64) hl << 16));
862 }
863
864 else {
865 hh = ((t_uint64) ah * bh); /* form four cross products */
866 hl = ((t_uint64) ah * bl); /* using 32 x 32 = */
867 lh = ((t_uint64) al * bh); /* 64-bit multiplies */
868 ll = ((t_uint64) al * bl);
869
870 carry = ((ll >> 32) + (uint32) hl + (uint32) lh) >> 32; /* form carry */
871
872 product->mantissa = hh + (hl >> 32) + (lh >> 32) + carry; /* sum partials */
873
874 if (sign) /* negate if required */
875 complement (product);
876 }
877 }
878 return;
879 }
880
881
882 /* Divide two unpacked numbers.
883
884 As with multiply, the single-precision firmware (FDV) operates differently
885 from the firmware extended-precision (.XDIV) and the hardware divisions of
886 any precision. Firmware implementations utilize a "divide and correct"
887 algorithm, wherein the quotient is estimated and then corrected by comparing
888 the dividend to the product of the quotient and the divisor. The hardware
889 uses a series of shifts and subtracts. This means that firmware FDV and
890 hardware FDV once again return slightly different values.
891
892 Under simulation, the classic divide-and-correct method is employed, using
893 64-bit / 32-bit = 32-bit divisions. This involves dividing the 64-bit
894 dividend "a1a2a3a4" by the first 32-bit digit "b1b2" of the 64-bit divisor
895 "b1b2b3b4". The resulting 32-bit quotient is ...
896
897 The microcoded single-precision division avoids overflows by right-shifting
898 some values, which leads to a loss of precision in the LSBs. We duplicate
899 the firmware algorithm here to preserve the fidelity of the simulation.
900 */
901
902 static void divide (FPU *quotient, FPU dividend, FPU divisor)
903 {
904 uint32 sign = 0;
905 t_int64 bh, bl, r1, r0, p1, p0;
906 t_uint64 q, q1, q0;
907 t_bool firmware;
908 int32 ah, div, cp;
909 int16 dh, dl, pq1, pq2, cq;
910
911 quotient->precision = dividend.precision; /* set precision */
912
913 if (divisor.mantissa == 0) { /* division by zero? */
914 if (dividend.mantissa < 0)
915 quotient->mantissa = FP_MSIGN; /* return minus infinity */
916 else
917 quotient->mantissa = ~FP_MSIGN; /* or plus infinity */
918 quotient->exponent = FP_MAXPEXP + 1;
919 }
920
921 else if (dividend.mantissa == 0) /* dividend zero? */
922 quotient->mantissa = quotient->exponent = 0; /* yes; result is zero */
923
924 else {
925 firmware = (UNIT_CPU_MODEL != UNIT_1000_F); /* set firmware flag */
926
927 if (!firmware || (quotient->precision != fp_f)) { /* hardware or FFP? */
928 if (dividend.mantissa < 0) { /* negative? */
929 complement (&dividend); /* complement operand */
930 sign = ~sign; /* track sign */
931 }
932 if (divisor.mantissa < 0) { /* negative? */
933 complement (&divisor); /* complement operand */
934 sign = ~sign; /* track sign */
935 }
936 }
937
938 quotient->exponent = /* division subtracts exponents */
939 dividend.exponent - divisor.exponent;
940
941 bh = divisor.mantissa >> 32; /* split divisor */
942 bl = divisor.mantissa & DMASK32; /* into high and low parts */
943
944 if (firmware && (quotient->precision == fp_f)) { /* single-precision firmware? */
945 quotient->exponent = quotient->exponent + 1; /* fix exponent */
946
947 ah = (int32) (dividend.mantissa >> 32); /* split dividend */
948 dh = (int16) (bh >> 16); /* split divisor again */
949 dl = (int16) bh;
950
951 div = ah >> 2; /* ASR 2 to prevent overflow */
952
953 pq1 = div / dh; /* form first partial quotient */
954 div = ((div % dh) & ~1) << 15; /* ASR 1, move rem to upper */
955 pq2 = div / dh; /* form second partial quotient */
956
957 div = (uint16) dl << 13; /* move divisor LSB to upper, LSR 3 */
958 cq = div / dh; /* form correction quotient */
959 cp = -cq * pq1; /* and correction product */
960
961 cp = (((cp >> 14) & ~3) + (int32) pq2) << 1; /* add corr prod and 2nd partial quo */
962 quotient->mantissa = /* add 1st partial quo and align */
963 (t_uint64) (((int32) pq1 << 16) + cp) << 32;
964 }
965
966 else { /* hardware or FFP */
967 q1 = (t_uint64) (dividend.mantissa / bh); /* form 1st trial quotient */
968 r1 = dividend.mantissa % bh; /* and remainder */
969 p1 = (r1 << 24) - (bl >> 8) * q1; /* calculate correction */
970
971 while (p1 < 0) { /* correction needed? */
972 q1 = q1 - 1; /* trial quotient too large */
973 p1 = p1 + (divisor.mantissa >> 8); /* increase remainder */
974 }
975
976 q0 = (t_uint64) ((p1 << 8) / bh); /* form 2nd trial quotient */
977 r0 = (p1 << 8) % bh; /* and remainder */
978 p0 = (r0 << 24) - (bl >> 8) * q0; /* calculate correction */
979
980 while (p0 < 0) { /* correction needed? */
981 q0 = q0 - 1; /* trial quotient too large */
982 p0 = p0 + (divisor.mantissa >> 8); /* increase remainder */
983 }
984
985 q = (q1 << 32) + q0; /* sum quotient digits */
986
987 if (q1 & 0xffffffff00000000) { /* did we lose MSB? */
988 q = (q >> 1) | 0x8000000000000000; /* shift right and replace bit */
989 quotient->exponent = quotient->exponent + 1;/* bump exponent for shift */
990 }
991
992 if (q & 0x8000000000000000) /* lose normalization? */
993 q = q >> 1; /* correct */
994
995 quotient->mantissa = (t_int64) q;
996 }
997
998 if (sign)
999 complement (quotient); /* negate if required */
1000 }
1001 return;
1002 }
1003
1004
1005 /* Fix an unpacked number.
1006
1007 A floating-point value is converted to an integer. The desired precision of
1008 the result (single or double integer) must be set before calling.
1009
1010 Values less than 0.5 (i.e., with negative exponents) underflow to zero. If
1011 the value exceeds the specified integer range, the maximum integer value is
1012 returned and overflow is set. Otherwise, the floating-point value is
1013 right-shifted to zero the exponent. The result is then rounded.
1014 */
1015
1016 static uint32 fix (FPU *result, FPU operand)
1017 {
1018 uint32 overflow;
1019 t_bool bits_lost;
1020
1021 if (operand.exponent < 0) { /* value < 0.5? */
1022 result->mantissa = 0; /* result rounds to zero */
1023 overflow = 0; /* clear for underflow */
1024 }
1025
1026 else if (operand.exponent > /* value > integer size? */
1027 (int32) op_bits[result->precision]) {
1028 result->mantissa = /* return max int value */
1029 (t_uint64) int_p_max[result->precision] <<
1030 op_start[result->precision];
1031 overflow = 1; /* and set overflow */
1032 }
1033
1034 else { /* value in range */
1035 bits_lost = asr (&operand, /* shift to zero exponent */
1036 op_bits[result->precision] - operand.exponent);
1037
1038 if (operand.mantissa < 0) { /* value negative? */
1039 if (bits_lost) /* bits lost? */
1040 operand.mantissa = operand.mantissa | 1; /* include one for rounding */
1041
1042 operand.mantissa = operand.mantissa + /* round result */
1043 p_half_lsb[result->precision];
1044 }
1045
1046 result->mantissa = operand.mantissa & /* mask to precision */
1047 op_mask[result->precision];
1048 overflow = 0;
1049 }
1050
1051 result->exponent = 0; /* tidy up for integer value */
1052 return overflow;
1053 }
1054
1055
1056 /* Float an integer to an unpacked number.
1057
1058 An integer is converted to a floating-point value. The desired precision of
1059 the result must be set before calling.
1060
1061 Conversion is simply a matter of copying the integer value, setting an
1062 exponent that reflects the right-aligned position of the bits, and
1063 normalizing.
1064 */
1065
1066 static void ffloat (FPU *result, FPU operand)
1067 {
1068 result->mantissa = operand.mantissa; /* set value */
1069 result->exponent = op_bits[operand.precision]; /* set exponent */
1070 normalize (result); /* normalize */
1071 return;
1072 }
1073
1074
1075
1076 /* High-level floating-point routines. */
1077
1078
1079 /* Determine operand precisions.
1080
1081 The precisions of the operands and result are determined by decoding an
1082 operation opcode and returned to the caller. Pass NULL for both of the
1083 operands if only the result precision is wanted. Pass NULL for the result if
1084 only the operand precisions are wanted.
1085 */
1086
1087 void fp_prec (uint16 opcode, OPSIZE *operand_l, OPSIZE *operand_r, OPSIZE *result)
1088 {
1089 OPSIZE fp_size, int_size;
1090
1091 fp_size = (OPSIZE) ((opcode & 0003) + 2); /* fp_f, fp_x, fp_t, fp_e */
1092 int_size = (OPSIZE) ((opcode & 0004) >> 2); /* in_s, in_d */
1093
1094 if (operand_l && operand_r) { /* want operand precisions? */
1095 switch (opcode & 0120) { /* mask out opcode bit 5 */
1096 case 0000: /* add/mpy */
1097 case 0020: /* sub/div */
1098 *operand_l = fp_size; /* assume first op is fp */
1099
1100 if (opcode & 0004) /* operand internal? */
1101 *operand_r = fp_a; /* second op is accum */
1102 else
1103 *operand_r = fp_size; /* second op is fp */
1104 break;
1105
1106 case 0100: /* fix/accum as integer */
1107 *operand_l = fp_size; /* first op is fp */
1108 *operand_r = fp_a; /* second op is always null */
1109 break;
1110
1111 case 0120: /* flt/accum as float */
1112 *operand_l = int_size; /* first op is integer */
1113 *operand_r = fp_a; /* second op is always null */
1114 break;
1115 }
1116
1117 if (opcode & 0010) /* operand internal? */
1118 *operand_l = fp_a; /* first op is accum */
1119 }
1120
1121 if (result) /* want result precision? */
1122 if ((opcode & 0120) == 0100) /* fix? */
1123 *result = int_size; /* result is integer */
1124 else /* all others */
1125 *result = fp_size; /* result is fp */
1126
1127 return;
1128 }
1129
1130
1131 /* Floating Point Processor executor.
1132
1133 The executor simulates the MPP interface between the CPU and the FPP. The
1134 operation to be performed is specified by the supplied opcode, which conforms
1135 to the FPP hardware interface, as follows:
1136
1137 Bits Value Action
1138 ---- ----- ----------------------------------------------
1139 7 0 Exponent range is standard (+/-127)
1140 1 Exponent range is expanded (+/-511)
1141
1142 6-4 000 Add
1143 001 Subtract
1144 010 Multiply
1145 011 Divide
1146 100 Fix
1147 101 Float
1148 110 (diagnostic)
1149 111 (diagnostic)
1150
1151 3 0 Left operand is supplied
1152 1 Left operand in accumulator
1153
1154 2 0 Right operand is supplied (ADD/SUB/MPY/DIV)
1155 Single integer operation (FIX/FLT)
1156 1 Right operand in accumulator (ADD/SUB/MPY/DIV)
1157 Double integer operation (FIX/FLT)
1158
1159 1-0 00 2-word operation
1160 01 3-word operation
1161 10 4-word operation
1162 11 5-word operation
1163
1164 If the opcode specifies that the left (or right) operand is in the
1165 accumulator, then the value supplied for that parameter is not used. All
1166 results are automatically left in the accumulator. If the result is not
1167 needed externally, then NULL may be passed for the result parameter.
1168
1169 To support accumulator set/get operations under simulation, the opcode is
1170 expanded to include a special mode, indicated by bit 15 = 1. In this mode,
1171 if the result parameter is NULL, then the accumulator is set from the value
1172 passed as operand_l. If the result parameter is not null, then the
1173 accumulator value is returned as the result, and operand_l is ignored. The
1174 precision of the operation is performed as specified by the OPSIZE value
1175 passed in bits 2-0 of the opcode.
1176
1177 The function returns 1 if the operation overflows and 0 if not.
1178 */
1179
1180 uint32 fp_exec (uint16 opcode, OP *result, OP operand_l, OP operand_r)
1181 {
1182 static FPU accumulator;
1183 FPU uoperand_l, uoperand_r;
1184 OPSIZE op_l_prec, op_r_prec, rslt_prec;
1185 uint32 overflow;
1186
1187 if (opcode & SIGN) { /* accumulator mode? */
1188 rslt_prec = (OPSIZE) (opcode & 0017); /* get operation precision */
1189
1190 if (result) { /* get accumulator? */
1191 op_l_prec = accumulator.precision; /* save accum prec temp */
1192 accumulator.precision = rslt_prec; /* set desired precision */
1193 *result = pack (accumulator); /* pack accumulator */
1194 accumulator.precision = op_l_prec; /* restore correct prec */
1195 }
1196 else /* set accumulator */
1197 accumulator = unpack (operand_l, rslt_prec); /* unpack from operand */
1198
1199 return 0; /* no overflow from accum ops */
1200 }
1201
1202 fp_prec (opcode, &op_l_prec, &op_r_prec, &rslt_prec); /* calc precs from opcode */
1203
1204 if (op_l_prec == fp_a) /* left operand in accum? */
1205 uoperand_l = accumulator; /* copy it */
1206 else /* operand supplied */
1207 uoperand_l = unpack (operand_l, op_l_prec); /* unpack from parameter */
1208
1209 if (op_r_prec == fp_a) /* right operand in accum? */
1210 uoperand_r = accumulator; /* copy it */
1211 else /* operand supplied */
1212 uoperand_r = unpack (operand_r, op_r_prec); /* unpack from parameter */
1213
1214
1215 switch (opcode & 0160) { /* dispatch operation */
1216
1217 case 0000: /* add */
1218 add (&accumulator, uoperand_l, uoperand_r);
1219 break;
1220
1221 case 0020: /* subtract */
1222 complement (&uoperand_r);
1223 add (&accumulator, uoperand_l, uoperand_r);
1224 break;
1225
1226 case 0040: /* multiply */
1227 multiply (&accumulator, uoperand_l, uoperand_r);
1228 break;
1229
1230 case 0060: /* divide */
1231 divide (&accumulator, uoperand_l, uoperand_r);
1232 break;
1233
1234 case 0100: /* fix */
1235 accumulator.precision = rslt_prec;
1236 overflow = fix (&accumulator, uoperand_l);
1237
1238 if (result) /* result wanted? */
1239 *result = pack_int (accumulator.mantissa, /* pack integer */
1240 rslt_prec);
1241 return overflow;
1242
1243 case 0120: /* float */
1244 accumulator.precision = rslt_prec;
1245 ffloat (&accumulator, uoperand_l);
1246
1247 if (result) /* result wanted? */
1248 *result = pack (accumulator); /* pack FP (FLT does not round) */
1249 return 0;
1250
1251 case 0140: /* (diagnostic) */
1252 case 0160: /* (diagnostic) */
1253 return 0;
1254 }
1255
1256 if (UNIT_CPU_MODEL != UNIT_1000_F) /* firmware implementation? */
1257 accumulator.mantissa = accumulator.mantissa & /* mask to precision */
1258 op_mask[accumulator.precision];
1259
1260 normalize (&accumulator); /* normalize */
1261 overflow = roundovf (&accumulator, opcode & 0200); /* round and check for overflow */
1262
1263 if (result) /* result wanted? */
1264 *result = pack (accumulator); /* pack result */
1265
1266 return overflow;
1267 }
1268
1269
1270 /* Set or get accumulator at desired precision.
1271
1272 This function provides access to the FPP accumulator. In hardware, the
1273 accumulator may be read at a given precision by sending the FPP an opcode
1274 encoded with the desired precision and then reading words from the FPP
1275 /without/ initiating the operation, i.e., without starting the processor.
1276
1277 Under simulation, pass this function a NULL operand and the desired
1278 precision to read the accumulator. Pass a pointer to an operand and the
1279 desired precision to set the accumulator; the return value in this case is
1280 not defined.
1281 */
1282
1283 OP fp_accum (const OP *operand, OPSIZE precision)
1284 {
1285 OP result = NOP;
1286 uint16 opcode = (uint16) precision | SIGN; /* add special mode bit */
1287
1288 if (operand)
1289 fp_exec (opcode, NULL, *operand, NOP); /* set accum */
1290 else
1291 fp_exec (opcode, &result, NOP, NOP); /* get accum */
1292 return result;
1293 }
1294
1295
1296 /* Pack an unpacked floating-point number.
1297
1298 An unpacked mantissa is passed as a "packed" number with an unused exponent.
1299 The mantissa and separately-passed exponent are packed into the in-memory
1300 floating-point format. Note that all bits are significant in the mantissa
1301 (no masking is done).
1302 */
1303
1304 uint32 fp_pack (OP *result, OP mantissa, int32 exponent, OPSIZE precision)
1305 {
1306 FPU unpacked;
1307
1308 unpacked.mantissa = unpack_int (mantissa, precision); /* unpack mantissa */
1309 unpacked.exponent = exponent; /* set exponent */
1310 unpacked.precision = precision; /* set precision */
1311 *result = pack (unpacked); /* pack them */
1312 return 0;
1313 }
1314
1315
1316 /* Normalize, round, and pack an unpacked floating-point number.
1317
1318 An unpacked mantissa is passed as a "packed" number with an unused exponent.
1319 The mantissa and separately-passed exponent are normalized, rounded, and
1320 packed into the in-memory floating-point format. Note that all bits are
1321 significant in the mantissa (no masking is done).
1322 */
1323
1324 uint32 fp_nrpack (OP *result, OP mantissa, int32 exponent, OPSIZE precision)
1325 {
1326 FPU unpacked;
1327
1328 unpacked.mantissa = unpack_int (mantissa, precision); /* unpack mantissa */
1329 unpacked.exponent = exponent; /* set exponent */
1330 unpacked.precision = precision; /* set precision */
1331 return nrpack (result, unpacked, FALSE); /* norm/rnd/pack them */
1332 }
1333
1334
1335 /* Unpack a packed floating-point number.
1336
1337 A floating-point number, packed into the in-memory format, is unpacked into
1338 separate mantissa and exponent values. The unpacked mantissa is returned in
1339 a "packed" structure with an exponent of zero. Mantissa or exponent may be
1340 null if that part isn't wanted.
1341 */
1342
1343 uint32 fp_unpack (OP *mantissa, int32 *exponent, OP packed, OPSIZE precision)
1344
1345 {
1346 FPU unpacked;
1347
1348 unpacked = unpack (packed, precision); /* unpack mantissa and exponent */
1349
1350 if (exponent) /* exponent wanted? */
1351 *exponent = unpacked.exponent; /* return exponent */
1352
1353 if (mantissa) /* mantissa wanted? */
1354 *mantissa = pack_int (unpacked.mantissa, fp_t); /* return full-size mantissa */
1355 return 0;
1356 }
1357
1358
1359 /* Complement an unpacked mantissa.
1360
1361 An unpacked mantissa is passed as a "packed" number with a zero exponent.
1362 The exponent increment, i.e., either zero or one, depending on whether a
1363 renormalization was required, is returned. Note that all bits are
1364 significant in the mantissa.
1365 */
1366
1367 uint32 fp_ucom (OP *mantissa, OPSIZE precision)
1368 {
1369 FPU unpacked;
1370
1371 unpacked.mantissa = unpack_int (*mantissa, precision); /* unpack mantissa */
1372 unpacked.exponent = 0; /* clear undefined exponent */
1373 unpacked.precision = precision; /* set precision */
1374 complement (&unpacked); /* negate it */
1375 *mantissa = pack_int (unpacked.mantissa, precision); /* replace mantissa */
1376 return (uint32) unpacked.exponent; /* return exponent increment */
1377 }
1378
1379
1380 /* Complement a floating-point number. */
1381
1382 uint32 fp_pcom (OP *packed, OPSIZE precision)
1383 {
1384 FPU unpacked;
1385
1386 unpacked = unpack (*packed, precision); /* unpack the number */
1387 complement (&unpacked); /* negate it */
1388 return nrpack (packed, unpacked, FALSE); /* and norm/rnd/pack */
1389 }
1390
1391
1392 /* Truncate a floating-point number. */
1393
1394 uint32 fp_trun (OP *result, OP source, OPSIZE precision)
1395 {
1396 t_bool bits_lost;
1397 FPU unpacked;
1398 FPU one = { FP_ONEHALF, 1, 0 }; /* 0.5 * 2 ** 1 = 1.0 */
1399 OP zero = { { 0, 0, 0, 0, 0 } }; /* 0.0 */
1400 t_uint64 mask = mant_mask[precision] & ~FP_MSIGN;
1401
1402 unpacked = unpack (source, precision);
1403 if (unpacked.exponent < 0) /* number < 0.5? */
1404 *result = zero; /* return 0 */
1405 else if (unpacked.exponent >= (int32) op_bits[precision]) /* no fractional bits? */
1406 *result = source; /* already integer */
1407 else {
1408 mask = (mask >> unpacked.exponent) & mask; /* mask fractional bits */
1409 bits_lost = ((unpacked.mantissa & mask) != 0); /* flag if bits lost */
1410 unpacked.mantissa = unpacked.mantissa & ~mask; /* mask off fraction */
1411 if ((unpacked.mantissa < 0) && bits_lost) /* negative? */
1412 add (&unpacked, unpacked, one); /* truncate toward zero */
1413 nrpack (result, unpacked, FALSE); /* (overflow cannot occur) */
1414 }
1415 return 0; /* clear overflow on return */
1416 }
1417
1418
1419 /* Convert a floating-point number from one precision to another. */
1420
1421 uint32 fp_cvt (OP *result, OPSIZE source_precision, OPSIZE dest_precision)
1422 {
1423 FPU unpacked;
1424
1425 unpacked = unpack (*result, source_precision);
1426 unpacked.precision = dest_precision;
1427 return nrpack (result, unpacked, FALSE); /* norm/rnd/pack */
1428 }
1429
1430
1431 #endif /* end of int64 support */