fpa11_cprt.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:7k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.     NetWinder Floating Point Emulator
  3.     (c) Rebel.COM, 1998,1999
  4.     (c) Philip Blundell, 1999
  5.     Direct questions, comments to Scott Bambrough <scottb@netwinder.org>
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include "fpa11.h"
  19. #include "milieu.h"
  20. #include "softfloat.h"
  21. #include "fpopcode.h"
  22. #include "fpa11.inl"
  23. #include "fpmodule.h"
  24. #include "fpmodule.inl"
  25. extern flag floatx80_is_nan(floatx80);
  26. extern flag float64_is_nan( float64);
  27. extern flag float32_is_nan( float32);
  28. void SetRoundingMode(const unsigned int opcode);
  29. unsigned int PerformFLT(const unsigned int opcode);
  30. unsigned int PerformFIX(const unsigned int opcode);
  31. static unsigned int
  32. PerformComparison(const unsigned int opcode);
  33. unsigned int EmulateCPRT(const unsigned int opcode)
  34. {
  35.   unsigned int nRc = 1;
  36.   //printk("EmulateCPRT(0x%08x)n",opcode);
  37.   if (opcode & 0x800000)
  38.   {
  39.      /* This is some variant of a comparison (PerformComparison will
  40. sort out which one).  Since most of the other CPRT
  41. instructions are oddball cases of some sort or other it makes
  42. sense to pull this out into a fast path.  */
  43.      return PerformComparison(opcode);
  44.   }
  45.   /* Hint to GCC that we'd like a jump table rather than a load of CMPs */
  46.   switch ((opcode & 0x700000) >> 20)
  47.   {
  48.     case  FLT_CODE >> 20: nRc = PerformFLT(opcode); break;
  49.     case  FIX_CODE >> 20: nRc = PerformFIX(opcode); break;
  50.     
  51.     case  WFS_CODE >> 20: writeFPSR(readRegister(getRd(opcode))); break;
  52.     case  RFS_CODE >> 20: writeRegister(getRd(opcode),readFPSR()); break;
  53. #if 0    /* We currently have no use for the FPCR, so there's no point
  54.     in emulating it. */
  55.     case  WFC_CODE >> 20: writeFPCR(readRegister(getRd(opcode)));
  56.     case  RFC_CODE >> 20: writeRegister(getRd(opcode),readFPCR()); break;
  57. #endif
  58.     default: nRc = 0;
  59.   }
  60.   
  61.   return nRc;
  62. }
  63. unsigned int PerformFLT(const unsigned int opcode)
  64. {
  65.    FPA11 *fpa11 = GET_FPA11();
  66.    
  67.    unsigned int nRc = 1;
  68.    SetRoundingMode(opcode);
  69.    switch (opcode & MASK_ROUNDING_PRECISION)
  70.    {
  71.       case ROUND_SINGLE:
  72.       {
  73.         fpa11->fType[getFn(opcode)] = typeSingle;
  74.         fpa11->fpreg[getFn(opcode)].fSingle =
  75.    int32_to_float32(readRegister(getRd(opcode)));
  76.       }
  77.       break;
  78.       case ROUND_DOUBLE:
  79.       {
  80.         fpa11->fType[getFn(opcode)] = typeDouble;
  81.         fpa11->fpreg[getFn(opcode)].fDouble =
  82.             int32_to_float64(readRegister(getRd(opcode)));
  83.       }
  84.       break;
  85.         
  86.       case ROUND_EXTENDED:
  87.       {
  88.         fpa11->fType[getFn(opcode)] = typeExtended;
  89.         fpa11->fpreg[getFn(opcode)].fExtended =
  90.    int32_to_floatx80(readRegister(getRd(opcode)));
  91.       }
  92.       break;
  93.       
  94.       default: nRc = 0;
  95.   }
  96.   
  97.   return nRc;
  98. }
  99. unsigned int PerformFIX(const unsigned int opcode)
  100. {
  101.    FPA11 *fpa11 = GET_FPA11();
  102.    unsigned int nRc = 1;
  103.    unsigned int Fn = getFm(opcode);
  104.    
  105.    SetRoundingMode(opcode);
  106.    switch (fpa11->fType[Fn])
  107.    {
  108.       case typeSingle:
  109.       {
  110.          writeRegister(getRd(opcode),
  111.                float32_to_int32(fpa11->fpreg[Fn].fSingle));
  112.       }
  113.       break;
  114.       case typeDouble:
  115.       {
  116.          writeRegister(getRd(opcode),
  117.                float64_to_int32(fpa11->fpreg[Fn].fDouble));
  118.       }
  119.       break;
  120.                       
  121.       case typeExtended:
  122.       {
  123.          writeRegister(getRd(opcode),
  124.                floatx80_to_int32(fpa11->fpreg[Fn].fExtended));
  125.       }
  126.       break;
  127.       
  128.       default: nRc = 0;
  129.   }
  130.   
  131.   return nRc;
  132. }
  133.    
  134. static unsigned int __inline__
  135. PerformComparisonOperation(floatx80 Fn, floatx80 Fm)
  136. {
  137.    unsigned int flags = 0;
  138.    /* test for less than condition */
  139.    if (floatx80_lt(Fn,Fm))
  140.    {
  141.       flags |= CC_NEGATIVE;
  142.    }
  143.   
  144.    /* test for equal condition */
  145.    if (floatx80_eq(Fn,Fm))
  146.    {
  147.       flags |= CC_ZERO;
  148.    }
  149.    /* test for greater than or equal condition */
  150.    if (floatx80_lt(Fm,Fn))
  151.    {
  152.       flags |= CC_CARRY;
  153.    }
  154.    
  155.    writeConditionCodes(flags);
  156.    return 1;
  157. }
  158. /* This instruction sets the flags N, Z, C, V in the FPSR. */
  159.    
  160. static unsigned int PerformComparison(const unsigned int opcode)
  161. {
  162.    FPA11 *fpa11 = GET_FPA11();
  163.    unsigned int Fn, Fm;
  164.    floatx80 rFn, rFm;
  165.    int e_flag = opcode & 0x400000; /* 1 if CxFE */
  166.    int n_flag = opcode & 0x200000; /* 1 if CNxx */
  167.    unsigned int flags = 0;
  168.    //printk("PerformComparison(0x%08x)n",opcode);
  169.    Fn = getFn(opcode);
  170.    Fm = getFm(opcode);
  171.    /* Check for unordered condition and convert all operands to 80-bit
  172.       format.
  173.       ?? Might be some mileage in avoiding this conversion if possible.
  174.       Eg, if both operands are 32-bit, detect this and do a 32-bit
  175.       comparison (cheaper than an 80-bit one).  */
  176.    switch (fpa11->fType[Fn])
  177.    {
  178.       case typeSingle: 
  179.         //printk("single.n");
  180. if (float32_is_nan(fpa11->fpreg[Fn].fSingle))
  181.    goto unordered;
  182.         rFn = float32_to_floatx80(fpa11->fpreg[Fn].fSingle);
  183.       break;
  184.       case typeDouble: 
  185.         //printk("double.n");
  186. if (float64_is_nan(fpa11->fpreg[Fn].fDouble))
  187.    goto unordered;
  188.         rFn = float64_to_floatx80(fpa11->fpreg[Fn].fDouble);
  189.       break;
  190.       
  191.       case typeExtended: 
  192.         //printk("extended.n");
  193. if (floatx80_is_nan(fpa11->fpreg[Fn].fExtended))
  194.    goto unordered;
  195.         rFn = fpa11->fpreg[Fn].fExtended;
  196.       break;
  197.       
  198.       default: return 0;
  199.    }
  200.    if (CONSTANT_FM(opcode))
  201.    {
  202.      //printk("Fm is a constant: #%d.n",Fm);
  203.      rFm = getExtendedConstant(Fm);
  204.      if (floatx80_is_nan(rFm))
  205.         goto unordered;
  206.    }
  207.    else
  208.    {
  209.      //printk("Fm = r%d which contains a ",Fm);
  210.       switch (fpa11->fType[Fm])
  211.       {
  212.          case typeSingle: 
  213.            //printk("single.n");
  214.    if (float32_is_nan(fpa11->fpreg[Fm].fSingle))
  215.       goto unordered;
  216.            rFm = float32_to_floatx80(fpa11->fpreg[Fm].fSingle);
  217.          break;
  218.          case typeDouble: 
  219.            //printk("double.n");
  220.    if (float64_is_nan(fpa11->fpreg[Fm].fDouble))
  221.       goto unordered;
  222.            rFm = float64_to_floatx80(fpa11->fpreg[Fm].fDouble);
  223.          break;
  224.       
  225.          case typeExtended: 
  226.            //printk("extended.n");
  227.    if (floatx80_is_nan(fpa11->fpreg[Fm].fExtended))
  228.       goto unordered;
  229.            rFm = fpa11->fpreg[Fm].fExtended;
  230.          break;
  231.       
  232.          default: return 0;
  233.       }
  234.    }
  235.    if (n_flag)
  236.    {
  237.       rFm.high ^= 0x8000;
  238.    }
  239.    return PerformComparisonOperation(rFn,rFm);
  240.  unordered:
  241.    /* ?? The FPA data sheet is pretty vague about this, in particular
  242.       about whether the non-E comparisons can ever raise exceptions.
  243.       This implementation is based on a combination of what it says in
  244.       the data sheet, observation of how the Acorn emulator actually
  245.       behaves (and how programs expect it to) and guesswork.  */
  246.    flags |= CC_OVERFLOW;
  247.    flags &= ~(CC_ZERO | CC_NEGATIVE);
  248.    if (BIT_AC & readFPSR()) flags |= CC_CARRY;
  249.    if (e_flag) float_raise(float_flag_invalid);
  250.    writeConditionCodes(flags);
  251.    return 1;
  252. }