fpu.c 29.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 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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
/*
 * Copyright (C) 1996-1998 by the Board of Trustees
 *    of Leland Stanford Junior University.
 * 
 * This file is part of the SimOS distribution. 
 * See LICENSE file for terms of the license. 
 *
 */

/*****************************************************************
 * fpu.c
 * 
 * $Author: blythe $
 * $Date: 2002/05/29 01:09:10 $
 *****************************************************************/
#include <math.h>
#include <limits.h>
#ifdef __alpha
#include <float.h>
#else
# ifndef i386
# include <ieeefp.h>
# endif
#endif
#include "mipsy.h"
#include "cpu.h"
#include "cpu_state.h"
#include "sim_error.h"
#include "machine_params.h"
#include "cp0.h"
#include "fpu.h"
#include "cpu_stats.h"

extern Result MipsyPrefetch(CPUState *P, VA vAddr, uint hint);
/*
 * CHECK_64BIT_ALLOWED() - Check to see if 64bit instructions are permitted,
 *                         if not raise an exception. 
 */
#if defined(SIM_MIPS64)
#define CHECK_64BIT_ALLOWED(_p)  \
      if ((_p)->is32bitMode && !IS_KERNEL_MODE((_p))) {  \
         EXCEPTION((_p), EXC_II);                 \
         return C1_CONTINUE;                             \
      }
#else
#define CHECK_64BIT_ALLOWED(_p) \
      if (1) {                                           \
         EXCEPTION((_p), EXC_II);                 \
         return C1_CONTINUE;                             \
      }

#endif

/* Enumeration of floating point exception types */
typedef enum { FPE_I = 0, FPE_U, FPE_O, FPE_Z, FPE_V, FPE_E, FPECount } FPUException;
static int FPCSRFlag[FPECount] = {
   0x00000004, 0x00000008, 0x00000010, 0x00000020, 0x00000040, 0x00000000
};
static int FPCSREnable[FPECount] = {
   0x00000080, 0x00000100, 0x00000200, 0x00000400, 0x00000800, 0x00000000
};
static int FPCSRCause[FPECount] = {
   0x00001000, 0x00002000, 0x00004000, 0x00008000, 0x00010000, 0x00020000
};

/*****************************************************************
 * WARNING: An ugly feature of the sgi IEEE fp implementation greatly
 * complicates this c-based fpu model. FP operations on doubles take
 * place in a reversed order in the registers than they would in
 * memory. As a result, operations on doubles in my memory-based fp
 * registers would all be word-swapped. As a result, I swap every
 * single float reference so that reg 1 is in reg 0's place in memory
 * and vice versa. It's a pain, but will speed up the double
 * operations. 
 * 
 * There are a few side effects of this change:
 * -> The "real" fpu state must be copied into mipsy's special
 *    structure and out of it on entry and exit.
 * -> Gdb get reg needs to be aware of the special state
 * -> People debugging the fpu should be aware that FPR doesn't
 *    have the most up-to-date copy!!!
 ****************************************************************/
 
void
MipsyInitFPU(void)
{
   CPUState *P;
   int i,j;
   
   for (i=0; i < TOTAL_CPUS; i++) {
      P = &PE[i];
      for (j=0; j < NUM_FP_REGS; j++) {
#ifdef NEW_FP_REGS
         LongLongReg(j) = P->FPR[j];
#else
         IntReg(j) = P->FPR[j];
#endif
      }
   }
}

void 
MipsyExitFPU(void)
{
   CPUState *P;
   int i,j;
   
   for (i=0; i < TOTAL_CPUS; i++) {
      P = &PE[i];
      for (j=0; j < NUM_FP_REGS; j++) {
#ifdef NEW_FP_REGS
         P->FPR[j] = LongLongReg(j);
#else
         P->FPR[j] = IntReg(j);
#endif
      }
   }
}

static Result
MipsyFPUException(CPUState *P, FPUException type)
{
   P->FCR[31] |= FPCSRFlag[type];
   P->FCR[31] |= FPCSRCause[type];
   if (P->FCR[31] & FPCSREnable[type]) {
      EXCEPTION(P, EXC_FPE);
      return C1_CONTINUE;
   } else {
      return C1_SUCCESS;
   }
}

/*****************************************************************
 * MipsyFPU
 *****************************************************************/
static Result 
MipsyFPU(CPUState *P, Inst instr) 
{
   uint fmt = FORMAT(instr);
   uint ft  = FT(instr); /* 2nd source register */
   uint fs  = FS(instr); /* 1st source reg */
   uint fd  = FD(instr); /* Destination */
   uint func = FUNC(instr);
   StatusReg statusReg;

   /* Clear the exception flags from the FCR */
   P->FCR[31] &= ~FPCSR_EXCEPTIONS;
   
   switch(func+(fmt<<8)) {
      
   case fabs_op+F_SSINGLE:
      FloatReg(fd) = (FloatReg(fs) >= 0.0 ? FloatReg(fs) : -FloatReg(fs));
      SIM_DEBUG(('f', "C1:ABS.S: f%d %e becomes f%d %e\n", fs, FloatReg(fs), fd, 
                 FloatReg(fd)));
      break;
   case fabs_op+F_SDOUBLE:
      DoubleReg(fd) = (DoubleReg(fs) >= 0.0 ? DoubleReg(fs) : -DoubleReg(fs));
      SIM_DEBUG(('f', "C1:ABS.D: f%d %Le becomes f%d %Le\n", fs, DoubleReg(fs), fd, 
                 DoubleReg(fd)));
      break;
      
   case fadd_op+F_SSINGLE:
      FloatReg(fd) = FloatReg(fs) + FloatReg(ft);
      SIM_DEBUG(('f', "C1:ADD.S: f%d %e + f%d %e = f%d %e\n", fs, FloatReg(fs), ft, 
                 FloatReg(ft), fd, FloatReg(fd)));
      break;
   case fadd_op+F_SDOUBLE:
      DoubleReg(fd) = DoubleReg(fs) + DoubleReg(ft);
      SIM_DEBUG(('f', "C1:ADD.D: f%d %Le + f%d %Le = f%d %Le\n", fs, DoubleReg(fs), ft, 
                 DoubleReg(ft), fd, DoubleReg(fd)));
      break;

   case fcvtd_op+F_SSINGLE:
      DoubleReg(fd) = FloatReg(fs);
      SIM_DEBUG(('f', "C1:CVTD.S: f%d %e becomes f%d %Le\n", fs, FloatReg(fs), fd, 
                 DoubleReg(fd))); 
      break;
   case fcvtd_op+F_W: {
      int old = IntReg(fs);

      DoubleReg(fd) = (double)old;
      SIM_DEBUG(('f', "C1:CVTD.W: f%d %d becomes f%d %Le\n", fs, old, fd, 
                 DoubleReg(fd)));
      }
      break;

   case fcvts_op+F_SDOUBLE:
      FloatReg(fd) = DoubleReg(fs);
      SIM_DEBUG(('f', "C1:CVTS.D: f%d %Le becomes f%d %e\n", fs, DoubleReg(fs), fd, 
            FloatReg(fd)));
      break;
     
   case fcvts_op+F_W:
      FloatReg(fd) = IntReg(fs);
      SIM_DEBUG(('f', "C1:CVTS.W: f%d %d becomes f%d %e\n", fs, IntReg(fs), fd, 
                FloatReg(fd)));
      break;

   case fcvts_op+F_L:
      FloatReg(fd) = LongLongReg(fs);
      /*CPUWarning("MIPSY: cvt.s.l @%#llx (partial impl.)\n", P->PC);*/
      SIM_DEBUG(('f', "C1:CVTS.L: f%d %lld becomes f%d %e\n", fs,
		 LongLongReg(fs), fd, FloatReg(fd)));
      break;

   case fcvtd_op+F_L:
      DoubleReg(fd) = LongLongReg(fs);
      /*CPUWarning("MIPSY: cvt.d.l @%#llx (partial impl.)\n", P->PC);*/
      SIM_DEBUG(('f', "C1:CVTD.L: f%d %lld becomes f%d %Le\n", fs,
		 LongLongReg(fs), fd, DoubleReg(fd)));
      break;

   case fcvtl_op+F_SSINGLE: {
      uint roundMode = P->FCR[31] & 0x03;
      CHECK_64BIT_ALLOWED(P);
      switch (roundMode) {
      case 0:                 /* RN: Round to nearest         */
         LongLongReg(fd) = (FloatReg(fs) > 0.0 ? FloatReg(fs) + 0.5 : FloatReg(fs) - 0.5 );
         break;
         
      case 1:                 /* RZ: Round toward 0           */
         LongLongReg(fd) = FloatReg(fs);
         break;
         
      case 2:                 /* RP: Round to +infinity       */
         LongLongReg(fd) = (FloatReg(fs) >= 0.0 ? 
                     FloatReg(fs) + SP_NEAR_ONE : 
                     FloatReg(fs));
         break;
         
      case 3:                 /* RM: Round to -infinity       */
         LongLongReg(fd) = (FloatReg(fs) >= 0.0 ? 
                     FloatReg(fs) :
                     FloatReg(fs) - SP_NEAR_ONE);
         break;
      }

      SIM_DEBUG(('f', "C1:CVTW.S: f%d %e becomes f%d %d rm = %d\n", 
                 fs, FloatReg(fs), fd, IntReg(fd), roundMode));
      break;
   }
      
   case fcvtl_op+F_SDOUBLE: {
      uint roundMode = P->FCR[31] & 0x03;
      CHECK_64BIT_ALLOWED(P);
      switch (roundMode) {
      case 0:                 /* RN: Round to nearest         */
         LongLongReg(fd) = (DoubleReg(fs) > 0.0 ? DoubleReg(fs) + 0.5 : DoubleReg(fs) - 0.5 );
         break;
         
      case 1:                 /* RZ: Round toward 0           */
         LongLongReg(fd) = DoubleReg(fs);
         break;
         
      case 2:                 /* RP: Round to +infinity       */
         LongLongReg(fd) = (DoubleReg(fs) >= 0.0 ? 
                     DoubleReg(fs) + DP_NEAR_ONE : 
                     DoubleReg(fs));
         break;
         
      case 3:                 /* RM: Round to -infinity       */
         LongLongReg(fd) = (DoubleReg(fs) >= 0.0 ? 
                     DoubleReg(fs) :
                     DoubleReg(fs) - DP_NEAR_ONE);
         break;
      }
      SIM_DEBUG(('f', "C1:CVTL.D: f%d %Le becomes f%d %d rm = %d\n", 
                 fs, DoubleReg(fs), fd, IntReg(fd), roundMode));
      break;
   }

   case fcvtw_op+F_SSINGLE: {
      uint roundMode = P->FCR[31] & 0x03;
      float rounded;
      switch (roundMode) {
      case 0:                 /* RN: Round to nearest         */
         rounded = (FloatReg(fs) > 0.0 ? FloatReg(fs) + 0.5 : FloatReg(fs) - 0.5 );
         break;
         
      case 1:                 /* RZ: Round toward 0           */
         rounded = FloatReg(fs);
         break;
         
      case 2:                 /* RP: Round to +infinity       */
         rounded = (FloatReg(fs) >= 0.0 ? 
                     FloatReg(fs) + SP_NEAR_ONE : 
                     FloatReg(fs));
         break;
         
      case 3:                 /* RM: Round to -infinity       */
         rounded = (FloatReg(fs) >= 0.0 ? 
                     FloatReg(fs) :
                     FloatReg(fs) - SP_NEAR_ONE);
         break;
      }

      IntReg(fd) = rounded;
      SIM_DEBUG(('f', "C1:CVTW.S: f%d %e becomes f%d %d rm = %d\n", 
                 fs, FloatReg(fs), fd, IntReg(fd), roundMode));

      /* Check for valid operand */
      if ((rounded > (float)INT_MAX) || (rounded < (float)INT_MIN)) {
         int ret = MipsyFPUException(P, FPE_V);
         return ret;
      }
      break;
   }
      
   case fcvtw_op+F_SDOUBLE: {
      uint roundMode = P->FCR[31] & 0x03;
      double rounded;

      switch (roundMode) {
      case 0:                 /* RN: Round to nearest         */
         rounded = (DoubleReg(fs) > 0.0 ? DoubleReg(fs) + 0.5 : DoubleReg(fs) - 0.5 );
         break;
         
      case 1:                 /* RZ: Round toward 0           */
         rounded = DoubleReg(fs);
         break;
         
      case 2:                 /* RP: Round to +infinity       */
         rounded = (DoubleReg(fs) >= 0.0 ? 
                     DoubleReg(fs) + DP_NEAR_ONE : 
                     DoubleReg(fs));
         break;
         
      case 3:                 /* RM: Round to -infinity       */
         rounded = (DoubleReg(fs) >= 0.0 ? 
                     DoubleReg(fs) :
                     DoubleReg(fs) - DP_NEAR_ONE);
         break;
      }

      IntReg(fd) = rounded;
      SIM_DEBUG(('f', "C1:CVTW.D: f%d %Le becomes f%d %d rm = %d\n", 
                 fs, DoubleReg(fs), fd, IntReg(fd), roundMode));


      /* Check for valid operand */
      if ((rounded > (double)INT_MAX) || (rounded < (double)INT_MIN)) {
         int ret = MipsyFPUException(P, FPE_V);
         return ret;
      }

      break;
   }

   case fdiv_op+F_SSINGLE:
      if (FloatReg(ft) == 0.0) {
         CPUWarning("MIPSY: FDIV divide by zero @%#llx\n", P->PC);
      } 
      FloatReg(fd) = FloatReg(fs) / FloatReg(ft);
      SIM_DEBUG(('f', "C1:FDIV.S: f%d %e / f%d %e = f%d %e\n", 
                 fs, FloatReg(fs), ft, FloatReg(ft), fd, FloatReg(fd)));
      break;
   case fdiv_op+F_SDOUBLE:
      if (DoubleReg(ft) == 0.0) {
         CPUWarning("MIPSY: FDIV divide by zero\n");
      } 
      DoubleReg(fd) = DoubleReg(fs) / DoubleReg(ft);
      SIM_DEBUG(('f', "C1:FDIV.D f%d %Le / f%d %Le = f%d %Le\n", fs, DoubleReg(fs), ft, 
                 DoubleReg(ft), fd, DoubleReg(fd)));
      break;

   case fmov_op+F_SSINGLE:
      FloatReg(fd) = FloatReg(fs);
      SIM_DEBUG(('f', "C1:FMOV.S %e to %e\n", FloatReg(fs), 
                 FloatReg(fd)));
      break;  
   case fmov_op+F_SDOUBLE:
      DoubleReg(fd) = DoubleReg(fs);
      SIM_DEBUG(('f', "C1:FMOV.D %Le to %Le\n", DoubleReg(fs), 
                 DoubleReg(fd)));
      break;  

   case fmovc_op+F_SSINGLE:
      /* Move conditional on FP condition */
      if (TF(instr)) { 
         if (P->FCR[31] & M_csr_c) {
            FloatReg(fd) = FloatReg(fs);
         }
      } else {
         if (!(P->FCR[31] & M_csr_c)) {
            FloatReg(fd) = FloatReg(fs);
         }
      }
      break;
   case fmovc_op+F_SDOUBLE:
      /* Move conditional on FP condition */
      if (TF(instr)) { 
         if (P->FCR[31] & M_csr_c) {
            DoubleReg(fd) = DoubleReg(fs);
         }
      } else {
         if (!(P->FCR[31] & M_csr_c)) {
            DoubleReg(fd) = DoubleReg(fs);
         }
      }
      break;

   case fmovn_op+F_SSINGLE:
      if (P->R[ft] != 0) { 
         FloatReg(fd) = FloatReg(fs);
      }
      SIM_DEBUG(('f', "C1:FMOVN.S %e to %e\n", FloatReg(fs), FloatReg(fd)));
      break;  
   case fmovn_op+F_SDOUBLE:
      if (P->R[ft] != 0) { 
         DoubleReg(fd) = DoubleReg(fs);
      }
      SIM_DEBUG(('f', "C1:FMOVN.D %Le to %Le\n", DoubleReg(fs), DoubleReg(fd)));
      break;  

   case fmovz_op+F_SSINGLE:
      if (P->R[ft] == 0) { 
         FloatReg(fd) = FloatReg(fs);
      }
      SIM_DEBUG(('f', "C1:FMOVZ.S %e to %e\n", FloatReg(fs), FloatReg(fd)));
      break;  
   case fmovz_op+F_SDOUBLE:
      if (P->R[ft] == 0) { 
         DoubleReg(fd) = DoubleReg(fs);
      }
      SIM_DEBUG(('f', "C1:FMOVZ.D %Le to %Le\n", DoubleReg(fs), DoubleReg(fd)));
      break;  

   case fmul_op+F_SSINGLE:
      FloatReg(fd) = FloatReg(fs) * FloatReg(ft);
      SIM_DEBUG(('f', "C1:FMUL.S: f%d %e * f%d %e = f%d %e\n", 
                 fs, FloatReg(fs), ft, FloatReg(ft), fd, FloatReg(fd)));
      break;
   case fmul_op+F_SDOUBLE:
      DoubleReg(fd) = DoubleReg(fs) * DoubleReg(ft);
      SIM_DEBUG(('f', "C1:FMUL.D: f%d %Le * f%d %Le = f%d %Le\n", fs, DoubleReg(fs), ft, 
                 DoubleReg(ft), fd, DoubleReg(fd)));
      break;

   case fneg_op+F_SSINGLE:
      FloatReg(fd) = -FloatReg(fs);
      SIM_DEBUG(('f', "C1:NEG.S: f%d %e becomes f%d %e\n", 
                 fs, FloatReg(fs), fd, FloatReg(fd)));
      break;
   case fneg_op+F_SDOUBLE:
      DoubleReg(fd) = -DoubleReg(fs);
      SIM_DEBUG(('f', "C1:NEG.D: f%d %Le becomes f%d %Le\n", 
                 fs, FloatReg(fs), fd, FloatReg(fd)));
      break;

   case fsub_op+F_SSINGLE:
      FloatReg(fd) = FloatReg(fs) - FloatReg(ft);
      SIM_DEBUG(('f', "C1:SUB.S: f%d %e - f%d %e = f%d %e\n", 
                 fs, FloatReg(fs), ft, FloatReg(ft), fd, FloatReg(fd)));
      break;
   case fsub_op+F_SDOUBLE:
      DoubleReg(fd) = DoubleReg(fs) - DoubleReg(ft);
      SIM_DEBUG(('f', "C1:SUB.D: f%d %Le - f%d %Le = f%d %Le\n", 
                 fs, DoubleReg(fs), ft, DoubleReg(ft), fd, DoubleReg(fd)));
      break;
    
   case fsqrt_op+F_SSINGLE:	
      FloatReg(fd) = (float)sqrt((double)FloatReg(fs));
      break;
   case fsqrt_op+F_SDOUBLE:
      DoubleReg(fd) = sqrt (DoubleReg(fs));
      break;

   case ftrunc_op+F_SSINGLE:
      IntReg(fd) = FloatReg(fs);
      break;
   case ftrunc_op+F_SDOUBLE:
      IntReg(fd) = DoubleReg(fs);
      break;

   case ftruncl_op+F_SSINGLE:
      CHECK_64BIT_ALLOWED(P);
      LongLongReg(fd) = FloatReg(fs);
      break;
   case ftruncl_op+F_SDOUBLE:
      CHECK_64BIT_ALLOWED(P);
      LongLongReg(fd) = DoubleReg(fs);
      break;

   case ffloorl_op+F_SSINGLE:
      IntReg(fd) = (FloatReg(fs) >= 0.0 ? FloatReg(fs) : FloatReg(fs) - SP_NEAR_ONE );
      break;
   case ffloorl_op+F_SDOUBLE:
      IntReg(fd) = (DoubleReg(fs) >= 0.0 ? DoubleReg(fs) : DoubleReg(fs) - DP_NEAR_ONE );
      break;

   case frecip_op+F_SSINGLE: {
      statusReg.ts_data = P->CP0[C0_SR];
      /* Make sure we can execute mips4 instructions */
      if (IS_USER_MODE(P) && !(statusReg.s32.ts_xx)) {
         EXCEPTION(P, EXC_II); 
         return C1_CONTINUE;
      }
      if (FloatReg(fs) == 0.0) {
         CPUWarning("MIPSY: FDIV divide by zero\n");
      } 
      FloatReg(fd) = 1.0 / FloatReg(fs);
      SIM_DEBUG(('f', "C1:FRECIP.S: f%d %e / f%d %e = f%d %e\n", 
                 fs, FloatReg(fs), ft, FloatReg(ft), fd, FloatReg(fd)));
      break;
   }
   case frecip_op+F_SDOUBLE: {
      statusReg.ts_data = P->CP0[C0_SR];
      /* Make sure we can execute mips4 instructions */
      if (IS_USER_MODE(P) && !(statusReg.s32.ts_xx)) {
         EXCEPTION(P, EXC_II); 
         return C1_CONTINUE;
      }
      if (DoubleReg(fs) == 0.0) {
         CPUWarning("MIPSY: FDIV divide by zero\n");
      } 
      DoubleReg(fd) = 1.0 / DoubleReg(fs);
      SIM_DEBUG(('f', "C1:FRECIP.D f%d %Le / f%d %Le = f%d %Le\n", fs, DoubleReg(fs), ft, 
                 DoubleReg(ft), fd, DoubleReg(fd)));
      break;
   }
   case frsqrt_op+F_SSINGLE:
      statusReg.ts_data = P->CP0[C0_SR];
      /* Make sure we can execute mips4 instructions */
      if (IS_USER_MODE(P) && !(statusReg.s32.ts_xx)) {
         EXCEPTION(P, EXC_II); 
         return C1_CONTINUE;
      }
      if (FloatReg(fs) == 0.0) {
         CPUWarning("MIPSY: FRSQRT divide by zero\n");
      } 
      FloatReg(fd) = 1.0 / (float)sqrt((double)FloatReg(fs));
      break;
      
   case frsqrt_op+F_SDOUBLE:
      statusReg.ts_data = P->CP0[C0_SR];
      /* Make sure we can execute mips4 instructions */
      if (IS_USER_MODE(P) && !(statusReg.s32.ts_xx)) {
         EXCEPTION(P, EXC_II); 
         return C1_CONTINUE;
      }
      if (DoubleReg(fs) == 0.0) {
         CPUWarning("MIPSY: FDIV divide by zero\n");
      } 
      DoubleReg(fd) = 1.0 / sqrt(DoubleReg(fs));
      break;

   case fround_op+F_SSINGLE:
      IntReg(fd) = (FloatReg(fs) > 0.0 ? FloatReg(fs) + 0.5 : FloatReg(fs) - 0.5 );
      break;
   case fround_op+F_SDOUBLE:
      IntReg(fd) = (DoubleReg(fs) > 0.0 ? DoubleReg(fs) + 0.5 : DoubleReg(fs) - 0.5 );
      break;

   case froundl_op+F_SSINGLE:
      CHECK_64BIT_ALLOWED(P);
      LongLongReg(fd) = (FloatReg(fs) > 0.0 ? FloatReg(fs) + 0.5 : FloatReg(fs) - 0.5 );
      break;
   case froundl_op+F_SDOUBLE:
      CHECK_64BIT_ALLOWED(P);
      LongLongReg(fd) = (DoubleReg(fs) > 0.0 ? DoubleReg(fs) + 0.5 : DoubleReg(fs) - 0.5 );
      break;

   case fceil_op+F_SSINGLE:
      IntReg(fd) = (FloatReg(fs) >= 0.0 ? FloatReg(fs) + SP_NEAR_ONE : FloatReg(fs) );
      break;
   case fceil_op+F_SDOUBLE:
      IntReg(fd) = (DoubleReg(fs) >= 0.0 ? DoubleReg(fs) + DP_NEAR_ONE : DoubleReg(fs) );
      break;

   case fceill_op+F_SSINGLE:
      CHECK_64BIT_ALLOWED(P);
      LongLongReg(fd) = (FloatReg(fs) >= 0.0 ? FloatReg(fs) + SP_NEAR_ONE : FloatReg(fs) );
      break;
   case fceill_op+F_SDOUBLE:
      CHECK_64BIT_ALLOWED(P);
      LongLongReg(fd) = (DoubleReg(fs) >= 0.0 ? DoubleReg(fs) + DP_NEAR_ONE : DoubleReg(fs) );
      break;

      /* floating point comparison operations */

   case F_C_EQ+F_SSINGLE:
      if (FloatReg(fs) == FloatReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;
   case F_C_EQ+F_SDOUBLE:
      if (DoubleReg(fs) == DoubleReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;

   case F_C_NGL+F_SSINGLE:
      if (FloatReg(fs) == FloatReg(ft))
         IntReg(fd) |= CONDBIT;
      else
         P->FCR[31] &= ~M_csr_c;
      break;
   case F_C_NGL+F_SDOUBLE:
      if (DoubleReg(fs) == DoubleReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;
      
   case F_C_LE+F_SSINGLE:
      if (FloatReg(fs) <= FloatReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;
   case F_C_LE+F_SDOUBLE:
      if (DoubleReg(fs) <= DoubleReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;

   case F_C_LT+F_SSINGLE:
      if (FloatReg(fs) < FloatReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;
   case F_C_LT+F_SDOUBLE:
      if (DoubleReg(fs) < DoubleReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;

   case F_C_NGE+F_SSINGLE:
      if (FloatReg(fs) < FloatReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;
   case F_C_NGE+F_SDOUBLE:
      if (DoubleReg(fs) < DoubleReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;

   case F_C_NGLE+F_SSINGLE:
      P->FCR[31] &= ~M_csr_c;
      break;
   case F_C_NGLE+F_SDOUBLE:
      P->FCR[31] &= ~M_csr_c;
      break;

   case F_C_NGT+F_SSINGLE:
      if (FloatReg(fs) <= FloatReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;
   case F_C_NGT+F_SDOUBLE:
      if (DoubleReg(fs) <= DoubleReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;

   case F_C_OLE+F_SSINGLE:
      if (FloatReg(fs) < FloatReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;
   case F_C_OLE+F_SDOUBLE:
      if (DoubleReg(fs) < DoubleReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;

   case F_C_OLT+F_SSINGLE:
      if (FloatReg(fs) < FloatReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;
   case F_C_OLT+F_SDOUBLE:

     if (DoubleReg(fs) < DoubleReg(ft)) {
       P->FCR[31] |= M_csr_c;
     } else {
       P->FCR[31] &= ~M_csr_c;
     }
     break;

   case F_C_SEQ+F_SSINGLE:
      if (FloatReg(fs) == FloatReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;

   case F_C_SEQ+F_SDOUBLE:
      if (DoubleReg(fs) == DoubleReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;

   case F_C_SF+F_SSINGLE:
      P->FCR[31] &= ~M_csr_c;
      break;
   case F_C_SF+F_SDOUBLE:
      P->FCR[31] &= ~M_csr_c;
      break;

   case F_C_UEQ+F_SSINGLE:
      if (FloatReg(fs) == FloatReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;
   case F_C_UEQ+F_SDOUBLE:
      if (DoubleReg(fs) == DoubleReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;

   case F_C_ULE+F_SSINGLE:
      if (FloatReg(fs) <= FloatReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;
   case F_C_ULE+F_SDOUBLE:
      if (DoubleReg(fs) <= DoubleReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;

   case F_C_ULT+F_SSINGLE:
      if (FloatReg(fs) < FloatReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;
   case F_C_ULT+F_SDOUBLE:
      if (DoubleReg(fs) < DoubleReg(ft))
         P->FCR[31] |= M_csr_c;
      else
         P->FCR[31] &= ~M_csr_c;
      break;

   case F_C_UN+F_SSINGLE:
      P->FCR[31] &= ~M_csr_c;
      break;
   case F_C_UN+F_SDOUBLE:
      P->FCR[31] &= ~M_csr_c;
      break;

   default:
      CPUWarning("ERROR!!! FPU instruction 0x%x unimplemented at PC 0x%llx\n",
		 instr, (Reg64)P->PC);
      ASSERT(0);
   } /* cp1_function */

   return C1_SUCCESS;
}

/*****************************************************************
 * MipsyFPU
 *****************************************************************/
static Result 
MipsyFPUx(CPUState *P, Inst instr) 
{
   uint ft  = FT(instr); /* 2nd source register */
   uint fs  = FS(instr); /* 1st source reg */
   uint fr  = FORMAT(instr); /* 3rd source reg. */
   uint fd  = FD(instr); /* destionation */

   uint func = FUNC(instr);

   switch(func) {
   case madd_s_op:
      FloatReg(fd) = (FloatReg(fs) * FloatReg(ft)) + FloatReg(fr);
      break;
   case madd_d_op:
      DoubleReg(fd) = (DoubleReg(fs) * DoubleReg(ft)) + DoubleReg(fr);
      break;
   case msub_s_op:
      FloatReg(fd) = (FloatReg(fs) * FloatReg(ft)) - FloatReg(fr);
      break;
   case msub_d_op:
      DoubleReg(fd) = (DoubleReg(fs) * DoubleReg(ft)) - DoubleReg(fr);
      break;
   case nmadd_s_op:
      FloatReg(fd) = -((FloatReg(fs) * FloatReg(ft)) + FloatReg(fr));
      break;
   case nmadd_d_op:
      DoubleReg(fd) = -((DoubleReg(fs) * DoubleReg(ft)) + DoubleReg(fr));
      break;
   case nmsub_s_op:
      FloatReg(fd) = -((FloatReg(fs) * FloatReg(ft)) - FloatReg(fr));
      break;
   case nmsub_d_op:
      DoubleReg(fd) = -((DoubleReg(fs) * DoubleReg(ft)) - DoubleReg(fr));
      break;
   default:
      EXCEPTION(P, EXC_II);
      return C1_CONTINUE;
   }
   return C1_SUCCESS;
}

/*****************************************************************
 * Set the rounding mode of the host processor to match that of the
 * simulated machine. This is needed since we're actually executing the
 * instructions on the host platform. 
 *****************************************************************/
static void 
SetRoundingMode(int modeNum)
{
#if defined(sgi) || defined(sparc)
   fp_rnd newMode = FP_RN; 
   
   switch (modeNum) {
   case 0:
      newMode = FP_RN;
      break;
   case 1:
      newMode = FP_RZ;
      break;
   case 2:
      newMode = FP_RP;
      break;
   case 3:
      newMode = FP_RM;
      break;
   default:
      CPUWarning("Bad rounding mode set in FCR31 (%d)\n", modeNum);
      break;
   }
   fpsetround(newMode);
#endif
#ifdef __alpha
   write_rnd(modeNum);
#endif
#ifdef linux
   {
      static int fpwarned = 0;
      if (!fpwarned) {
         CPUWarning("MIPSY: could not figure out how to fpsetround on x86 \n");
         fpwarned = 1;
      }
   }
#endif
}

/*****************************************************************
 * All fpu (COP1) insts come through here.
 *****************************************************************/
int 
ExecuteC1Instruction(CPUState *P, Inst instr)
{
   int immediate   = IMMED(instr);
   uint fs  = FS(instr); /* 1st source reg */
   uint ft  = FT(instr); /* 2nd source register */
   uint sub_value = FORMAT(instr);
   uint br_value  = ft;
   uint major_op;
   Result result = C1_SUCCESS;
   uint func = FUNC(instr);
   StatusReg statusReg;
   
   /* The kernel sets the cop1 usable flag to 0 when it */
   /* first starts a new context to limit the saving of */
   /* FPU registers to those procs who use them. The */
   /* first time a process uses a FPU instruction it */
   /* needs to trap into the kernel for it to set this */
   /* bit. */
   
   statusReg.ts_data = P->CP0[C0_SR];

   if (!(statusReg.s32.ts_cu1)) {
      CauseReg  causeReg;
      
      causeReg.tc_data = P->CP0[C0_CAUSE];
      causeReg.s32.tc_ce = 1;
      P->CP0[C0_CAUSE] = causeReg.tc_data;
      
      EXCEPTION(P, EXC_CPU); 
      return C1_CONTINUE;
   }

   SetRoundingMode(P->FCR[31] & 0x3);

   major_op = MAJOR_OP(instr);
   if (major_op == cop1_op) { 

      switch(sub_value) {

      case bc_op:
         if (br_value & 0x1) { /* BC1T */
            /* Check if the condition bit in the csr is set */
            if (P->FCR[31] & M_csr_c) {
               P->branchTarget = P->nPC + SIGN_EXTEND(18, (immediate << 2)); 
               P->branchStatus = BranchStatus_taken;
            }  else { 
		if( br_value & 0x2 ) { /* branch likely */
		    /* Skip instruction in delay slot */
		    P->nPC = P->nPC + 4;
		} else {
		    P->branchStatus = BranchStatus_nottaken;
                } 
            }
         } else { /* BC1F */
            /* Check if condition bit not set */
            if (!(P->FCR[31] & M_csr_c)) {
               P->branchTarget = P->nPC + SIGN_EXTEND(18, (immediate << 2));
               P->branchStatus = BranchStatus_taken;
            }  else { 
		if( br_value & 0x2 ) { /* branch likely */
		    /* Skip instruction in delay slot */
		    P->nPC = P->nPC + 4;
		} else {
		    P->branchStatus = BranchStatus_nottaken;
                }
            }
         }
         break;
      
      case cfc_op:
         if (fs == 31) {
            P->R[ft] = P->FCR[fs];
         } else {
            P->R[ft] = (3 << 8); /* R3010 FPU */
         }
         SIM_DEBUG(('f', "C1: CFC1 got %#x from fcr %d\n", P->R[ft], fs));
         break;
      
      case ctc_op:
         P->FCR[fs] = P->R[ft];
         SIM_DEBUG(('f', "C1: CTC1 put %#x into fcr %d\n", P->R[ft], fs));
         break;
      
      case mfc_op: 
         P->R[ft] = (Reg32_s)IntReg(fs);
         SIM_DEBUG(('f', "C1: mfc1 put f%d %d into r%d \n", fs, IntReg(fs), ft));
         break;
      
      case mtc_op:
         IntReg(fs) = (Reg32)P->R[ft];
         SIM_DEBUG(('f', "C1: mtc1 put f%d %d into f%d -> %d\n", ft, P->R[ft], 
               fs, IntReg(fs)));
         break;
  
      case dmfc_op: 
         CHECK_64BIT_ALLOWED(P)
            P->R[ft] = LongLongReg(fs);
         SIM_DEBUG(('f', "C1: dmfc1 put f%d %lld into r%d \n", fs, LongLongReg(fs), ft));
         break;
      
      case dmtc_op:
         CHECK_64BIT_ALLOWED(P)
            LongLongReg(fs) = P->R[ft];
         SIM_DEBUG(('f', "C1: dmtc1 put f%d %lldd into f%d -> %lld\n", ft, P->R[ft], 
               fs, LongLongReg(fs)));
         break;
      default: 
         /* S, D, W format instructions */
         switch (func) {
         case fmovc_op:
         case fmovn_op:
         case fmovz_op: { 
            /* Make sure we can execute mips4 instructions */
            if (IS_USER_MODE(P) && !(statusReg.s32.ts_xx)) {
               EXCEPTION(P, EXC_II); 
               return C1_CONTINUE;
            }
            break;
         }
         default:
            STATS_INC(P->myNum, numFPOps, 1);
            break;
         } 
         result = MipsyFPU(P, instr);
         break;
      } /* switch subvalue */
   } else if (major_op == cop1x_op) {
      if (IS_USER_MODE(P) && !(statusReg.s32.ts_xx)) {
         EXCEPTION(P, EXC_II); 
         return C1_CONTINUE;
      }
      STATS_INC(P->myNum, numFPOps, 1);
      result = MipsyFPUx(P,instr);
      
   } else if (major_op == spec_op) { 
      /* Must be on the MOVC instructions */
      ASSERT(FUNC(instr) == movc_op);
      /* Move conditional on FP condition */
      if (TF(instr)) { 
         if (P->FCR[31] & M_csr_c) {
            P->R[RT(instr)] = P->R[RS(instr)];
         }
      } else {
         if (!(P->FCR[31] & M_csr_c)) {
            P->R[RT(instr)] = P->R[RS(instr)];
         }
      }
   } else {
      ASSERT(0); /* unknown C1 instruction */
   }
      
   return result;
}