fpa11_cpdt.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. NetWinder Floating Point Emulator
  3. (c) Rebel.com, 1998-1999
  4. (c) Philip Blundell, 1998, 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 "softfloat.h"
  20. #include "fpopcode.h"
  21. #include "fpmodule.h"
  22. #include "fpmodule.inl"
  23. #include <linux/uaccess.h>
  24. static inline void loadSingle(const unsigned int Fn, const unsigned int __user *pMem)
  25. {
  26. FPA11 *fpa11 = GET_FPA11();
  27. fpa11->fType[Fn] = typeSingle;
  28. get_user(fpa11->fpreg[Fn].fSingle, pMem);
  29. }
  30. static inline void loadDouble(const unsigned int Fn, const unsigned int __user *pMem)
  31. {
  32. FPA11 *fpa11 = GET_FPA11();
  33. unsigned int *p;
  34. p = (unsigned int *) &fpa11->fpreg[Fn].fDouble;
  35. fpa11->fType[Fn] = typeDouble;
  36. #ifdef __ARMEB__
  37. get_user(p[0], &pMem[0]); /* sign & exponent */
  38. get_user(p[1], &pMem[1]);
  39. #else
  40. get_user(p[0], &pMem[1]);
  41. get_user(p[1], &pMem[0]); /* sign & exponent */
  42. #endif
  43. }
  44. #ifdef CONFIG_FPE_NWFPE_XP
  45. static inline void loadExtended(const unsigned int Fn, const unsigned int __user *pMem)
  46. {
  47. FPA11 *fpa11 = GET_FPA11();
  48. unsigned int *p;
  49. p = (unsigned int *) &fpa11->fpreg[Fn].fExtended;
  50. fpa11->fType[Fn] = typeExtended;
  51. get_user(p[0], &pMem[0]); /* sign & exponent */
  52. #ifdef __ARMEB__
  53. get_user(p[1], &pMem[1]); /* ms bits */
  54. get_user(p[2], &pMem[2]); /* ls bits */
  55. #else
  56. get_user(p[1], &pMem[2]); /* ls bits */
  57. get_user(p[2], &pMem[1]); /* ms bits */
  58. #endif
  59. }
  60. #endif
  61. static inline void loadMultiple(const unsigned int Fn, const unsigned int __user *pMem)
  62. {
  63. FPA11 *fpa11 = GET_FPA11();
  64. register unsigned int *p;
  65. unsigned long x;
  66. p = (unsigned int *) &(fpa11->fpreg[Fn]);
  67. get_user(x, &pMem[0]);
  68. fpa11->fType[Fn] = (x >> 14) & 0x00000003;
  69. switch (fpa11->fType[Fn]) {
  70. case typeSingle:
  71. case typeDouble:
  72. {
  73. get_user(p[0], &pMem[2]); /* Single */
  74. get_user(p[1], &pMem[1]); /* double msw */
  75. p[2] = 0; /* empty */
  76. }
  77. break;
  78. #ifdef CONFIG_FPE_NWFPE_XP
  79. case typeExtended:
  80. {
  81. get_user(p[1], &pMem[2]);
  82. get_user(p[2], &pMem[1]); /* msw */
  83. p[0] = (x & 0x80003fff);
  84. }
  85. break;
  86. #endif
  87. }
  88. }
  89. static inline void storeSingle(struct roundingData *roundData, const unsigned int Fn, unsigned int __user *pMem)
  90. {
  91. FPA11 *fpa11 = GET_FPA11();
  92. union {
  93. float32 f;
  94. unsigned int i[1];
  95. } val;
  96. switch (fpa11->fType[Fn]) {
  97. case typeDouble:
  98. val.f = float64_to_float32(roundData, fpa11->fpreg[Fn].fDouble);
  99. break;
  100. #ifdef CONFIG_FPE_NWFPE_XP
  101. case typeExtended:
  102. val.f = floatx80_to_float32(roundData, fpa11->fpreg[Fn].fExtended);
  103. break;
  104. #endif
  105. default:
  106. val.f = fpa11->fpreg[Fn].fSingle;
  107. }
  108. put_user(val.i[0], pMem);
  109. }
  110. static inline void storeDouble(struct roundingData *roundData, const unsigned int Fn, unsigned int __user *pMem)
  111. {
  112. FPA11 *fpa11 = GET_FPA11();
  113. union {
  114. float64 f;
  115. unsigned int i[2];
  116. } val;
  117. switch (fpa11->fType[Fn]) {
  118. case typeSingle:
  119. val.f = float32_to_float64(fpa11->fpreg[Fn].fSingle);
  120. break;
  121. #ifdef CONFIG_FPE_NWFPE_XP
  122. case typeExtended:
  123. val.f = floatx80_to_float64(roundData, fpa11->fpreg[Fn].fExtended);
  124. break;
  125. #endif
  126. default:
  127. val.f = fpa11->fpreg[Fn].fDouble;
  128. }
  129. #ifdef __ARMEB__
  130. put_user(val.i[0], &pMem[0]); /* msw */
  131. put_user(val.i[1], &pMem[1]); /* lsw */
  132. #else
  133. put_user(val.i[1], &pMem[0]); /* msw */
  134. put_user(val.i[0], &pMem[1]); /* lsw */
  135. #endif
  136. }
  137. #ifdef CONFIG_FPE_NWFPE_XP
  138. static inline void storeExtended(const unsigned int Fn, unsigned int __user *pMem)
  139. {
  140. FPA11 *fpa11 = GET_FPA11();
  141. union {
  142. floatx80 f;
  143. unsigned int i[3];
  144. } val;
  145. switch (fpa11->fType[Fn]) {
  146. case typeSingle:
  147. val.f = float32_to_floatx80(fpa11->fpreg[Fn].fSingle);
  148. break;
  149. case typeDouble:
  150. val.f = float64_to_floatx80(fpa11->fpreg[Fn].fDouble);
  151. break;
  152. default:
  153. val.f = fpa11->fpreg[Fn].fExtended;
  154. }
  155. put_user(val.i[0], &pMem[0]); /* sign & exp */
  156. #ifdef __ARMEB__
  157. put_user(val.i[1], &pMem[1]); /* msw */
  158. put_user(val.i[2], &pMem[2]);
  159. #else
  160. put_user(val.i[1], &pMem[2]);
  161. put_user(val.i[2], &pMem[1]); /* msw */
  162. #endif
  163. }
  164. #endif
  165. static inline void storeMultiple(const unsigned int Fn, unsigned int __user *pMem)
  166. {
  167. FPA11 *fpa11 = GET_FPA11();
  168. register unsigned int nType, *p;
  169. p = (unsigned int *) &(fpa11->fpreg[Fn]);
  170. nType = fpa11->fType[Fn];
  171. switch (nType) {
  172. case typeSingle:
  173. case typeDouble:
  174. {
  175. put_user(p[0], &pMem[2]); /* single */
  176. put_user(p[1], &pMem[1]); /* double msw */
  177. put_user(nType << 14, &pMem[0]);
  178. }
  179. break;
  180. #ifdef CONFIG_FPE_NWFPE_XP
  181. case typeExtended:
  182. {
  183. put_user(p[2], &pMem[1]); /* msw */
  184. put_user(p[1], &pMem[2]);
  185. put_user((p[0] & 0x80003fff) | (nType << 14), &pMem[0]);
  186. }
  187. break;
  188. #endif
  189. }
  190. }
  191. unsigned int PerformLDF(const unsigned int opcode)
  192. {
  193. unsigned int __user *pBase, *pAddress, *pFinal;
  194. unsigned int nRc = 1, write_back = WRITE_BACK(opcode);
  195. pBase = (unsigned int __user *) readRegister(getRn(opcode));
  196. if (REG_PC == getRn(opcode)) {
  197. pBase += 2;
  198. write_back = 0;
  199. }
  200. pFinal = pBase;
  201. if (BIT_UP_SET(opcode))
  202. pFinal += getOffset(opcode);
  203. else
  204. pFinal -= getOffset(opcode);
  205. if (PREINDEXED(opcode))
  206. pAddress = pFinal;
  207. else
  208. pAddress = pBase;
  209. switch (opcode & MASK_TRANSFER_LENGTH) {
  210. case TRANSFER_SINGLE:
  211. loadSingle(getFd(opcode), pAddress);
  212. break;
  213. case TRANSFER_DOUBLE:
  214. loadDouble(getFd(opcode), pAddress);
  215. break;
  216. #ifdef CONFIG_FPE_NWFPE_XP
  217. case TRANSFER_EXTENDED:
  218. loadExtended(getFd(opcode), pAddress);
  219. break;
  220. #endif
  221. default:
  222. nRc = 0;
  223. }
  224. if (write_back)
  225. writeRegister(getRn(opcode), (unsigned long) pFinal);
  226. return nRc;
  227. }
  228. unsigned int PerformSTF(const unsigned int opcode)
  229. {
  230. unsigned int __user *pBase, *pAddress, *pFinal;
  231. unsigned int nRc = 1, write_back = WRITE_BACK(opcode);
  232. struct roundingData roundData;
  233. roundData.mode = SetRoundingMode(opcode);
  234. roundData.precision = SetRoundingPrecision(opcode);
  235. roundData.exception = 0;
  236. pBase = (unsigned int __user *) readRegister(getRn(opcode));
  237. if (REG_PC == getRn(opcode)) {
  238. pBase += 2;
  239. write_back = 0;
  240. }
  241. pFinal = pBase;
  242. if (BIT_UP_SET(opcode))
  243. pFinal += getOffset(opcode);
  244. else
  245. pFinal -= getOffset(opcode);
  246. if (PREINDEXED(opcode))
  247. pAddress = pFinal;
  248. else
  249. pAddress = pBase;
  250. switch (opcode & MASK_TRANSFER_LENGTH) {
  251. case TRANSFER_SINGLE:
  252. storeSingle(&roundData, getFd(opcode), pAddress);
  253. break;
  254. case TRANSFER_DOUBLE:
  255. storeDouble(&roundData, getFd(opcode), pAddress);
  256. break;
  257. #ifdef CONFIG_FPE_NWFPE_XP
  258. case TRANSFER_EXTENDED:
  259. storeExtended(getFd(opcode), pAddress);
  260. break;
  261. #endif
  262. default:
  263. nRc = 0;
  264. }
  265. if (roundData.exception)
  266. float_raise(roundData.exception);
  267. if (write_back)
  268. writeRegister(getRn(opcode), (unsigned long) pFinal);
  269. return nRc;
  270. }
  271. unsigned int PerformLFM(const unsigned int opcode)
  272. {
  273. unsigned int __user *pBase, *pAddress, *pFinal;
  274. unsigned int i, Fd, write_back = WRITE_BACK(opcode);
  275. pBase = (unsigned int __user *) readRegister(getRn(opcode));
  276. if (REG_PC == getRn(opcode)) {
  277. pBase += 2;
  278. write_back = 0;
  279. }
  280. pFinal = pBase;
  281. if (BIT_UP_SET(opcode))
  282. pFinal += getOffset(opcode);
  283. else
  284. pFinal -= getOffset(opcode);
  285. if (PREINDEXED(opcode))
  286. pAddress = pFinal;
  287. else
  288. pAddress = pBase;
  289. Fd = getFd(opcode);
  290. for (i = getRegisterCount(opcode); i > 0; i--) {
  291. loadMultiple(Fd, pAddress);
  292. pAddress += 3;
  293. Fd++;
  294. if (Fd == 8)
  295. Fd = 0;
  296. }
  297. if (write_back)
  298. writeRegister(getRn(opcode), (unsigned long) pFinal);
  299. return 1;
  300. }
  301. unsigned int PerformSFM(const unsigned int opcode)
  302. {
  303. unsigned int __user *pBase, *pAddress, *pFinal;
  304. unsigned int i, Fd, write_back = WRITE_BACK(opcode);
  305. pBase = (unsigned int __user *) readRegister(getRn(opcode));
  306. if (REG_PC == getRn(opcode)) {
  307. pBase += 2;
  308. write_back = 0;
  309. }
  310. pFinal = pBase;
  311. if (BIT_UP_SET(opcode))
  312. pFinal += getOffset(opcode);
  313. else
  314. pFinal -= getOffset(opcode);
  315. if (PREINDEXED(opcode))
  316. pAddress = pFinal;
  317. else
  318. pAddress = pBase;
  319. Fd = getFd(opcode);
  320. for (i = getRegisterCount(opcode); i > 0; i--) {
  321. storeMultiple(Fd, pAddress);
  322. pAddress += 3;
  323. Fd++;
  324. if (Fd == 8)
  325. Fd = 0;
  326. }
  327. if (write_back)
  328. writeRegister(getRn(opcode), (unsigned long) pFinal);
  329. return 1;
  330. }
  331. unsigned int EmulateCPDT(const unsigned int opcode)
  332. {
  333. unsigned int nRc = 0;
  334. if (LDF_OP(opcode)) {
  335. nRc = PerformLDF(opcode);
  336. } else if (LFM_OP(opcode)) {
  337. nRc = PerformLFM(opcode);
  338. } else if (STF_OP(opcode)) {
  339. nRc = PerformSTF(opcode);
  340. } else if (SFM_OP(opcode)) {
  341. nRc = PerformSFM(opcode);
  342. } else {
  343. nRc = 0;
  344. }
  345. return nRc;
  346. }