fpa11_cprt.c 8.4 KB

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