forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fpp_native.cpp
1386 lines (1286 loc) · 29.8 KB
/
fpp_native.cpp
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
/*
* UAE - The Un*x Amiga Emulator
*
* MC68881/68882/68040/68060 FPU emulation
*
* Copyright 1996 Herman ten Brugge
* Modified 2005 Peter Keunecke
* 68040+ exceptions and more by Toni Wilen
*/
#define __USE_ISOC9X /* We might be able to pick up a NaN */
#include <math.h>
#include <float.h>
#include <fenv.h>
#include "sysconfig.h"
#include "sysdeps.h"
#define USE_HOST_ROUNDING 1
#define SOFTFLOAT_CONVERSIONS 1
#include "options.h"
#include "memory.h"
#include "newcpu.h"
#include "fpp.h"
#include "uae/attributes.h"
#include "uae/vm.h"
#include "newcpu.h"
#ifdef JIT
uae_u32 xhex_exp_1[] ={0xa2bb4a9a, 0xadf85458, 0x4000};
uae_u32 xhex_ln_10[] ={0xaaa8ac17, 0x935d8ddd, 0x4000};
uae_u32 xhex_l10_2[] ={0xfbcff798, 0x9a209a84, 0x3ffd};
uae_u32 xhex_l10_e[] ={0x37287195, 0xde5bd8a9, 0x3ffd};
uae_u32 xhex_1e16[] ={0x04000000, 0x8e1bc9bf, 0x4034};
uae_u32 xhex_1e32[] ={0x2b70b59e, 0x9dc5ada8, 0x4069};
uae_u32 xhex_1e64[] ={0xffcfa6d5, 0xc2781f49, 0x40d3};
uae_u32 xhex_1e128[] ={0x80e98ce0, 0x93ba47c9, 0x41a8};
uae_u32 xhex_1e256[] ={0x9df9de8e, 0xaa7eebfb, 0x4351};
uae_u32 xhex_1e512[] ={0xa60e91c7, 0xe319a0ae, 0x46a3};
uae_u32 xhex_1e1024[]={0x81750c17, 0xc9767586, 0x4d48};
uae_u32 xhex_1e2048[]={0xc53d5de5, 0x9e8b3b5d, 0x5a92};
uae_u32 xhex_1e4096[]={0x8a20979b, 0xc4605202, 0x7525};
double fp_1e8 = 1.0e8;
float fp_1e0 = 1, fp_1e1 = 10, fp_1e2 = 100, fp_1e4 = 10000;
#endif
#ifdef USE_LONG_DOUBLE
static uae_u32 xhex_nan[] ={0xffffffff, 0xffffffff, 0x7fff};
static long double *fp_nan = (long double *)xhex_nan;
#else
static uae_u32 dhex_nan[] ={0xffffffff, 0x7fffffff};
static double *fp_nan = (double *)dhex_nan;
#endif
static const double twoto32 = 4294967296.0;
#define FPCR_ROUNDING_MODE 0x00000030
#define FPCR_ROUND_NEAR 0x00000000
#define FPCR_ROUND_ZERO 0x00000010
#define FPCR_ROUND_MINF 0x00000020
#define FPCR_ROUND_PINF 0x00000030
#define FPCR_ROUNDING_PRECISION 0x000000c0
#define FPCR_PRECISION_SINGLE 0x00000040
#define FPCR_PRECISION_DOUBLE 0x00000080
#define FPCR_PRECISION_EXTENDED 0x00000000
static struct float_status fs;
static uae_u32 fpu_mode_control = 0;
static int fpu_prec;
static int temp_prec;
#if defined(CPU_i386) || defined(CPU_x86_64)
/* The main motivation for dynamically creating an x86(-64) function in
* memory is because MSVC (x64) does not allow you to use inline assembly,
* and the x86-64 versions of _control87/_controlfp functions only modifies
* SSE2 registers. */
static uae_u16 x87_cw = 0;
static uae_u8 *x87_fldcw_code = NULL;
typedef void (uae_cdecl *x87_fldcw_function)(void);
void init_fpucw_x87(void)
{
if (x87_fldcw_code) {
return;
}
x87_fldcw_code = (uae_u8 *) uae_vm_alloc(
uae_vm_page_size(), UAE_VM_32BIT, UAE_VM_READ_WRITE_EXECUTE);
uae_u8 *c = x87_fldcw_code;
/* mov eax,0x0 */
*(c++) = 0xb8;
*(c++) = 0x00;
*(c++) = 0x00;
*(c++) = 0x00;
*(c++) = 0x00;
#ifdef CPU_x86_64
/* Address override prefix */
*(c++) = 0x67;
#endif
/* fldcw WORD PTR [eax+addr] */
*(c++) = 0xd9;
*(c++) = 0xa8;
*(c++) = (((uintptr_t) &x87_cw) ) & 0xff;
*(c++) = (((uintptr_t) &x87_cw) >> 8) & 0xff;
*(c++) = (((uintptr_t) &x87_cw) >> 16) & 0xff;
*(c++) = (((uintptr_t) &x87_cw) >> 24) & 0xff;
/* ret */
*(c++) = 0xc3;
/* Write-protect the function */
uae_vm_protect(x87_fldcw_code, uae_vm_page_size(), UAE_VM_READ_EXECUTE);
}
static void set_fpucw_x87(uae_u32 m68k_cw)
{
#ifdef _MSC_VER
static int ex = 0;
// RN, RZ, RM, RP
static const unsigned int fp87_round[4] = { _RC_NEAR, _RC_CHOP, _RC_DOWN, _RC_UP };
// Extend X, Single S, Double D, Undefined
static const unsigned int fp87_prec[4] = { _PC_53, _PC_24, _PC_53, 0 };
int round = (m68k_cw >> 4) & 3;
#ifdef WIN64
// x64 only sets SSE2, must also call x87_fldcw_code() to set FPU rounding mode.
_controlfp(ex | fp87_round[round], _MCW_RC);
#else
int prec = (m68k_cw >> 6) & 3;
// x86 sets both FPU and SSE2 rounding mode, don't need x87_fldcw_code()
_control87(ex | fp87_round[round] | fp87_prec[prec], _MCW_RC | _MCW_PC);
return;
#endif
#endif
static const uae_u16 x87_cw_tab[] = {
#ifdef USE_LONG_DOUBLE
0x137f, 0x1f7f, 0x177f, 0x1b7f, /* Extended */
#else
0x127f, 0x1e7f, 0x167f, 0x1a7f, /* Double */
#endif
0x107f, 0x1c7f, 0x147f, 0x187f, /* Single */
0x127f, 0x1e7f, 0x167f, 0x1a7f, /* Double */
0x127f, 0x1e7f, 0x167f, 0x1a7f, /* undefined (Double) */
};
x87_cw = x87_cw_tab[(m68k_cw >> 4) & 0xf];
#if defined(X86_MSVC_ASSEMBLY) && 0
__asm { fldcw word ptr x87_cw }
#elif defined(__GNUC__) && 0
__asm__("fldcw %0" : : "m" (*&x87_cw));
#else
((x87_fldcw_function) x87_fldcw_code)();
#endif
}
#endif /* defined(CPU_i386) || defined(CPU_x86_64) */
static void native_set_fpucw(uae_u32 m68k_cw)
{
#if defined(CPU_i386) || defined(CPU_x86_64)
set_fpucw_x87(m68k_cw);
#endif
}
/* Functions for setting host/library modes and getting status */
static void fp_set_mode(uae_u32 mode_control)
{
if (mode_control == fpu_mode_control && !currprefs.compfpu)
return;
switch(mode_control & FPCR_ROUNDING_PRECISION) {
case FPCR_PRECISION_EXTENDED: // X
fpu_prec = PREC_EXTENDED;
break;
case FPCR_PRECISION_SINGLE: // S
fpu_prec = PREC_FLOAT;
break;
case FPCR_PRECISION_DOUBLE: // D
default: // undefined
fpu_prec = PREC_DOUBLE;
break;
}
#if USE_HOST_ROUNDING
if ((mode_control & FPCR_ROUNDING_MODE) != (fpu_mode_control & FPCR_ROUNDING_MODE)) {
switch(mode_control & FPCR_ROUNDING_MODE) {
case FPCR_ROUND_NEAR: // to neareset
fesetround(FE_TONEAREST);
break;
case FPCR_ROUND_ZERO: // to zero
fesetround(FE_TOWARDZERO);
break;
case FPCR_ROUND_MINF: // to minus
fesetround(FE_DOWNWARD);
break;
case FPCR_ROUND_PINF: // to plus
fesetround(FE_UPWARD);
break;
}
}
native_set_fpucw(mode_control);
#endif
fpu_mode_control = mode_control;
}
static void fp_get_status(uae_u32 *status)
{
// These can't be properly emulated using host FPU.
#if 0
int exp_flags = fetestexcept(FE_ALL_EXCEPT);
if (exp_flags) {
if (exp_flags & FE_INEXACT)
*status |= FPSR_INEX2;
if (exp_flags & FE_DIVBYZERO)
*status |= FPSR_DZ;
if (exp_flags & FE_UNDERFLOW)
*status |= FPSR_UNFL;
if (exp_flags & FE_OVERFLOW)
*status |= FPSR_OVFL;
if (exp_flags & FE_INVALID)
*status |= FPSR_OPERR;
}
/* FIXME: how to detect SNAN? */
#endif
}
static uae_u32 fp_get_support_flags(void)
{
return 0;
}
static void fp_clear_status(void)
{
#if 0
feclearexcept (FE_ALL_EXCEPT);
#endif
}
/* Functions for detecting float type */
static bool fp_is_init(fpdata *fpd)
{
return false;
}
static bool fp_is_snan(fpdata *fpd)
{
return 0; /* FIXME: how to detect SNAN */
}
static bool fp_unset_snan(fpdata *fpd)
{
/* FIXME: how to unset SNAN */
return 0;
}
static bool fp_is_nan(fpdata *fpd)
{
return isnan(fpd->fp) != 0;
}
static bool fp_is_infinity(fpdata *fpd)
{
return isinf(fpd->fp) != 0;
}
static bool fp_is_zero(fpdata *fpd)
{
return fpd->fp == 0.0;
}
static bool fp_is_neg(fpdata *fpd)
{
return signbit(fpd->fp) != 0;
}
static bool fp_is_denormal(fpdata *fpd)
{
return false;
//return (isnormal(fpd->fp) == 0); /* FIXME: how to differ denormal/unnormal? */
}
static bool fp_is_unnormal(fpdata *fpd)
{
return false;
//return (isnormal(fpd->fp) == 0); /* FIXME: how to differ denormal/unnormal? */
}
/* Functions for converting between float formats */
/* FIXME: how to preserve/fix denormals and unnormals? */
static void fp_to_native(fptype *fp, fpdata *fpd)
{
*fp = fpd->fp;
}
static void fp_from_native(fptype fp, fpdata *fpd)
{
fpd->fp = fp;
}
static void fp_to_single(fpdata *fpd, uae_u32 wrd1)
{
union {
float f;
uae_u32 u;
} val;
val.u = wrd1;
fpd->fp = (fptype) val.f;
}
static uae_u32 fp_from_single(fpdata *fpd)
{
union {
float f;
uae_u32 u;
} val;
val.f = (float) fpd->fp;
return val.u;
}
static void fp_to_double(fpdata *fpd, uae_u32 wrd1, uae_u32 wrd2)
{
union {
double d;
uae_u32 u[2];
} val;
#ifdef WORDS_BIGENDIAN
val.u[0] = wrd1;
val.u[1] = wrd2;
#else
val.u[1] = wrd1;
val.u[0] = wrd2;
#endif
fpd->fp = (fptype) val.d;
}
static void fp_from_double(fpdata *fpd, uae_u32 *wrd1, uae_u32 *wrd2)
{
union {
double d;
uae_u32 u[2];
} val;
val.d = (double) fpd->fp;
#ifdef WORDS_BIGENDIAN
*wrd1 = val.u[0];
*wrd2 = val.u[1];
#else
*wrd1 = val.u[1];
*wrd2 = val.u[0];
#endif
}
#ifdef USE_LONG_DOUBLE
static void fp_to_exten(fpdata *fpd, uae_u32 wrd1, uae_u32 wrd2, uae_u32 wrd3)
{
union {
long double ld;
uae_u32 u[3];
} val;
#if WORDS_BIGENDIAN
val.u[0] = (wrd1 & 0xffff0000) | ((wrd2 & 0xffff0000) >> 16);
val.u[1] = (wrd2 & 0x0000ffff) | ((wrd3 & 0xffff0000) >> 16);
val.u[2] = (wrd3 & 0x0000ffff) << 16;
#else
val.u[0] = wrd3;
val.u[1] = wrd2;
val.u[2] = wrd1 >> 16;
#endif
fpd->fp = val.ld;
}
static void fp_from_exten(fpdata *fpd, uae_u32 *wrd1, uae_u32 *wrd2, uae_u32 *wrd3)
{
union {
long double ld;
uae_u32 u[3];
} val;
val.ld = fpd->fp;
#if WORDS_BIGENDIAN
*wrd1 = val.u[0] & 0xffff0000;
*wrd2 = ((val.u[0] & 0x0000ffff) << 16) | ((val.u[1] & 0xffff0000) >> 16);
*wrd3 = ((val.u[1] & 0x0000ffff) << 16) | ((val.u[2] & 0xffff0000) >> 16);
#else
*wrd3 = val.u[0];
*wrd2 = val.u[1];
*wrd1 = val.u[2] << 16;
#endif
}
#else // if !USE_LONG_DOUBLE
static void fp_to_exten(fpdata *fpd, uae_u32 wrd1, uae_u32 wrd2, uae_u32 wrd3)
{
#if SOFTFLOAT_CONVERSIONS
floatx80 fx80;
fx80.high = wrd1 >> 16;
fx80.low = (((uae_u64)wrd2) << 32) | wrd3;
fs.float_exception_flags = 0;
float64 f = floatx80_to_float64(fx80, &fs);
// overflow -> infinity
if (fs.float_exception_flags & float_flag_overflow)
f = 0x7ff0000000000000 | (f & 0x8000000000000000);
fp_to_double(fpd, f >> 32, (uae_u32)f);
#else
double frac;
if ((wrd1 & 0x7fff0000) == 0 && wrd2 == 0 && wrd3 == 0) {
fpd->fp = (wrd1 & 0x80000000) ? -0.0 : +0.0;
return;
}
frac = ((double)wrd2 + ((double)wrd3 / twoto32)) / 2147483648.0;
if (wrd1 & 0x80000000)
frac = -frac;
fpd->fp = ldexp (frac, ((wrd1 >> 16) & 0x7fff) - 16383);
#endif
}
static void fp_from_exten(fpdata *fpd, uae_u32 *wrd1, uae_u32 *wrd2, uae_u32 *wrd3)
{
#if SOFTFLOAT_CONVERSIONS
uae_u32 w1, w2;
fp_from_double(fpd, &w1, &w2);
floatx80 f = float64_to_floatx80(((uae_u64)w1 << 32) | w2, &fs);
*wrd1 = f.high << 16;
*wrd2 = f.low >> 32;
*wrd3 = (uae_u32)f.low;
#else
int expon;
double frac;
fptype v;
if (fp_is_zero(fpd)) {
*wrd1 = signbit(fpd->fp) ? 0x80000000 : 0;
*wrd2 = 0;
*wrd3 = 0;
return;
} else if (fp_is_nan(fpd)) {
*wrd1 = 0x7fff0000;
*wrd2 = 0xffffffff;
*wrd3 = 0xffffffff;
return;
}
v = fpd->fp;
if (v < 0) {
*wrd1 = 0x80000000;
v = -v;
} else {
*wrd1 = 0;
}
frac = frexp (v, &expon);
frac += 0.5 / (twoto32 * twoto32);
if (frac >= 1.0) {
frac /= 2.0;
expon++;
}
*wrd1 |= (((expon + 16383 - 1) & 0x7fff) << 16);
*wrd2 = (uae_u32) (frac * twoto32);
*wrd3 = (uae_u32) ((frac * twoto32 - *wrd2) * twoto32);
#endif
}
#endif // !USE_LONG_DOUBLE
#if USE_HOST_ROUNDING == 0
#ifdef USE_LONG_DOUBLE
#define fp_round_to_minus_infinity(x) floorl(x)
#define fp_round_to_plus_infinity(x) ceill(x)
#define fp_round_to_zero(x) ((x) >= 0.0 ? floorl(x) : ceill(x))
#define fp_round_to_nearest(x) roundl(x)
#else // if !USE_LONG_DOUBLE
#define fp_round_to_minus_infinity(x) floor(x)
#define fp_round_to_plus_infinity(x) ceil(x)
#define fp_round_to_zero(x) ((x) >= 0.0 ? floor(x) : ceil(x))
#define fp_round_to_nearest(x) round(x)
#endif // !USE_LONG_DOUBLE
#endif // USE_HOST_ROUNDING
static uae_s64 fp_to_int(fpdata *src, int size)
{
static const fptype fxsizes[6] =
{
-128.0, 127.0,
-32768.0, 32767.0,
-2147483648.0, 2147483647.0
};
fptype fp = src->fp;
fp_is_init(src);
if (fp_is_nan(src)) {
uae_u32 w1, w2, w3;
fp_from_exten(src, &w1, &w2, &w3);
uae_s64 v = 0;
// return mantissa
switch (size)
{
case 0:
v = w2 >> 24;
break;
case 1:
v = w2 >> 16;
break;
case 2:
v = w2 >> 0;
break;
}
return v;
}
if (fp < fxsizes[size * 2 + 0]) {
fp = fxsizes[size * 2 + 0];
}
if (fp > fxsizes[size * 2 + 1]) {
fp = fxsizes[size * 2 + 1];
}
#if USE_HOST_ROUNDING
return lrintl(fp);
#else
uae_s64 result = (int)fp;
switch (regs.fpcr & 0x30)
{
case FPCR_ROUND_ZERO:
result = (int)fp_round_to_zero (fp);
break;
case FPCR_ROUND_MINF:
result = (int)fp_round_to_minus_infinity (fp);
break;
case FPCR_ROUND_NEAR:
result = fp_round_to_nearest (fp);
break;
case FPCR_ROUND_PINF:
result = (int)fp_round_to_plus_infinity (fp);
break;
}
return result;
#endif
}
static void fp_from_int(fpdata *fpd, uae_s32 src)
{
fpd->fp = (fptype) src;
}
/* Functions for rounding */
// round to float with extended precision exponent
static void fp_round32(fpdata *fpd)
{
int expon;
float mant;
mant = (float)(frexpl(fpd->fp, &expon) * 2.0);
fpd->fp = ldexpl((fptype)mant, expon - 1);
}
// round to double with extended precision exponent
static void fp_round64(fpdata *fpd)
{
int expon;
double mant;
mant = (double)(frexpl(fpd->fp, &expon) * 2.0);
fpd->fp = ldexpl((fptype)mant, expon - 1);
}
// round to float
static void fp_round_single(fpdata *fpd)
{
fpd->fp = (float) fpd->fp;
}
// round to double
static void fp_round_double(fpdata *fpd)
{
#ifdef USE_LONG_DOUBLE
fpd->fp = (double) fpd->fp;
#endif
}
static const TCHAR *fp_print(fpdata *fpd, int mode)
{
static TCHAR fsout[32];
bool n;
if (mode < 0) {
uae_u32 w1, w2, w3;
fp_from_exten(fpd, &w1, &w2, &w3);
_stprintf(fsout, _T("%04X-%08X-%08X"), w1 >> 16, w2, w3);
return fsout;
}
n = signbit(fpd->fp) ? 1 : 0;
if(isinf(fpd->fp)) {
_stprintf(fsout, _T("%c%s"), n ? '-' : '+', _T("inf"));
} else if(isnan(fpd->fp)) {
_stprintf(fsout, _T("%c%s"), n ? '-' : '+', _T("nan"));
} else {
if(n)
fpd->fp *= -1.0;
#ifdef USE_LONG_DOUBLE
_stprintf(fsout, _T("#%Le"), fpd->fp);
#else
_stprintf(fsout, _T("#%e"), fpd->fp);
#endif
}
if (mode == 0 || mode > _tcslen(fsout))
return fsout;
fsout[mode] = 0;
return fsout;
}
static void fp_round_prec(fpdata *fpd, int prec)
{
if (prec == PREC_DOUBLE) {
fp_round_double(fpd);
} else if (prec == PREC_FLOAT) {
fp_round_single(fpd);
}
}
static void fp_round(fpdata *fpd)
{
if (!currprefs.fpu_strict)
return;
fp_round_prec(fpd, fpu_prec);
}
static void fp_set_prec(int prec)
{
#if 0
temp_fpu_mode_control = fpu_mode_control;
if (prec && fpu_prec > prec) {
fpu_mode_control &= ~FPCR_ROUNDING_PRECISION;
switch (prec)
{
case PREC_EXTENDED:
fpu_mode_control |= FPCR_PRECISION_EXTENDED;
break;
case PREC_DOUBLE:
default:
fpu_mode_control |= FPCR_PRECISION_DOUBLE;
break;
case PREC_FLOAT:
fpu_mode_control |= FPCR_PRECISION_SINGLE;
break;
}
fp_set_mode(fpu_mode_control);
}
#endif
temp_prec = prec;
}
static void fp_reset_prec(fpdata *fpd)
{
#if 0
fp_set_mode(temp_fpu_mode_control);
#else
int prec = temp_prec;
if (temp_prec == PREC_NORMAL)
prec = fpu_prec;
fp_round_prec(fpd, prec);
#endif
}
// Use default precision/rounding mode when calling C-library math functions.
static void fp_normal_prec(void)
{
temp_prec = fpu_mode_control;
#ifdef USE_LONG_DOUBLE
if ((fpu_mode_control & FPCR_ROUNDING_PRECISION) != FPCR_PRECISION_EXTENDED || (fpu_mode_control & FPCR_ROUNDING_MODE) != FPCR_ROUND_NEAR) {
fp_set_mode(FPCR_PRECISION_EXTENDED | FPCR_ROUND_NEAR);
}
#else
if ((fpu_mode_control & FPCR_ROUNDING_PRECISION) == FPCR_PRECISION_SINGLE || (fpu_mode_control & FPCR_ROUNDING_MODE) != FPCR_ROUND_NEAR) {
fp_set_mode(FPCR_PRECISION_DOUBLE | FPCR_ROUND_NEAR);
}
#endif
}
static void fp_reset_normal_prec(void)
{
fp_set_mode(temp_prec);
}
/* Arithmetic functions */
static void fp_move(fpdata *a, fpdata *b, int prec)
{
fp_set_prec(prec);
a->fp = b->fp;
fp_reset_prec(a);
}
static void fp_int(fpdata *a, fpdata *b)
{
fptype bb = b->fp;
#if USE_HOST_ROUNDING
a->fp = rintl(bb);
#else
switch (regs.fpcr & FPCR_ROUNDING_MODE)
{
case FPCR_ROUND_NEAR:
a->fp = fp_round_to_nearest(bb);
case FPCR_ROUND_ZERO:
a->fp = fp_round_to_zero(bb);
case FPCR_ROUND_MINF:
a->fp = fp_round_to_minus_infinity(bb);
case FPCR_ROUND_PINF:
a->fp = fp_round_to_plus_infinity(bb);
default: /* never reached */
break;
}
#endif
}
static void fp_getexp(fpdata *a, fpdata *b)
{
int expon;
fp_normal_prec();
frexpl(b->fp, &expon);
a->fp = (fptype)expon - 1;
fp_reset_normal_prec();
}
static void fp_getman(fpdata *a, fpdata *b)
{
int expon;
fp_normal_prec();
a->fp = frexpl(b->fp, &expon) * 2.0;
fp_reset_normal_prec();
}
static void fp_div(fpdata *a, fpdata *b, int prec)
{
fp_set_prec(prec);
a->fp = a->fp / b->fp;
fp_reset_prec(a);
}
static void fp_mod(fpdata *a, fpdata *b, uae_u64 *q, uae_u8 *s)
{
fptype quot;
#if USE_HOST_ROUNDING
quot = truncl(a->fp / b->fp);
#else
quot = fp_round_to_zero(a->fp / b->fp);
#endif
if (quot < 0.0) {
*s = 1;
quot = -quot;
} else {
*s = 0;
}
*q = (uae_u64)quot;
a->fp = fmodl(a->fp, b->fp);
fp_round(a);
}
static void fp_rem(fpdata *a, fpdata *b, uae_u64 *q, uae_u8 *s)
{
fptype quot;
#if USE_HOST_ROUNDING
quot = roundl(a->fp / b->fp);
#else
quot = fp_round_to_nearest(a->fp / b->fp);
#endif
if (quot < 0.0) {
*s = 1;
quot = -quot;
} else {
*s = 0;
}
*q = (uae_u64)quot;
a->fp = remainderl(a->fp, b->fp);
fp_round(a);
}
static void fp_scale(fpdata *a, fpdata *b)
{
fp_normal_prec();
a->fp = ldexpl(a->fp, (int)b->fp);
fp_reset_normal_prec();
fp_round(a);
}
static void fp_sinh(fpdata *a, fpdata *b)
{
fp_normal_prec();
a->fp = sinhl(b->fp);
fp_reset_normal_prec();
fp_round(a);
}
static void fp_intrz(fpdata *a, fpdata *b)
{
#if USE_HOST_ROUNDING
a->fp = truncl(b->fp);
#else
a->fp = fp_round_to_zero (b->fp);
#endif
fp_round(a);
}
static void fp_sqrt(fpdata *a, fpdata *b, int prec)
{
fp_set_prec(prec);
a->fp = sqrtl(b->fp);
fp_reset_prec(a);
}
static void fp_lognp1(fpdata *a, fpdata *b)
{
fp_normal_prec();
a->fp = log1pl(b->fp);
fp_reset_normal_prec();
fp_round(a);
}
static void fp_etoxm1(fpdata *a, fpdata *b)
{
fp_normal_prec();
a->fp = expm1l(b->fp);
fp_reset_normal_prec();
fp_round(a);
}
static void fp_tanh(fpdata *a, fpdata *b)
{
fp_normal_prec();
a->fp = tanhl(b->fp);
fp_reset_normal_prec();
fp_round(a);
}
static void fp_atan(fpdata *a, fpdata *b)
{
fp_normal_prec();
a->fp = atanl(b->fp);
fp_reset_normal_prec();
fp_round(a);
}
static void fp_atanh(fpdata *a, fpdata *b)
{
fp_normal_prec();
a->fp = atanhl(b->fp);
fp_reset_normal_prec();
fp_round(a);
}
static void fp_sin(fpdata *a, fpdata *b)
{
fp_normal_prec();
a->fp = sinl(b->fp);
fp_reset_normal_prec();
fp_round(a);
}
static void fp_asin(fpdata *a, fpdata *b)
{
fp_normal_prec();
a->fp = asinl(b->fp);
fp_reset_normal_prec();
fp_round(a);
}
static void fp_tan(fpdata *a, fpdata *b)
{
fp_normal_prec();
a->fp = tanl(b->fp);
fp_reset_normal_prec();
fp_round(a);
}
static void fp_etox(fpdata *a, fpdata *b)
{
fp_normal_prec();
a->fp = expl(b->fp);
fp_reset_normal_prec();
fp_round(a);
}
static void fp_twotox(fpdata *a, fpdata *b)
{
fp_normal_prec();
a->fp = powl(2.0, b->fp);
fp_reset_normal_prec();
fp_round(a);
}
static void fp_tentox(fpdata *a, fpdata *b)
{
fp_normal_prec();
a->fp = powl(10.0, b->fp);
fp_reset_normal_prec();
fp_round(a);
}
static void fp_logn(fpdata *a, fpdata *b)
{
fp_normal_prec();
a->fp = logl(b->fp);
fp_reset_normal_prec();
fp_round(a);
}
static void fp_log10(fpdata *a, fpdata *b)
{
fp_normal_prec();
a->fp = log10l(b->fp);
fp_reset_normal_prec();
fp_round(a);
}
static void fp_log2(fpdata *a, fpdata *b)
{
fp_normal_prec();
a->fp = log2l(b->fp);
fp_reset_normal_prec();
fp_round(a);
}
static void fp_abs(fpdata *a, fpdata *b, int prec)
{
fp_set_prec(prec);
a->fp = b->fp < 0.0 ? -b->fp : b->fp;
fp_reset_prec(a);
}
static void fp_cosh(fpdata *a, fpdata *b)
{
fp_normal_prec();
a->fp = coshl(b->fp);
fp_reset_normal_prec();
fp_round(a);
}
static void fp_neg(fpdata *a, fpdata *b, int prec)
{
fp_set_prec(prec);
a->fp = -b->fp;
fp_reset_prec(a);
}
static void fp_acos(fpdata *a, fpdata *b)
{
fp_normal_prec();
a->fp = acosl(b->fp);
fp_reset_normal_prec();
fp_round(a);
}
static void fp_cos(fpdata *a, fpdata *b)
{
fp_normal_prec();
a->fp = cosl(b->fp);
fp_reset_normal_prec();
fp_round(a);
}
static void fp_sincos(fpdata *a, fpdata *b, fpdata *c)
{
fp_normal_prec();
c->fp = cosl(b->fp);
a->fp = sinl(b->fp);
fp_reset_normal_prec();
fp_round(a);
fp_round(c);
}
static void fp_sub(fpdata *a, fpdata *b, int prec)
{
fp_set_prec(prec);
a->fp = a->fp - b->fp;
fp_reset_prec(a);
}
static void fp_add(fpdata *a, fpdata *b, int prec)
{
fp_set_prec(prec);
a->fp = a->fp + b->fp;
fp_reset_prec(a);
}
static void fp_mul(fpdata *a, fpdata *b, int prec)
{
fp_set_prec(prec);
a->fp = a->fp * b->fp;
fp_reset_prec(a);
}
static void fp_sglmul(fpdata *a, fpdata *b)
{
fptype z;
float mant;
int expon;
/* FIXME: truncate mantissa of a and b to single precision */
z = a->fp * b->fp;
mant = (float)(frexpl(z, &expon) * 2.0);
a->fp = ldexpl((fptype)mant, expon - 1);
}
static void fp_sgldiv(fpdata *a, fpdata *b)
{
fptype z;
float mant;
int expon;
z = a->fp / b->fp;
mant = (float)(frexpl(z, &expon) * 2.0);
a->fp = ldexpl((fptype)mant, expon - 1);
}
static void fp_normalize(fpdata *a)
{
}
static void fp_cmp(fpdata *a, fpdata *b)
{
fptype v = 1.0;
if (currprefs.fpu_strict) {
fp_is_init(a);
bool a_neg = fp_is_neg(a);
bool a_inf = fp_is_infinity(a);
bool a_zero = fp_is_zero(a);
bool a_nan = fp_is_nan(a);
fp_is_init(b);
bool b_neg = fp_is_neg(b);
bool b_inf = fp_is_infinity(b);
bool b_zero = fp_is_zero(b);
bool b_nan = fp_is_nan(b);
if (a_nan || b_nan) {
// FCMP never returns N + NaN
v = *fp_nan;
} else if (a_zero && b_zero) {
if ((a_neg && b_neg) || (a_neg && !b_neg))
v = -0.0;
else
v = 0.0;
} else if (a_zero && b_inf) {
if (!b_neg)
v = -1.0;
else
v = 1.0;