-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfloat.c
More file actions
760 lines (658 loc) · 21 KB
/
float.c
File metadata and controls
760 lines (658 loc) · 21 KB
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
/*--------------------------------------------------------------------------
**
** Copyright (c) 2003-2011, Steve Peltz, Tom Hunter
**
** Name: float.c
**
** Description:
** Perform CDC 6600 floating point unit functions.
**
** Note:
** Lot of this source file has been "borrowed" from a floating point
** implementation done by Steve Peltz for his Zephyr emulator.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License version 3 as
** published by the Free Software Foundation.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License version 3 for more details.
**
** You should have received a copy of the GNU General Public License
** version 3 along with this program in file "license-gpl-3.0.txt".
** If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
**
**--------------------------------------------------------------------------
*/
/*
** -------------
** Include Files
** -------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "const.h"
#include "types.h"
#include "proto.h"
/*
** -----------------
** Private Constants
** -----------------
*/
#define ID ((CpWord)01777)
#define OR ((CpWord)03777)
#define IR(x) (((x) & ID) != ID)
#define OVFL(s) ((OR ^ (s >> 48)) << 48)
#define IND (ID << 48)
/*
** -----------------------
** Private Macro Functions
** -----------------------
*/
/*
** The following code fragments are used throughout:
**
** sign = SignX(v, 60); extract sign bit
** v ^= sign; make absolute
** exponent = (v >> 48); extract exponent
**
** exponent -= 02000; un-bias exponent
** exponent -= (exponent >> 11); make two's complement
**
** exponent += 02000 + (exponent >> 11); make one's comp
**
** (if exponent is still less than zero (two's complement) then
** it is an underflow condition)
*/
#define SignX(v, bit) (((v) & ((CpWord)1 << ((bit) - 1))) == 0 ? 0 : Mask60)
/*
** -----------------------------------------
** Private Typedef and Structure Definitions
** -----------------------------------------
*/
/*
** ---------------------------
** Private Function Prototypes
** ---------------------------
*/
/*
** ----------------
** Public Variables
** ----------------
*/
/*
** -----------------
** Private Variables
** -----------------
*/
/*
**--------------------------------------------------------------------------
**
** Public Functions
**
**--------------------------------------------------------------------------
*/
/*
** Visual C++ 6.0 Enterprise Edition appears to have a bug in its optimizer
** which results in incorrect maths. The line affected by this bug is marked
** with a comment "line triggers Visual C++ OPTIMIZER BUG". To avoid the problem
** optimization is turned off for the whole file.
*/
#if defined(_WIN32)
#pragma optimize("", off)
#endif
/*--------------------------------------------------------------------------
** Purpose: Floating point addition.
** Extract both signs, make absolute, extract exponents,
** check for special cases, shift smaller exponent to the right,
** do a 96 bit signed one's complement add, shift right by one
** if overflow.
** Return upper 48 bits and adjusted exponent for
**
** Parameters: Name Description.
** v1 First operand
** v2 Second operand
** doRound TRUE if rounding required, FALSE otherwise.
** doDouble TRUE if double precision required, FALSE otherwise.
**
** Returns: Upper 48 bits and adjusted exponent for single precision.
** Lower 48 bits and adjusted exponent for double precision.
**
**------------------------------------------------------------------------*/
CpWord floatAdd(CpWord v1, CpWord v2, bool doRound, bool doDouble)
{
CpWord sign1;
CpWord sign2;
int exponent1;
int exponent2;
int shift;
CpWord upper; /* upper half of 98 bit adder register - 50 bits */
CpWord lower; /* bottom half is 48 bits */
CpWord round = 0;
CpWord result;
sign1 = SignX(v1, 60);
sign2 = SignX(v2, 60);
v1 ^= sign1;
v2 ^= sign2;
exponent1 = (int)(v1 >> 48);
exponent2 = (int)(v2 >> 48);
if (!IR(exponent1))
{
if ((exponent1 == ID) || (exponent2 == ID) || ((exponent2 == OR) && (sign1 != sign2)))
{
return IND;
}
return OVFL(sign1);
}
if (!IR(exponent2))
{
if (exponent2 == ID)
{
return IND;
}
return OVFL(sign2);
}
exponent1 -= 02000;
exponent2 -= 02000;
exponent1 -= (exponent1 >> 11);
exponent2 -= (exponent2 >> 11);
/*
** pre-set round bit - second rounding bit is inserted if
** both values are normalized or the signs are different.
** bit 47 is the rounding bit. calculate this part here
** even though it won't be needed if shift is 48 or more,
** so the variables can be over-used.
**
** if both variables are normalized (both have bit 47 set), then
** the round bit will be set. if the signs are different, all 60
** bits will be set, so the value must be masked later on if it is
** used.
*/
if (doRound)
{
round = (v1 & v2) | (sign1 ^ sign2);
}
sign1 >>= 10;
sign2 >>= 10;
/*
** move value with larger exponent to 98 bit "register", and
** leave smaller value in v1, larger exponent in exponent1,
** and calculate shift count. note that the lower 48 bits of
** the first value are extended with the sign.
**
** value with larger exponent always has a rounding bit
** inserted after the least significant bit (top bit of
** lower part of register).
*/
if (exponent1 > exponent2)
{
upper = (v1 & Mask48) ^ sign1;
v1 = v2 & Mask48;
lower = sign1 >> 2;
if (doRound)
{
lower = Sign48 ^ lower; /* rounding bit */
}
sign1 = sign2;
shift = exponent1 - exponent2;
}
else
{
upper = (v2 & Mask48) ^ sign2;
v1 &= Mask48;
lower = sign2 >> 2;
if (doRound)
{
lower = Sign48 ^ lower; /* rounding bit */
}
shift = exponent2 - exponent1;
exponent1 = exponent2;
}
/*
** sign1 is 50 bit sign extension of second (smaller exponent) value;
** make sign2 be the 48 bit version for the lower half.
*/
sign2 = sign1 >> 2;
/*
** three possible choices - if shift count is less than 48 bits,
** add to both upper and lower (or just upper if shift count is
** zero, but that gets taken care of automatically); if shift count
** is 48 through 95, add to just the lower half; else just add in
** sign bits (result is shifted off the end of the register).
**
** if shift is less than 48, insert shifted rounding bit.
*/
if (shift < 48)
{
upper += (v1 >> shift) ^ sign1;
if (doRound)
{
round = (round & Sign48) >> shift;
lower += (((v1 << (48 - shift)) & Mask48) | round) ^ sign2;
}
else
{
lower += ((v1 << (48 - shift)) & Mask48) ^ sign2;
}
}
else if (shift < 96)
{
upper += sign1;
lower += (v1 >> (shift - 48)) ^ sign2;
}
else
{
upper += sign1;
lower += sign2;
}
/*
** carry-out from lower to upper, mask off overflow, add one if
** adding one would cause an end-around carry, then carry-out
** again from lower to upper. this is the same algorithm as the
** 18 or 60 bit one's complement add uses, and adjusts for -0
** the same way the cyber does (cyber actually inverts the second
** operand and subtracts, rather than adds)
*/
upper += lower >> 48;
lower &= Mask48;
lower += (upper + ((lower + 1) >> 48)) >> 50; // line triggers Visual C++ OPTIMIZER BUG
upper += lower >> 48;
upper &= Mask50;
lower &= Mask48;
/*
** get sign of result, and make result absolute
*/
sign1 = SignX(upper, 50);
upper ^= (sign1 >> 10);
lower ^= (sign1 >> 12);
if (doDouble)
{
if ((features & Has175Float) != 0)
{
/*
** check if exponent will be out of range after subtracting 48 (if
** resulting exponent is less than -1777 (octal), a positive zero is
** returned).
*/
if (exponent1 < -01717)
{
return(0);
}
}
/*
** post-normalize - shift bottom bit of upper half to upper
** bit of bottom half (instead of shifting the upper half, as
** the other add routines do)
*/
if (upper >> 48)
{
lower = ((upper & 1) << 47) | (lower >> 1);
exponent1 += 1;
}
/*
** check if exponent will be out of range after subtracting 48 (if
** resulting exponent is -1777 (octal), the result is an underflow,
** but the significant digits are still returned; if it is less than
** that, a positive zero is returned). -1777 is 0000 in one's complement
** biased form.
*/
if (exponent1 < -01717)
{
return(0);
}
exponent1 -= 48;
exponent1 += 02000 + (exponent1 >> 11);
result = ((((CpWord) exponent1) << 48) | (lower & Mask48)) ^ sign1;
}
else
{
/*
** post-normalize if necessary
*/
if (upper >> 48)
{
upper >>= 1;
exponent1 += 1;
}
exponent1 += 02000 + (exponent1 >> 11);
result = ((((CpWord) exponent1) << 48) | upper) ^ sign1;
}
return(result);
}
/*--------------------------------------------------------------------------
** Purpose: Floating multiply
** do four 24 bit multiplies, combine, offset exponent and return
** upper 48 bits of result. does a one bit post-normalize if
** necessary if both values were normalized to begin with.
**
** Double precision multiply. returns the bottom half of the 96
** bit product. also used for integer multiply by checking for
** both exponents zero and one or both values not normalized.
**
** Rounding multiply is almost identical to floating multiply,
** except that a single rounding bit is added to the result in
** bit 46 of the 96 bit product.
**
** Parameters: Name Description.
** v1 First operand
** v2 Second operand
** doRound TRUE if rounding required, FALSE otherwise.
** doDouble TRUE if double precision required, FALSE otherwise.
**
** Returns: Upper 48 bits and adjusted exponent for single precision.
** Lower 48 bits and adjusted exponent for double precision.
**
**------------------------------------------------------------------------*/
CpWord floatMultiply(CpWord v1, CpWord v2, bool doRound, bool doDouble)
{
CpWord sign1;
CpWord sign2;
int exponent1;
int exponent2;
int norm; /* flag for post-normalize */
CpWord upper; /* upper 48 bits of product */
CpWord middle; /* middle cross-product */
CpWord lower; /* lower 48 bits of product */
sign1 = SignX(v1, 60);
sign2 = SignX(v2, 60);
v1 ^= sign1;
v2 ^= sign2;
/*
** get sign of result
*/
sign1 ^= sign2;
exponent1 = (int)(v1 >> 48);
exponent2 = (int)(v2 >> 48);
if (!IR(exponent1))
{
if ((exponent1 == ID) || (exponent2 == ID) || !exponent2)
{
return IND;
}
return OVFL(sign1);
}
if (!IR(exponent2))
{
if ((exponent2 == ID) || !exponent1)
{
return IND;
}
return OVFL(sign1);
}
v1 &= Mask48;
v2 &= Mask48;
/*
** get the post-normalize flag
*/
norm = (int)((v1 & v2) >> 47);
/*
** form middle cross-product, upper and lower product, and add them
** all together, with a carry from lower to upper.
*/
middle = (v1 & Mask24) * (v2 >> 24);
if (doRound)
{
/*
** rounding bit (46) is bit 22 in the middle cross-product.
*/
middle += ((CpWord)1 << 22);
}
middle += (v1 >> 24) * (v2 & Mask24);
lower = (v1 & Mask24) * (v2 & Mask24);
lower += (middle & Mask24) << 24;
upper = (v1 >> 24) * (v2 >> 24);
upper += (middle >> 24) + (lower >> 48);
/*
** do an integer multiply if one or both values are not normalized
** and both exponents are zero (this is really only specified for
** the double precision multiply, but the same check is done for
** floating and rounding as well - this is necessary to make -0
** results come out correctly.
*/
if (doDouble)
{
if (!(norm | exponent1 | exponent2))
{
return (lower & Mask48) ^ sign1;
}
if (!(exponent1 && exponent2))
{
return 0;
}
upper = (v1 >> 24) * (v2 >> 24);
upper += (middle >> 24) + (lower >> 48);
exponent1 -= 02000;
exponent2 -= 02000;
exponent1 -= (exponent1 >> 11);
exponent2 -= (exponent2 >> 11);
exponent1 += exponent2;
if ((features & Has175Float) != 0)
{
if (exponent1 > 01777)
{
return OVFL(sign1);
}
if (exponent1 <= -01777)
{
return(0);
}
}
if (norm && !(upper >> 47))
{
lower <<= 1;
exponent1 -= 1;
}
/*
** since the bottom half is returned, exponent doesn't need to be
** offset by 48.
*/
if (exponent1 > 01777)
{
return OVFL(sign1);
}
exponent1 += 02000 + (exponent1 >> 11);
if (exponent1 < 0)
{
return 0;
}
return ((((CpWord) exponent1) << 48) | (lower & Mask48)) ^ sign1;
}
if (!(norm | exponent1 | exponent2))
{
return upper ^ sign1;
}
/*
** if not an integer multiply and one or both exponents are zero
** (underflow), return positive zero.
*/
if (!(exponent1 && exponent2))
return 0;
exponent1 -= 02000;
exponent2 -= 02000;
exponent1 -= (exponent1 >> 11);
exponent2 -= (exponent2 >> 11);
exponent1 += exponent2; /* add exponents together for multiply */
if ((features & Has175Float) != 0)
{
if ((exponent1 + 48) > 01777)
{
return OVFL(sign1);
}
if ((exponent1 + 48) <= -01777)
{
return(0);
}
}
/*
** post normalize if necessary
*/
if (norm && !(upper >> 47))
{
upper = (upper << 1) | ((lower >> 47) & 1);
exponent1 -= 1;
}
/*
** offset exponent by 48, since we ended up with a 96 bit product
** and are only returning the upper 48 bits. check for overflow
** values (biased values less than 0000 or greater than 3777).
*/
if (exponent1 > 01717)
{
return OVFL(sign1);
}
exponent1 += 48;
exponent1 += 02000 + (exponent1 >> 11);
if (exponent1 < 0)
{
return 0;
}
return ((((CpWord) exponent1) << 48) | upper) ^ sign1;
}
/*--------------------------------------------------------------------------
** Purpose: Floating divide implemented using shift and subtract.
**
** Rounding divide is identical to floating divide, except
** that as the dividend gets shifted in, 1/3 is shifted in
** (1/3 is alternating bits: 25252525... octal).
**
** Parameters: Name Description.
** v1 First operand
** v2 Second operand
** doRound TRUE if rounding required, FALSE otherwise.
**
** Returns: Upper 48 bits and adjusted exponent for single precision.
** Lower 48 bits and adjusted exponent for double precision.
**
**------------------------------------------------------------------------*/
CpWord floatDivide(CpWord v1, CpWord v2, bool doRound)
{
CpWord sign1;
CpWord sign2;
int round = 0;
int exponent1;
int exponent2;
sign1 = SignX(v1, 60);
sign2 = SignX(v2, 60);
v1 ^= sign1;
v2 ^= sign2;
exponent1 = (int)(v1 >> 48);
exponent2 = (int)(v2 >> 48);
sign1 ^= sign2;
/*
** indefinite divided by anything is indefinite
** anything divided by indefinite is indefinite
** infinite divided by infinite is indefinite
** infinite divided by anything else is infinite
*/
if (!IR(exponent1))
{
if ((exponent1 == ID) || (exponent2 == ID) || (exponent2 == OR))
{
return IND;
}
return OVFL(sign1);
}
if (!IR(exponent2))
{
if (exponent2 == ID)
{
return IND;
}
return 0;
}
/*
** exponent = 0 is taken to mean value = 0
** if non-zero divided by zero, return overflow
** if zero divided by non-zero, return positive zero
** if zero divided by zero, return positive indefinite
*/
if (!(exponent1 && exponent2))
{
if (exponent1)
{
return OVFL(sign1);
}
if (exponent2)
{
return 0;
}
return IND;
}
v1 &= Mask48;
v2 &= Mask48;
/*
** if divisor is less than half of dividend, return indefinite - divisor
** should be normalized, but it isn't checked for explicitly.
*/
if (v1 >= (v2 << 1))
return IND;
exponent1 -= 02000;
exponent2 -= 02000;
exponent1 -= (exponent1 >> 11);
exponent2 -= (exponent2 >> 11);
/*
** divide exponents by subtracting
*/
exponent1 -= exponent2;
/*
** pre-normalize if necessary. this is guaranteed to make v1 >= v2
** due to earlier check.
*/
if (v1 < v2)
{
v1 <<= 1;
exponent1 -= 1;
if (doRound)
{
round = 1; /* round bit (of zero) got shifted in */
}
}
/*
** figure out final exponent and check for overflow before
** actually doing the divides
*/
if (exponent1 > 02056) /* 1777 + 0057 (octal) */
{
return OVFL(sign1);
}
exponent1 -= 47;
exponent1 += 02000 + (exponent1 >> 11);
if (exponent1 < 0)
{
return 0;
}
sign2 = 0; /* used to accumulate the result */
/*
** main divide loop - shift and subtract for 48 bits
*/
for (exponent2 = 47; exponent2 >= 0; exponent2--)
{
sign2 <<= 1;
if (v1 >= v2)
{
v1 -= v2;
sign2 += 1;
}
if (doRound)
{
v1 = (v1 << 1) | round; /* shift in rounding bit */
round = 1 - round; /* toggle round back and forth */
}
else
{
v1 <<= 1;
}
}
return ((((CpWord) exponent1) << 48) | sign2) ^ sign1;
}
/*
**--------------------------------------------------------------------------
**
** Private Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------- End Of File ------------------------------*/