PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/2.6.32_froyo_photon_nightly/arch/x86/math-emu/reg_ld_str.c

http://photon-android.googlecode.com/
C | 1219 lines | 943 code | 136 blank | 140 comment | 281 complexity | 474e8f43982760086a18a0d7a2e6ff5c MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. /*---------------------------------------------------------------------------+
  2. | reg_ld_str.c |
  3. | |
  4. | All of the functions which transfer data between user memory and FPU_REGs.|
  5. | |
  6. | Copyright (C) 1992,1993,1994,1996,1997 |
  7. | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
  8. | E-mail billm@suburbia.net |
  9. | |
  10. | |
  11. +---------------------------------------------------------------------------*/
  12. /*---------------------------------------------------------------------------+
  13. | Note: |
  14. | The file contains code which accesses user memory. |
  15. | Emulator static data may change when user memory is accessed, due to |
  16. | other processes using the emulator while swapping is in progress. |
  17. +---------------------------------------------------------------------------*/
  18. #include "fpu_emu.h"
  19. #include <asm/uaccess.h>
  20. #include "fpu_system.h"
  21. #include "exception.h"
  22. #include "reg_constant.h"
  23. #include "control_w.h"
  24. #include "status_w.h"
  25. #define DOUBLE_Emax 1023 /* largest valid exponent */
  26. #define DOUBLE_Ebias 1023
  27. #define DOUBLE_Emin (-1022) /* smallest valid exponent */
  28. #define SINGLE_Emax 127 /* largest valid exponent */
  29. #define SINGLE_Ebias 127
  30. #define SINGLE_Emin (-126) /* smallest valid exponent */
  31. static u_char normalize_no_excep(FPU_REG *r, int exp, int sign)
  32. {
  33. u_char tag;
  34. setexponent16(r, exp);
  35. tag = FPU_normalize_nuo(r);
  36. stdexp(r);
  37. if (sign)
  38. setnegative(r);
  39. return tag;
  40. }
  41. int FPU_tagof(FPU_REG *ptr)
  42. {
  43. int exp;
  44. exp = exponent16(ptr) & 0x7fff;
  45. if (exp == 0) {
  46. if (!(ptr->sigh | ptr->sigl)) {
  47. return TAG_Zero;
  48. }
  49. /* The number is a de-normal or pseudodenormal. */
  50. return TAG_Special;
  51. }
  52. if (exp == 0x7fff) {
  53. /* Is an Infinity, a NaN, or an unsupported data type. */
  54. return TAG_Special;
  55. }
  56. if (!(ptr->sigh & 0x80000000)) {
  57. /* Unsupported data type. */
  58. /* Valid numbers have the ms bit set to 1. */
  59. /* Unnormal. */
  60. return TAG_Special;
  61. }
  62. return TAG_Valid;
  63. }
  64. /* Get a long double from user memory */
  65. int FPU_load_extended(long double __user *s, int stnr)
  66. {
  67. FPU_REG *sti_ptr = &st(stnr);
  68. RE_ENTRANT_CHECK_OFF;
  69. FPU_access_ok(VERIFY_READ, s, 10);
  70. __copy_from_user(sti_ptr, s, 10);
  71. RE_ENTRANT_CHECK_ON;
  72. return FPU_tagof(sti_ptr);
  73. }
  74. /* Get a double from user memory */
  75. int FPU_load_double(double __user *dfloat, FPU_REG *loaded_data)
  76. {
  77. int exp, tag, negative;
  78. unsigned m64, l64;
  79. RE_ENTRANT_CHECK_OFF;
  80. FPU_access_ok(VERIFY_READ, dfloat, 8);
  81. FPU_get_user(m64, 1 + (unsigned long __user *)dfloat);
  82. FPU_get_user(l64, (unsigned long __user *)dfloat);
  83. RE_ENTRANT_CHECK_ON;
  84. negative = (m64 & 0x80000000) ? SIGN_Negative : SIGN_Positive;
  85. exp = ((m64 & 0x7ff00000) >> 20) - DOUBLE_Ebias + EXTENDED_Ebias;
  86. m64 &= 0xfffff;
  87. if (exp > DOUBLE_Emax + EXTENDED_Ebias) {
  88. /* Infinity or NaN */
  89. if ((m64 == 0) && (l64 == 0)) {
  90. /* +- infinity */
  91. loaded_data->sigh = 0x80000000;
  92. loaded_data->sigl = 0x00000000;
  93. exp = EXP_Infinity + EXTENDED_Ebias;
  94. tag = TAG_Special;
  95. } else {
  96. /* Must be a signaling or quiet NaN */
  97. exp = EXP_NaN + EXTENDED_Ebias;
  98. loaded_data->sigh = (m64 << 11) | 0x80000000;
  99. loaded_data->sigh |= l64 >> 21;
  100. loaded_data->sigl = l64 << 11;
  101. tag = TAG_Special; /* The calling function must look for NaNs */
  102. }
  103. } else if (exp < DOUBLE_Emin + EXTENDED_Ebias) {
  104. /* Zero or de-normal */
  105. if ((m64 == 0) && (l64 == 0)) {
  106. /* Zero */
  107. reg_copy(&CONST_Z, loaded_data);
  108. exp = 0;
  109. tag = TAG_Zero;
  110. } else {
  111. /* De-normal */
  112. loaded_data->sigh = m64 << 11;
  113. loaded_data->sigh |= l64 >> 21;
  114. loaded_data->sigl = l64 << 11;
  115. return normalize_no_excep(loaded_data, DOUBLE_Emin,
  116. negative)
  117. | (denormal_operand() < 0 ? FPU_Exception : 0);
  118. }
  119. } else {
  120. loaded_data->sigh = (m64 << 11) | 0x80000000;
  121. loaded_data->sigh |= l64 >> 21;
  122. loaded_data->sigl = l64 << 11;
  123. tag = TAG_Valid;
  124. }
  125. setexponent16(loaded_data, exp | negative);
  126. return tag;
  127. }
  128. /* Get a float from user memory */
  129. int FPU_load_single(float __user *single, FPU_REG *loaded_data)
  130. {
  131. unsigned m32;
  132. int exp, tag, negative;
  133. RE_ENTRANT_CHECK_OFF;
  134. FPU_access_ok(VERIFY_READ, single, 4);
  135. FPU_get_user(m32, (unsigned long __user *)single);
  136. RE_ENTRANT_CHECK_ON;
  137. negative = (m32 & 0x80000000) ? SIGN_Negative : SIGN_Positive;
  138. if (!(m32 & 0x7fffffff)) {
  139. /* Zero */
  140. reg_copy(&CONST_Z, loaded_data);
  141. addexponent(loaded_data, negative);
  142. return TAG_Zero;
  143. }
  144. exp = ((m32 & 0x7f800000) >> 23) - SINGLE_Ebias + EXTENDED_Ebias;
  145. m32 = (m32 & 0x7fffff) << 8;
  146. if (exp < SINGLE_Emin + EXTENDED_Ebias) {
  147. /* De-normals */
  148. loaded_data->sigh = m32;
  149. loaded_data->sigl = 0;
  150. return normalize_no_excep(loaded_data, SINGLE_Emin, negative)
  151. | (denormal_operand() < 0 ? FPU_Exception : 0);
  152. } else if (exp > SINGLE_Emax + EXTENDED_Ebias) {
  153. /* Infinity or NaN */
  154. if (m32 == 0) {
  155. /* +- infinity */
  156. loaded_data->sigh = 0x80000000;
  157. loaded_data->sigl = 0x00000000;
  158. exp = EXP_Infinity + EXTENDED_Ebias;
  159. tag = TAG_Special;
  160. } else {
  161. /* Must be a signaling or quiet NaN */
  162. exp = EXP_NaN + EXTENDED_Ebias;
  163. loaded_data->sigh = m32 | 0x80000000;
  164. loaded_data->sigl = 0;
  165. tag = TAG_Special; /* The calling function must look for NaNs */
  166. }
  167. } else {
  168. loaded_data->sigh = m32 | 0x80000000;
  169. loaded_data->sigl = 0;
  170. tag = TAG_Valid;
  171. }
  172. setexponent16(loaded_data, exp | negative); /* Set the sign. */
  173. return tag;
  174. }
  175. /* Get a long long from user memory */
  176. int FPU_load_int64(long long __user *_s)
  177. {
  178. long long s;
  179. int sign;
  180. FPU_REG *st0_ptr = &st(0);
  181. RE_ENTRANT_CHECK_OFF;
  182. FPU_access_ok(VERIFY_READ, _s, 8);
  183. if (copy_from_user(&s, _s, 8))
  184. FPU_abort;
  185. RE_ENTRANT_CHECK_ON;
  186. if (s == 0) {
  187. reg_copy(&CONST_Z, st0_ptr);
  188. return TAG_Zero;
  189. }
  190. if (s > 0)
  191. sign = SIGN_Positive;
  192. else {
  193. s = -s;
  194. sign = SIGN_Negative;
  195. }
  196. significand(st0_ptr) = s;
  197. return normalize_no_excep(st0_ptr, 63, sign);
  198. }
  199. /* Get a long from user memory */
  200. int FPU_load_int32(long __user *_s, FPU_REG *loaded_data)
  201. {
  202. long s;
  203. int negative;
  204. RE_ENTRANT_CHECK_OFF;
  205. FPU_access_ok(VERIFY_READ, _s, 4);
  206. FPU_get_user(s, _s);
  207. RE_ENTRANT_CHECK_ON;
  208. if (s == 0) {
  209. reg_copy(&CONST_Z, loaded_data);
  210. return TAG_Zero;
  211. }
  212. if (s > 0)
  213. negative = SIGN_Positive;
  214. else {
  215. s = -s;
  216. negative = SIGN_Negative;
  217. }
  218. loaded_data->sigh = s;
  219. loaded_data->sigl = 0;
  220. return normalize_no_excep(loaded_data, 31, negative);
  221. }
  222. /* Get a short from user memory */
  223. int FPU_load_int16(short __user *_s, FPU_REG *loaded_data)
  224. {
  225. int s, negative;
  226. RE_ENTRANT_CHECK_OFF;
  227. FPU_access_ok(VERIFY_READ, _s, 2);
  228. /* Cast as short to get the sign extended. */
  229. FPU_get_user(s, _s);
  230. RE_ENTRANT_CHECK_ON;
  231. if (s == 0) {
  232. reg_copy(&CONST_Z, loaded_data);
  233. return TAG_Zero;
  234. }
  235. if (s > 0)
  236. negative = SIGN_Positive;
  237. else {
  238. s = -s;
  239. negative = SIGN_Negative;
  240. }
  241. loaded_data->sigh = s << 16;
  242. loaded_data->sigl = 0;
  243. return normalize_no_excep(loaded_data, 15, negative);
  244. }
  245. /* Get a packed bcd array from user memory */
  246. int FPU_load_bcd(u_char __user *s)
  247. {
  248. FPU_REG *st0_ptr = &st(0);
  249. int pos;
  250. u_char bcd;
  251. long long l = 0;
  252. int sign;
  253. RE_ENTRANT_CHECK_OFF;
  254. FPU_access_ok(VERIFY_READ, s, 10);
  255. RE_ENTRANT_CHECK_ON;
  256. for (pos = 8; pos >= 0; pos--) {
  257. l *= 10;
  258. RE_ENTRANT_CHECK_OFF;
  259. FPU_get_user(bcd, s + pos);
  260. RE_ENTRANT_CHECK_ON;
  261. l += bcd >> 4;
  262. l *= 10;
  263. l += bcd & 0x0f;
  264. }
  265. RE_ENTRANT_CHECK_OFF;
  266. FPU_get_user(sign, s + 9);
  267. sign = sign & 0x80 ? SIGN_Negative : SIGN_Positive;
  268. RE_ENTRANT_CHECK_ON;
  269. if (l == 0) {
  270. reg_copy(&CONST_Z, st0_ptr);
  271. addexponent(st0_ptr, sign); /* Set the sign. */
  272. return TAG_Zero;
  273. } else {
  274. significand(st0_ptr) = l;
  275. return normalize_no_excep(st0_ptr, 63, sign);
  276. }
  277. }
  278. /*===========================================================================*/
  279. /* Put a long double into user memory */
  280. int FPU_store_extended(FPU_REG *st0_ptr, u_char st0_tag,
  281. long double __user * d)
  282. {
  283. /*
  284. The only exception raised by an attempt to store to an
  285. extended format is the Invalid Stack exception, i.e.
  286. attempting to store from an empty register.
  287. */
  288. if (st0_tag != TAG_Empty) {
  289. RE_ENTRANT_CHECK_OFF;
  290. FPU_access_ok(VERIFY_WRITE, d, 10);
  291. FPU_put_user(st0_ptr->sigl, (unsigned long __user *)d);
  292. FPU_put_user(st0_ptr->sigh,
  293. (unsigned long __user *)((u_char __user *) d + 4));
  294. FPU_put_user(exponent16(st0_ptr),
  295. (unsigned short __user *)((u_char __user *) d +
  296. 8));
  297. RE_ENTRANT_CHECK_ON;
  298. return 1;
  299. }
  300. /* Empty register (stack underflow) */
  301. EXCEPTION(EX_StackUnder);
  302. if (control_word & CW_Invalid) {
  303. /* The masked response */
  304. /* Put out the QNaN indefinite */
  305. RE_ENTRANT_CHECK_OFF;
  306. FPU_access_ok(VERIFY_WRITE, d, 10);
  307. FPU_put_user(0, (unsigned long __user *)d);
  308. FPU_put_user(0xc0000000, 1 + (unsigned long __user *)d);
  309. FPU_put_user(0xffff, 4 + (short __user *)d);
  310. RE_ENTRANT_CHECK_ON;
  311. return 1;
  312. } else
  313. return 0;
  314. }
  315. /* Put a double into user memory */
  316. int FPU_store_double(FPU_REG *st0_ptr, u_char st0_tag, double __user *dfloat)
  317. {
  318. unsigned long l[2];
  319. unsigned long increment = 0; /* avoid gcc warnings */
  320. int precision_loss;
  321. int exp;
  322. FPU_REG tmp;
  323. l[0] = 0;
  324. l[1] = 0;
  325. if (st0_tag == TAG_Valid) {
  326. reg_copy(st0_ptr, &tmp);
  327. exp = exponent(&tmp);
  328. if (exp < DOUBLE_Emin) { /* It may be a denormal */
  329. addexponent(&tmp, -DOUBLE_Emin + 52); /* largest exp to be 51 */
  330. denormal_arg:
  331. if ((precision_loss = FPU_round_to_int(&tmp, st0_tag))) {
  332. #ifdef PECULIAR_486
  333. /* Did it round to a non-denormal ? */
  334. /* This behaviour might be regarded as peculiar, it appears
  335. that the 80486 rounds to the dest precision, then
  336. converts to decide underflow. */
  337. if (!
  338. ((tmp.sigh == 0x00100000) && (tmp.sigl == 0)
  339. && (st0_ptr->sigl & 0x000007ff)))
  340. #endif /* PECULIAR_486 */
  341. {
  342. EXCEPTION(EX_Underflow);
  343. /* This is a special case: see sec 16.2.5.1 of
  344. the 80486 book */
  345. if (!(control_word & CW_Underflow))
  346. return 0;
  347. }
  348. EXCEPTION(precision_loss);
  349. if (!(control_word & CW_Precision))
  350. return 0;
  351. }
  352. l[0] = tmp.sigl;
  353. l[1] = tmp.sigh;
  354. } else {
  355. if (tmp.sigl & 0x000007ff) {
  356. precision_loss = 1;
  357. switch (control_word & CW_RC) {
  358. case RC_RND:
  359. /* Rounding can get a little messy.. */
  360. increment = ((tmp.sigl & 0x7ff) > 0x400) | /* nearest */
  361. ((tmp.sigl & 0xc00) == 0xc00); /* odd -> even */
  362. break;
  363. case RC_DOWN: /* towards -infinity */
  364. increment =
  365. signpositive(&tmp) ? 0 : tmp.
  366. sigl & 0x7ff;
  367. break;
  368. case RC_UP: /* towards +infinity */
  369. increment =
  370. signpositive(&tmp) ? tmp.
  371. sigl & 0x7ff : 0;
  372. break;
  373. case RC_CHOP:
  374. increment = 0;
  375. break;
  376. }
  377. /* Truncate the mantissa */
  378. tmp.sigl &= 0xfffff800;
  379. if (increment) {
  380. if (tmp.sigl >= 0xfffff800) {
  381. /* the sigl part overflows */
  382. if (tmp.sigh == 0xffffffff) {
  383. /* The sigh part overflows */
  384. tmp.sigh = 0x80000000;
  385. exp++;
  386. if (exp >= EXP_OVER)
  387. goto overflow;
  388. } else {
  389. tmp.sigh++;
  390. }
  391. tmp.sigl = 0x00000000;
  392. } else {
  393. /* We only need to increment sigl */
  394. tmp.sigl += 0x00000800;
  395. }
  396. }
  397. } else
  398. precision_loss = 0;
  399. l[0] = (tmp.sigl >> 11) | (tmp.sigh << 21);
  400. l[1] = ((tmp.sigh >> 11) & 0xfffff);
  401. if (exp > DOUBLE_Emax) {
  402. overflow:
  403. EXCEPTION(EX_Overflow);
  404. if (!(control_word & CW_Overflow))
  405. return 0;
  406. set_precision_flag_up();
  407. if (!(control_word & CW_Precision))
  408. return 0;
  409. /* This is a special case: see sec 16.2.5.1 of the 80486 book */
  410. /* Overflow to infinity */
  411. l[1] = 0x7ff00000; /* Set to + INF */
  412. } else {
  413. if (precision_loss) {
  414. if (increment)
  415. set_precision_flag_up();
  416. else
  417. set_precision_flag_down();
  418. }
  419. /* Add the exponent */
  420. l[1] |= (((exp + DOUBLE_Ebias) & 0x7ff) << 20);
  421. }
  422. }
  423. } else if (st0_tag == TAG_Zero) {
  424. /* Number is zero */
  425. } else if (st0_tag == TAG_Special) {
  426. st0_tag = FPU_Special(st0_ptr);
  427. if (st0_tag == TW_Denormal) {
  428. /* A denormal will always underflow. */
  429. #ifndef PECULIAR_486
  430. /* An 80486 is supposed to be able to generate
  431. a denormal exception here, but... */
  432. /* Underflow has priority. */
  433. if (control_word & CW_Underflow)
  434. denormal_operand();
  435. #endif /* PECULIAR_486 */
  436. reg_copy(st0_ptr, &tmp);
  437. goto denormal_arg;
  438. } else if (st0_tag == TW_Infinity) {
  439. l[1] = 0x7ff00000;
  440. } else if (st0_tag == TW_NaN) {
  441. /* Is it really a NaN ? */
  442. if ((exponent(st0_ptr) == EXP_OVER)
  443. && (st0_ptr->sigh & 0x80000000)) {
  444. /* See if we can get a valid NaN from the FPU_REG */
  445. l[0] =
  446. (st0_ptr->sigl >> 11) | (st0_ptr->
  447. sigh << 21);
  448. l[1] = ((st0_ptr->sigh >> 11) & 0xfffff);
  449. if (!(st0_ptr->sigh & 0x40000000)) {
  450. /* It is a signalling NaN */
  451. EXCEPTION(EX_Invalid);
  452. if (!(control_word & CW_Invalid))
  453. return 0;
  454. l[1] |= (0x40000000 >> 11);
  455. }
  456. l[1] |= 0x7ff00000;
  457. } else {
  458. /* It is an unsupported data type */
  459. EXCEPTION(EX_Invalid);
  460. if (!(control_word & CW_Invalid))
  461. return 0;
  462. l[1] = 0xfff80000;
  463. }
  464. }
  465. } else if (st0_tag == TAG_Empty) {
  466. /* Empty register (stack underflow) */
  467. EXCEPTION(EX_StackUnder);
  468. if (control_word & CW_Invalid) {
  469. /* The masked response */
  470. /* Put out the QNaN indefinite */
  471. RE_ENTRANT_CHECK_OFF;
  472. FPU_access_ok(VERIFY_WRITE, dfloat, 8);
  473. FPU_put_user(0, (unsigned long __user *)dfloat);
  474. FPU_put_user(0xfff80000,
  475. 1 + (unsigned long __user *)dfloat);
  476. RE_ENTRANT_CHECK_ON;
  477. return 1;
  478. } else
  479. return 0;
  480. }
  481. if (getsign(st0_ptr))
  482. l[1] |= 0x80000000;
  483. RE_ENTRANT_CHECK_OFF;
  484. FPU_access_ok(VERIFY_WRITE, dfloat, 8);
  485. FPU_put_user(l[0], (unsigned long __user *)dfloat);
  486. FPU_put_user(l[1], 1 + (unsigned long __user *)dfloat);
  487. RE_ENTRANT_CHECK_ON;
  488. return 1;
  489. }
  490. /* Put a float into user memory */
  491. int FPU_store_single(FPU_REG *st0_ptr, u_char st0_tag, float __user *single)
  492. {
  493. long templ = 0;
  494. unsigned long increment = 0; /* avoid gcc warnings */
  495. int precision_loss;
  496. int exp;
  497. FPU_REG tmp;
  498. if (st0_tag == TAG_Valid) {
  499. reg_copy(st0_ptr, &tmp);
  500. exp = exponent(&tmp);
  501. if (exp < SINGLE_Emin) {
  502. addexponent(&tmp, -SINGLE_Emin + 23); /* largest exp to be 22 */
  503. denormal_arg:
  504. if ((precision_loss = FPU_round_to_int(&tmp, st0_tag))) {
  505. #ifdef PECULIAR_486
  506. /* Did it round to a non-denormal ? */
  507. /* This behaviour might be regarded as peculiar, it appears
  508. that the 80486 rounds to the dest precision, then
  509. converts to decide underflow. */
  510. if (!((tmp.sigl == 0x00800000) &&
  511. ((st0_ptr->sigh & 0x000000ff)
  512. || st0_ptr->sigl)))
  513. #endif /* PECULIAR_486 */
  514. {
  515. EXCEPTION(EX_Underflow);
  516. /* This is a special case: see sec 16.2.5.1 of
  517. the 80486 book */
  518. if (!(control_word & CW_Underflow))
  519. return 0;
  520. }
  521. EXCEPTION(precision_loss);
  522. if (!(control_word & CW_Precision))
  523. return 0;
  524. }
  525. templ = tmp.sigl;
  526. } else {
  527. if (tmp.sigl | (tmp.sigh & 0x000000ff)) {
  528. unsigned long sigh = tmp.sigh;
  529. unsigned long sigl = tmp.sigl;
  530. precision_loss = 1;
  531. switch (control_word & CW_RC) {
  532. case RC_RND:
  533. increment = ((sigh & 0xff) > 0x80) /* more than half */
  534. ||(((sigh & 0xff) == 0x80) && sigl) /* more than half */
  535. ||((sigh & 0x180) == 0x180); /* round to even */
  536. break;
  537. case RC_DOWN: /* towards -infinity */
  538. increment = signpositive(&tmp)
  539. ? 0 : (sigl | (sigh & 0xff));
  540. break;
  541. case RC_UP: /* towards +infinity */
  542. increment = signpositive(&tmp)
  543. ? (sigl | (sigh & 0xff)) : 0;
  544. break;
  545. case RC_CHOP:
  546. increment = 0;
  547. break;
  548. }
  549. /* Truncate part of the mantissa */
  550. tmp.sigl = 0;
  551. if (increment) {
  552. if (sigh >= 0xffffff00) {
  553. /* The sigh part overflows */
  554. tmp.sigh = 0x80000000;
  555. exp++;
  556. if (exp >= EXP_OVER)
  557. goto overflow;
  558. } else {
  559. tmp.sigh &= 0xffffff00;
  560. tmp.sigh += 0x100;
  561. }
  562. } else {
  563. tmp.sigh &= 0xffffff00; /* Finish the truncation */
  564. }
  565. } else
  566. precision_loss = 0;
  567. templ = (tmp.sigh >> 8) & 0x007fffff;
  568. if (exp > SINGLE_Emax) {
  569. overflow:
  570. EXCEPTION(EX_Overflow);
  571. if (!(control_word & CW_Overflow))
  572. return 0;
  573. set_precision_flag_up();
  574. if (!(control_word & CW_Precision))
  575. return 0;
  576. /* This is a special case: see sec 16.2.5.1 of the 80486 book. */
  577. /* Masked response is overflow to infinity. */
  578. templ = 0x7f800000;
  579. } else {
  580. if (precision_loss) {
  581. if (increment)
  582. set_precision_flag_up();
  583. else
  584. set_precision_flag_down();
  585. }
  586. /* Add the exponent */
  587. templ |= ((exp + SINGLE_Ebias) & 0xff) << 23;
  588. }
  589. }
  590. } else if (st0_tag == TAG_Zero) {
  591. templ = 0;
  592. } else if (st0_tag == TAG_Special) {
  593. st0_tag = FPU_Special(st0_ptr);
  594. if (st0_tag == TW_Denormal) {
  595. reg_copy(st0_ptr, &tmp);
  596. /* A denormal will always underflow. */
  597. #ifndef PECULIAR_486
  598. /* An 80486 is supposed to be able to generate
  599. a denormal exception here, but... */
  600. /* Underflow has priority. */
  601. if (control_word & CW_Underflow)
  602. denormal_operand();
  603. #endif /* PECULIAR_486 */
  604. goto denormal_arg;
  605. } else if (st0_tag == TW_Infinity) {
  606. templ = 0x7f800000;
  607. } else if (st0_tag == TW_NaN) {
  608. /* Is it really a NaN ? */
  609. if ((exponent(st0_ptr) == EXP_OVER)
  610. && (st0_ptr->sigh & 0x80000000)) {
  611. /* See if we can get a valid NaN from the FPU_REG */
  612. templ = st0_ptr->sigh >> 8;
  613. if (!(st0_ptr->sigh & 0x40000000)) {
  614. /* It is a signalling NaN */
  615. EXCEPTION(EX_Invalid);
  616. if (!(control_word & CW_Invalid))
  617. return 0;
  618. templ |= (0x40000000 >> 8);
  619. }
  620. templ |= 0x7f800000;
  621. } else {
  622. /* It is an unsupported data type */
  623. EXCEPTION(EX_Invalid);
  624. if (!(control_word & CW_Invalid))
  625. return 0;
  626. templ = 0xffc00000;
  627. }
  628. }
  629. #ifdef PARANOID
  630. else {
  631. EXCEPTION(EX_INTERNAL | 0x164);
  632. return 0;
  633. }
  634. #endif
  635. } else if (st0_tag == TAG_Empty) {
  636. /* Empty register (stack underflow) */
  637. EXCEPTION(EX_StackUnder);
  638. if (control_word & EX_Invalid) {
  639. /* The masked response */
  640. /* Put out the QNaN indefinite */
  641. RE_ENTRANT_CHECK_OFF;
  642. FPU_access_ok(VERIFY_WRITE, single, 4);
  643. FPU_put_user(0xffc00000,
  644. (unsigned long __user *)single);
  645. RE_ENTRANT_CHECK_ON;
  646. return 1;
  647. } else
  648. return 0;
  649. }
  650. #ifdef PARANOID
  651. else {
  652. EXCEPTION(EX_INTERNAL | 0x163);
  653. return 0;
  654. }
  655. #endif
  656. if (getsign(st0_ptr))
  657. templ |= 0x80000000;
  658. RE_ENTRANT_CHECK_OFF;
  659. FPU_access_ok(VERIFY_WRITE, single, 4);
  660. FPU_put_user(templ, (unsigned long __user *)single);
  661. RE_ENTRANT_CHECK_ON;
  662. return 1;
  663. }
  664. /* Put a long long into user memory */
  665. int FPU_store_int64(FPU_REG *st0_ptr, u_char st0_tag, long long __user *d)
  666. {
  667. FPU_REG t;
  668. long long tll;
  669. int precision_loss;
  670. if (st0_tag == TAG_Empty) {
  671. /* Empty register (stack underflow) */
  672. EXCEPTION(EX_StackUnder);
  673. goto invalid_operand;
  674. } else if (st0_tag == TAG_Special) {
  675. st0_tag = FPU_Special(st0_ptr);
  676. if ((st0_tag == TW_Infinity) || (st0_tag == TW_NaN)) {
  677. EXCEPTION(EX_Invalid);
  678. goto invalid_operand;
  679. }
  680. }
  681. reg_copy(st0_ptr, &t);
  682. precision_loss = FPU_round_to_int(&t, st0_tag);
  683. ((long *)&tll)[0] = t.sigl;
  684. ((long *)&tll)[1] = t.sigh;
  685. if ((precision_loss == 1) ||
  686. ((t.sigh & 0x80000000) &&
  687. !((t.sigh == 0x80000000) && (t.sigl == 0) && signnegative(&t)))) {
  688. EXCEPTION(EX_Invalid);
  689. /* This is a special case: see sec 16.2.5.1 of the 80486 book */
  690. invalid_operand:
  691. if (control_word & EX_Invalid) {
  692. /* Produce something like QNaN "indefinite" */
  693. tll = 0x8000000000000000LL;
  694. } else
  695. return 0;
  696. } else {
  697. if (precision_loss)
  698. set_precision_flag(precision_loss);
  699. if (signnegative(&t))
  700. tll = -tll;
  701. }
  702. RE_ENTRANT_CHECK_OFF;
  703. FPU_access_ok(VERIFY_WRITE, d, 8);
  704. if (copy_to_user(d, &tll, 8))
  705. FPU_abort;
  706. RE_ENTRANT_CHECK_ON;
  707. return 1;
  708. }
  709. /* Put a long into user memory */
  710. int FPU_store_int32(FPU_REG *st0_ptr, u_char st0_tag, long __user *d)
  711. {
  712. FPU_REG t;
  713. int precision_loss;
  714. if (st0_tag == TAG_Empty) {
  715. /* Empty register (stack underflow) */
  716. EXCEPTION(EX_StackUnder);
  717. goto invalid_operand;
  718. } else if (st0_tag == TAG_Special) {
  719. st0_tag = FPU_Special(st0_ptr);
  720. if ((st0_tag == TW_Infinity) || (st0_tag == TW_NaN)) {
  721. EXCEPTION(EX_Invalid);
  722. goto invalid_operand;
  723. }
  724. }
  725. reg_copy(st0_ptr, &t);
  726. precision_loss = FPU_round_to_int(&t, st0_tag);
  727. if (t.sigh ||
  728. ((t.sigl & 0x80000000) &&
  729. !((t.sigl == 0x80000000) && signnegative(&t)))) {
  730. EXCEPTION(EX_Invalid);
  731. /* This is a special case: see sec 16.2.5.1 of the 80486 book */
  732. invalid_operand:
  733. if (control_word & EX_Invalid) {
  734. /* Produce something like QNaN "indefinite" */
  735. t.sigl = 0x80000000;
  736. } else
  737. return 0;
  738. } else {
  739. if (precision_loss)
  740. set_precision_flag(precision_loss);
  741. if (signnegative(&t))
  742. t.sigl = -(long)t.sigl;
  743. }
  744. RE_ENTRANT_CHECK_OFF;
  745. FPU_access_ok(VERIFY_WRITE, d, 4);
  746. FPU_put_user(t.sigl, (unsigned long __user *)d);
  747. RE_ENTRANT_CHECK_ON;
  748. return 1;
  749. }
  750. /* Put a short into user memory */
  751. int FPU_store_int16(FPU_REG *st0_ptr, u_char st0_tag, short __user *d)
  752. {
  753. FPU_REG t;
  754. int precision_loss;
  755. if (st0_tag == TAG_Empty) {
  756. /* Empty register (stack underflow) */
  757. EXCEPTION(EX_StackUnder);
  758. goto invalid_operand;
  759. } else if (st0_tag == TAG_Special) {
  760. st0_tag = FPU_Special(st0_ptr);
  761. if ((st0_tag == TW_Infinity) || (st0_tag == TW_NaN)) {
  762. EXCEPTION(EX_Invalid);
  763. goto invalid_operand;
  764. }
  765. }
  766. reg_copy(st0_ptr, &t);
  767. precision_loss = FPU_round_to_int(&t, st0_tag);
  768. if (t.sigh ||
  769. ((t.sigl & 0xffff8000) &&
  770. !((t.sigl == 0x8000) && signnegative(&t)))) {
  771. EXCEPTION(EX_Invalid);
  772. /* This is a special case: see sec 16.2.5.1 of the 80486 book */
  773. invalid_operand:
  774. if (control_word & EX_Invalid) {
  775. /* Produce something like QNaN "indefinite" */
  776. t.sigl = 0x8000;
  777. } else
  778. return 0;
  779. } else {
  780. if (precision_loss)
  781. set_precision_flag(precision_loss);
  782. if (signnegative(&t))
  783. t.sigl = -t.sigl;
  784. }
  785. RE_ENTRANT_CHECK_OFF;
  786. FPU_access_ok(VERIFY_WRITE, d, 2);
  787. FPU_put_user((short)t.sigl, d);
  788. RE_ENTRANT_CHECK_ON;
  789. return 1;
  790. }
  791. /* Put a packed bcd array into user memory */
  792. int FPU_store_bcd(FPU_REG *st0_ptr, u_char st0_tag, u_char __user *d)
  793. {
  794. FPU_REG t;
  795. unsigned long long ll;
  796. u_char b;
  797. int i, precision_loss;
  798. u_char sign = (getsign(st0_ptr) == SIGN_NEG) ? 0x80 : 0;
  799. if (st0_tag == TAG_Empty) {
  800. /* Empty register (stack underflow) */
  801. EXCEPTION(EX_StackUnder);
  802. goto invalid_operand;
  803. } else if (st0_tag == TAG_Special) {
  804. st0_tag = FPU_Special(st0_ptr);
  805. if ((st0_tag == TW_Infinity) || (st0_tag == TW_NaN)) {
  806. EXCEPTION(EX_Invalid);
  807. goto invalid_operand;
  808. }
  809. }
  810. reg_copy(st0_ptr, &t);
  811. precision_loss = FPU_round_to_int(&t, st0_tag);
  812. ll = significand(&t);
  813. /* Check for overflow, by comparing with 999999999999999999 decimal. */
  814. if ((t.sigh > 0x0de0b6b3) ||
  815. ((t.sigh == 0x0de0b6b3) && (t.sigl > 0xa763ffff))) {
  816. EXCEPTION(EX_Invalid);
  817. /* This is a special case: see sec 16.2.5.1 of the 80486 book */
  818. invalid_operand:
  819. if (control_word & CW_Invalid) {
  820. /* Produce the QNaN "indefinite" */
  821. RE_ENTRANT_CHECK_OFF;
  822. FPU_access_ok(VERIFY_WRITE, d, 10);
  823. for (i = 0; i < 7; i++)
  824. FPU_put_user(0, d + i); /* These bytes "undefined" */
  825. FPU_put_user(0xc0, d + 7); /* This byte "undefined" */
  826. FPU_put_user(0xff, d + 8);
  827. FPU_put_user(0xff, d + 9);
  828. RE_ENTRANT_CHECK_ON;
  829. return 1;
  830. } else
  831. return 0;
  832. } else if (precision_loss) {
  833. /* Precision loss doesn't stop the data transfer */
  834. set_precision_flag(precision_loss);
  835. }
  836. RE_ENTRANT_CHECK_OFF;
  837. FPU_access_ok(VERIFY_WRITE, d, 10);
  838. RE_ENTRANT_CHECK_ON;
  839. for (i = 0; i < 9; i++) {
  840. b = FPU_div_small(&ll, 10);
  841. b |= (FPU_div_small(&ll, 10)) << 4;
  842. RE_ENTRANT_CHECK_OFF;
  843. FPU_put_user(b, d + i);
  844. RE_ENTRANT_CHECK_ON;
  845. }
  846. RE_ENTRANT_CHECK_OFF;
  847. FPU_put_user(sign, d + 9);
  848. RE_ENTRANT_CHECK_ON;
  849. return 1;
  850. }
  851. /*===========================================================================*/
  852. /* r gets mangled such that sig is int, sign:
  853. it is NOT normalized */
  854. /* The return value (in eax) is zero if the result is exact,
  855. if bits are changed due to rounding, truncation, etc, then
  856. a non-zero value is returned */
  857. /* Overflow is signalled by a non-zero return value (in eax).
  858. In the case of overflow, the returned significand always has the
  859. largest possible value */
  860. int FPU_round_to_int(FPU_REG *r, u_char tag)
  861. {
  862. u_char very_big;
  863. unsigned eax;
  864. if (tag == TAG_Zero) {
  865. /* Make sure that zero is returned */
  866. significand(r) = 0;
  867. return 0; /* o.k. */
  868. }
  869. if (exponent(r) > 63) {
  870. r->sigl = r->sigh = ~0; /* The largest representable number */
  871. return 1; /* overflow */
  872. }
  873. eax = FPU_shrxs(&r->sigl, 63 - exponent(r));
  874. very_big = !(~(r->sigh) | ~(r->sigl)); /* test for 0xfff...fff */
  875. #define half_or_more (eax & 0x80000000)
  876. #define frac_part (eax)
  877. #define more_than_half ((eax & 0x80000001) == 0x80000001)
  878. switch (control_word & CW_RC) {
  879. case RC_RND:
  880. if (more_than_half /* nearest */
  881. || (half_or_more && (r->sigl & 1))) { /* odd -> even */
  882. if (very_big)
  883. return 1; /* overflow */
  884. significand(r)++;
  885. return PRECISION_LOST_UP;
  886. }
  887. break;
  888. case RC_DOWN:
  889. if (frac_part && getsign(r)) {
  890. if (very_big)
  891. return 1; /* overflow */
  892. significand(r)++;
  893. return PRECISION_LOST_UP;
  894. }
  895. break;
  896. case RC_UP:
  897. if (frac_part && !getsign(r)) {
  898. if (very_big)
  899. return 1; /* overflow */
  900. significand(r)++;
  901. return PRECISION_LOST_UP;
  902. }
  903. break;
  904. case RC_CHOP:
  905. break;
  906. }
  907. return eax ? PRECISION_LOST_DOWN : 0;
  908. }
  909. /*===========================================================================*/
  910. u_char __user *fldenv(fpu_addr_modes addr_modes, u_char __user *s)
  911. {
  912. unsigned short tag_word = 0;
  913. u_char tag;
  914. int i;
  915. if ((addr_modes.default_mode == VM86) ||
  916. ((addr_modes.default_mode == PM16)
  917. ^ (addr_modes.override.operand_size == OP_SIZE_PREFIX))) {
  918. RE_ENTRANT_CHECK_OFF;
  919. FPU_access_ok(VERIFY_READ, s, 0x0e);
  920. FPU_get_user(control_word, (unsigned short __user *)s);
  921. FPU_get_user(partial_status, (unsigned short __user *)(s + 2));
  922. FPU_get_user(tag_word, (unsigned short __user *)(s + 4));
  923. FPU_get_user(instruction_address.offset,
  924. (unsigned short __user *)(s + 6));
  925. FPU_get_user(instruction_address.selector,
  926. (unsigned short __user *)(s + 8));
  927. FPU_get_user(operand_address.offset,
  928. (unsigned short __user *)(s + 0x0a));
  929. FPU_get_user(operand_address.selector,
  930. (unsigned short __user *)(s + 0x0c));
  931. RE_ENTRANT_CHECK_ON;
  932. s += 0x0e;
  933. if (addr_modes.default_mode == VM86) {
  934. instruction_address.offset
  935. += (instruction_address.selector & 0xf000) << 4;
  936. operand_address.offset +=
  937. (operand_address.selector & 0xf000) << 4;
  938. }
  939. } else {
  940. RE_ENTRANT_CHECK_OFF;
  941. FPU_access_ok(VERIFY_READ, s, 0x1c);
  942. FPU_get_user(control_word, (unsigned short __user *)s);
  943. FPU_get_user(partial_status, (unsigned short __user *)(s + 4));
  944. FPU_get_user(tag_word, (unsigned short __user *)(s + 8));
  945. FPU_get_user(instruction_address.offset,
  946. (unsigned long __user *)(s + 0x0c));
  947. FPU_get_user(instruction_address.selector,
  948. (unsigned short __user *)(s + 0x10));
  949. FPU_get_user(instruction_address.opcode,
  950. (unsigned short __user *)(s + 0x12));
  951. FPU_get_user(operand_address.offset,
  952. (unsigned long __user *)(s + 0x14));
  953. FPU_get_user(operand_address.selector,
  954. (unsigned long __user *)(s + 0x18));
  955. RE_ENTRANT_CHECK_ON;
  956. s += 0x1c;
  957. }
  958. #ifdef PECULIAR_486
  959. control_word &= ~0xe080;
  960. #endif /* PECULIAR_486 */
  961. top = (partial_status >> SW_Top_Shift) & 7;
  962. if (partial_status & ~control_word & CW_Exceptions)
  963. partial_status |= (SW_Summary | SW_Backward);
  964. else
  965. partial_status &= ~(SW_Summary | SW_Backward);
  966. for (i = 0; i < 8; i++) {
  967. tag = tag_word & 3;
  968. tag_word >>= 2;
  969. if (tag == TAG_Empty)
  970. /* New tag is empty. Accept it */
  971. FPU_settag(i, TAG_Empty);
  972. else if (FPU_gettag(i) == TAG_Empty) {
  973. /* Old tag is empty and new tag is not empty. New tag is determined
  974. by old reg contents */
  975. if (exponent(&fpu_register(i)) == -EXTENDED_Ebias) {
  976. if (!
  977. (fpu_register(i).sigl | fpu_register(i).
  978. sigh))
  979. FPU_settag(i, TAG_Zero);
  980. else
  981. FPU_settag(i, TAG_Special);
  982. } else if (exponent(&fpu_register(i)) ==
  983. 0x7fff - EXTENDED_Ebias) {
  984. FPU_settag(i, TAG_Special);
  985. } else if (fpu_register(i).sigh & 0x80000000)
  986. FPU_settag(i, TAG_Valid);
  987. else
  988. FPU_settag(i, TAG_Special); /* An Un-normal */
  989. }
  990. /* Else old tag is not empty and new tag is not empty. Old tag
  991. remains correct */
  992. }
  993. return s;
  994. }
  995. void frstor(fpu_addr_modes addr_modes, u_char __user *data_address)
  996. {
  997. int i, regnr;
  998. u_char __user *s = fldenv(addr_modes, data_address);
  999. int offset = (top & 7) * 10, other = 80 - offset;
  1000. /* Copy all registers in stack order. */
  1001. RE_ENTRANT_CHECK_OFF;
  1002. FPU_access_ok(VERIFY_READ, s, 80);
  1003. __copy_from_user(register_base + offset, s, other);
  1004. if (offset)
  1005. __copy_from_user(register_base, s + other, offset);
  1006. RE_ENTRANT_CHECK_ON;
  1007. for (i = 0; i < 8; i++) {
  1008. regnr = (i + top) & 7;
  1009. if (FPU_gettag(regnr) != TAG_Empty)
  1010. /* The loaded data over-rides all other cases. */
  1011. FPU_settag(regnr, FPU_tagof(&st(i)));
  1012. }
  1013. }
  1014. u_char __user *fstenv(fpu_addr_modes addr_modes, u_char __user *d)
  1015. {
  1016. if ((addr_modes.default_mode == VM86) ||
  1017. ((addr_modes.default_mode == PM16)
  1018. ^ (addr_modes.override.operand_size == OP_SIZE_PREFIX))) {
  1019. RE_ENTRANT_CHECK_OFF;
  1020. FPU_access_ok(VERIFY_WRITE, d, 14);
  1021. #ifdef PECULIAR_486
  1022. FPU_put_user(control_word & ~0xe080, (unsigned long __user *)d);
  1023. #else
  1024. FPU_put_user(control_word, (unsigned short __user *)d);
  1025. #endif /* PECULIAR_486 */
  1026. FPU_put_user(status_word(), (unsigned short __user *)(d + 2));
  1027. FPU_put_user(fpu_tag_word, (unsigned short __user *)(d + 4));
  1028. FPU_put_user(instruction_address.offset,
  1029. (unsigned short __user *)(d + 6));
  1030. FPU_put_user(operand_address.offset,
  1031. (unsigned short __user *)(d + 0x0a));
  1032. if (addr_modes.default_mode == VM86) {
  1033. FPU_put_user((instruction_address.
  1034. offset & 0xf0000) >> 4,
  1035. (unsigned short __user *)(d + 8));
  1036. FPU_put_user((operand_address.offset & 0xf0000) >> 4,
  1037. (unsigned short __user *)(d + 0x0c));
  1038. } else {
  1039. FPU_put_user(instruction_address.selector,
  1040. (unsigned short __user *)(d + 8));
  1041. FPU_put_user(operand_address.selector,
  1042. (unsigned short __user *)(d + 0x0c));
  1043. }
  1044. RE_ENTRANT_CHECK_ON;
  1045. d += 0x0e;
  1046. } else {
  1047. RE_ENTRANT_CHECK_OFF;
  1048. FPU_access_ok(VERIFY_WRITE, d, 7 * 4);
  1049. #ifdef PECULIAR_486
  1050. control_word &= ~0xe080;
  1051. /* An 80486 sets nearly all of the reserved bits to 1. */
  1052. control_word |= 0xffff0040;
  1053. partial_status = status_word() | 0xffff0000;
  1054. fpu_tag_word |= 0xffff0000;
  1055. I387->soft.fcs &= ~0xf8000000;
  1056. I387->soft.fos |= 0xffff0000;
  1057. #endif /* PECULIAR_486 */
  1058. if (__copy_to_user(d, &control_word, 7 * 4))
  1059. FPU_abort;
  1060. RE_ENTRANT_CHECK_ON;
  1061. d += 0x1c;
  1062. }
  1063. control_word |= CW_Exceptions;
  1064. partial_status &= ~(SW_Summary | SW_Backward);
  1065. return d;
  1066. }
  1067. void fsave(fpu_addr_modes addr_modes, u_char __user *data_address)
  1068. {
  1069. u_char __user *d;
  1070. int offset = (top & 7) * 10, other = 80 - offset;
  1071. d = fstenv(addr_modes, data_address);
  1072. RE_ENTRANT_CHECK_OFF;
  1073. FPU_access_ok(VERIFY_WRITE, d, 80);
  1074. /* Copy all registers in stack order. */
  1075. if (__copy_to_user(d, register_base + offset, other))
  1076. FPU_abort;
  1077. if (offset)
  1078. if (__copy_to_user(d + other, register_base, offset))
  1079. FPU_abort;
  1080. RE_ENTRANT_CHECK_ON;
  1081. finit();
  1082. }
  1083. /*===========================================================================*/