fpa11_cprt.c 9.1 KB

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