-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfparithmetic.c
More file actions
1980 lines (1695 loc) · 49 KB
/
fparithmetic.c
File metadata and controls
1980 lines (1695 loc) · 49 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
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
/*
*
* fparithmetic.c
*
* Arithmetic support routines.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fenv.h>
#include "t4debug.h"
#include "arithmetic.h"
#include "fparithmetic.h"
#pragma STDC FENV_ACCESS ON
#define T4_CRTHACKS 1
#ifdef _MSC_VER
#define __INT32_MAX__ INT32_MAX
#endif
#if defined(linux) && defined(__PPC__)
#define __INT32_MAX__ __INT_MAX__
#endif
#define __INT32_MIN__ ((-__INT32_MAX__)-1)
#define FE_T800_EXCEPT (FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW)
#define REAL64_SIGN 0x8000000000000000ULL
#define REAL64_EXP 0x7FF0000000000000ULL
#define REAL64_FRAC 0x000FFFFFFFFFFFFFULL
#define REAL64_UNDEFINED ((uint64_t)0x7ff2bad2bad2bad2ULL)
#define INT64(x) ((int64_t)(x))
#define INT32(x) ((int32_t)(x))
#define REAL32_SIGN 0x80000000UL
#define REAL32_EXP 0x7F800000UL
#define REAL32_FRAC 0x007FFFFFUL
#define REAL32_UNDEFINED ((uint32_t)0x7f82bad2UL)
#define NAN32_DivZeroByZero ((uint32_t)0x7fc00000UL)
#define NAN32_DivInfByInf ((uint32_t)0x7fa00000UL)
#define NAN32_MulZeroByInf ((uint32_t)0x7f900000UL)
#define NAN32_AddOppositeInf ((uint32_t)0x7f880000UL)
#define NAN32_SubSameInf ((uint32_t)0x7f880000UL)
#define NAN32_NegativeSqrt ((uint32_t)0x7f840000UL)
#define NAN32_ConversionNaN ((uint32_t)0x7f820000UL)
#define NAN32_RemFromInf ((uint32_t)0x7f804000UL)
#define NAN32_RemByZero ((uint32_t)0x7f802000UL)
#define NAN64_DivZeroByZero ((uint64_t)0x7ff8000000000000ULL)
#define NAN64_DivInfByInf ((uint64_t)0x7ff4000000000000ULL)
#define NAN64_MulZeroByInf ((uint64_t)0x7ff2000000000000ULL)
#define NAN64_AddOppositeInf ((uint64_t)0x7ff1000000000000ULL)
#define NAN64_SubSameInf ((uint64_t)0x7ff1000000000000ULL)
#define NAN64_NegativeSqrt ((uint64_t)0x7ff0800000000000ULL)
#define NAN64_ConversionNaN ((uint64_t)0x7ff0400000000000ULL)
#define NAN64_RemFromInf ((uint64_t)0x7ff0080000000000ULL)
#define NAN64_RemByZero ((uint64_t)0x7ff0040000000000ULL)
#define NAN64_RemInvalidQuot ((uint64_t)0x7ff0020000000000ULL)
#undef TRUE
#undef FALSE
#define FALSE 0x0000
#define TRUE 0x0001
const char *RMODE = "ZNPM";
fpreal32_t Zero;
fpreal32_t MinusZero;
fpreal32_t RMostNeg;
fpreal32_t RMostPos;
fpreal32_t RInf;
fpreal32_t RMinusInf;
fpreal32_t RUndefined;
fpreal32_t RInt32Min;
fpreal32_t RInt32Max;
fpreal32_t RInt64Min;
fpreal32_t RInt64Max;
fpreal64_t DZero;
fpreal64_t DMinusZero;
fpreal64_t DMostNeg;
fpreal64_t DMostPos;
fpreal64_t DInf;
fpreal64_t DMinusInf;
fpreal64_t DUndefined;
fpreal64_t DInt32Min;
fpreal64_t DInt32Max;
fpreal64_t DInt64Min;
fpreal64_t DInt64Max;
/*
* T800 FPU Not-a-numbers.
*/
static fpreal32_t DivZeroByZero_NaN;
static fpreal32_t DivInfByInf_NaN;
static fpreal32_t MulZeroByInf_NaN;
static fpreal32_t AddOppositeInf_NaN;
static fpreal32_t SubSameInf_NaN;
static fpreal32_t NegativeSqrt_NaN;
static fpreal32_t Conversion_NaN;
static fpreal32_t RemFromInf_NaN;
static fpreal32_t RemByZero_NaN;
static fpreal64_t DDivZeroByZero_NaN;
static fpreal64_t DDivInfByInf_NaN;
static fpreal64_t DMulZeroByInf_NaN;
static fpreal64_t DAddOppositeInf_NaN;
static fpreal64_t DSubSameInf_NaN;
static fpreal64_t DNegativeSqrt_NaN;
static fpreal64_t DConversion_NaN;
static fpreal64_t DRemFromInf_NaN;
static fpreal64_t DRemByZero_NaN;
static fpreal64_t DRemInvalidQuot_NaN;
extern int emudebug;
extern int FP_Error;
extern int RoundingMode;
extern void fp_pushdb (fpreal64_t);
extern void fp_pushsn (fpreal32_t);
extern void handler (int);
fpreal32_t BargSN, AargSN, ResultSN;
fpreal64_t BargDB, AargDB, ResultDB;
void db_dump (char*, fpreal64_t);
void sn_dump (char*, fpreal32_t);
void sn_setbits (REAL32*, uint32_t);
void db_setbits (REAL64*, uint64_t);
#ifdef FPA_STANDALONE
int emudebug = 0;
int RoundingMode;
void fp_pushdb (fpreal64_t x) { return; }
void fp_pushsn (fpreal32_t x) { return; }
void handler (int x) { exit (1); }
#endif
void fp_init (void)
{
fenv_t fpenv;
int rc;
rc = feholdexcept (&fpenv);
if (rc)
printf ("-W-EMUFPU: Warning - cannot initialize FP environment!\n");
fp_setrounding ("fpinit", ROUND_N);
/* REAL32 constants. */
Zero.bits = ZERO32;
MinusZero.bits = (REAL32_SIGN | Zero.bits);
RInf.bits = REAL32_EXP;
RMinusInf.bits = (REAL32_SIGN | RInf.bits);
RMostPos.bits = RInf.bits - 1;
RMostNeg.bits = RMinusInf.bits - 1;
RUndefined.bits = REAL32_UNDEFINED;
RInt32Min.bits = 0xcf000000UL;
RInt32Max.bits = 0x4effffffUL;
RInt64Min.bits = 0xdf000000UL;
RInt64Max.bits = 0x5effffffUL;
DivZeroByZero_NaN.bits = NAN32_DivZeroByZero;
DivInfByInf_NaN.bits = NAN32_DivInfByInf;
MulZeroByInf_NaN.bits = NAN32_MulZeroByInf;
AddOppositeInf_NaN.bits = NAN32_AddOppositeInf;
SubSameInf_NaN.bits = NAN32_SubSameInf;
NegativeSqrt_NaN.bits = NAN32_NegativeSqrt;
Conversion_NaN.bits = NAN32_ConversionNaN;
RemFromInf_NaN.bits = NAN32_RemFromInf;
RemByZero_NaN.bits = NAN32_RemByZero;
/* REAL64 constants. */
DZero.bits = ZERO64;
DMinusZero.bits = (REAL64_SIGN | DZero.bits);
DInf.bits = REAL64_EXP;
DMinusInf.bits = (REAL64_SIGN | DInf.bits);
DMostPos.bits = DInf.bits - 1;
DMostNeg.bits = DMinusInf.bits - 1;
DUndefined.bits = REAL64_UNDEFINED;
DInt32Min.bits = 0xc1e0000000000000ULL;
DInt32Max.bits = 0x41dfffffffffffffULL;
DInt64Min.bits = 0xc3e0000000000000ULL;
DInt64Max.bits = 0x43dfffffffffffffULL;
DDivZeroByZero_NaN.bits = NAN64_DivZeroByZero;
DDivInfByInf_NaN.bits = NAN64_DivInfByInf;
DMulZeroByInf_NaN.bits = NAN64_MulZeroByInf;
DAddOppositeInf_NaN.bits = NAN64_AddOppositeInf;
DSubSameInf_NaN.bits = NAN64_SubSameInf;
DNegativeSqrt_NaN.bits = NAN64_NegativeSqrt;
DConversion_NaN.bits = NAN64_ConversionNaN;
DRemFromInf_NaN.bits = NAN64_RemFromInf;
DRemByZero_NaN.bits = NAN64_RemByZero;
DRemInvalidQuot_NaN.bits = NAN64_RemInvalidQuot;
fp_clrexcept ();
}
void db_setbits (REAL64 *ptr, uint64_t bits)
{
uint64_t *rawPtr;
rawPtr = (uint64_t *) ptr;
*rawPtr = bits;
}
void sn_setbits (REAL32 *ptr, uint32_t bits)
{
uint32_t *rawPtr;
rawPtr = (uint32_t *) ptr;
*rawPtr = bits;
}
void fp_setrounding (const char *where, int mode)
{
int fpu_mode;
int rc;
fpu_mode = FE_TONEAREST;
switch (mode)
{
case ROUND_P:
fpu_mode = FE_UPWARD;
break;
case ROUND_M:
fpu_mode = FE_DOWNWARD;
break;
case ROUND_Z:
fpu_mode = FE_TOWARDZERO;
break;
case ROUND_N:
fpu_mode = FE_TONEAREST;
break;
default :
printf ("-E-EMUFPU: Error - unknown rounding mode! (%d)\n", mode);
handler (-1);
}
RoundingMode = mode;
EMUDBG3 ("-I-EMUFPU: RoundingMode set to '%c' (%s).\n", RMODE[mode - 1], where);
rc = fesetround (fpu_mode);
if (rc != 0)
printf ("-W-EMUFPU: Warning - cannot set rounding mode! (%d)\n", mode);
}
int db_sign (uint64_t fpbits)
{
return (REAL64_SIGN & fpbits) ? 1 : 0;
}
int fp_signdb (fpreal64_t fp)
{
return db_sign (fp.bits);
}
int db_exp (uint64_t fpbits)
{
return (REAL64_EXP & fpbits) >> 52;
}
int fp_expdb (fpreal64_t fp)
{
return db_exp (fp.bits);
}
uint64_t db_frac (uint64_t fpbits)
{
return (REAL64_FRAC & fpbits);
}
uint64_t fp_fracdb (fpreal64_t fp)
{
return db_frac (fp.bits);
}
uint64_t db_expfrac (uint64_t fpbits)
{
return ((REAL64_EXP | REAL64_FRAC) & fpbits);
}
uint64_t fp_expfracdb (fpreal64_t fp)
{
return db_expfrac (fp.bits);
}
int db_inf (uint64_t fpbits)
{
return (2047 == db_exp (fpbits)) && (0LL == db_frac (fpbits));
}
int fp_infdb (fpreal64_t fp)
{
return db_inf (fp.bits);
}
int db_nan (uint64_t fpbits)
{
return (2047 == db_exp (fpbits)) && (0LL != db_frac (fpbits));
}
int fp_nandb(fpreal64_t fp)
{
return db_nan (fp.bits);
}
int db_notfinite (uint64_t fpbits)
{
return (2047 == db_exp (fpbits));
}
int fp_notfinitedb(fpreal64_t fp)
{
return db_notfinite (fp.bits);
}
int db_zero (uint64_t fpbits)
{
/* Minus zero is the same as plus zero. */
return ((fpbits & ~REAL64_SIGN) == ZERO64);
}
int fp_zerodb(fpreal64_t fp)
{
return db_zero (fp.bits);
}
int sn_sign (uint32_t fpbits)
{
return (REAL32_SIGN & fpbits) ? 1 : 0;
}
int fp_signsn(fpreal32_t fp)
{
return sn_sign (fp.bits);
}
int sn_exp (uint32_t fpbits)
{
return (REAL32_EXP & fpbits) >> 23;
}
int fp_expsn(fpreal32_t fp)
{
return sn_exp (fp.bits);
}
uint32_t sn_frac (uint32_t fpbits)
{
return (REAL32_FRAC & fpbits);
}
uint32_t fp_fracsn(fpreal32_t fp)
{
return sn_frac (fp.bits);
}
uint32_t sn_expfrac (uint32_t fpbits)
{
return ((REAL32_EXP | REAL32_FRAC) & fpbits);
}
uint32_t fp_expfracsn (fpreal32_t fp)
{
return sn_expfrac (fp.bits);
}
int sn_inf (uint32_t fpbits)
{
return (255 == sn_exp (fpbits)) && (0L == sn_frac (fpbits));
}
int fp_infsn(fpreal32_t fp)
{
return sn_inf (fp.bits);
}
int sn_nan (uint32_t fpbits)
{
return (255 == sn_exp (fpbits)) && (0L != sn_frac (fpbits));
}
int fp_nansn(fpreal32_t fp)
{
return sn_nan (fp.bits);
}
int sn_notfinite (uint32_t fpbits)
{
return (255 == sn_exp (fpbits));
}
int fp_notfinitesn(fpreal32_t fp)
{
return sn_notfinite (fp.bits);
}
int sn_zero (uint32_t fpbits)
{
/* Minus zero is the same as plus zero. */
return ((fpbits & ~REAL32_SIGN) == ZERO32);
}
int fp_zerosn(fpreal32_t fp)
{
return sn_zero (fp.bits);
}
void db_dump (char *msg, fpreal64_t x)
{
printf ("%s: fp = %lf (#%016llX, %d,%04d,%014llX)\n",
msg, x.fp, x.bits,
fp_signdb(x) ? 1 : 0,
fp_expdb(x),
fp_fracdb(x));
}
void sn_dump (char *msg, fpreal32_t x)
{
printf ("%s: fp = %f (#%08X, %d,%03d,%06X)\n",
msg, x.fp, x.bits,
fp_signsn(x) ? 1 : 0,
fp_expsn(x),
fp_fracsn(x));
}
/* Clear native FPU exceptions. */
void fp_clrexcept (void)
{
int rc;
rc = feclearexcept (FE_ALL_EXCEPT);
if (rc)
printf ("-W-EMUFPU: Warning - cannot clear native FPU exceptions!\n");
}
/* Check native FPU exceptions. */
void fp_chkexcept (char *msg)
{
int exc;
return; /* XXX */
exc = fetestexcept (FE_T800_EXCEPT);
if (exc)
{
printf ("-W-EMUFPU: Warning - FPU exception flags are set! (%s)\n", msg);
}
}
#define setclear(x) (x ? "Set" : "Clear")
/* Translate native FPU exception to FP_Error. */
void translate_except (int excp)
{
#if 0
printf ("-W-EMUFPU: FPExceptFlag = %d\n", excp);
printf ("-W-EMUFPU: Invalid %s\n", setclear (excp & FE_INVALID));
printf ("-W-EMUFPU: DivideByZero %s\n", setclear (excp & FE_DIVBYZERO));
printf ("-W-EMUFPU: Overflow %s\n", setclear (excp & FE_OVERFLOW));
printf ("-W-EMUFPU: Underflow %s\n", setclear (excp & FE_UNDERFLOW));
printf ("-W-EMUFPU: Inexact %s\n", setclear (excp & FE_INEXACT));
#endif
if (excp & FE_INVALID)
FP_Error = TRUE;
else if (excp & FE_DIVBYZERO)
FP_Error = TRUE;
else if (excp & FE_OVERFLOW)
FP_Error = TRUE;
}
/* Synchronize native FPU exceptions and FP_Error. */
void fp_syncexcept (void)
{
int excp;
if (FP_Error) /* FP_Error already set ? */
return; /* Do nothing. */
excp = fetestexcept (FE_T800_EXCEPT); /* Get native FPU exceptions. */
if (0 == excp) /* No exceptions ? */
return; /* Do nothing. */
translate_except (excp); /* Translate native exceptions to FP_Error */
fp_clrexcept (); /* Clear exceptions. */
}
void db_check_except (void)
{
int excp;
return; /* XXX */
excp = fetestexcept (FE_T800_EXCEPT);
if (0 == excp)
return;
#if 0
printf ("-W-EMUFPU: Native FPU exception!\n");
printf ("-W-EMUFPU: Operation arguments.\n");
db_dump ("-W-EMUFPU: Barg", BargDB);
db_dump ("-W-EMUFPU: Aarg", AargDB);
db_dump ("-W-EMUFPU: Result", ResultDB);
#endif
translate_except (excp);
fp_clrexcept ();
}
void sn_check_except (void)
{
int excp;
return; /* XXX */
excp = fetestexcept (FE_T800_EXCEPT);
if (0 == excp)
return;
#if 0
printf ("-W-EMUFPU: Native FPU exception!\n");
printf ("-W-EMUFPU: Operation arguments.\n");
sn_dump ("-W-EMUFPU: Barg", BargSN);
sn_dump ("-W-EMUFPU: Aarg", AargSN);
sn_dump ("-W-EMUFPU: Result", ResultSN);
#endif
translate_except (excp);
fp_clrexcept ();
}
fpreal64_t db_correct_sign (fpreal64_t result, fpreal64_t fb, fpreal64_t fa)
{
if (fp_signdb (fb) == fp_signdb (fa))
{
result.bits &= ~REAL64_SIGN;
}
else
{
result.bits |= REAL64_SIGN;
}
return result;
}
fpreal64_t db_change_sign (fpreal64_t fp)
{
fp.bits ^= REAL64_SIGN;
return fp;
}
fpreal32_t sn_correct_sign (fpreal32_t result, fpreal32_t fb, fpreal32_t fa)
{
if (fp_signsn (fb) == fp_signsn (fa))
{
result.bits &= ~REAL32_SIGN;
}
else
{
result.bits |= REAL32_SIGN;
}
return result;
}
fpreal32_t sn_change_sign (fpreal32_t fp)
{
fp.bits ^= REAL32_SIGN;
return fp;
}
fpreal64_t fp_state (int length, fpreal64_t r64, uint32_t *fps)
{
fpreal32_t r32;
int sign, exp, exp_width;
uint64_t frac;
uint32_t status;
if (length == 32)
{
/* Get the REAL32 exponent. */
r32.bits = r64.bits;
exp = fp_expsn (r32);
exp -= 127;
exp_width = 8;
/* Make it quasi REAL64. */
r64.bits <<= 32;
sign = fp_signdb (r64);;
if (sign)
r64 = db_change_sign (r64);
r64.bits >>= 3;
if (sign)
r64 = db_change_sign (r64);
}
else
{
sign = fp_signdb (r64);
exp = fp_expdb (r64);
exp -= 1023;
exp_width = 11;
}
frac = fp_fracdb (r64);
/* FPS encoding */
status = 0;
status |= sign ? 1 << 31 : 0;
status |= (((exp & 1) == 0) || (exp == 0)) ? 1 << 20 : 0;
status |= (length == 32) ? 1 << 19 : 0;
status |= ((exp >> (exp_width - 2)) & 3) << 17;
status |= (RoundingMode - 1) << 7;
status |= ((frac >> 49) & 7) << 4;
status |= (exp == 0) ? 1 << 3 : 0;
/* Bit2 = GuardBit */
/* Bit1 = RoundBit */
/* Bit0 = StickyBit */
*fps = status;
return r64;
}
void fp_setstate (fpreal64_t r64, uint32_t fps)
{
int length, sign;
fpreal32_t r32;
length = ((fps >> 19) & 1) ? 32 : 64;
if (length == 32)
{
sign = fp_signdb (r64);
r64.bits <<= 3;
r32.bits = (r64.bits >> 32) & 0xffffffff;
if (sign)
r32 = sn_change_sign (r32);
fp_pushsn (r32);
}
else
fp_pushdb (r64);
fp_setrounding ("fp_setstate", ((fps >> 7) & 3) + 1);
}
/* Do a binary REAL64 operation, return REAL64 result. */
fpreal64_t db_binary (fpreal64_t fb, fpreal64_t fa, fpreal64_t (*opr)(fpreal64_t, fpreal64_t))
{
fpreal64_t result;
#ifndef NDEBUG
fp_chkexcept ("Enter db_binary ()");
BargDB = fb; AargDB = fa; ResultDB = DUndefined;
#endif
if (fp_infdb (fa) || fp_infdb (fb))
FP_Error = TRUE;
result = opr(fb, fa);
#ifndef NDEBUG
ResultDB = result;
#endif
db_check_except ();
return result;
}
/* Do a binary operation on two REAL64 numbers, return the result flag. */
int db_binary2word (fpreal64_t fb, fpreal64_t fa, int (*opr)(fpreal64_t, fpreal64_t))
{
int result;
#ifndef NDEBUG
BargDB = fb; AargDB = fa; ResultDB = DUndefined;
#endif
if (fp_notfinitedb (fb) || fp_notfinitedb (fa))
{
FP_Error = TRUE;
}
result = opr(fb, fa);
#ifndef NDEBUG
ResultDB.fp = t4_i32_to_fp64 (result);
#endif
db_check_except ();
return result;
}
/* Do an unary REAL64 operation, return REAL64 result. */
fpreal64_t db_unary (fpreal64_t fa, fpreal64_t (*opr)(fpreal64_t))
{
fpreal64_t result;
#ifndef NDEBUG
AargDB = fa; ResultDB = DUndefined;
#endif
if (fp_nandb (fa))
{
FP_Error = TRUE;
return fa;
}
if (fp_infdb (fa))
FP_Error = TRUE;
result = opr(fa);
#ifndef NDEBUG
ResultDB = result;
#endif
db_check_except ();
return result;
}
/* Do a binary REAL32 operation, return REAL32 result. */
fpreal32_t sn_binary (fpreal32_t fb, fpreal32_t fa, fpreal32_t (*opr)(fpreal32_t, fpreal32_t))
{
fpreal32_t result;
#ifndef NDEBUG
fp_chkexcept ("Enter sn_binary ()");
BargSN = fb; AargSN = fa; ResultSN = RUndefined;
#endif
if (fp_infsn (fb) || fp_infsn (fa))
FP_Error = TRUE;
result = opr(fb, fa);
#ifndef NDEBUG
ResultSN = result;
#endif
sn_check_except ();
return result;
}
/* Do a binary operation on two REAL32 numbers, return the result flag. */
int sn_binary2word (fpreal32_t fb, fpreal32_t fa, int (*opr)(fpreal32_t, fpreal32_t))
{
int result;
#ifndef NDEBUG
fp_chkexcept ("Enter sn_binary2word ()");
BargSN = fb; AargSN = fa; ResultSN = RUndefined;
#endif
if (fp_notfinitesn (fb) || fp_notfinitesn (fa))
{
FP_Error = TRUE;
}
result = opr(fb, fa);
#ifndef NDEBUG
ResultSN.fp = t4_i32_to_fp32 (result);
#endif
sn_check_except ();
return result;
}
/* Do an unary REAL32 operation, return REAL32 result. */
fpreal32_t sn_unary (fpreal32_t fa, fpreal32_t (*opr)(fpreal32_t))
{
fpreal32_t result;
#ifndef NDEBUG
fp_chkexcept ("Enter sn_unary ()");
AargSN = fa; ResultSN = RUndefined;
#endif
if (fp_nansn (fa))
{
FP_Error = TRUE;
return fa;
}
if (fp_infsn (fa))
FP_Error = TRUE;
result = opr(fa);
#ifndef NDEBUG
ResultSN = result;
#endif
sn_check_except ();
return result;
}
REAL64 DQuotRem (REAL64 X, REAL64 Y, REAL64 *N)
{
REAL64 rem;
rem = t4_fpremainder64 (X, Y);
*N = t4_fprint64 (t4_fpdiv64 (t4_fpsub64 (X, rem), Y));
return rem;
}
REAL32 RQuotRem (REAL32 X, REAL32 Y, REAL32 *N)
{
REAL32 rem;
rem = t4_fpremainder32 (X, Y);
*N = t4_fprint32 (t4_fpdiv32 (t4_fpsub32 (X, rem), Y));
return rem;
}
/*
* REAL64 basic operations.
*/
fpreal64_t db_add (fpreal64_t fb, fpreal64_t fa)
{
fpreal64_t result;
uint64_t fraca, fracb;
if (fp_nandb (fb) && fp_nandb (fa))
{
FP_Error = TRUE;
fracb = fp_fracdb (fb);
fraca = fp_fracdb (fa);
if (fracb >= fraca)
result = fb;
else
result = fa;
return result;
}
else if (fp_nandb (fb))
{
FP_Error = TRUE;
return fb;
}
else if (fp_nandb (fa))
{
FP_Error = TRUE;
return fa;
}
else if (fp_infdb (fb) && fp_infdb (fa))
{
FP_Error = TRUE;
if (fp_signdb (fb) != fp_signdb (fa))
return DAddOppositeInf_NaN;
}
result.fp = t4_fpadd64 (fb.fp, fa.fp);
return result;
}
fpreal64_t db_sub (fpreal64_t fb, fpreal64_t fa)
{
fpreal64_t result;
uint64_t fracb, fraca;
if (fp_nandb (fb) && fp_nandb (fa))
{
FP_Error = TRUE;
fracb = fp_fracdb (fb);
fraca = fp_fracdb (fa);
if (fracb >= fraca)
result = fb;
else
result = db_change_sign (fa);
return result;
}
else if (fp_nandb (fb))
{
FP_Error = TRUE;
return fb;
}
else if (fp_nandb (fa))
{
FP_Error = TRUE;
return db_change_sign (fa);
}
if (fp_infdb (fb) && fp_infdb (fa))
{
FP_Error = TRUE;
if (fp_signdb (fb) == fp_signdb (fa))
return DSubSameInf_NaN;
}
result.fp = t4_fpsub64 (fb.fp, fa.fp);
return result;
}
fpreal64_t db_mul(fpreal64_t fb, fpreal64_t fa)
{
fpreal64_t result;
uint64_t fracb, fraca;
if (fp_nandb (fb) && fp_nandb (fa))
{
FP_Error = TRUE;
fracb = fp_fracdb (fb);
fraca = fp_fracdb (fa);
if (fracb >= fraca)
result = fb;
else
result = fa;
return db_correct_sign (result, fb, fa);
}
else if (fp_nandb (fb))
{
FP_Error = TRUE;
return db_correct_sign (fb, fb, fa);
}
else if (fp_nandb (fa))
{
FP_Error = TRUE;
return db_correct_sign (fa, fb, fa);
}
if ((fp_zerodb (fb) && fp_infdb (fa)) ||
(fp_infdb (fb) && fp_zerodb (fa)))
{
FP_Error = TRUE;
return DMulZeroByInf_NaN;
}
result.fp = t4_fpmul64 (fb.fp, fa.fp);
return result;
}
fpreal64_t db_div(fpreal64_t fb, fpreal64_t fa)
{
fpreal64_t result;
uint64_t fracb, fraca;
if (fp_nandb (fb) && fp_nandb (fa))
{
FP_Error = TRUE;
fracb = fp_fracdb (fb);
fraca = fp_fracdb (fa);
if (fracb >= fraca)
result = fb;
else
result = fa;
return db_correct_sign (result, fb, fa);
}
else if (fp_nandb (fb))
{
FP_Error = TRUE;
return db_correct_sign (fb, fb, fa);
}
else if (fp_nandb (fa))
{
FP_Error = TRUE;
return db_correct_sign (fa, fb, fa);
}
else if (fp_zerodb (fb) && fp_zerodb (fa))
{
FP_Error = TRUE;
return DDivZeroByZero_NaN;
}
else if (fp_infdb (fb) && fp_infdb (fa))
{
FP_Error = TRUE;
return DDivInfByInf_NaN;
}
result.fp = t4_fpdiv64 (fb.fp, fa.fp);
return result;
}
fpreal64_t db_mulby2 (fpreal64_t fa) { fpreal64_t result; result.fp = t4_fpldexp64 (fa.fp, 1); return result; }
fpreal64_t db_divby2 (fpreal64_t fa) { fpreal64_t result; result.fp = t4_fpldexp64 (fa.fp, -1); return result; }
fpreal64_t db_expinc32 (fpreal64_t fa) { fpreal64_t result; result.fp = t4_fpldexp64 (fa.fp, 32); return result; }
fpreal64_t db_expdec32 (fpreal64_t fa) { fpreal64_t result; result.fp = t4_fpldexp64 (fa.fp, -32); return result; }
fpreal64_t db_sqrt (fpreal64_t fa)
{
fpreal64_t result;
if (fp_notfinitedb (fa))