reg_ld_str.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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. if (st0_tag == TAG_Valid) {
  324. reg_copy(st0_ptr, &tmp);
  325. exp = exponent(&tmp);
  326. if (exp < DOUBLE_Emin) { /* It may be a denormal */
  327. addexponent(&tmp, -DOUBLE_Emin + 52); /* largest exp to be 51 */
  328. denormal_arg:
  329. if ((precision_loss = FPU_round_to_int(&tmp, st0_tag))) {
  330. #ifdef PECULIAR_486
  331. /* Did it round to a non-denormal ? */
  332. /* This behaviour might be regarded as peculiar, it appears
  333. that the 80486 rounds to the dest precision, then
  334. converts to decide underflow. */
  335. if (!
  336. ((tmp.sigh == 0x00100000) && (tmp.sigl == 0)
  337. && (st0_ptr->sigl & 0x000007ff)))
  338. #endif /* PECULIAR_486 */
  339. {
  340. EXCEPTION(EX_Underflow);
  341. /* This is a special case: see sec 16.2.5.1 of
  342. the 80486 book */
  343. if (!(control_word & CW_Underflow))
  344. return 0;
  345. }
  346. EXCEPTION(precision_loss);
  347. if (!(control_word & CW_Precision))
  348. return 0;
  349. }
  350. l[0] = tmp.sigl;
  351. l[1] = tmp.sigh;
  352. } else {
  353. if (tmp.sigl & 0x000007ff) {
  354. precision_loss = 1;
  355. switch (control_word & CW_RC) {
  356. case RC_RND:
  357. /* Rounding can get a little messy.. */
  358. increment = ((tmp.sigl & 0x7ff) > 0x400) | /* nearest */
  359. ((tmp.sigl & 0xc00) == 0xc00); /* odd -> even */
  360. break;
  361. case RC_DOWN: /* towards -infinity */
  362. increment =
  363. signpositive(&tmp) ? 0 : tmp.
  364. sigl & 0x7ff;
  365. break;
  366. case RC_UP: /* towards +infinity */
  367. increment =
  368. signpositive(&tmp) ? tmp.
  369. sigl & 0x7ff : 0;
  370. break;
  371. case RC_CHOP:
  372. increment = 0;
  373. break;
  374. }
  375. /* Truncate the mantissa */
  376. tmp.sigl &= 0xfffff800;
  377. if (increment) {
  378. if (tmp.sigl >= 0xfffff800) {
  379. /* the sigl part overflows */
  380. if (tmp.sigh == 0xffffffff) {
  381. /* The sigh part overflows */
  382. tmp.sigh = 0x80000000;
  383. exp++;
  384. if (exp >= EXP_OVER)
  385. goto overflow;
  386. } else {
  387. tmp.sigh++;
  388. }
  389. tmp.sigl = 0x00000000;
  390. } else {
  391. /* We only need to increment sigl */
  392. tmp.sigl += 0x00000800;
  393. }
  394. }
  395. } else
  396. precision_loss = 0;
  397. l[0] = (tmp.sigl >> 11) | (tmp.sigh << 21);
  398. l[1] = ((tmp.sigh >> 11) & 0xfffff);
  399. if (exp > DOUBLE_Emax) {
  400. overflow:
  401. EXCEPTION(EX_Overflow);
  402. if (!(control_word & CW_Overflow))
  403. return 0;
  404. set_precision_flag_up();
  405. if (!(control_word & CW_Precision))
  406. return 0;
  407. /* This is a special case: see sec 16.2.5.1 of the 80486 book */
  408. /* Overflow to infinity */
  409. l[0] = 0x00000000; /* Set to */
  410. l[1] = 0x7ff00000; /* + INF */
  411. } else {
  412. if (precision_loss) {
  413. if (increment)
  414. set_precision_flag_up();
  415. else
  416. set_precision_flag_down();
  417. }
  418. /* Add the exponent */
  419. l[1] |= (((exp + DOUBLE_Ebias) & 0x7ff) << 20);
  420. }
  421. }
  422. } else if (st0_tag == TAG_Zero) {
  423. /* Number is zero */
  424. l[0] = 0;
  425. l[1] = 0;
  426. } else if (st0_tag == TAG_Special) {
  427. st0_tag = FPU_Special(st0_ptr);
  428. if (st0_tag == TW_Denormal) {
  429. /* A denormal will always underflow. */
  430. #ifndef PECULIAR_486
  431. /* An 80486 is supposed to be able to generate
  432. a denormal exception here, but... */
  433. /* Underflow has priority. */
  434. if (control_word & CW_Underflow)
  435. denormal_operand();
  436. #endif /* PECULIAR_486 */
  437. reg_copy(st0_ptr, &tmp);
  438. goto denormal_arg;
  439. } else if (st0_tag == TW_Infinity) {
  440. l[0] = 0;
  441. l[1] = 0x7ff00000;
  442. } else if (st0_tag == TW_NaN) {
  443. /* Is it really a NaN ? */
  444. if ((exponent(st0_ptr) == EXP_OVER)
  445. && (st0_ptr->sigh & 0x80000000)) {
  446. /* See if we can get a valid NaN from the FPU_REG */
  447. l[0] =
  448. (st0_ptr->sigl >> 11) | (st0_ptr->
  449. sigh << 21);
  450. l[1] = ((st0_ptr->sigh >> 11) & 0xfffff);
  451. if (!(st0_ptr->sigh & 0x40000000)) {
  452. /* It is a signalling NaN */
  453. EXCEPTION(EX_Invalid);
  454. if (!(control_word & CW_Invalid))
  455. return 0;
  456. l[1] |= (0x40000000 >> 11);
  457. }
  458. l[1] |= 0x7ff00000;
  459. } else {
  460. /* It is an unsupported data type */
  461. EXCEPTION(EX_Invalid);
  462. if (!(control_word & CW_Invalid))
  463. return 0;
  464. l[0] = 0;
  465. l[1] = 0xfff80000;
  466. }
  467. }
  468. } else if (st0_tag == TAG_Empty) {
  469. /* Empty register (stack underflow) */
  470. EXCEPTION(EX_StackUnder);
  471. if (control_word & CW_Invalid) {
  472. /* The masked response */
  473. /* Put out the QNaN indefinite */
  474. RE_ENTRANT_CHECK_OFF;
  475. FPU_access_ok(VERIFY_WRITE, dfloat, 8);
  476. FPU_put_user(0, (unsigned long __user *)dfloat);
  477. FPU_put_user(0xfff80000,
  478. 1 + (unsigned long __user *)dfloat);
  479. RE_ENTRANT_CHECK_ON;
  480. return 1;
  481. } else
  482. return 0;
  483. }
  484. if (getsign(st0_ptr))
  485. l[1] |= 0x80000000;
  486. RE_ENTRANT_CHECK_OFF;
  487. FPU_access_ok(VERIFY_WRITE, dfloat, 8);
  488. FPU_put_user(l[0], (unsigned long __user *)dfloat);
  489. FPU_put_user(l[1], 1 + (unsigned long __user *)dfloat);
  490. RE_ENTRANT_CHECK_ON;
  491. return 1;
  492. }
  493. /* Put a float into user memory */
  494. int FPU_store_single(FPU_REG *st0_ptr, u_char st0_tag, float __user *single)
  495. {
  496. long templ = 0;
  497. unsigned long increment = 0; /* avoid gcc warnings */
  498. int precision_loss;
  499. int exp;
  500. FPU_REG tmp;
  501. if (st0_tag == TAG_Valid) {
  502. reg_copy(st0_ptr, &tmp);
  503. exp = exponent(&tmp);
  504. if (exp < SINGLE_Emin) {
  505. addexponent(&tmp, -SINGLE_Emin + 23); /* largest exp to be 22 */
  506. denormal_arg:
  507. if ((precision_loss = FPU_round_to_int(&tmp, st0_tag))) {
  508. #ifdef PECULIAR_486
  509. /* Did it round to a non-denormal ? */
  510. /* This behaviour might be regarded as peculiar, it appears
  511. that the 80486 rounds to the dest precision, then
  512. converts to decide underflow. */
  513. if (!((tmp.sigl == 0x00800000) &&
  514. ((st0_ptr->sigh & 0x000000ff)
  515. || st0_ptr->sigl)))
  516. #endif /* PECULIAR_486 */
  517. {
  518. EXCEPTION(EX_Underflow);
  519. /* This is a special case: see sec 16.2.5.1 of
  520. the 80486 book */
  521. if (!(control_word & CW_Underflow))
  522. return 0;
  523. }
  524. EXCEPTION(precision_loss);
  525. if (!(control_word & CW_Precision))
  526. return 0;
  527. }
  528. templ = tmp.sigl;
  529. } else {
  530. if (tmp.sigl | (tmp.sigh & 0x000000ff)) {
  531. unsigned long sigh = tmp.sigh;
  532. unsigned long sigl = tmp.sigl;
  533. precision_loss = 1;
  534. switch (control_word & CW_RC) {
  535. case RC_RND:
  536. increment = ((sigh & 0xff) > 0x80) /* more than half */
  537. ||(((sigh & 0xff) == 0x80) && sigl) /* more than half */
  538. ||((sigh & 0x180) == 0x180); /* round to even */
  539. break;
  540. case RC_DOWN: /* towards -infinity */
  541. increment = signpositive(&tmp)
  542. ? 0 : (sigl | (sigh & 0xff));
  543. break;
  544. case RC_UP: /* towards +infinity */
  545. increment = signpositive(&tmp)
  546. ? (sigl | (sigh & 0xff)) : 0;
  547. break;
  548. case RC_CHOP:
  549. increment = 0;
  550. break;
  551. }
  552. /* Truncate part of the mantissa */
  553. tmp.sigl = 0;
  554. if (increment) {
  555. if (sigh >= 0xffffff00) {
  556. /* The sigh part overflows */
  557. tmp.sigh = 0x80000000;
  558. exp++;
  559. if (exp >= EXP_OVER)
  560. goto overflow;
  561. } else {
  562. tmp.sigh &= 0xffffff00;
  563. tmp.sigh += 0x100;
  564. }
  565. } else {
  566. tmp.sigh &= 0xffffff00; /* Finish the truncation */
  567. }
  568. } else
  569. precision_loss = 0;
  570. templ = (tmp.sigh >> 8) & 0x007fffff;
  571. if (exp > SINGLE_Emax) {
  572. overflow:
  573. EXCEPTION(EX_Overflow);
  574. if (!(control_word & CW_Overflow))
  575. return 0;
  576. set_precision_flag_up();
  577. if (!(control_word & CW_Precision))
  578. return 0;
  579. /* This is a special case: see sec 16.2.5.1 of the 80486 book. */
  580. /* Masked response is overflow to infinity. */
  581. templ = 0x7f800000;
  582. } else {
  583. if (precision_loss) {
  584. if (increment)
  585. set_precision_flag_up();
  586. else
  587. set_precision_flag_down();
  588. }
  589. /* Add the exponent */
  590. templ |= ((exp + SINGLE_Ebias) & 0xff) << 23;
  591. }
  592. }
  593. } else if (st0_tag == TAG_Zero) {
  594. templ = 0;
  595. } else if (st0_tag == TAG_Special) {
  596. st0_tag = FPU_Special(st0_ptr);
  597. if (st0_tag == TW_Denormal) {
  598. reg_copy(st0_ptr, &tmp);
  599. /* A denormal will always underflow. */
  600. #ifndef PECULIAR_486
  601. /* An 80486 is supposed to be able to generate
  602. a denormal exception here, but... */
  603. /* Underflow has priority. */
  604. if (control_word & CW_Underflow)
  605. denormal_operand();
  606. #endif /* PECULIAR_486 */
  607. goto denormal_arg;
  608. } else if (st0_tag == TW_Infinity) {
  609. templ = 0x7f800000;
  610. } else if (st0_tag == TW_NaN) {
  611. /* Is it really a NaN ? */
  612. if ((exponent(st0_ptr) == EXP_OVER)
  613. && (st0_ptr->sigh & 0x80000000)) {
  614. /* See if we can get a valid NaN from the FPU_REG */
  615. templ = st0_ptr->sigh >> 8;
  616. if (!(st0_ptr->sigh & 0x40000000)) {
  617. /* It is a signalling NaN */
  618. EXCEPTION(EX_Invalid);
  619. if (!(control_word & CW_Invalid))
  620. return 0;
  621. templ |= (0x40000000 >> 8);
  622. }
  623. templ |= 0x7f800000;
  624. } else {
  625. /* It is an unsupported data type */
  626. EXCEPTION(EX_Invalid);
  627. if (!(control_word & CW_Invalid))
  628. return 0;
  629. templ = 0xffc00000;
  630. }
  631. }
  632. #ifdef PARANOID
  633. else {
  634. EXCEPTION(EX_INTERNAL | 0x164);
  635. return 0;
  636. }
  637. #endif
  638. } else if (st0_tag == TAG_Empty) {
  639. /* Empty register (stack underflow) */
  640. EXCEPTION(EX_StackUnder);
  641. if (control_word & EX_Invalid) {
  642. /* The masked response */
  643. /* Put out the QNaN indefinite */
  644. RE_ENTRANT_CHECK_OFF;
  645. FPU_access_ok(VERIFY_WRITE, single, 4);
  646. FPU_put_user(0xffc00000,
  647. (unsigned long __user *)single);
  648. RE_ENTRANT_CHECK_ON;
  649. return 1;
  650. } else
  651. return 0;
  652. }
  653. #ifdef PARANOID
  654. else {
  655. EXCEPTION(EX_INTERNAL | 0x163);
  656. return 0;
  657. }
  658. #endif
  659. if (getsign(st0_ptr))
  660. templ |= 0x80000000;
  661. RE_ENTRANT_CHECK_OFF;
  662. FPU_access_ok(VERIFY_WRITE, single, 4);
  663. FPU_put_user(templ, (unsigned long __user *)single);
  664. RE_ENTRANT_CHECK_ON;
  665. return 1;
  666. }
  667. /* Put a long long into user memory */
  668. int FPU_store_int64(FPU_REG *st0_ptr, u_char st0_tag, long long __user *d)
  669. {
  670. FPU_REG t;
  671. long long tll;
  672. int precision_loss;
  673. if (st0_tag == TAG_Empty) {
  674. /* Empty register (stack underflow) */
  675. EXCEPTION(EX_StackUnder);
  676. goto invalid_operand;
  677. } else if (st0_tag == TAG_Special) {
  678. st0_tag = FPU_Special(st0_ptr);
  679. if ((st0_tag == TW_Infinity) || (st0_tag == TW_NaN)) {
  680. EXCEPTION(EX_Invalid);
  681. goto invalid_operand;
  682. }
  683. }
  684. reg_copy(st0_ptr, &t);
  685. precision_loss = FPU_round_to_int(&t, st0_tag);
  686. ((long *)&tll)[0] = t.sigl;
  687. ((long *)&tll)[1] = t.sigh;
  688. if ((precision_loss == 1) ||
  689. ((t.sigh & 0x80000000) &&
  690. !((t.sigh == 0x80000000) && (t.sigl == 0) && signnegative(&t)))) {
  691. EXCEPTION(EX_Invalid);
  692. /* This is a special case: see sec 16.2.5.1 of the 80486 book */
  693. invalid_operand:
  694. if (control_word & EX_Invalid) {
  695. /* Produce something like QNaN "indefinite" */
  696. tll = 0x8000000000000000LL;
  697. } else
  698. return 0;
  699. } else {
  700. if (precision_loss)
  701. set_precision_flag(precision_loss);
  702. if (signnegative(&t))
  703. tll = -tll;
  704. }
  705. RE_ENTRANT_CHECK_OFF;
  706. FPU_access_ok(VERIFY_WRITE, d, 8);
  707. if (copy_to_user(d, &tll, 8))
  708. FPU_abort;
  709. RE_ENTRANT_CHECK_ON;
  710. return 1;
  711. }
  712. /* Put a long into user memory */
  713. int FPU_store_int32(FPU_REG *st0_ptr, u_char st0_tag, long __user *d)
  714. {
  715. FPU_REG t;
  716. int precision_loss;
  717. if (st0_tag == TAG_Empty) {
  718. /* Empty register (stack underflow) */
  719. EXCEPTION(EX_StackUnder);
  720. goto invalid_operand;
  721. } else if (st0_tag == TAG_Special) {
  722. st0_tag = FPU_Special(st0_ptr);
  723. if ((st0_tag == TW_Infinity) || (st0_tag == TW_NaN)) {
  724. EXCEPTION(EX_Invalid);
  725. goto invalid_operand;
  726. }
  727. }
  728. reg_copy(st0_ptr, &t);
  729. precision_loss = FPU_round_to_int(&t, st0_tag);
  730. if (t.sigh ||
  731. ((t.sigl & 0x80000000) &&
  732. !((t.sigl == 0x80000000) && signnegative(&t)))) {
  733. EXCEPTION(EX_Invalid);
  734. /* This is a special case: see sec 16.2.5.1 of the 80486 book */
  735. invalid_operand:
  736. if (control_word & EX_Invalid) {
  737. /* Produce something like QNaN "indefinite" */
  738. t.sigl = 0x80000000;
  739. } else
  740. return 0;
  741. } else {
  742. if (precision_loss)
  743. set_precision_flag(precision_loss);
  744. if (signnegative(&t))
  745. t.sigl = -(long)t.sigl;
  746. }
  747. RE_ENTRANT_CHECK_OFF;
  748. FPU_access_ok(VERIFY_WRITE, d, 4);
  749. FPU_put_user(t.sigl, (unsigned long __user *)d);
  750. RE_ENTRANT_CHECK_ON;
  751. return 1;
  752. }
  753. /* Put a short into user memory */
  754. int FPU_store_int16(FPU_REG *st0_ptr, u_char st0_tag, short __user *d)
  755. {
  756. FPU_REG t;
  757. int precision_loss;
  758. if (st0_tag == TAG_Empty) {
  759. /* Empty register (stack underflow) */
  760. EXCEPTION(EX_StackUnder);
  761. goto invalid_operand;
  762. } else if (st0_tag == TAG_Special) {
  763. st0_tag = FPU_Special(st0_ptr);
  764. if ((st0_tag == TW_Infinity) || (st0_tag == TW_NaN)) {
  765. EXCEPTION(EX_Invalid);
  766. goto invalid_operand;
  767. }
  768. }
  769. reg_copy(st0_ptr, &t);
  770. precision_loss = FPU_round_to_int(&t, st0_tag);
  771. if (t.sigh ||
  772. ((t.sigl & 0xffff8000) &&
  773. !((t.sigl == 0x8000) && signnegative(&t)))) {
  774. EXCEPTION(EX_Invalid);
  775. /* This is a special case: see sec 16.2.5.1 of the 80486 book */
  776. invalid_operand:
  777. if (control_word & EX_Invalid) {
  778. /* Produce something like QNaN "indefinite" */
  779. t.sigl = 0x8000;
  780. } else
  781. return 0;
  782. } else {
  783. if (precision_loss)
  784. set_precision_flag(precision_loss);
  785. if (signnegative(&t))
  786. t.sigl = -t.sigl;
  787. }
  788. RE_ENTRANT_CHECK_OFF;
  789. FPU_access_ok(VERIFY_WRITE, d, 2);
  790. FPU_put_user((short)t.sigl, d);
  791. RE_ENTRANT_CHECK_ON;
  792. return 1;
  793. }
  794. /* Put a packed bcd array into user memory */
  795. int FPU_store_bcd(FPU_REG *st0_ptr, u_char st0_tag, u_char __user *d)
  796. {
  797. FPU_REG t;
  798. unsigned long long ll;
  799. u_char b;
  800. int i, precision_loss;
  801. u_char sign = (getsign(st0_ptr) == SIGN_NEG) ? 0x80 : 0;
  802. if (st0_tag == TAG_Empty) {
  803. /* Empty register (stack underflow) */
  804. EXCEPTION(EX_StackUnder);
  805. goto invalid_operand;
  806. } else if (st0_tag == TAG_Special) {
  807. st0_tag = FPU_Special(st0_ptr);
  808. if ((st0_tag == TW_Infinity) || (st0_tag == TW_NaN)) {
  809. EXCEPTION(EX_Invalid);
  810. goto invalid_operand;
  811. }
  812. }
  813. reg_copy(st0_ptr, &t);
  814. precision_loss = FPU_round_to_int(&t, st0_tag);
  815. ll = significand(&t);
  816. /* Check for overflow, by comparing with 999999999999999999 decimal. */
  817. if ((t.sigh > 0x0de0b6b3) ||
  818. ((t.sigh == 0x0de0b6b3) && (t.sigl > 0xa763ffff))) {
  819. EXCEPTION(EX_Invalid);
  820. /* This is a special case: see sec 16.2.5.1 of the 80486 book */
  821. invalid_operand:
  822. if (control_word & CW_Invalid) {
  823. /* Produce the QNaN "indefinite" */
  824. RE_ENTRANT_CHECK_OFF;
  825. FPU_access_ok(VERIFY_WRITE, d, 10);
  826. for (i = 0; i < 7; i++)
  827. FPU_put_user(0, d + i); /* These bytes "undefined" */
  828. FPU_put_user(0xc0, d + 7); /* This byte "undefined" */
  829. FPU_put_user(0xff, d + 8);
  830. FPU_put_user(0xff, d + 9);
  831. RE_ENTRANT_CHECK_ON;
  832. return 1;
  833. } else
  834. return 0;
  835. } else if (precision_loss) {
  836. /* Precision loss doesn't stop the data transfer */
  837. set_precision_flag(precision_loss);
  838. }
  839. RE_ENTRANT_CHECK_OFF;
  840. FPU_access_ok(VERIFY_WRITE, d, 10);
  841. RE_ENTRANT_CHECK_ON;
  842. for (i = 0; i < 9; i++) {
  843. b = FPU_div_small(&ll, 10);
  844. b |= (FPU_div_small(&ll, 10)) << 4;
  845. RE_ENTRANT_CHECK_OFF;
  846. FPU_put_user(b, d + i);
  847. RE_ENTRANT_CHECK_ON;
  848. }
  849. RE_ENTRANT_CHECK_OFF;
  850. FPU_put_user(sign, d + 9);
  851. RE_ENTRANT_CHECK_ON;
  852. return 1;
  853. }
  854. /*===========================================================================*/
  855. /* r gets mangled such that sig is int, sign:
  856. it is NOT normalized */
  857. /* The return value (in eax) is zero if the result is exact,
  858. if bits are changed due to rounding, truncation, etc, then
  859. a non-zero value is returned */
  860. /* Overflow is signalled by a non-zero return value (in eax).
  861. In the case of overflow, the returned significand always has the
  862. largest possible value */
  863. int FPU_round_to_int(FPU_REG *r, u_char tag)
  864. {
  865. u_char very_big;
  866. unsigned eax;
  867. if (tag == TAG_Zero) {
  868. /* Make sure that zero is returned */
  869. significand(r) = 0;
  870. return 0; /* o.k. */
  871. }
  872. if (exponent(r) > 63) {
  873. r->sigl = r->sigh = ~0; /* The largest representable number */
  874. return 1; /* overflow */
  875. }
  876. eax = FPU_shrxs(&r->sigl, 63 - exponent(r));
  877. very_big = !(~(r->sigh) | ~(r->sigl)); /* test for 0xfff...fff */
  878. #define half_or_more (eax & 0x80000000)
  879. #define frac_part (eax)
  880. #define more_than_half ((eax & 0x80000001) == 0x80000001)
  881. switch (control_word & CW_RC) {
  882. case RC_RND:
  883. if (more_than_half /* nearest */
  884. || (half_or_more && (r->sigl & 1))) { /* odd -> even */
  885. if (very_big)
  886. return 1; /* overflow */
  887. significand(r)++;
  888. return PRECISION_LOST_UP;
  889. }
  890. break;
  891. case RC_DOWN:
  892. if (frac_part && getsign(r)) {
  893. if (very_big)
  894. return 1; /* overflow */
  895. significand(r)++;
  896. return PRECISION_LOST_UP;
  897. }
  898. break;
  899. case RC_UP:
  900. if (frac_part && !getsign(r)) {
  901. if (very_big)
  902. return 1; /* overflow */
  903. significand(r)++;
  904. return PRECISION_LOST_UP;
  905. }
  906. break;
  907. case RC_CHOP:
  908. break;
  909. }
  910. return eax ? PRECISION_LOST_DOWN : 0;
  911. }
  912. /*===========================================================================*/
  913. u_char __user *fldenv(fpu_addr_modes addr_modes, u_char __user *s)
  914. {
  915. unsigned short tag_word = 0;
  916. u_char tag;
  917. int i;
  918. if ((addr_modes.default_mode == VM86) ||
  919. ((addr_modes.default_mode == PM16)
  920. ^ (addr_modes.override.operand_size == OP_SIZE_PREFIX))) {
  921. RE_ENTRANT_CHECK_OFF;
  922. FPU_access_ok(VERIFY_READ, s, 0x0e);
  923. FPU_get_user(control_word, (unsigned short __user *)s);
  924. FPU_get_user(partial_status, (unsigned short __user *)(s + 2));
  925. FPU_get_user(tag_word, (unsigned short __user *)(s + 4));
  926. FPU_get_user(instruction_address.offset,
  927. (unsigned short __user *)(s + 6));
  928. FPU_get_user(instruction_address.selector,
  929. (unsigned short __user *)(s + 8));
  930. FPU_get_user(operand_address.offset,
  931. (unsigned short __user *)(s + 0x0a));
  932. FPU_get_user(operand_address.selector,
  933. (unsigned short __user *)(s + 0x0c));
  934. RE_ENTRANT_CHECK_ON;
  935. s += 0x0e;
  936. if (addr_modes.default_mode == VM86) {
  937. instruction_address.offset
  938. += (instruction_address.selector & 0xf000) << 4;
  939. operand_address.offset +=
  940. (operand_address.selector & 0xf000) << 4;
  941. }
  942. } else {
  943. RE_ENTRANT_CHECK_OFF;
  944. FPU_access_ok(VERIFY_READ, s, 0x1c);
  945. FPU_get_user(control_word, (unsigned short __user *)s);
  946. FPU_get_user(partial_status, (unsigned short __user *)(s + 4));
  947. FPU_get_user(tag_word, (unsigned short __user *)(s + 8));
  948. FPU_get_user(instruction_address.offset,
  949. (unsigned long __user *)(s + 0x0c));
  950. FPU_get_user(instruction_address.selector,
  951. (unsigned short __user *)(s + 0x10));
  952. FPU_get_user(instruction_address.opcode,
  953. (unsigned short __user *)(s + 0x12));
  954. FPU_get_user(operand_address.offset,
  955. (unsigned long __user *)(s + 0x14));
  956. FPU_get_user(operand_address.selector,
  957. (unsigned long __user *)(s + 0x18));
  958. RE_ENTRANT_CHECK_ON;
  959. s += 0x1c;
  960. }
  961. #ifdef PECULIAR_486
  962. control_word &= ~0xe080;
  963. #endif /* PECULIAR_486 */
  964. top = (partial_status >> SW_Top_Shift) & 7;
  965. if (partial_status & ~control_word & CW_Exceptions)
  966. partial_status |= (SW_Summary | SW_Backward);
  967. else
  968. partial_status &= ~(SW_Summary | SW_Backward);
  969. for (i = 0; i < 8; i++) {
  970. tag = tag_word & 3;
  971. tag_word >>= 2;
  972. if (tag == TAG_Empty)
  973. /* New tag is empty. Accept it */
  974. FPU_settag(i, TAG_Empty);
  975. else if (FPU_gettag(i) == TAG_Empty) {
  976. /* Old tag is empty and new tag is not empty. New tag is determined
  977. by old reg contents */
  978. if (exponent(&fpu_register(i)) == -EXTENDED_Ebias) {
  979. if (!
  980. (fpu_register(i).sigl | fpu_register(i).
  981. sigh))
  982. FPU_settag(i, TAG_Zero);
  983. else
  984. FPU_settag(i, TAG_Special);
  985. } else if (exponent(&fpu_register(i)) ==
  986. 0x7fff - EXTENDED_Ebias) {
  987. FPU_settag(i, TAG_Special);
  988. } else if (fpu_register(i).sigh & 0x80000000)
  989. FPU_settag(i, TAG_Valid);
  990. else
  991. FPU_settag(i, TAG_Special); /* An Un-normal */
  992. }
  993. /* Else old tag is not empty and new tag is not empty. Old tag
  994. remains correct */
  995. }
  996. return s;
  997. }
  998. void frstor(fpu_addr_modes addr_modes, u_char __user *data_address)
  999. {
  1000. int i, regnr;
  1001. u_char __user *s = fldenv(addr_modes, data_address);
  1002. int offset = (top & 7) * 10, other = 80 - offset;
  1003. /* Copy all registers in stack order. */
  1004. RE_ENTRANT_CHECK_OFF;
  1005. FPU_access_ok(VERIFY_READ, s, 80);
  1006. __copy_from_user(register_base + offset, s, other);
  1007. if (offset)
  1008. __copy_from_user(register_base, s + other, offset);
  1009. RE_ENTRANT_CHECK_ON;
  1010. for (i = 0; i < 8; i++) {
  1011. regnr = (i + top) & 7;
  1012. if (FPU_gettag(regnr) != TAG_Empty)
  1013. /* The loaded data over-rides all other cases. */
  1014. FPU_settag(regnr, FPU_tagof(&st(i)));
  1015. }
  1016. }
  1017. u_char __user *fstenv(fpu_addr_modes addr_modes, u_char __user *d)
  1018. {
  1019. if ((addr_modes.default_mode == VM86) ||
  1020. ((addr_modes.default_mode == PM16)
  1021. ^ (addr_modes.override.operand_size == OP_SIZE_PREFIX))) {
  1022. RE_ENTRANT_CHECK_OFF;
  1023. FPU_access_ok(VERIFY_WRITE, d, 14);
  1024. #ifdef PECULIAR_486
  1025. FPU_put_user(control_word & ~0xe080, (unsigned long __user *)d);
  1026. #else
  1027. FPU_put_user(control_word, (unsigned short __user *)d);
  1028. #endif /* PECULIAR_486 */
  1029. FPU_put_user(status_word(), (unsigned short __user *)(d + 2));
  1030. FPU_put_user(fpu_tag_word, (unsigned short __user *)(d + 4));
  1031. FPU_put_user(instruction_address.offset,
  1032. (unsigned short __user *)(d + 6));
  1033. FPU_put_user(operand_address.offset,
  1034. (unsigned short __user *)(d + 0x0a));
  1035. if (addr_modes.default_mode == VM86) {
  1036. FPU_put_user((instruction_address.
  1037. offset & 0xf0000) >> 4,
  1038. (unsigned short __user *)(d + 8));
  1039. FPU_put_user((operand_address.offset & 0xf0000) >> 4,
  1040. (unsigned short __user *)(d + 0x0c));
  1041. } else {
  1042. FPU_put_user(instruction_address.selector,
  1043. (unsigned short __user *)(d + 8));
  1044. FPU_put_user(operand_address.selector,
  1045. (unsigned short __user *)(d + 0x0c));
  1046. }
  1047. RE_ENTRANT_CHECK_ON;
  1048. d += 0x0e;
  1049. } else {
  1050. RE_ENTRANT_CHECK_OFF;
  1051. FPU_access_ok(VERIFY_WRITE, d, 7 * 4);
  1052. #ifdef PECULIAR_486
  1053. control_word &= ~0xe080;
  1054. /* An 80486 sets nearly all of the reserved bits to 1. */
  1055. control_word |= 0xffff0040;
  1056. partial_status = status_word() | 0xffff0000;
  1057. fpu_tag_word |= 0xffff0000;
  1058. I387.soft.fcs &= ~0xf8000000;
  1059. I387.soft.fos |= 0xffff0000;
  1060. #endif /* PECULIAR_486 */
  1061. if (__copy_to_user(d, &control_word, 7 * 4))
  1062. FPU_abort;
  1063. RE_ENTRANT_CHECK_ON;
  1064. d += 0x1c;
  1065. }
  1066. control_word |= CW_Exceptions;
  1067. partial_status &= ~(SW_Summary | SW_Backward);
  1068. return d;
  1069. }
  1070. void fsave(fpu_addr_modes addr_modes, u_char __user *data_address)
  1071. {
  1072. u_char __user *d;
  1073. int offset = (top & 7) * 10, other = 80 - offset;
  1074. d = fstenv(addr_modes, data_address);
  1075. RE_ENTRANT_CHECK_OFF;
  1076. FPU_access_ok(VERIFY_WRITE, d, 80);
  1077. /* Copy all registers in stack order. */
  1078. if (__copy_to_user(d, register_base + offset, other))
  1079. FPU_abort;
  1080. if (offset)
  1081. if (__copy_to_user(d + other, register_base, offset))
  1082. FPU_abort;
  1083. RE_ENTRANT_CHECK_ON;
  1084. finit();
  1085. }
  1086. /*===========================================================================*/