fpa11_cprt.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. NetWinder Floating Point Emulator
  3. (c) Rebel.COM, 1998,1999
  4. (c) Philip Blundell, 1999, 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. #include "fpa11.inl"
  21. #include "fpmodule.h"
  22. #include "fpmodule.inl"
  23. #include "softfloat.h"
  24. unsigned int PerformFLT(const unsigned int opcode);
  25. unsigned int PerformFIX(const unsigned int opcode);
  26. static unsigned int PerformComparison(const unsigned int opcode);
  27. unsigned int EmulateCPRT(const unsigned int opcode)
  28. {
  29. if (opcode & 0x800000) {
  30. /* This is some variant of a comparison (PerformComparison
  31. will sort out which one). Since most of the other CPRT
  32. instructions are oddball cases of some sort or other it
  33. makes sense to pull this out into a fast path. */
  34. return PerformComparison(opcode);
  35. }
  36. /* Hint to GCC that we'd like a jump table rather than a load of CMPs */
  37. switch ((opcode & 0x700000) >> 20) {
  38. case FLT_CODE >> 20:
  39. return PerformFLT(opcode);
  40. break;
  41. case FIX_CODE >> 20:
  42. return PerformFIX(opcode);
  43. break;
  44. case WFS_CODE >> 20:
  45. writeFPSR(readRegister(getRd(opcode)));
  46. break;
  47. case RFS_CODE >> 20:
  48. writeRegister(getRd(opcode), readFPSR());
  49. break;
  50. default:
  51. return 0;
  52. }
  53. return 1;
  54. }
  55. unsigned int PerformFLT(const unsigned int opcode)
  56. {
  57. FPA11 *fpa11 = GET_FPA11();
  58. struct roundingData roundData;
  59. roundData.mode = SetRoundingMode(opcode);
  60. roundData.precision = SetRoundingPrecision(opcode);
  61. roundData.exception = 0;
  62. switch (opcode & MASK_ROUNDING_PRECISION) {
  63. case ROUND_SINGLE:
  64. {
  65. fpa11->fType[getFn(opcode)] = typeSingle;
  66. fpa11->fpreg[getFn(opcode)].fSingle = int32_to_float32(&roundData, readRegister(getRd(opcode)));
  67. }
  68. break;
  69. case ROUND_DOUBLE:
  70. {
  71. fpa11->fType[getFn(opcode)] = typeDouble;
  72. fpa11->fpreg[getFn(opcode)].fDouble = int32_to_float64(readRegister(getRd(opcode)));
  73. }
  74. break;
  75. #ifdef CONFIG_FPE_NWFPE_XP
  76. case ROUND_EXTENDED:
  77. {
  78. fpa11->fType[getFn(opcode)] = typeExtended;
  79. fpa11->fpreg[getFn(opcode)].fExtended = int32_to_floatx80(readRegister(getRd(opcode)));
  80. }
  81. break;
  82. #endif
  83. default:
  84. return 0;
  85. }
  86. if (roundData.exception)
  87. float_raise(roundData.exception);
  88. return 1;
  89. }
  90. unsigned int PerformFIX(const unsigned int opcode)
  91. {
  92. FPA11 *fpa11 = GET_FPA11();
  93. unsigned int Fn = getFm(opcode);
  94. struct roundingData roundData;
  95. roundData.mode = SetRoundingMode(opcode);
  96. roundData.precision = SetRoundingPrecision(opcode);
  97. roundData.exception = 0;
  98. switch (fpa11->fType[Fn]) {
  99. case typeSingle:
  100. {
  101. writeRegister(getRd(opcode), float32_to_int32(&roundData, fpa11->fpreg[Fn].fSingle));
  102. }
  103. break;
  104. case typeDouble:
  105. {
  106. writeRegister(getRd(opcode), float64_to_int32(&roundData, fpa11->fpreg[Fn].fDouble));
  107. }
  108. break;
  109. #ifdef CONFIG_FPE_NWFPE_XP
  110. case typeExtended:
  111. {
  112. writeRegister(getRd(opcode), floatx80_to_int32(&roundData, fpa11->fpreg[Fn].fExtended));
  113. }
  114. break;
  115. #endif
  116. default:
  117. return 0;
  118. }
  119. if (roundData.exception)
  120. float_raise(roundData.exception);
  121. return 1;
  122. }
  123. /* This instruction sets the flags N, Z, C, V in the FPSR. */
  124. static unsigned int PerformComparison(const unsigned int opcode)
  125. {
  126. FPA11 *fpa11 = GET_FPA11();
  127. unsigned int Fn = getFn(opcode), Fm = getFm(opcode);
  128. int e_flag = opcode & 0x400000; /* 1 if CxFE */
  129. int n_flag = opcode & 0x200000; /* 1 if CNxx */
  130. unsigned int flags = 0;
  131. #ifdef CONFIG_FPE_NWFPE_XP
  132. floatx80 rFn, rFm;
  133. /* Check for unordered condition and convert all operands to 80-bit
  134. format.
  135. ?? Might be some mileage in avoiding this conversion if possible.
  136. Eg, if both operands are 32-bit, detect this and do a 32-bit
  137. comparison (cheaper than an 80-bit one). */
  138. switch (fpa11->fType[Fn]) {
  139. case typeSingle:
  140. //printk("single.\n");
  141. if (float32_is_nan(fpa11->fpreg[Fn].fSingle))
  142. goto unordered;
  143. rFn = float32_to_floatx80(fpa11->fpreg[Fn].fSingle);
  144. break;
  145. case typeDouble:
  146. //printk("double.\n");
  147. if (float64_is_nan(fpa11->fpreg[Fn].fDouble))
  148. goto unordered;
  149. rFn = float64_to_floatx80(fpa11->fpreg[Fn].fDouble);
  150. break;
  151. case typeExtended:
  152. //printk("extended.\n");
  153. if (floatx80_is_nan(fpa11->fpreg[Fn].fExtended))
  154. goto unordered;
  155. rFn = fpa11->fpreg[Fn].fExtended;
  156. break;
  157. default:
  158. return 0;
  159. }
  160. if (CONSTANT_FM(opcode)) {
  161. //printk("Fm is a constant: #%d.\n",Fm);
  162. rFm = getExtendedConstant(Fm);
  163. if (floatx80_is_nan(rFm))
  164. goto unordered;
  165. } else {
  166. //printk("Fm = r%d which contains a ",Fm);
  167. switch (fpa11->fType[Fm]) {
  168. case typeSingle:
  169. //printk("single.\n");
  170. if (float32_is_nan(fpa11->fpreg[Fm].fSingle))
  171. goto unordered;
  172. rFm = float32_to_floatx80(fpa11->fpreg[Fm].fSingle);
  173. break;
  174. case typeDouble:
  175. //printk("double.\n");
  176. if (float64_is_nan(fpa11->fpreg[Fm].fDouble))
  177. goto unordered;
  178. rFm = float64_to_floatx80(fpa11->fpreg[Fm].fDouble);
  179. break;
  180. case typeExtended:
  181. //printk("extended.\n");
  182. if (floatx80_is_nan(fpa11->fpreg[Fm].fExtended))
  183. goto unordered;
  184. rFm = fpa11->fpreg[Fm].fExtended;
  185. break;
  186. default:
  187. return 0;
  188. }
  189. }
  190. if (n_flag)
  191. rFm.high ^= 0x8000;
  192. /* test for less than condition */
  193. if (floatx80_lt(rFn, rFm))
  194. flags |= CC_NEGATIVE;
  195. /* test for equal condition */
  196. if (floatx80_eq(rFn, rFm))
  197. flags |= CC_ZERO;
  198. /* test for greater than or equal condition */
  199. if (floatx80_lt(rFm, rFn))
  200. flags |= CC_CARRY;
  201. #else
  202. if (CONSTANT_FM(opcode)) {
  203. /* Fm is a constant. Do the comparison in whatever precision
  204. Fn happens to be stored in. */
  205. if (fpa11->fType[Fn] == typeSingle) {
  206. float32 rFm = getSingleConstant(Fm);
  207. float32 rFn = fpa11->fpreg[Fn].fSingle;
  208. if (float32_is_nan(rFn))
  209. goto unordered;
  210. if (n_flag)
  211. rFm ^= 0x80000000;
  212. /* test for less than condition */
  213. if (float32_lt_nocheck(rFn, rFm))
  214. flags |= CC_NEGATIVE;
  215. /* test for equal condition */
  216. if (float32_eq_nocheck(rFn, rFm))
  217. flags |= CC_ZERO;
  218. /* test for greater than or equal condition */
  219. if (float32_lt_nocheck(rFm, rFn))
  220. flags |= CC_CARRY;
  221. } else {
  222. float64 rFm = getDoubleConstant(Fm);
  223. float64 rFn = fpa11->fpreg[Fn].fDouble;
  224. if (float64_is_nan(rFn))
  225. goto unordered;
  226. if (n_flag)
  227. rFm ^= 0x8000000000000000ULL;
  228. /* test for less than condition */
  229. if (float64_lt_nocheck(rFn, rFm))
  230. flags |= CC_NEGATIVE;
  231. /* test for equal condition */
  232. if (float64_eq_nocheck(rFn, rFm))
  233. flags |= CC_ZERO;
  234. /* test for greater than or equal condition */
  235. if (float64_lt_nocheck(rFm, rFn))
  236. flags |= CC_CARRY;
  237. }
  238. } else {
  239. /* Both operands are in registers. */
  240. if (fpa11->fType[Fn] == typeSingle
  241. && fpa11->fType[Fm] == typeSingle) {
  242. float32 rFm = fpa11->fpreg[Fm].fSingle;
  243. float32 rFn = fpa11->fpreg[Fn].fSingle;
  244. if (float32_is_nan(rFn)
  245. || float32_is_nan(rFm))
  246. goto unordered;
  247. if (n_flag)
  248. rFm ^= 0x80000000;
  249. /* test for less than condition */
  250. if (float32_lt_nocheck(rFn, rFm))
  251. flags |= CC_NEGATIVE;
  252. /* test for equal condition */
  253. if (float32_eq_nocheck(rFn, rFm))
  254. flags |= CC_ZERO;
  255. /* test for greater than or equal condition */
  256. if (float32_lt_nocheck(rFm, rFn))
  257. flags |= CC_CARRY;
  258. } else {
  259. /* Promote 32-bit operand to 64 bits. */
  260. float64 rFm, rFn;
  261. rFm = (fpa11->fType[Fm] == typeSingle) ?
  262. float32_to_float64(fpa11->fpreg[Fm].fSingle)
  263. : fpa11->fpreg[Fm].fDouble;
  264. rFn = (fpa11->fType[Fn] == typeSingle) ?
  265. float32_to_float64(fpa11->fpreg[Fn].fSingle)
  266. : fpa11->fpreg[Fn].fDouble;
  267. if (float64_is_nan(rFn)
  268. || float64_is_nan(rFm))
  269. goto unordered;
  270. if (n_flag)
  271. rFm ^= 0x8000000000000000ULL;
  272. /* test for less than condition */
  273. if (float64_lt_nocheck(rFn, rFm))
  274. flags |= CC_NEGATIVE;
  275. /* test for equal condition */
  276. if (float64_eq_nocheck(rFn, rFm))
  277. flags |= CC_ZERO;
  278. /* test for greater than or equal condition */
  279. if (float64_lt_nocheck(rFm, rFn))
  280. flags |= CC_CARRY;
  281. }
  282. }
  283. #endif
  284. writeConditionCodes(flags);
  285. return 1;
  286. unordered:
  287. /* ?? The FPA data sheet is pretty vague about this, in particular
  288. about whether the non-E comparisons can ever raise exceptions.
  289. This implementation is based on a combination of what it says in
  290. the data sheet, observation of how the Acorn emulator actually
  291. behaves (and how programs expect it to) and guesswork. */
  292. flags |= CC_OVERFLOW;
  293. flags &= ~(CC_ZERO | CC_NEGATIVE);
  294. if (BIT_AC & readFPSR())
  295. flags |= CC_CARRY;
  296. if (e_flag)
  297. float_raise(float_flag_invalid);
  298. writeConditionCodes(flags);
  299. return 1;
  300. }