fpa11_cpdo.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. NetWinder Floating Point Emulator
  3. (c) Rebel.COM, 1998,1999
  4. (c) Philip Blundell, 2001
  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 "fpopcode.h"
  20. unsigned int SingleCPDO(struct roundingData *roundData, const unsigned int opcode, FPREG * rFd);
  21. unsigned int DoubleCPDO(struct roundingData *roundData, const unsigned int opcode, FPREG * rFd);
  22. unsigned int ExtendedCPDO(struct roundingData *roundData, const unsigned int opcode, FPREG * rFd);
  23. unsigned int EmulateCPDO(const unsigned int opcode)
  24. {
  25. FPA11 *fpa11 = GET_FPA11();
  26. FPREG *rFd;
  27. unsigned int nType, nDest, nRc;
  28. struct roundingData roundData;
  29. /* Get the destination size. If not valid let Linux perform
  30. an invalid instruction trap. */
  31. nDest = getDestinationSize(opcode);
  32. if (typeNone == nDest)
  33. return 0;
  34. roundData.mode = SetRoundingMode(opcode);
  35. roundData.precision = SetRoundingPrecision(opcode);
  36. roundData.exception = 0;
  37. /* Compare the size of the operands in Fn and Fm.
  38. Choose the largest size and perform operations in that size,
  39. in order to make use of all the precision of the operands.
  40. If Fm is a constant, we just grab a constant of a size
  41. matching the size of the operand in Fn. */
  42. if (MONADIC_INSTRUCTION(opcode))
  43. nType = nDest;
  44. else
  45. nType = fpa11->fType[getFn(opcode)];
  46. if (!CONSTANT_FM(opcode)) {
  47. register unsigned int Fm = getFm(opcode);
  48. if (nType < fpa11->fType[Fm]) {
  49. nType = fpa11->fType[Fm];
  50. }
  51. }
  52. rFd = &fpa11->fpreg[getFd(opcode)];
  53. switch (nType) {
  54. case typeSingle:
  55. nRc = SingleCPDO(&roundData, opcode, rFd);
  56. break;
  57. case typeDouble:
  58. nRc = DoubleCPDO(&roundData, opcode, rFd);
  59. break;
  60. #ifdef CONFIG_FPE_NWFPE_XP
  61. case typeExtended:
  62. nRc = ExtendedCPDO(&roundData, opcode, rFd);
  63. break;
  64. #endif
  65. default:
  66. nRc = 0;
  67. }
  68. /* The CPDO functions used to always set the destination type
  69. to be the same as their working size. */
  70. if (nRc != 0) {
  71. /* If the operation succeeded, check to see if the result in the
  72. destination register is the correct size. If not force it
  73. to be. */
  74. fpa11->fType[getFd(opcode)] = nDest;
  75. #ifdef CONFIG_FPE_NWFPE_XP
  76. if (nDest != nType) {
  77. switch (nDest) {
  78. case typeSingle:
  79. {
  80. if (typeDouble == nType)
  81. rFd->fSingle = float64_to_float32(&roundData, rFd->fDouble);
  82. else
  83. rFd->fSingle = floatx80_to_float32(&roundData, rFd->fExtended);
  84. }
  85. break;
  86. case typeDouble:
  87. {
  88. if (typeSingle == nType)
  89. rFd->fDouble = float32_to_float64(rFd->fSingle);
  90. else
  91. rFd->fDouble = floatx80_to_float64(&roundData, rFd->fExtended);
  92. }
  93. break;
  94. case typeExtended:
  95. {
  96. if (typeSingle == nType)
  97. rFd->fExtended = float32_to_floatx80(rFd->fSingle);
  98. else
  99. rFd->fExtended = float64_to_floatx80(rFd->fDouble);
  100. }
  101. break;
  102. }
  103. }
  104. #else
  105. if (nDest != nType) {
  106. if (nDest == typeSingle)
  107. rFd->fSingle = float64_to_float32(&roundData, rFd->fDouble);
  108. else
  109. rFd->fDouble = float32_to_float64(rFd->fSingle);
  110. }
  111. #endif
  112. }
  113. if (roundData.exception)
  114. float_raise(roundData.exception);
  115. return nRc;
  116. }