fpa11_cprt.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. case WFS_CODE >> 20: writeFPSR(readRegister(getRd(opcode))); break;
  51. case RFS_CODE >> 20: writeRegister(getRd(opcode),readFPSR()); break;
  52. #if 0 /* We currently have no use for the FPCR, so there's no point
  53. in emulating it. */
  54. case WFC_CODE >> 20: writeFPCR(readRegister(getRd(opcode)));
  55. case RFC_CODE >> 20: writeRegister(getRd(opcode),readFPCR()); break;
  56. #endif
  57. default: nRc = 0;
  58. }
  59. return nRc;
  60. }
  61. unsigned int PerformFLT(const unsigned int opcode)
  62. {
  63. FPA11 *fpa11 = GET_FPA11();
  64. unsigned int nRc = 1;
  65. SetRoundingMode(opcode);
  66. switch (opcode & MASK_ROUNDING_PRECISION)
  67. {
  68. case ROUND_SINGLE:
  69. {
  70. fpa11->fType[getFn(opcode)] = typeSingle;
  71. fpa11->fpreg[getFn(opcode)].fSingle =
  72. int32_to_float32(readRegister(getRd(opcode)));
  73. }
  74. break;
  75. case ROUND_DOUBLE:
  76. {
  77. fpa11->fType[getFn(opcode)] = typeDouble;
  78. fpa11->fpreg[getFn(opcode)].fDouble =
  79. int32_to_float64(readRegister(getRd(opcode)));
  80. }
  81. break;
  82. case ROUND_EXTENDED:
  83. {
  84. fpa11->fType[getFn(opcode)] = typeExtended;
  85. fpa11->fpreg[getFn(opcode)].fExtended =
  86. int32_to_floatx80(readRegister(getRd(opcode)));
  87. }
  88. break;
  89. default: nRc = 0;
  90. }
  91. return nRc;
  92. }
  93. unsigned int PerformFIX(const unsigned int opcode)
  94. {
  95. FPA11 *fpa11 = GET_FPA11();
  96. unsigned int nRc = 1;
  97. unsigned int Fn = getFm(opcode);
  98. SetRoundingMode(opcode);
  99. switch (fpa11->fType[Fn])
  100. {
  101. case typeSingle:
  102. {
  103. writeRegister(getRd(opcode),
  104. float32_to_int32(fpa11->fpreg[Fn].fSingle));
  105. }
  106. break;
  107. case typeDouble:
  108. {
  109. writeRegister(getRd(opcode),
  110. float64_to_int32(fpa11->fpreg[Fn].fDouble));
  111. }
  112. break;
  113. case typeExtended:
  114. {
  115. writeRegister(getRd(opcode),
  116. floatx80_to_int32(fpa11->fpreg[Fn].fExtended));
  117. }
  118. break;
  119. default: nRc = 0;
  120. }
  121. return nRc;
  122. }
  123. static unsigned int __inline__
  124. PerformComparisonOperation(floatx80 Fn, floatx80 Fm)
  125. {
  126. unsigned int flags = 0;
  127. /* test for less than condition */
  128. if (floatx80_lt(Fn,Fm))
  129. {
  130. flags |= CC_NEGATIVE;
  131. }
  132. /* test for equal condition */
  133. if (floatx80_eq(Fn,Fm))
  134. {
  135. flags |= CC_ZERO;
  136. }
  137. /* test for greater than or equal condition */
  138. if (floatx80_lt(Fm,Fn))
  139. {
  140. flags |= CC_CARRY;
  141. }
  142. writeConditionCodes(flags);
  143. return 1;
  144. }
  145. /* This instruction sets the flags N, Z, C, V in the FPSR. */
  146. static unsigned int PerformComparison(const unsigned int opcode)
  147. {
  148. FPA11 *fpa11 = GET_FPA11();
  149. unsigned int Fn, Fm;
  150. floatx80 rFn, rFm;
  151. int e_flag = opcode & 0x400000; /* 1 if CxFE */
  152. int n_flag = opcode & 0x200000; /* 1 if CNxx */
  153. unsigned int flags = 0;
  154. //printk("PerformComparison(0x%08x)\n",opcode);
  155. Fn = getFn(opcode);
  156. Fm = getFm(opcode);
  157. /* Check for unordered condition and convert all operands to 80-bit
  158. format.
  159. ?? Might be some mileage in avoiding this conversion if possible.
  160. Eg, if both operands are 32-bit, detect this and do a 32-bit
  161. comparison (cheaper than an 80-bit one). */
  162. switch (fpa11->fType[Fn])
  163. {
  164. case typeSingle:
  165. //printk("single.\n");
  166. if (float32_is_nan(fpa11->fpreg[Fn].fSingle))
  167. goto unordered;
  168. rFn = float32_to_floatx80(fpa11->fpreg[Fn].fSingle);
  169. break;
  170. case typeDouble:
  171. //printk("double.\n");
  172. if (float64_is_nan(fpa11->fpreg[Fn].fDouble))
  173. goto unordered;
  174. rFn = float64_to_floatx80(fpa11->fpreg[Fn].fDouble);
  175. break;
  176. case typeExtended:
  177. //printk("extended.\n");
  178. if (floatx80_is_nan(fpa11->fpreg[Fn].fExtended))
  179. goto unordered;
  180. rFn = fpa11->fpreg[Fn].fExtended;
  181. break;
  182. default: return 0;
  183. }
  184. if (CONSTANT_FM(opcode))
  185. {
  186. //printk("Fm is a constant: #%d.\n",Fm);
  187. rFm = getExtendedConstant(Fm);
  188. if (floatx80_is_nan(rFm))
  189. goto unordered;
  190. }
  191. else
  192. {
  193. //printk("Fm = r%d which contains a ",Fm);
  194. switch (fpa11->fType[Fm])
  195. {
  196. case typeSingle:
  197. //printk("single.\n");
  198. if (float32_is_nan(fpa11->fpreg[Fm].fSingle))
  199. goto unordered;
  200. rFm = float32_to_floatx80(fpa11->fpreg[Fm].fSingle);
  201. break;
  202. case typeDouble:
  203. //printk("double.\n");
  204. if (float64_is_nan(fpa11->fpreg[Fm].fDouble))
  205. goto unordered;
  206. rFm = float64_to_floatx80(fpa11->fpreg[Fm].fDouble);
  207. break;
  208. case typeExtended:
  209. //printk("extended.\n");
  210. if (floatx80_is_nan(fpa11->fpreg[Fm].fExtended))
  211. goto unordered;
  212. rFm = fpa11->fpreg[Fm].fExtended;
  213. break;
  214. default: return 0;
  215. }
  216. }
  217. if (n_flag)
  218. {
  219. rFm.high ^= 0x8000;
  220. }
  221. return PerformComparisonOperation(rFn,rFm);
  222. unordered:
  223. /* ?? The FPA data sheet is pretty vague about this, in particular
  224. about whether the non-E comparisons can ever raise exceptions.
  225. This implementation is based on a combination of what it says in
  226. the data sheet, observation of how the Acorn emulator actually
  227. behaves (and how programs expect it to) and guesswork. */
  228. flags |= CC_OVERFLOW;
  229. flags &= ~(CC_ZERO | CC_NEGATIVE);
  230. if (BIT_AC & readFPSR()) flags |= CC_CARRY;
  231. if (e_flag) float_raise(float_flag_invalid);
  232. writeConditionCodes(flags);
  233. return 1;
  234. }