[r3864]: dosbox / trunk / src / fpu / fpu_instructions.h Maximize Restore History

Download this file

fpu_instructions.h    606 lines (526 with data), 14.6 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
/*
* Copyright (C) 2002-2014 The DOSBox Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
static void FPU_FINIT(void) {
FPU_SetCW(0x37F);
fpu.sw = 0;
TOP=FPU_GET_TOP();
fpu.tags[0] = TAG_Empty;
fpu.tags[1] = TAG_Empty;
fpu.tags[2] = TAG_Empty;
fpu.tags[3] = TAG_Empty;
fpu.tags[4] = TAG_Empty;
fpu.tags[5] = TAG_Empty;
fpu.tags[6] = TAG_Empty;
fpu.tags[7] = TAG_Empty;
fpu.tags[8] = TAG_Valid; // is only used by us
}
static void FPU_FCLEX(void){
fpu.sw &= 0x7f00; //should clear exceptions
}
static void FPU_FNOP(void){
return;
}
static void FPU_PREP_PUSH(void){
TOP = (TOP - 1) &7;
if (GCC_UNLIKELY(fpu.tags[TOP] != TAG_Empty)) E_Exit("FPU stack overflow");
fpu.tags[TOP] = TAG_Valid;
}
static void FPU_PUSH(double in){
FPU_PREP_PUSH();
fpu.regs[TOP].d = in;
// LOG(LOG_FPU,LOG_ERROR)("Pushed at %d %g to the stack",newtop,in);
return;
}
static void FPU_FPOP(void){
if (GCC_UNLIKELY(fpu.tags[TOP] == TAG_Empty)) E_Exit("FPU stack underflow");
fpu.tags[TOP]=TAG_Empty;
//maybe set zero in it as well
TOP = ((TOP+1)&7);
// LOG(LOG_FPU,LOG_ERROR)("popped from %d %g off the stack",top,fpu.regs[top].d);
return;
}
static double FROUND(double in){
switch(fpu.round){
case ROUND_Nearest:
if (in-floor(in)>0.5) return (floor(in)+1);
else if (in-floor(in)<0.5) return (floor(in));
else return (((static_cast<Bit64s>(floor(in)))&1)!=0)?(floor(in)+1):(floor(in));
break;
case ROUND_Down:
return (floor(in));
break;
case ROUND_Up:
return (ceil(in));
break;
case ROUND_Chop:
return in; //the cast afterwards will do it right maybe cast here
break;
default:
return in;
break;
}
}
#define BIAS80 16383
#define BIAS64 1023
static Real64 FPU_FLD80(PhysPt addr) {
struct {
Bit16s begin;
FPU_Reg eind;
} test;
test.eind.l.lower = mem_readd(addr);
test.eind.l.upper = mem_readd(addr+4);
test.begin = mem_readw(addr+8);
Bit64s exp64 = (((test.begin&0x7fff) - BIAS80));
Bit64s blah = ((exp64 >0)?exp64:-exp64)&0x3ff;
Bit64s exp64final = ((exp64 >0)?blah:-blah) +BIAS64;
Bit64s mant64 = (test.eind.ll >> 11) & LONGTYPE(0xfffffffffffff);
Bit64s sign = (test.begin&0x8000)?1:0;
FPU_Reg result;
result.ll = (sign <<63)|(exp64final << 52)| mant64;
if(test.eind.l.lower == 0 && test.eind.l.upper == 0x80000000 && (test.begin&0x7fff) == 0x7fff) {
//Detect INF and -INF (score 3.11 when drawing a slur.)
result.d = sign?-HUGE_VAL:HUGE_VAL;
}
return result.d;
//mant64= test.mant80/2***64 * 2 **53
}
static void FPU_ST80(PhysPt addr,Bitu reg) {
struct {
Bit16s begin;
FPU_Reg eind;
} test;
Bit64s sign80 = (fpu.regs[reg].ll&LONGTYPE(0x8000000000000000))?1:0;
Bit64s exp80 = fpu.regs[reg].ll&LONGTYPE(0x7ff0000000000000);
Bit64s exp80final = (exp80>>52);
Bit64s mant80 = fpu.regs[reg].ll&LONGTYPE(0x000fffffffffffff);
Bit64s mant80final = (mant80 << 11);
if(fpu.regs[reg].d != 0){ //Zero is a special case
// Elvira wants the 8 and tcalc doesn't
mant80final |= LONGTYPE(0x8000000000000000);
//Ca-cyber doesn't like this when result is zero.
exp80final += (BIAS80 - BIAS64);
}
test.begin = (static_cast<Bit16s>(sign80)<<15)| static_cast<Bit16s>(exp80final);
test.eind.ll = mant80final;
mem_writed(addr,test.eind.l.lower);
mem_writed(addr+4,test.eind.l.upper);
mem_writew(addr+8,test.begin);
}
static void FPU_FLD_F32(PhysPt addr,Bitu store_to) {
union {
float f;
Bit32u l;
} blah;
blah.l = mem_readd(addr);
fpu.regs[store_to].d = static_cast<Real64>(blah.f);
}
static void FPU_FLD_F64(PhysPt addr,Bitu store_to) {
fpu.regs[store_to].l.lower = mem_readd(addr);
fpu.regs[store_to].l.upper = mem_readd(addr+4);
}
static void FPU_FLD_F80(PhysPt addr) {
fpu.regs[TOP].d = FPU_FLD80(addr);
}
static void FPU_FLD_I16(PhysPt addr,Bitu store_to) {
Bit16s blah = mem_readw(addr);
fpu.regs[store_to].d = static_cast<Real64>(blah);
}
static void FPU_FLD_I32(PhysPt addr,Bitu store_to) {
Bit32s blah = mem_readd(addr);
fpu.regs[store_to].d = static_cast<Real64>(blah);
}
static void FPU_FLD_I64(PhysPt addr,Bitu store_to) {
FPU_Reg blah;
blah.l.lower = mem_readd(addr);
blah.l.upper = mem_readd(addr+4);
fpu.regs[store_to].d = static_cast<Real64>(blah.ll);
}
static void FPU_FBLD(PhysPt addr,Bitu store_to) {
Bit64u val = 0;
Bitu in = 0;
Bit64u base = 1;
for(Bitu i = 0;i < 9;i++){
in = mem_readb(addr + i);
val += ( (in&0xf) * base); //in&0xf shouldn't be higher then 9
base *= 10;
val += ((( in>>4)&0xf) * base);
base *= 10;
}
//last number, only now convert to float in order to get
//the best signification
Real64 temp = static_cast<Real64>(val);
in = mem_readb(addr + 9);
temp += ( (in&0xf) * base );
if(in&0x80) temp *= -1.0;
fpu.regs[store_to].d = temp;
}
static INLINE void FPU_FLD_F32_EA(PhysPt addr) {
FPU_FLD_F32(addr,8);
}
static INLINE void FPU_FLD_F64_EA(PhysPt addr) {
FPU_FLD_F64(addr,8);
}
static INLINE void FPU_FLD_I32_EA(PhysPt addr) {
FPU_FLD_I32(addr,8);
}
static INLINE void FPU_FLD_I16_EA(PhysPt addr) {
FPU_FLD_I16(addr,8);
}
static void FPU_FST_F32(PhysPt addr) {
union {
float f;
Bit32u l;
} blah;
//should depend on rounding method
blah.f = static_cast<float>(fpu.regs[TOP].d);
mem_writed(addr,blah.l);
}
static void FPU_FST_F64(PhysPt addr) {
mem_writed(addr,fpu.regs[TOP].l.lower);
mem_writed(addr+4,fpu.regs[TOP].l.upper);
}
static void FPU_FST_F80(PhysPt addr) {
FPU_ST80(addr,TOP);
}
static void FPU_FST_I16(PhysPt addr) {
mem_writew(addr,static_cast<Bit16s>(FROUND(fpu.regs[TOP].d)));
}
static void FPU_FST_I32(PhysPt addr) {
mem_writed(addr,static_cast<Bit32s>(FROUND(fpu.regs[TOP].d)));
}
static void FPU_FST_I64(PhysPt addr) {
FPU_Reg blah;
blah.ll = static_cast<Bit64s>(FROUND(fpu.regs[TOP].d));
mem_writed(addr,blah.l.lower);
mem_writed(addr+4,blah.l.upper);
}
static void FPU_FBST(PhysPt addr) {
FPU_Reg val = fpu.regs[TOP];
bool sign = false;
if(fpu.regs[TOP].ll & LONGTYPE(0x8000000000000000)) { //sign
sign=true;
val.d=-val.d;
}
//numbers from back to front
Real64 temp=val.d;
Bitu p;
for(Bitu i=0;i<9;i++){
val.d=temp;
temp = static_cast<Real64>(static_cast<Bit64s>(floor(val.d/10.0)));
p = static_cast<Bitu>(val.d - 10.0*temp);
val.d=temp;
temp = static_cast<Real64>(static_cast<Bit64s>(floor(val.d/10.0)));
p |= (static_cast<Bitu>(val.d - 10.0*temp)<<4);
mem_writeb(addr+i,p);
}
val.d=temp;
temp = static_cast<Real64>(static_cast<Bit64s>(floor(val.d/10.0)));
p = static_cast<Bitu>(val.d - 10.0*temp);
if(sign)
p|=0x80;
mem_writeb(addr+9,p);
}
static void FPU_FADD(Bitu op1, Bitu op2){
fpu.regs[op1].d+=fpu.regs[op2].d;
//flags and such :)
return;
}
static void FPU_FSIN(void){
fpu.regs[TOP].d = sin(fpu.regs[TOP].d);
FPU_SET_C2(0);
//flags and such :)
return;
}
static void FPU_FSINCOS(void){
Real64 temp = fpu.regs[TOP].d;
fpu.regs[TOP].d = sin(temp);
FPU_PUSH(cos(temp));
FPU_SET_C2(0);
//flags and such :)
return;
}
static void FPU_FCOS(void){
fpu.regs[TOP].d = cos(fpu.regs[TOP].d);
FPU_SET_C2(0);
//flags and such :)
return;
}
static void FPU_FSQRT(void){
fpu.regs[TOP].d = sqrt(fpu.regs[TOP].d);
//flags and such :)
return;
}
static void FPU_FPATAN(void){
fpu.regs[STV(1)].d = atan2(fpu.regs[STV(1)].d,fpu.regs[TOP].d);
FPU_FPOP();
//flags and such :)
return;
}
static void FPU_FPTAN(void){
fpu.regs[TOP].d = tan(fpu.regs[TOP].d);
FPU_PUSH(1.0);
FPU_SET_C2(0);
//flags and such :)
return;
}
static void FPU_FDIV(Bitu st, Bitu other){
fpu.regs[st].d= fpu.regs[st].d/fpu.regs[other].d;
//flags and such :)
return;
}
static void FPU_FDIVR(Bitu st, Bitu other){
fpu.regs[st].d= fpu.regs[other].d/fpu.regs[st].d;
// flags and such :)
return;
}
static void FPU_FMUL(Bitu st, Bitu other){
fpu.regs[st].d*=fpu.regs[other].d;
//flags and such :)
return;
}
static void FPU_FSUB(Bitu st, Bitu other){
fpu.regs[st].d = fpu.regs[st].d - fpu.regs[other].d;
//flags and such :)
return;
}
static void FPU_FSUBR(Bitu st, Bitu other){
fpu.regs[st].d= fpu.regs[other].d - fpu.regs[st].d;
//flags and such :)
return;
}
static void FPU_FXCH(Bitu st, Bitu other){
FPU_Tag tag = fpu.tags[other];
FPU_Reg reg = fpu.regs[other];
fpu.tags[other] = fpu.tags[st];
fpu.regs[other] = fpu.regs[st];
fpu.tags[st] = tag;
fpu.regs[st] = reg;
}
static void FPU_FST(Bitu st, Bitu other){
fpu.tags[other] = fpu.tags[st];
fpu.regs[other] = fpu.regs[st];
}
static void FPU_FCOM(Bitu st, Bitu other){
if(((fpu.tags[st] != TAG_Valid) && (fpu.tags[st] != TAG_Zero)) ||
((fpu.tags[other] != TAG_Valid) && (fpu.tags[other] != TAG_Zero))){
FPU_SET_C3(1);FPU_SET_C2(1);FPU_SET_C0(1);return;
}
if(fpu.regs[st].d == fpu.regs[other].d){
FPU_SET_C3(1);FPU_SET_C2(0);FPU_SET_C0(0);return;
}
if(fpu.regs[st].d < fpu.regs[other].d){
FPU_SET_C3(0);FPU_SET_C2(0);FPU_SET_C0(1);return;
}
// st > other
FPU_SET_C3(0);FPU_SET_C2(0);FPU_SET_C0(0);return;
}
static void FPU_FUCOM(Bitu st, Bitu other){
//does atm the same as fcom
FPU_FCOM(st,other);
}
static void FPU_FRNDINT(void){
Bit64s temp= static_cast<Bit64s>(FROUND(fpu.regs[TOP].d));
fpu.regs[TOP].d=static_cast<double>(temp);
}
static void FPU_FPREM(void){
Real64 valtop = fpu.regs[TOP].d;
Real64 valdiv = fpu.regs[STV(1)].d;
Bit64s ressaved = static_cast<Bit64s>( (valtop/valdiv) );
// Some backups
// Real64 res=valtop - ressaved*valdiv;
// res= fmod(valtop,valdiv);
fpu.regs[TOP].d = valtop - ressaved*valdiv;
FPU_SET_C0(static_cast<Bitu>(ressaved&4));
FPU_SET_C3(static_cast<Bitu>(ressaved&2));
FPU_SET_C1(static_cast<Bitu>(ressaved&1));
FPU_SET_C2(0);
}
static void FPU_FPREM1(void){
Real64 valtop = fpu.regs[TOP].d;
Real64 valdiv = fpu.regs[STV(1)].d;
double quot = valtop/valdiv;
double quotf = floor(quot);
Bit64s ressaved;
if (quot-quotf>0.5) ressaved = static_cast<Bit64s>(quotf+1);
else if (quot-quotf<0.5) ressaved = static_cast<Bit64s>(quotf);
else ressaved = static_cast<Bit64s>((((static_cast<Bit64s>(quotf))&1)!=0)?(quotf+1):(quotf));
fpu.regs[TOP].d = valtop - ressaved*valdiv;
FPU_SET_C0(static_cast<Bitu>(ressaved&4));
FPU_SET_C3(static_cast<Bitu>(ressaved&2));
FPU_SET_C1(static_cast<Bitu>(ressaved&1));
FPU_SET_C2(0);
}
static void FPU_FXAM(void){
if(fpu.regs[TOP].ll & LONGTYPE(0x8000000000000000)) //sign
{
FPU_SET_C1(1);
}
else
{
FPU_SET_C1(0);
}
if(fpu.tags[TOP] == TAG_Empty)
{
FPU_SET_C3(1);FPU_SET_C2(0);FPU_SET_C0(1);
return;
}
if(fpu.regs[TOP].d == 0.0) //zero or normalized number.
{
FPU_SET_C3(1);FPU_SET_C2(0);FPU_SET_C0(0);
}
else
{
FPU_SET_C3(0);FPU_SET_C2(1);FPU_SET_C0(0);
}
}
static void FPU_F2XM1(void){
fpu.regs[TOP].d = pow(2.0,fpu.regs[TOP].d) - 1;
return;
}
static void FPU_FYL2X(void){
fpu.regs[STV(1)].d*=log(fpu.regs[TOP].d)/log(static_cast<Real64>(2.0));
FPU_FPOP();
return;
}
static void FPU_FYL2XP1(void){
fpu.regs[STV(1)].d*=log(fpu.regs[TOP].d+1.0)/log(static_cast<Real64>(2.0));
FPU_FPOP();
return;
}
static void FPU_FSCALE(void){
fpu.regs[TOP].d *= pow(2.0,static_cast<Real64>(static_cast<Bit64s>(fpu.regs[STV(1)].d)));
return; //2^x where x is chopped.
}
static void FPU_FSTENV(PhysPt addr){
FPU_SET_TOP(TOP);
if(!cpu.code.big) {
mem_writew(addr+0,static_cast<Bit16u>(fpu.cw));
mem_writew(addr+2,static_cast<Bit16u>(fpu.sw));
mem_writew(addr+4,static_cast<Bit16u>(FPU_GetTag()));
} else {
mem_writed(addr+0,static_cast<Bit32u>(fpu.cw));
mem_writed(addr+4,static_cast<Bit32u>(fpu.sw));
mem_writed(addr+8,static_cast<Bit32u>(FPU_GetTag()));
}
}
static void FPU_FLDENV(PhysPt addr){
Bit16u tag;
Bit32u tagbig;
Bitu cw;
if(!cpu.code.big) {
cw = mem_readw(addr+0);
fpu.sw = mem_readw(addr+2);
tag = mem_readw(addr+4);
} else {
cw = mem_readd(addr+0);
fpu.sw = (Bit16u)mem_readd(addr+4);
tagbig = mem_readd(addr+8);
tag = static_cast<Bit16u>(tagbig);
}
FPU_SetTag(tag);
FPU_SetCW(cw);
TOP = FPU_GET_TOP();
}
static void FPU_FSAVE(PhysPt addr){
FPU_FSTENV(addr);
Bitu start = (cpu.code.big?28:14);
for(Bitu i = 0;i < 8;i++){
FPU_ST80(addr+start,STV(i));
start += 10;
}
FPU_FINIT();
}
static void FPU_FRSTOR(PhysPt addr){
FPU_FLDENV(addr);
Bitu start = (cpu.code.big?28:14);
for(Bitu i = 0;i < 8;i++){
fpu.regs[STV(i)].d = FPU_FLD80(addr+start);
start += 10;
}
}
static void FPU_FXTRACT(void) {
// function stores real bias in st and
// pushes the significant number onto the stack
// if double ever uses a different base please correct this function
FPU_Reg test = fpu.regs[TOP];
Bit64s exp80 = test.ll&LONGTYPE(0x7ff0000000000000);
Bit64s exp80final = (exp80>>52) - BIAS64;
Real64 mant = test.d / (pow(2.0,static_cast<Real64>(exp80final)));
fpu.regs[TOP].d = static_cast<Real64>(exp80final);
FPU_PUSH(mant);
}
static void FPU_FCHS(void){
fpu.regs[TOP].d = -1.0*(fpu.regs[TOP].d);
}
static void FPU_FABS(void){
fpu.regs[TOP].d = fabs(fpu.regs[TOP].d);
}
static void FPU_FTST(void){
fpu.regs[8].d = 0.0;
FPU_FCOM(TOP,8);
}
static void FPU_FLD1(void){
FPU_PREP_PUSH();
fpu.regs[TOP].d = 1.0;
}
static void FPU_FLDL2T(void){
FPU_PREP_PUSH();
fpu.regs[TOP].d = L2T;
}
static void FPU_FLDL2E(void){
FPU_PREP_PUSH();
fpu.regs[TOP].d = L2E;
}
static void FPU_FLDPI(void){
FPU_PREP_PUSH();
fpu.regs[TOP].d = PI;
}
static void FPU_FLDLG2(void){
FPU_PREP_PUSH();
fpu.regs[TOP].d = LG2;
}
static void FPU_FLDLN2(void){
FPU_PREP_PUSH();
fpu.regs[TOP].d = LN2;
}
static void FPU_FLDZ(void){
FPU_PREP_PUSH();
fpu.regs[TOP].d = 0.0;
fpu.tags[TOP] = TAG_Zero;
}
static INLINE void FPU_FADD_EA(Bitu op1){
FPU_FADD(op1,8);
}
static INLINE void FPU_FMUL_EA(Bitu op1){
FPU_FMUL(op1,8);
}
static INLINE void FPU_FSUB_EA(Bitu op1){
FPU_FSUB(op1,8);
}
static INLINE void FPU_FSUBR_EA(Bitu op1){
FPU_FSUBR(op1,8);
}
static INLINE void FPU_FDIV_EA(Bitu op1){
FPU_FDIV(op1,8);
}
static INLINE void FPU_FDIVR_EA(Bitu op1){
FPU_FDIVR(op1,8);
}
static INLINE void FPU_FCOM_EA(Bitu op1){
FPU_FCOM(op1,8);
}