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