Debian Codegraph @

src/fpu/fpu_instructions.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: fpu_instructions.h,v 1.33 2009-05-27 09:15:41 qbix79 Exp $ */
20
21
22 static void FPU_FINIT(void) {
23 FPU_SetCW(0x37F);
24 fpu.sw = 0;
25 TOP=FPU_GET_TOP();
26 fpu.tags[0] = TAG_Empty;
27 fpu.tags[1] = TAG_Empty;
28 fpu.tags[2] = TAG_Empty;
29 fpu.tags[3] = TAG_Empty;
30 fpu.tags[4] = TAG_Empty;
31 fpu.tags[5] = TAG_Empty;
32 fpu.tags[6] = TAG_Empty;
33 fpu.tags[7] = TAG_Empty;
34 fpu.tags[8] = TAG_Valid; // is only used by us
35 }
36
37 static void FPU_FCLEX(void){
38 fpu.sw &= 0x7f00; //should clear exceptions
39 }
40
41 static void FPU_FNOP(void){
42 return;
43 }
44
45 static void FPU_PUSH(double in){
46 TOP = (TOP - 1) &7;
47 //actually check if empty
48 fpu.tags[TOP] = TAG_Valid;
49 fpu.regs[TOP].d = in;
50 // LOG(LOG_FPU,LOG_ERROR)("Pushed at %d %g to the stack",newtop,in);
51 return;
52 }
53
54 static void FPU_PREP_PUSH(void){
55 TOP = (TOP - 1) &7;
56 fpu.tags[TOP] = TAG_Valid;
57 }
58
59 static void FPU_FPOP(void){
60 fpu.tags[TOP]=TAG_Empty;
61 //maybe set zero in it as well
62 TOP = ((TOP+1)&7);
63 // LOG(LOG_FPU,LOG_ERROR)("popped from %d %g off the stack",top,fpu.regs[top].d);
64 return;
65 }
66
67 static double FROUND(double in){
68 switch(fpu.round){
69 case ROUND_Nearest:
70 if (in-floor(in)>0.5) return (floor(in)+1);
71 else if (in-floor(in)<0.5) return (floor(in));
72 else return (((static_cast<Bit64s>(floor(in)))&1)!=0)?(floor(in)+1):(floor(in));
73 break;
74 case ROUND_Down:
75 return (floor(in));
76 break;
77 case ROUND_Up:
78 return (ceil(in));
79 break;
80 case ROUND_Chop:
81 return in; //the cast afterwards will do it right maybe cast here
82 break;
83 default:
84 return in;
85 break;
86 }
87 }
88
89 #define BIAS80 16383
90 #define BIAS64 1023
91
92 static Real64 FPU_FLD80(PhysPt addr) {
93 struct {
94 Bit16s begin;
95 FPU_Reg eind;
96 } test;
97 test.eind.l.lower = mem_readd(addr);
98 test.eind.l.upper = mem_readd(addr+4);
99 test.begin = mem_readw(addr+8);
100
101 Bit64s exp64 = (((test.begin&0x7fff) - BIAS80));
102 Bit64s blah = ((exp64 >0)?exp64:-exp64)&0x3ff;
103 Bit64s exp64final = ((exp64 >0)?blah:-blah) +BIAS64;
104
105 Bit64s mant64 = (test.eind.ll >> 11) & LONGTYPE(0xfffffffffffff);
106 Bit64s sign = (test.begin&0x8000)?1:0;
107 FPU_Reg result;
108 result.ll = (sign <<63)|(exp64final << 52)| mant64;
109 return result.d;
110
111 //mant64= test.mant80/2***64 * 2 **53
112 }
113
114 static void FPU_ST80(PhysPt addr,Bitu reg) {
115 struct {
116 Bit16s begin;
117 FPU_Reg eind;
118 } test;
119 Bit64s sign80 = (fpu.regs[reg].ll&LONGTYPE(0x8000000000000000))?1:0;
120 Bit64s exp80 = fpu.regs[reg].ll&LONGTYPE(0x7ff0000000000000);
121 Bit64s exp80final = (exp80>>52);
122 Bit64s mant80 = fpu.regs[reg].ll&LONGTYPE(0x000fffffffffffff);
123 Bit64s mant80final = (mant80 << 11);
124 if(fpu.regs[reg].d != 0){ //Zero is a special case
125 // Elvira wants the 8 and tcalc doesn't
126 mant80final |= LONGTYPE(0x8000000000000000);
127 //Ca-cyber doesn't like this when result is zero.
128 exp80final += (BIAS80 - BIAS64);
129 }
130 test.begin = (static_cast<Bit16s>(sign80)<<15)| static_cast<Bit16s>(exp80final);
131 test.eind.ll = mant80final;
132 mem_writed(addr,test.eind.l.lower);
133 mem_writed(addr+4,test.eind.l.upper);
134 mem_writew(addr+8,test.begin);
135 }
136
137
138 static void FPU_FLD_F32(PhysPt addr,Bitu store_to) {
139 union {
140 float f;
141 Bit32u l;
142 } blah;
143 blah.l = mem_readd(addr);
144 fpu.regs[store_to].d = static_cast<Real64>(blah.f);
145 }
146
147 static void FPU_FLD_F64(PhysPt addr,Bitu store_to) {
148 fpu.regs[store_to].l.lower = mem_readd(addr);
149 fpu.regs[store_to].l.upper = mem_readd(addr+4);
150 }
151
152 static void FPU_FLD_F80(PhysPt addr) {
153 fpu.regs[TOP].d = FPU_FLD80(addr);
154 }
155
156 static void FPU_FLD_I16(PhysPt addr,Bitu store_to) {
157 Bit16s blah = mem_readw(addr);
158 fpu.regs[store_to].d = static_cast<Real64>(blah);
159 }
160
161 static void FPU_FLD_I32(PhysPt addr,Bitu store_to) {
162 Bit32s blah = mem_readd(addr);
163 fpu.regs[store_to].d = static_cast<Real64>(blah);
164 }
165
166 static void FPU_FLD_I64(PhysPt addr,Bitu store_to) {
167 FPU_Reg blah;
168 blah.l.lower = mem_readd(addr);
169 blah.l.upper = mem_readd(addr+4);
170 fpu.regs[store_to].d = static_cast<Real64>(blah.ll);
171 }
172
173 static void FPU_FBLD(PhysPt addr,Bitu store_to) {
174 Bit64u val = 0;
175 Bitu in = 0;
176 Bit64u base = 1;
177 for(Bitu i = 0;i < 9;i++){
178 in = mem_readb(addr + i);
179 val += ( (in&0xf) * base); //in&0xf shouldn't be higher then 9
180 base *= 10;
181 val += ((( in>>4)&0xf) * base);
182 base *= 10;
183 }
184
185 //last number, only now convert to float in order to get
186 //the best signification
187 Real64 temp = static_cast<Real64>(val);
188 in = mem_readb(addr + 9);
189 temp += ( (in&0xf) * base );
190 if(in&0x80) temp *= -1.0;
191 fpu.regs[store_to].d = temp;
192 }
193
194
195 static INLINE void FPU_FLD_F32_EA(PhysPt addr) {
196 FPU_FLD_F32(addr,8);
197 }
198 static INLINE void FPU_FLD_F64_EA(PhysPt addr) {
199 FPU_FLD_F64(addr,8);
200 }
201 static INLINE void FPU_FLD_I32_EA(PhysPt addr) {
202 FPU_FLD_I32(addr,8);
203 }
204 static INLINE void FPU_FLD_I16_EA(PhysPt addr) {
205 FPU_FLD_I16(addr,8);
206 }
207
208
209 static void FPU_FST_F32(PhysPt addr) {
210 union {
211 float f;
212 Bit32u l;
213 } blah;
214 //should depend on rounding method
215 blah.f = static_cast<float>(fpu.regs[TOP].d);
216 mem_writed(addr,blah.l);
217 }
218
219 static void FPU_FST_F64(PhysPt addr) {
220 mem_writed(addr,fpu.regs[TOP].l.lower);
221 mem_writed(addr+4,fpu.regs[TOP].l.upper);
222 }
223
224 static void FPU_FST_F80(PhysPt addr) {
225 FPU_ST80(addr,TOP);
226 }
227
228 static void FPU_FST_I16(PhysPt addr) {
229 mem_writew(addr,static_cast<Bit16s>(FROUND(fpu.regs[TOP].d)));
230 }
231
232 static void FPU_FST_I32(PhysPt addr) {
233 mem_writed(addr,static_cast<Bit32s>(FROUND(fpu.regs[TOP].d)));
234 }
235
236 static void FPU_FST_I64(PhysPt addr) {
237 FPU_Reg blah;
238 blah.ll = static_cast<Bit64s>(FROUND(fpu.regs[TOP].d));
239 mem_writed(addr,blah.l.lower);
240 mem_writed(addr+4,blah.l.upper);
241 }
242
243 static void FPU_FBST(PhysPt addr) {
244 FPU_Reg val = fpu.regs[TOP];
245 bool sign = false;
246 if(fpu.regs[TOP].ll & LONGTYPE(0x8000000000000000)) { //sign
247 sign=true;
248 val.d=-val.d;
249 }
250 //numbers from back to front
251 Real64 temp=val.d;
252 Bitu p;
253 for(Bitu i=0;i<9;i++){
254 val.d=temp;
255 temp = static_cast<Real64>(static_cast<Bit64s>(floor(val.d/10.0)));
256 p = static_cast<Bitu>(val.d - 10.0*temp);
257 val.d=temp;
258 temp = static_cast<Real64>(static_cast<Bit64s>(floor(val.d/10.0)));
259 p |= (static_cast<Bitu>(val.d - 10.0*temp)<<4);
260
261 mem_writeb(addr+i,p);
262 }
263 val.d=temp;
264 temp = static_cast<Real64>(static_cast<Bit64s>(floor(val.d/10.0)));
265 p = static_cast<Bitu>(val.d - 10.0*temp);
266 if(sign)
267 p|=0x80;
268 mem_writeb(addr+9,p);
269 }
270
271 static void FPU_FADD(Bitu op1, Bitu op2){
272 fpu.regs[op1].d+=fpu.regs[op2].d;
273 //flags and such :)
274 return;
275 }
276
277 static void FPU_FSIN(void){
278 fpu.regs[TOP].d = sin(fpu.regs[TOP].d);
279 FPU_SET_C2(0);
280 //flags and such :)
281 return;
282 }
283
284 static void FPU_FSINCOS(void){
285 Real64 temp = fpu.regs[TOP].d;
286 fpu.regs[TOP].d = sin(temp);
287 FPU_PUSH(cos(temp));
288 FPU_SET_C2(0);
289 //flags and such :)
290 return;
291 }
292
293 static void FPU_FCOS(void){
294 fpu.regs[TOP].d = cos(fpu.regs[TOP].d);
295 FPU_SET_C2(0);
296 //flags and such :)
297 return;
298 }
299
300 static void FPU_FSQRT(void){
301 fpu.regs[TOP].d = sqrt(fpu.regs[TOP].d);
302 //flags and such :)
303 return;
304 }
305 static void FPU_FPATAN(void){
306 fpu.regs[STV(1)].d = atan2(fpu.regs[STV(1)].d,fpu.regs[TOP].d);
307 FPU_FPOP();
308 //flags and such :)
309 return;
310 }
311 static void FPU_FPTAN(void){
312 fpu.regs[TOP].d = tan(fpu.regs[TOP].d);
313 FPU_PUSH(1.0);
314 FPU_SET_C2(0);
315 //flags and such :)
316 return;
317 }
318 static void FPU_FDIV(Bitu st, Bitu other){
319 fpu.regs[st].d= fpu.regs[st].d/fpu.regs[other].d;
320 //flags and such :)
321 return;
322 }
323
324 static void FPU_FDIVR(Bitu st, Bitu other){
325 fpu.regs[st].d= fpu.regs[other].d/fpu.regs[st].d;
326 // flags and such :)
327 return;
328 }
329
330 static void FPU_FMUL(Bitu st, Bitu other){
331 fpu.regs[st].d*=fpu.regs[other].d;
332 //flags and such :)
333 return;
334 }
335
336 static void FPU_FSUB(Bitu st, Bitu other){
337 fpu.regs[st].d = fpu.regs[st].d - fpu.regs[other].d;
338 //flags and such :)
339 return;
340 }
341
342 static void FPU_FSUBR(Bitu st, Bitu other){
343 fpu.regs[st].d= fpu.regs[other].d - fpu.regs[st].d;
344 //flags and such :)
345 return;
346 }
347
348 static void FPU_FXCH(Bitu st, Bitu other){
349 FPU_Tag tag = fpu.tags[other];
350 FPU_Reg reg = fpu.regs[other];
351 fpu.tags[other] = fpu.tags[st];
352 fpu.regs[other] = fpu.regs[st];
353 fpu.tags[st] = tag;
354 fpu.regs[st] = reg;
355 }
356
357 static void FPU_FST(Bitu st, Bitu other){
358 fpu.tags[other] = fpu.tags[st];
359 fpu.regs[other] = fpu.regs[st];
360 }
361
362
363 static void FPU_FCOM(Bitu st, Bitu other){
364 if(((fpu.tags[st] != TAG_Valid) && (fpu.tags[st] != TAG_Zero)) ||
365 ((fpu.tags[other] != TAG_Valid) && (fpu.tags[other] != TAG_Zero))){
366 FPU_SET_C3(1);FPU_SET_C2(1);FPU_SET_C0(1);return;
367 }
368 if(fpu.regs[st].d == fpu.regs[other].d){
369 FPU_SET_C3(1);FPU_SET_C2(0);FPU_SET_C0(0);return;
370 }
371 if(fpu.regs[st].d < fpu.regs[other].d){
372 FPU_SET_C3(0);FPU_SET_C2(0);FPU_SET_C0(1);return;
373 }
374 // st > other
375 FPU_SET_C3(0);FPU_SET_C2(0);FPU_SET_C0(0);return;
376 }
377
378 static void FPU_FUCOM(Bitu st, Bitu other){
379 //does atm the same as fcom
380 FPU_FCOM(st,other);
381 }
382
383 static void FPU_FRNDINT(void){
384 Bit64s temp= static_cast<Bit64s>(FROUND(fpu.regs[TOP].d));
385 fpu.regs[TOP].d=static_cast<double>(temp);
386 }
387
388 static void FPU_FPREM(void){
389 Real64 valtop = fpu.regs[TOP].d;
390 Real64 valdiv = fpu.regs[STV(1)].d;
391 Bit64s ressaved = static_cast<Bit64s>( (valtop/valdiv) );
392 // Some backups
393 // Real64 res=valtop - ressaved*valdiv;
394 // res= fmod(valtop,valdiv);
395 fpu.regs[TOP].d = valtop - ressaved*valdiv;
396 FPU_SET_C0(static_cast<Bitu>(ressaved&4));
397 FPU_SET_C3(static_cast<Bitu>(ressaved&2));
398 FPU_SET_C1(static_cast<Bitu>(ressaved&1));
399 FPU_SET_C2(0);
400 }
401
402 static void FPU_FPREM1(void){
403 Real64 valtop = fpu.regs[TOP].d;
404 Real64 valdiv = fpu.regs[STV(1)].d;
405 double quot = valtop/valdiv;
406 double quotf = floor(quot);
407 Bit64s ressaved;
408 if (quot-quotf>0.5) ressaved = static_cast<Bit64s>(quotf+1);
409 else if (quot-quotf<0.5) ressaved = static_cast<Bit64s>(quotf);
410 else ressaved = static_cast<Bit64s>((((static_cast<Bit64s>(quotf))&1)!=0)?(quotf+1):(quotf));
411 fpu.regs[TOP].d = valtop - ressaved*valdiv;
412 FPU_SET_C0(static_cast<Bitu>(ressaved&4));
413 FPU_SET_C3(static_cast<Bitu>(ressaved&2));
414 FPU_SET_C1(static_cast<Bitu>(ressaved&1));
415 FPU_SET_C2(0);
416 }
417
418 static void FPU_FXAM(void){
419 if(fpu.regs[TOP].ll & LONGTYPE(0x8000000000000000)) //sign
420 {
421 FPU_SET_C1(1);
422 }
423 else
424 {
425 FPU_SET_C1(0);
426 }
427 if(fpu.tags[TOP] == TAG_Empty)
428 {
429 FPU_SET_C3(1);FPU_SET_C2(0);FPU_SET_C0(1);
430 return;
431 }
432 if(fpu.regs[TOP].d == 0.0) //zero or normalized number.
433 {
434 FPU_SET_C3(1);FPU_SET_C2(0);FPU_SET_C0(0);
435 }
436 else
437 {
438 FPU_SET_C3(0);FPU_SET_C2(1);FPU_SET_C0(0);
439 }
440 }
441
442
443 static void FPU_F2XM1(void){
444 fpu.regs[TOP].d = pow(2.0,fpu.regs[TOP].d) - 1;
445 return;
446 }
447
448 static void FPU_FYL2X(void){
449 fpu.regs[STV(1)].d*=log(fpu.regs[TOP].d)/log(static_cast<Real64>(2.0));
450 FPU_FPOP();
451 return;
452 }
453
454 static void FPU_FYL2XP1(void){
455 fpu.regs[STV(1)].d*=log(fpu.regs[TOP].d+1.0)/log(static_cast<Real64>(2.0));
456 FPU_FPOP();
457 return;
458 }
459
460 static void FPU_FSCALE(void){
461 fpu.regs[TOP].d *= pow(2.0,static_cast<Real64>(static_cast<Bit64s>(fpu.regs[STV(1)].d)));
462 return; //2^x where x is chopped.
463 }
464
465 static void FPU_FSTENV(PhysPt addr){
466 FPU_SET_TOP(TOP);
467 if(!cpu.code.big) {
468 mem_writew(addr+0,static_cast<Bit16u>(fpu.cw));
469 mem_writew(addr+2,static_cast<Bit16u>(fpu.sw));
470 mem_writew(addr+4,static_cast<Bit16u>(FPU_GetTag()));
471 } else {
472 mem_writed(addr+0,static_cast<Bit32u>(fpu.cw));
473 mem_writed(addr+4,static_cast<Bit32u>(fpu.sw));
474 mem_writed(addr+8,static_cast<Bit32u>(FPU_GetTag()));
475 }
476 }
477
478 static void FPU_FLDENV(PhysPt addr){
479 Bit16u tag;
480 Bit32u tagbig;
481 Bitu cw;
482 if(!cpu.code.big) {
483 cw = mem_readw(addr+0);
484 fpu.sw = mem_readw(addr+2);
485 tag = mem_readw(addr+4);
486 } else {
487 cw = mem_readd(addr+0);
488 fpu.sw = (Bit16u)mem_readd(addr+4);
489 tagbig = mem_readd(addr+8);
490 tag = static_cast<Bit16u>(tagbig);
491 }
492 FPU_SetTag(tag);
493 FPU_SetCW(cw);
494 TOP = FPU_GET_TOP();
495 }
496
497 static void FPU_FSAVE(PhysPt addr){
498 FPU_FSTENV(addr);
499 Bitu start = (cpu.code.big?28:14);
500 for(Bitu i = 0;i < 8;i++){
501 FPU_ST80(addr+start,STV(i));
502 start += 10;
503 }
504 FPU_FINIT();
505 }
506
507 static void FPU_FRSTOR(PhysPt addr){
508 FPU_FLDENV(addr);
509 Bitu start = (cpu.code.big?28:14);
510 for(Bitu i = 0;i < 8;i++){
511 fpu.regs[STV(i)].d = FPU_FLD80(addr+start);
512 start += 10;
513 }
514 }
515
516 static void FPU_FXTRACT(void) {
517 // function stores real bias in st and
518 // pushes the significant number onto the stack
519 // if double ever uses a different base please correct this function
520
521 FPU_Reg test = fpu.regs[TOP];
522 Bit64s exp80 = test.ll&LONGTYPE(0x7ff0000000000000);
523 Bit64s exp80final = (exp80>>52) - BIAS64;
524 Real64 mant = test.d / (pow(2.0,static_cast<Real64>(exp80final)));
525 fpu.regs[TOP].d = static_cast<Real64>(exp80final);
526 FPU_PUSH(mant);
527 }
528
529 static void FPU_FCHS(void){
530 fpu.regs[TOP].d = -1.0*(fpu.regs[TOP].d);
531 }
532
533 static void FPU_FABS(void){
534 fpu.regs[TOP].d = fabs(fpu.regs[TOP].d);
535 }
536
537 static void FPU_FTST(void){
538 fpu.regs[8].d = 0.0;
539 FPU_FCOM(TOP,8);
540 }
541
542 static void FPU_FLD1(void){
543 FPU_PREP_PUSH();
544 fpu.regs[TOP].d = 1.0;
545 }
546
547 static void FPU_FLDL2T(void){
548 FPU_PREP_PUSH();
549 fpu.regs[TOP].d = L2T;
550 }
551
552 static void FPU_FLDL2E(void){
553 FPU_PREP_PUSH();
554 fpu.regs[TOP].d = L2E;
555 }
556
557 static void FPU_FLDPI(void){
558 FPU_PREP_PUSH();
559 fpu.regs[TOP].d = PI;
560 }
561
562 static void FPU_FLDLG2(void){
563 FPU_PREP_PUSH();
564 fpu.regs[TOP].d = LG2;
565 }
566
567 static void FPU_FLDLN2(void){
568 FPU_PREP_PUSH();
569 fpu.regs[TOP].d = LN2;
570 }
571
572 static void FPU_FLDZ(void){
573 FPU_PREP_PUSH();
574 fpu.regs[TOP].d = 0.0;
575 fpu.tags[TOP] = TAG_Zero;
576 }
577
578
579 static INLINE void FPU_FADD_EA(Bitu op1){
580 FPU_FADD(op1,8);
581 }
582 static INLINE void FPU_FMUL_EA(Bitu op1){
583 FPU_FMUL(op1,8);
584 }
585 static INLINE void FPU_FSUB_EA(Bitu op1){
586 FPU_FSUB(op1,8);
587 }
588 static INLINE void FPU_FSUBR_EA(Bitu op1){
589 FPU_FSUBR(op1,8);
590 }
591 static INLINE void FPU_FDIV_EA(Bitu op1){
592 FPU_FDIV(op1,8);
593 }
594 static INLINE void FPU_FDIVR_EA(Bitu op1){
595 FPU_FDIVR(op1,8);
596 }
597 static INLINE void FPU_FCOM_EA(Bitu op1){
598 FPU_FCOM(op1,8);
599 }
600