fpu_aux.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*---------------------------------------------------------------------------+
  2. | fpu_aux.c |
  3. | |
  4. | Code to implement some of the FPU auxiliary instructions. |
  5. | |
  6. | Copyright (C) 1992,1993,1994,1997 |
  7. | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
  8. | E-mail billm@suburbia.net |
  9. | |
  10. | |
  11. +---------------------------------------------------------------------------*/
  12. #include "fpu_system.h"
  13. #include "exception.h"
  14. #include "fpu_emu.h"
  15. #include "status_w.h"
  16. #include "control_w.h"
  17. static void fnop(void)
  18. {
  19. }
  20. static void fclex(void)
  21. {
  22. partial_status &=
  23. ~(SW_Backward | SW_Summary | SW_Stack_Fault | SW_Precision |
  24. SW_Underflow | SW_Overflow | SW_Zero_Div | SW_Denorm_Op |
  25. SW_Invalid);
  26. no_ip_update = 1;
  27. }
  28. /* Needs to be externally visible */
  29. void fpstate_init_soft(struct swregs_state *soft)
  30. {
  31. struct address *oaddr, *iaddr;
  32. memset(soft, 0, sizeof(*soft));
  33. soft->cwd = 0x037f;
  34. soft->swd = 0;
  35. soft->ftop = 0; /* We don't keep top in the status word internally. */
  36. soft->twd = 0xffff;
  37. /* The behaviour is different from that detailed in
  38. Section 15.1.6 of the Intel manual */
  39. oaddr = (struct address *)&soft->foo;
  40. oaddr->offset = 0;
  41. oaddr->selector = 0;
  42. iaddr = (struct address *)&soft->fip;
  43. iaddr->offset = 0;
  44. iaddr->selector = 0;
  45. iaddr->opcode = 0;
  46. soft->no_update = 1;
  47. }
  48. void finit(void)
  49. {
  50. fpstate_init_soft(&current->thread.fpu.state.soft);
  51. }
  52. /*
  53. * These are nops on the i387..
  54. */
  55. #define feni fnop
  56. #define fdisi fnop
  57. #define fsetpm fnop
  58. static FUNC const finit_table[] = {
  59. feni, fdisi, fclex, finit,
  60. fsetpm, FPU_illegal, FPU_illegal, FPU_illegal
  61. };
  62. void finit_(void)
  63. {
  64. (finit_table[FPU_rm]) ();
  65. }
  66. static void fstsw_ax(void)
  67. {
  68. *(short *)&FPU_EAX = status_word();
  69. no_ip_update = 1;
  70. }
  71. static FUNC const fstsw_table[] = {
  72. fstsw_ax, FPU_illegal, FPU_illegal, FPU_illegal,
  73. FPU_illegal, FPU_illegal, FPU_illegal, FPU_illegal
  74. };
  75. void fstsw_(void)
  76. {
  77. (fstsw_table[FPU_rm]) ();
  78. }
  79. static FUNC const fp_nop_table[] = {
  80. fnop, FPU_illegal, FPU_illegal, FPU_illegal,
  81. FPU_illegal, FPU_illegal, FPU_illegal, FPU_illegal
  82. };
  83. void fp_nop(void)
  84. {
  85. (fp_nop_table[FPU_rm]) ();
  86. }
  87. void fld_i_(void)
  88. {
  89. FPU_REG *st_new_ptr;
  90. int i;
  91. u_char tag;
  92. if (STACK_OVERFLOW) {
  93. FPU_stack_overflow();
  94. return;
  95. }
  96. /* fld st(i) */
  97. i = FPU_rm;
  98. if (NOT_EMPTY(i)) {
  99. reg_copy(&st(i), st_new_ptr);
  100. tag = FPU_gettagi(i);
  101. push();
  102. FPU_settag0(tag);
  103. } else {
  104. if (control_word & CW_Invalid) {
  105. /* The masked response */
  106. FPU_stack_underflow();
  107. } else
  108. EXCEPTION(EX_StackUnder);
  109. }
  110. }
  111. void fxch_i(void)
  112. {
  113. /* fxch st(i) */
  114. FPU_REG t;
  115. int i = FPU_rm;
  116. FPU_REG *st0_ptr = &st(0), *sti_ptr = &st(i);
  117. long tag_word = fpu_tag_word;
  118. int regnr = top & 7, regnri = ((regnr + i) & 7);
  119. u_char st0_tag = (tag_word >> (regnr * 2)) & 3;
  120. u_char sti_tag = (tag_word >> (regnri * 2)) & 3;
  121. if (st0_tag == TAG_Empty) {
  122. if (sti_tag == TAG_Empty) {
  123. FPU_stack_underflow();
  124. FPU_stack_underflow_i(i);
  125. return;
  126. }
  127. if (control_word & CW_Invalid) {
  128. /* Masked response */
  129. FPU_copy_to_reg0(sti_ptr, sti_tag);
  130. }
  131. FPU_stack_underflow_i(i);
  132. return;
  133. }
  134. if (sti_tag == TAG_Empty) {
  135. if (control_word & CW_Invalid) {
  136. /* Masked response */
  137. FPU_copy_to_regi(st0_ptr, st0_tag, i);
  138. }
  139. FPU_stack_underflow();
  140. return;
  141. }
  142. clear_C1();
  143. reg_copy(st0_ptr, &t);
  144. reg_copy(sti_ptr, st0_ptr);
  145. reg_copy(&t, sti_ptr);
  146. tag_word &= ~(3 << (regnr * 2)) & ~(3 << (regnri * 2));
  147. tag_word |= (sti_tag << (regnr * 2)) | (st0_tag << (regnri * 2));
  148. fpu_tag_word = tag_word;
  149. }
  150. static void fcmovCC(void)
  151. {
  152. /* fcmovCC st(i) */
  153. int i = FPU_rm;
  154. FPU_REG *st0_ptr = &st(0);
  155. FPU_REG *sti_ptr = &st(i);
  156. long tag_word = fpu_tag_word;
  157. int regnr = top & 7;
  158. int regnri = (top + i) & 7;
  159. u_char sti_tag = (tag_word >> (regnri * 2)) & 3;
  160. if (sti_tag == TAG_Empty) {
  161. FPU_stack_underflow();
  162. clear_C1();
  163. return;
  164. }
  165. reg_copy(sti_ptr, st0_ptr);
  166. tag_word &= ~(3 << (regnr * 2));
  167. tag_word |= (sti_tag << (regnr * 2));
  168. fpu_tag_word = tag_word;
  169. }
  170. void fcmovb(void)
  171. {
  172. if (FPU_EFLAGS & X86_EFLAGS_CF)
  173. fcmovCC();
  174. }
  175. void fcmove(void)
  176. {
  177. if (FPU_EFLAGS & X86_EFLAGS_ZF)
  178. fcmovCC();
  179. }
  180. void fcmovbe(void)
  181. {
  182. if (FPU_EFLAGS & (X86_EFLAGS_CF|X86_EFLAGS_ZF))
  183. fcmovCC();
  184. }
  185. void fcmovu(void)
  186. {
  187. if (FPU_EFLAGS & X86_EFLAGS_PF)
  188. fcmovCC();
  189. }
  190. void fcmovnb(void)
  191. {
  192. if (!(FPU_EFLAGS & X86_EFLAGS_CF))
  193. fcmovCC();
  194. }
  195. void fcmovne(void)
  196. {
  197. if (!(FPU_EFLAGS & X86_EFLAGS_ZF))
  198. fcmovCC();
  199. }
  200. void fcmovnbe(void)
  201. {
  202. if (!(FPU_EFLAGS & (X86_EFLAGS_CF|X86_EFLAGS_ZF)))
  203. fcmovCC();
  204. }
  205. void fcmovnu(void)
  206. {
  207. if (!(FPU_EFLAGS & X86_EFLAGS_PF))
  208. fcmovCC();
  209. }
  210. void ffree_(void)
  211. {
  212. /* ffree st(i) */
  213. FPU_settagi(FPU_rm, TAG_Empty);
  214. }
  215. void ffreep(void)
  216. {
  217. /* ffree st(i) + pop - unofficial code */
  218. FPU_settagi(FPU_rm, TAG_Empty);
  219. FPU_pop();
  220. }
  221. void fst_i_(void)
  222. {
  223. /* fst st(i) */
  224. FPU_copy_to_regi(&st(0), FPU_gettag0(), FPU_rm);
  225. }
  226. void fstp_i(void)
  227. {
  228. /* fstp st(i) */
  229. FPU_copy_to_regi(&st(0), FPU_gettag0(), FPU_rm);
  230. FPU_pop();
  231. }