-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhp2100_cpu_fpp.c
1471 lines (1134 loc) · 62.4 KB
/
hp2100_cpu_fpp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* hp2100_cpu_fpp.c: HP 1000 Floating-Point Processor simulator
Copyright (c) 2005-2018, J. David Bryan
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of the author shall not be used
in advertising or otherwise to promote the sale, use or other dealings in
this Software without prior written authorization from the author.
28-Jul-18 JDB Renamed source file from hp2100_fp1.c
07-Sep-17 JDB Replaced "uint16" casts with "HP_WORD" for OP assignments
16-May-16 JDB Reformulated the definitions of op_mask
24-Dec-14 JDB Added casts for explicit downward conversions
Changed fp_ucom return from uint32 to uint16
18-Mar-13 JDB Changed type of mantissa masks array to to unsigned
06-Feb-12 JDB Added missing precision on constant "one" in fp_trun
21-Jun-11 JDB Completed the comments for divide; no code changes
08-Jun-08 JDB Quieted bogus gcc warning in fp_exec
10-May-08 JDB Fixed uninitialized return in fp_accum when setting
19-Mar-08 JDB Reworked "complement" to avoid inlining bug in gcc-4.x
01-Dec-06 JDB Reworked into generalized multiple-precision ops for FPP
12-Oct-06 JDB Altered x_trun for F-Series FFP compatibility
Added F-Series ..TCM FFP helpers
Primary references:
- HP 1000 M/E/F-Series Computers Engineering and Reference Documentation
(92851-90001, March 1981)
- HP 1000 M/E/F-Series Computers Technical Reference Handbook
(5955-0282, March 1980)
- DOS/RTE Relocatable Library Reference Manual
(24998-90001, October 1981)
This module implements multiple-precision floating-point operations to
support the 1000 F-Series hardware Floating Point Processor. It employs
64-bit integer arithmetic for speed and simplicity of implementation. The
host compiler must support 64-bit integers, and the HAVE_INT64 symbol must be
defined during compilation. If this symbol is not defined, then FPP support
is not available.
HP 2100/1000 computers used a proprietary floating-point format. The 2100
had optional firmware that provided two-word floating-point add, subtract,
multiply, and divide, as well as single-integer fix and float. The 1000-M/E
provided the same two-word firmware operations as standard equipment.
Three-word extended-precision instructions for the 2100 and 1000-M/E were
provided by the optional Fast FORTRAN Processor firmware.
The 1000-F substituted a hardware floating point processor for the firmware
in previous machines. In addition to the two- and three-word formats, the
F-Series introduced a four-word double-precision format. A five-word format
that provided extra range in the exponent by unpacking it from the mantissa
was also provided, although this capability was not documented in the user
manual. In addition, the FPP improved the accuracy of floating-point
calculations, as the firmware versions sacrificed a few bits of precision to
gain speed. Consequently, operations on the F-Series may return results that
differ slightly from the same operations on the M/E-Series or the 2100.
F-Series units after date code 1920 also provided two-word double-integer
instructions in firmware, as well as double-integer fix and float operations.
Three floating-point precisions are defined, varying only in the length of
the mantissa and consequently the number of words occupied. Single-precision
format has a 23-bit mantissa occupying two words. Extended-precision format
has a 39-bit mantissa occupying three words. Double-precision format has a
55-bit mantissa occupying four words.
The floating-point formats are:
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| M | signed mantissa | M
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| signed mantissa | signed exponent | E | M + 1
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| M | signed mantissa | M
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| signed mantissa | M + 1
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| signed mantissa | signed exponent | E | M + 2
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| M | signed mantissa | M
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| signed mantissa | M + 1
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| signed mantissa | M + 2
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| signed mantissa | signed exponent | E | M + 3
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
Where:
M = Mantissa sign
E = Exponent sign
The mantissas and exponents are in twos-complement form, and the signs
indicate the polarity (0 = positive, 1 = negative). The exponent signs have
been rotated into the LSBs of the last words.
The FPP also supports a special five-word expanded-exponent format:
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| M | signed mantissa | M
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| signed mantissa | M + 1
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| signed mantissa | M + 2
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| signed mantissa | M + 3
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| signed exponent | E | M + 4
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
The exponent is a full 16-bit twos-complement value, but the allowed range is
only 10 bits, i.e., -512 to +511.
In a normalized value, the sign and MSB of the mantissa differ. Zero is
represented by all words = 0.
Internally, unpacked floating-point values are contained in a structure
having a signed 64-bit mantissa and a signed 32-bit exponent. Mantissas are
left-justified with the unused bits masked to zero. Exponents are
right-justified. The precision is indicated by the value of a structure
field.
HP terminology for the three-word floating-point format is confused. Some
documents refer to it as "double precision," while others use "extended
precision." The instruction mnemonics begin with "X" (e.g., .XADD),
suggesting the extended-precision term.
HP apparently intended that the four-word double-precision format would be
called "triple-precision," as the instruction mnemonics begin with "T" (e.g.,
".TADD" for the four-word add instruction). The source files for the
software simulations of these instructions for the M/E-Series also explicitly
refer to "triple precision math." However, the engineering documentation and
the F-Series reference manual both use the double-precision term.
This module adopts the single/extended/double terminology and uses the
initial letters of the instructions (F/X/T) to indicate the precision used.
The FPP hardware consisted of two circuit boards that interfaced to the main
CPU via the Microprogrammable Processor Port (MPP) that had been introduced
with the 1000 E-Series. One board contained argument registers and ALUs,
split into separate mantissa and exponent parts. The other contained a state
machine sequencer. FPP results were copied automatically to the argument
registers in addition to being available over the MPP, so that chained
operations could be executed from these "accumulators" without reloading.
The FPP operated independently of the CPU. An opcode, specifying one of the
six operations (add, subtract, multiply, divide, fix, or float) was sent to
the FPP, and a start command was given. Operands of appropriate precision
were then supplied to the FPP. Once the operands were received, the FPP
would execute and set a flag when the operation was complete. The result
would then be retrieved from the FPP. The floating-point instruction
firmware in the CPU initiated the desired FPP operation and handled operand
reads from and result writes to main memory.
Under simulation, "fp_exec" provides the six arithmetic operations analogous
to FPP execution. The remainder of the functions are helpers that were
provided by firmware in the 1000-F but that can reuse code needed to simulate
the FPP hardware. As with the hardware, "fp_exec" retains the last result
in an internal accumulator that may be referenced in subsequent operations.
Implementation notes:
1. This module also provides the floating-point support for the firmware
single-precision 1000-M/E base set and extended-precision FFP
instructions. Because the firmware and hardware implementations return
slightly different results, particularly with respect to round-off,
conditional checks are implemented in the arithmetic routines. In some
cases, entirely different algorithms are used to ensure fidelity with the
real machines. Functionally, this means that the 2100/1000-M/E and
1000-F floating-point diagnostics are not interchangeable, and failures
are to be expected if a diagnostic is run on the wrong machine.
*/
#include "hp2100_defs.h"
#include "hp2100_cpu.h"
#include "hp2100_cpu_fp.h"
#if defined (HAVE_INT64) /* we need int64 support */
/* Field widths */
#define IN_W_SIGN 1
#define IN_W_SMAGN 15
#define IN_W_DMAGN 31
#define FP_W_MSIGN 1
#define FP_W_FMANT 23
#define FP_W_XMANT 39
#define FP_W_TMANT 55
#define FP_W_EMANT 55
#define FP_W_EXPANDEXP 9
#define FP_W_EXP 7
#define FP_W_ESIGN 1
/* Starting bit numbers */
#define IN_V_SIGN (64 - IN_W_SIGN)
#define IN_V_SNUM (64 - IN_W_SIGN - IN_W_SMAGN)
#define IN_V_DNUM (64 - IN_W_SIGN - IN_W_DMAGN)
#define FP_V_FNUM (64 - FP_W_MSIGN - FP_W_FMANT - FP_W_EXP - FP_W_ESIGN)
#define FP_V_XNUM (64 - FP_W_MSIGN - FP_W_XMANT - FP_W_EXP - FP_W_ESIGN)
#define FP_V_TNUM (64 - FP_W_MSIGN - FP_W_TMANT - FP_W_EXP - FP_W_ESIGN)
#define FP_V_ENUM (64 - FP_W_MSIGN - FP_W_EMANT - FP_W_EXP - FP_W_ESIGN)
#define FP_V_MSIGN (64 - FP_W_MSIGN)
#define FP_V_FMANT (64 - FP_W_MSIGN - FP_W_FMANT)
#define FP_V_XMANT (64 - FP_W_MSIGN - FP_W_XMANT)
#define FP_V_TMANT (64 - FP_W_MSIGN - FP_W_TMANT)
#define FP_V_EMANT (64 - FP_W_MSIGN - FP_W_EMANT)
#define FP_V_EXP 1
#define FP_V_ESIGN 0
/* Right-aligned field masks */
#define IN_M_SIGN (((t_uint64) 1 << IN_W_SIGN) - 1)
#define IN_M_SMAGN (((t_uint64) 1 << IN_W_SMAGN) - 1)
#define IN_M_DMAGN (((t_uint64) 1 << IN_W_DMAGN) - 1)
#define FP_M_MSIGN (((t_uint64) 1 << FP_W_MSIGN) - 1)
#define FP_M_FMANT (((t_uint64) 1 << FP_W_FMANT) - 1)
#define FP_M_XMANT (((t_uint64) 1 << FP_W_XMANT) - 1)
#define FP_M_TMANT (((t_uint64) 1 << FP_W_TMANT) - 1)
#define FP_M_EMANT (((t_uint64) 1 << FP_W_EMANT) - 1)
#define FP_M_EXPANDEXP ((1 << FP_W_EXPANDEXP) - 1)
#define FP_M_EXP ((1 << FP_W_EXP) - 1)
#define FP_M_ESIGN ((1 << FP_W_ESIGN) - 1)
/* In-place field masks */
#define IN_SIGN (IN_M_SIGN << IN_V_SIGN)
#define IN_SMAGN (IN_M_SMAGN << IN_V_SNUM)
#define IN_DMAGN (IN_M_DMAGN << IN_V_DNUM)
#define FP_MSIGN (FP_M_MSIGN << FP_V_MSIGN)
#define FP_FMANT (FP_M_FMANT << FP_V_FMANT)
#define FP_XMANT (FP_M_XMANT << FP_V_XMANT)
#define FP_TMANT (FP_M_TMANT << FP_V_TMANT)
#define FP_EMANT (FP_M_EMANT << FP_V_EMANT)
#define FP_EXP (FP_M_EXP << FP_V_EXP)
#define FP_ESIGN (FP_M_ESIGN << FP_V_ESIGN)
/* In-place record masks */
#define IN_SSMAGN (IN_SIGN | IN_SMAGN)
#define IN_SDMAGN (IN_SIGN | IN_DMAGN)
#define FP_SFMANT (FP_MSIGN | FP_FMANT)
#define FP_SXMANT (FP_MSIGN | FP_XMANT)
#define FP_STMANT (FP_MSIGN | FP_TMANT)
#define FP_SEMANT (FP_MSIGN | FP_EMANT)
#define FP_SEXP (FP_ESIGN | FP_EXP)
/* Minima and maxima */
#define FP_ONEHALF ((t_int64) 1 << (FP_V_MSIGN - 1)) /* mantissa = 0.5 */
#define FP_MAXPMANT ((t_int64) FP_EMANT) /* maximum pos mantissa */
#define FP_MAXNMANT ((t_int64) FP_MSIGN) /* maximum neg mantissa */
#define FP_MAXPEXP (FP_M_EXPANDEXP) /* maximum pos expanded exponent */
#define FP_MAXNEXP (-(FP_MAXPEXP + 1)) /* maximum neg expanded exponent */
/* Floating-point helpers */
#define DENORM(x) ((((x) ^ (x) << 1) & FP_MSIGN) == 0)
#define TO_EXP(e) (int8) ((e >> FP_V_EXP & FP_M_EXP) | \
(e & FP_M_ESIGN ? ~FP_M_EXP : 0))
/* Property constants */
static const t_int64 p_half_lsb[6] = { ((t_int64) 1 << IN_V_SNUM) - 1, /* different than FP! */
((t_int64) 1 << IN_V_DNUM) - 1, /* different than FP! */
(t_int64) 1 << (FP_V_FMANT - 1),
(t_int64) 1 << (FP_V_XMANT - 1),
(t_int64) 1 << (FP_V_TMANT - 1),
(t_int64) 1 << (FP_V_EMANT - 1) };
static const t_int64 n_half_lsb[6] = { 0,
0,
((t_int64) 1 << (FP_V_FMANT - 1)) - 1,
((t_int64) 1 << (FP_V_XMANT - 1)) - 1,
((t_int64) 1 << (FP_V_TMANT - 1)) - 1,
((t_int64) 1 << (FP_V_EMANT - 1)) - 1 };
static const uint32 op_start[6] = { IN_V_SNUM,
IN_V_DNUM,
FP_V_FMANT,
FP_V_XMANT,
FP_V_TMANT,
FP_V_EMANT };
static const t_uint64 mant_mask[6] = { IN_SSMAGN,
IN_SDMAGN,
FP_SFMANT,
FP_SXMANT,
FP_STMANT,
FP_SEMANT };
static const uint32 op_bits[6] = { IN_W_SMAGN,
IN_W_DMAGN,
FP_W_FMANT + FP_W_MSIGN,
FP_W_XMANT + FP_W_MSIGN,
FP_W_TMANT + FP_W_MSIGN,
FP_W_EMANT + FP_W_MSIGN };
static const t_int64 op_mask[6] = { ~(((t_int64) 1 << IN_V_SNUM) - 1),
~(((t_int64) 1 << IN_V_DNUM) - 1),
~(((t_int64) 1 << FP_V_FNUM) - 1),
~(((t_int64) 1 << FP_V_XNUM) - 1),
~(((t_int64) 1 << FP_V_TNUM) - 1),
~(((t_int64) 1 << FP_V_ENUM) - 1) };
static const uint32 int_p_max[2] = { IN_M_SMAGN,
IN_M_DMAGN };
/* Internal unpacked floating-point representation */
typedef struct {
t_int64 mantissa;
int32 exponent;
OPSIZE precision;
} FPU;
/* Low-level helper routines */
/* Arithmetic shift right for mantissa only.
Returns TRUE if any one-bits are shifted out (for F-series only).
*/
static t_bool asr (FPU *operand, int32 shift)
{
t_uint64 mask;
t_bool bits_lost;
if (cpu_configuration & CPU_1000_F) { /* F-Series? */
mask = ((t_uint64) 1 << shift) - 1; /* mask for lost bits */
bits_lost = ((operand->mantissa & mask) != 0); /* flag if any lost */
}
else
bits_lost = FALSE;
operand->mantissa = operand->mantissa >> shift; /* mantissa is int, so ASR */
return bits_lost;
}
/* Logical shift right for mantissa and exponent.
Shifts mantissa and corrects exponent for mantissa overflow.
Returns TRUE if any one-bits are shifted out (for F-series only).
*/
static t_bool lsrx (FPU *operand, int32 shift)
{
t_uint64 mask;
t_bool bits_lost;
if (cpu_configuration & CPU_1000_F) { /* F-Series? */
mask = ((t_uint64) 1 << shift) - 1; /* mask for lost bits */
bits_lost = ((operand->mantissa & mask) != 0); /* flag if any lost */
}
else
bits_lost = FALSE;
operand->mantissa = (t_uint64) operand->mantissa >> shift; /* uint, so LSR */
operand->exponent = operand->exponent + shift; /* correct exponent */
return bits_lost;
}
/* Unpack an operand into a long integer.
Returns a left-aligned integer or mantissa. Does not mask to precision; this
should be done subsequently if desired.
*/
static t_int64 unpack_int (OP packed, OPSIZE precision)
{
uint32 i;
t_uint64 unpacked = 0;
if (precision == in_s)
unpacked = (t_uint64) packed.word << 48; /* unpack single integer */
else if (precision == in_d)
unpacked = (t_uint64) packed.dword << 32; /* unpack double integer */
else {
if (precision == fp_e) /* five word operand? */
precision = fp_t; /* only four mantissa words */
for (i = 0; i < 4; i++) /* unpack fp 2 to 4 words */
if (i < TO_COUNT (precision))
unpacked = unpacked << 16 | packed.fpk[i];
else
unpacked = unpacked << 16;
}
return (t_int64) unpacked;
}
/* Unpack a packed operand.
The packed value is split into separate mantissa and exponent variables. The
multiple words of the mantissa are concatenated into a single 64-bit signed
value, and the exponent is shifted with recovery of the sign.
*/
static FPU unpack (OP packed, OPSIZE precision)
{
FPU unpacked;
unpacked.precision = precision; /* set value's precision */
unpacked.mantissa = /* unpack and mask mantissa */
unpack_int (packed, precision) & (t_int64) mant_mask[precision];
switch (precision) {
case fp_f:
case fp_x:
case fp_t:
unpacked.exponent = /* unpack exponent from correct word */
TO_EXP (packed.fpk[(uint32) precision - 1]);
break;
case fp_e:
unpacked.exponent = /* unpack expanded exponent */
(int16) (packed.fpk[4] >> FP_V_EXP | /* rotate sign into place */
(packed.fpk[4] & 1 ? D16_SIGN : 0));
break;
case fp_a: /* no action for value in accum */
case in_s: /* integers don't use exponent */
case in_d: /* integers don't use exponent */
default:
unpacked.exponent = 0;
break;
}
return unpacked;
}
/* Pack a long integer into an operand */
static OP pack_int (t_int64 unpacked, OPSIZE precision)
{
int32 i;
OP packed;
if (precision == in_s)
packed.word = (HP_WORD) (unpacked >> 48) & D16_MASK; /* pack single integer */
else if (precision == in_d)
packed.dword = (uint32) (unpacked >> 32) & D32_MASK; /* pack double integer */
else {
if (precision == fp_e) /* five word operand? */
precision = fp_t; /* only four mantissa words */
for (i = 3; i >= 0; i--) { /* pack fp 2 to 4 words */
packed.fpk[i] = (HP_WORD) unpacked & D16_MASK;
unpacked = unpacked >> 16;
}
}
return packed;
}
/* Pack an unpacked floating-point number.
The 64-bit mantissa is split into the appropriate number of 16-bit words.
The exponent is rotated to incorporate the sign bit and merged into the
appropriate word.
*/
static OP pack (FPU unpacked)
{
OP packed;
uint8 exp;
packed = pack_int (unpacked.mantissa, unpacked.precision); /* pack mantissa */
exp = (uint8) (unpacked.exponent << FP_V_EXP | /* rotate exponent */
(unpacked.exponent < 0) << FP_V_ESIGN);
switch (unpacked.precision) { /* merge exponent into correct word */
case in_s: /* no action for integers */
case in_d:
break;
case fp_f: /* merge into last word */
case fp_x:
case fp_t:
packed.fpk[(uint32) unpacked.precision - 1] =
(packed.fpk[(uint32) unpacked.precision - 1] & ~FP_SEXP) | exp;
break;
case fp_e: /* place in separate word */
packed.fpk[4] = (HP_WORD) (unpacked.exponent << FP_V_EXP |
(unpacked.exponent < 0) << FP_V_ESIGN);
break;
case fp_a: /* no action for value in accum */
break;
}
return packed;
}
/* Normalize an unpacked floating-point number.
Floating-point numbers are in normal form if the sign bit and the MSB of the
mantissa differ. Unnormalized numbers are shifted as needed with appropriate
exponent modification.
*/
static void normalize (FPU *unpacked)
{
if (unpacked->mantissa) /* non-zero? */
while (DENORM (unpacked->mantissa)) { /* normal form? */
unpacked->exponent = unpacked->exponent - 1; /* no, so left shift */
unpacked->mantissa = unpacked->mantissa << 1;
}
else
unpacked->exponent = 0; /* clean for zero */
return;
}
/* Round an unpacked floating-point number and check for overflow.
An unpacked floating-point number is rounded by adding one-half of the LSB
value, maintaining symmetry around zero. If rounding resulted in a mantissa
overflow, the result logically is shifted to the right with an appropriate
exponent modification. Finally, the result is checked for exponent underflow
or overflow, and the appropriate approximation (zero or infinity) is
returned.
Rounding in hardware involves a special mantissa extension register that
holds three "guard" bits and one "sticky" bit. These represent the value of
bits right-shifted out the mantissa register. Under simulation, we track
such right-shifts and utilize the lower eight bits of the 64-bit mantissa
value to simulate the extension register.
Overflow depends on whether the FPP expanded-exponent form is being used
(this expands the exponent range by two bits). If overflow is detected, the
value representing infinity is dependent on whether the operation is on
behalf of the Fast FORTRAN Processor. The F-Series FPP returns positive
infinity on both positive and negative overflow for all precisions. The 2100
and M/E-Series FFPs return negative infinity on negative overflow of
extended-precision values. Single-precision overflows on these machines
always return positive infinity.
The number to be rounded must be normalized upon entry.
*/
static uint32 roundovf (FPU *unpacked, t_bool expand)
{
uint32 overflow;
t_bool sign;
sign = (unpacked->mantissa < 0); /* save mantissa sign */
if (sign) /* round and mask the number */
unpacked->mantissa =
(unpacked->mantissa + n_half_lsb[unpacked->precision]) &
(t_int64) mant_mask[unpacked->precision];
else
unpacked->mantissa =
(unpacked->mantissa + p_half_lsb[unpacked->precision]) &
(t_int64) mant_mask[unpacked->precision];
if (sign != (unpacked->mantissa < 0)) /* mantissa overflow? */
lsrx (unpacked, 1); /* correct by shifting */
else
normalize (unpacked); /* renorm may be needed */
if (unpacked->mantissa == 0) { /* result zero? */
unpacked->mantissa = 0; /* return zero */
unpacked->exponent = 0;
overflow = 0; /* with overflow clear */
}
else if (unpacked->exponent < /* result underflow? */
(FP_MAXNEXP >> (expand ? 0 : 2))) {
unpacked->mantissa = 0; /* return zero */
unpacked->exponent = 0;
overflow = 1; /* and set overflow */
}
else if (unpacked->exponent > /* result overflow? */
(FP_MAXPEXP >> (expand ? 0 : 2))) {
if (sign && /* negative value? */
(unpacked->precision == fp_x) && /* extended precision? */
!(cpu_configuration & CPU_1000_F)) { /* not F-Series? */
unpacked->mantissa = FP_MAXNMANT; /* return negative infinity */
unpacked->exponent = FP_MAXPEXP & FP_M_EXP;
}
else {
unpacked->mantissa = FP_MAXPMANT; /* return positive infinity */
unpacked->exponent = FP_MAXPEXP & FP_M_EXP;
}
overflow = 1; /* and set overflow */
}
else
overflow = 0; /* value is in range */
return overflow;
}
/* Normalize, round, and pack an unpacked floating-point number */
static uint32 nrpack (OP *packed, FPU unpacked, t_bool expand)
{
uint32 overflow;
normalize (&unpacked); /* normalize for rounding */
overflow = roundovf (&unpacked, expand); /* round and check for overflow */
*packed = pack (unpacked); /* pack result */
return overflow;
}
/* Low-level arithmetic routines */
/* Complement an unpacked number */
static void complement (FPU *result)
{
if (result->mantissa == FP_MAXNMANT) { /* maximum negative? */
result->mantissa = FP_ONEHALF; /* complement of -1.0 * 2 ^ n */
result->exponent = result->exponent + 1; /* is 0.5 * 2 ^ (n + 1) */
}
else
result->mantissa = -result->mantissa; /* negate mantissa */
return;
}
/* Add two unpacked numbers.
The mantissas are first aligned if necessary by scaling the smaller of the
two operands. If the magnitude of the difference between the exponents is
greater than the number of significant bits, then the smaller number has been
scaled to zero (swamped), and so the sum is simply the larger operand.
Otherwise, the sum is computed and checked for overflow, which has occurred
if the signs of the operands are the same but differ from that of the result.
Scaling and renormalization is performed if overflow occurred.
*/
static void add (FPU *sum, FPU augend, FPU addend)
{
int32 magn;
t_bool bits_lost;
if (augend.mantissa == 0)
*sum = addend; /* X + 0 = X */
else if (addend.mantissa == 0)
*sum = augend; /* 0 + X = X */
else {
magn = augend.exponent - addend.exponent; /* difference exponents */
if (magn > 0) { /* addend smaller? */
*sum = augend; /* preset augend */
bits_lost = asr (&addend, magn); /* align addend */
}
else { /* augend smaller? */
*sum = addend; /* preset addend */
magn = -magn; /* make difference positive */
bits_lost = asr (&augend, magn); /* align augend */
}
if (magn <= (int32) op_bits[augend.precision]) { /* value swamped? */
sum->mantissa = /* no, add mantissas */
addend.mantissa + augend.mantissa;
if (((addend.mantissa < 0) == (augend.mantissa < 0)) && /* mantissa overflow? */
((addend.mantissa < 0) != (sum->mantissa < 0))) {
bits_lost = bits_lost | lsrx (sum, 1); /* restore value */
sum->mantissa = /* restore sign */
sum-> mantissa | (addend.mantissa & FP_MSIGN);
}
if (bits_lost) /* any bits lost? */
sum->mantissa = sum->mantissa | 1; /* include one for rounding */
}
}
return;
}
/* Multiply two unpacked numbers.
The single-precision firmware (FMP) operates differently from the firmware
extended-precision (.XMPY) and the hardware multiplies of any precision.
Firmware implementations use the MPY micro-order to form 16-bit x 16-bit =
32-bit partial products and sum them to form the result. The hardware uses a
series of shifts and adds. This means that firmware FMP and hardware FMP
return slightly different values, as may be seen by attempting to run the
firmware FMP diagnostic on the FPP.
The FMP microcode calls a signed multiply routine to calculate three partial
products (all but LSB * LSB). Because the LSBs are unsigned, i.e., all bits
significant, the two MSB * LSB products are calculated using LSB/2. The
unsigned right-shift ensures a positive LSB with no significant bits lost,
because the lower eight bits are unused (they held the vacated exponent). In
order to sum the partial products, the LSB of the result of MSB * MSB is also
right-shifted before addition. Note, though, that this loses a significant
bit. After summation, the result is left-shifted to correct for the original
right shifts.
The .XMPY microcode negates both operands as necessary to produce positive
values and then forms six of the nine 16-bit x 16-bit = 32-bit unsigned
multiplications required for a full 96-bit product. Given a 48-bit
multiplicand "a1a2a3" and a 48-bit multiplier "b1b2b3", the firmware performs
these calculations to develop a 48-bit product:
a1 a2 a3
+-------+-------+-------+
b1 b2 b3
+-------+-------+-------+
_________________________
a1 * b3 [p1]
+-------+-------+
a2 * b2 [p2]
+-------+-------+
a1 * b2 [p3]
+-------+-------+
a3 * b1 [p4]
+-------+-------+
a2 * b1 [p5]
+-------+-------+
a1 * b1 [p6]
+-------+-------+
_________________________________
product
+-------+-------+-------+
The least-significant words of partial products [p1], [p2], and [p4] are used
only to develop a carry bit into the 48-bit sum. The product is complemented
as necessary to restore the sign.
The basic FPP hardware algorithm scans the multiplier and adds a shifted copy
of the multiplicand whenever a one-bit is detected. To avoid successive adds
when a string of ones is encountered (because adds are more expensive than
shifts), the hardware instead adds the multiplicand shifted by N + 1 + P and
subtracts the multiplicand shifted by P to obtain the equivalent value with a
maximum of two operations.
Instead of implementing either the .XMPY firmware algorithm or the hardware
shift-and-add algorithm directly, it is more efficient under simulation to
use 32 x 32 = 64-bit multiplications, thereby reducing the number required
from six to four (64-bit "c1c2" x 64-bit "d1d2"):
ah al
+-------+-------+
bh bl
+-------+-------+
_________________
al * bl [ll]
+-------+-------+
ah * bl [hl]
+-------+-------+
al * bh [lh]
+-------+-------+
ah * bh [hh]
+-------+-------+
_________________________________
product
+-------+-------+
However, the FMP algorithm is implemented directly from the microcode to
preserve the fidelity of the simulation, i.e., to lose the same amount
of precision.
*/
static void multiply (FPU *product, FPU multiplicand, FPU multiplier)
{
uint32 ah, al, bh, bl, sign = 0;
t_uint64 hh, hl, lh, ll, carry;
int16 ch, cl, dh, dl;
t_bool firmware;
product->precision = multiplicand.precision; /* set precision */
if ((multiplicand.mantissa == 0) || /* 0 * X = 0 */
(multiplier.mantissa == 0)) /* X * 0 = 0 */
product->mantissa = product->exponent = 0;
else {
firmware = !(cpu_configuration & CPU_1000_F); /* set firmware flag */
if (!firmware || (product->precision != fp_f)) { /* hardware? */
if (multiplicand.mantissa < 0) { /* negative? */
complement (&multiplicand); /* complement operand */
sign = ~sign; /* track sign */
}
if (multiplier.mantissa < 0) { /* negative? */
complement (&multiplier); /* complement operand */
sign = ~sign; /* track sign */
}
}
product->exponent = /* compute exponent */
multiplicand.exponent + multiplier.exponent + 1;
ah = (uint32) (multiplicand.mantissa >> 32); /* split multiplicand */
al = (uint32) (multiplicand.mantissa & D32_MASK); /* into high and low parts */
bh = (uint32) (multiplier.mantissa >> 32); /* split multiplier */
bl = (uint32) (multiplier.mantissa & D32_MASK); /* into high and low parts */
if (firmware && (product->precision == fp_f)) { /* single-precision firmware? */
ch = (int16) UPPER_WORD (ah); /* split 32-bit multiplicand */
cl = (int16) LOWER_WORD (ah) & ~LSB; /* into high and low parts */
dh = (int16) UPPER_WORD (bh); /* split 32-bit multiplier */
dl = (int16) LOWER_WORD (bh) & ~LSB; /* into high and low parts */
hh = (t_uint64) (((int32) ch * dh) & ~1); /* form cross products */
hl = (t_uint64) (((t_int64) ch * (t_int64) (uint16) dl +
(t_int64) dh * (t_int64) (uint16) cl) &
0xfffffffffffe0000);
product->mantissa = (t_uint64) (((t_int64) hh << 32) + /* sum partials */
((t_int64) hl << 16));
}
else {
hh = ((t_uint64) ah * bh); /* form four cross products */
hl = ((t_uint64) ah * bl); /* using 32 x 32 = */
lh = ((t_uint64) al * bh); /* 64-bit multiplies */
ll = ((t_uint64) al * bl);
carry = ((ll >> 32) + (uint32) hl + (uint32) lh) >> 32; /* form carry */
product->mantissa = hh + (hl >> 32) + (lh >> 32) + carry; /* sum partials */
if (sign) /* negate if required */
complement (product);
}
}
return;
}
/* Divide two unpacked numbers.
As with multiply, the single-precision firmware (FDV) operates differently
from the firmware extended-precision (.XDIV) and the hardware divisions of
any precision. Firmware implementations use the DIV micro-order to form
32-bit / 16-bit = 16-bit quotients and 16-bit remainders. These are used in
a "divide and correct" algorithm, wherein the quotient is estimated and then
corrected by comparing the dividend to the product of the quotient and the
divisor. The hardware uses a series of shifts and subtracts. This means
that firmware FDV and hardware FDV once again return slightly different
values.
Under simulation, the classic divide-and-correct method is employed, using
64-bit / 32-bit = 32-bit divisions. This method considers the 64-bit
dividend and divisor each to consist of two 32-bit "digits." The 64-bit
dividend "a1a2a3a4" is divided by the first 32-bit digit "b1b2" of the 64-bit
divisor "b1b2b3b4", yielding a 32-bit trial quotient digit and a 32-bit
remainder digit. A correction is developed by subtracting the product of the
second 32-bit digit "b3b4" of the divisor and the trial quotient digit from
the remainder (we take advantage of the eight bits vacated by the exponent
during unpacking to ensure that this product will not overflow into the sign
bit). If the remainder is negative, the trial quotient is too large, so it
is decremented, and the (full 64-bit) divisor is added to the correction.
This is repeated until the correction is non-negative, indicating that the
first quotient digit is correct. The process is then repeated using the
remainder as the dividend to develop the second 32-bit digit of the quotient.
The two digits are then concatenated for produce the final 64-bit value.
(See, "Divide-and-Correct Methods for Multiple Precision Division" by Marvin
L. Stein, Communications of the ACM, August 1964 for background.)
The microcoded single-precision division avoids overflows by right-shifting
some values, which leads to a loss of precision in the LSBs. We duplicate
the firmware algorithm here to preserve the fidelity of the simulation.
*/
static void divide (FPU *quotient, FPU dividend, FPU divisor)
{
uint32 sign = 0;
t_int64 bh, bl, r1, r0, p1, p0;
t_uint64 q, q1, q0;
t_bool firmware;
int32 ah, div, cp;
int16 dh, dl, pq1, pq2, cq;
quotient->precision = dividend.precision; /* set precision */
if (divisor.mantissa == 0) { /* division by zero? */
if (dividend.mantissa < 0)
quotient->mantissa = FP_MSIGN; /* return minus infinity */
else
quotient->mantissa = ~FP_MSIGN; /* or plus infinity */
quotient->exponent = FP_MAXPEXP + 1;
}
else if (dividend.mantissa == 0) /* dividend zero? */
quotient->mantissa = quotient->exponent = 0; /* yes; result is zero */
else {
firmware = !(cpu_configuration & CPU_1000_F); /* set firmware flag */
if (!firmware || (quotient->precision != fp_f)) { /* hardware or FFP? */
if (dividend.mantissa < 0) { /* negative? */
complement (÷nd); /* complement operand */
sign = ~sign; /* track sign */
}
if (divisor.mantissa < 0) { /* negative? */
complement (&divisor); /* complement operand */
sign = ~sign; /* track sign */
}
}
quotient->exponent = /* division subtracts exponents */
dividend.exponent - divisor.exponent;
bh = divisor.mantissa >> 32; /* split divisor */
bl = divisor.mantissa & D32_MASK; /* into high and low parts */
if (firmware && (quotient->precision == fp_f)) { /* single-precision firmware? */
quotient->exponent = quotient->exponent + 1; /* fix exponent */
ah = (int32) (dividend.mantissa >> 32); /* split dividend */
dh = (int16) (bh >> 16); /* split divisor again */
dl = (int16) bh;
div = ah >> 2; /* ASR 2 to prevent overflow */
pq1 = (int16) (div / dh); /* form first partial quotient */
div = ((div % dh) & ~1) << 15; /* ASR 1, move rem to upper */
pq2 = (int16) (div / dh); /* form second partial quotient */
div = (uint16) dl << 13; /* move divisor LSB to upper, LSR 3 */
cq = (int16) (div / dh); /* form correction quotient */
cp = -cq * pq1; /* and correction product */
cp = (((cp >> 14) & ~3) + (int32) pq2) << 1; /* add corr prod and 2nd partial quo */
quotient->mantissa = /* add 1st partial quo and align */
(t_uint64) (((int32) pq1 << 16) + cp) << 32;
}
else { /* hardware or FFP */
q1 = (t_uint64) (dividend.mantissa / bh); /* form 1st trial quotient */
r1 = dividend.mantissa % bh; /* and remainder */