reg_compare.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*---------------------------------------------------------------------------+
  3. | reg_compare.c |
  4. | |
  5. | Compare two floating point registers |
  6. | |
  7. | Copyright (C) 1992,1993,1994,1997 |
  8. | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
  9. | E-mail billm@suburbia.net |
  10. | |
  11. | |
  12. +---------------------------------------------------------------------------*/
  13. /*---------------------------------------------------------------------------+
  14. | compare() is the core FPU_REG comparison function |
  15. +---------------------------------------------------------------------------*/
  16. #include "fpu_system.h"
  17. #include "exception.h"
  18. #include "fpu_emu.h"
  19. #include "control_w.h"
  20. #include "status_w.h"
  21. static int compare(FPU_REG const *b, int tagb)
  22. {
  23. int diff, exp0, expb;
  24. u_char st0_tag;
  25. FPU_REG *st0_ptr;
  26. FPU_REG x, y;
  27. u_char st0_sign, signb = getsign(b);
  28. st0_ptr = &st(0);
  29. st0_tag = FPU_gettag0();
  30. st0_sign = getsign(st0_ptr);
  31. if (tagb == TAG_Special)
  32. tagb = FPU_Special(b);
  33. if (st0_tag == TAG_Special)
  34. st0_tag = FPU_Special(st0_ptr);
  35. if (((st0_tag != TAG_Valid) && (st0_tag != TW_Denormal))
  36. || ((tagb != TAG_Valid) && (tagb != TW_Denormal))) {
  37. if (st0_tag == TAG_Zero) {
  38. if (tagb == TAG_Zero)
  39. return COMP_A_eq_B;
  40. if (tagb == TAG_Valid)
  41. return ((signb ==
  42. SIGN_POS) ? COMP_A_lt_B : COMP_A_gt_B);
  43. if (tagb == TW_Denormal)
  44. return ((signb ==
  45. SIGN_POS) ? COMP_A_lt_B : COMP_A_gt_B)
  46. | COMP_Denormal;
  47. } else if (tagb == TAG_Zero) {
  48. if (st0_tag == TAG_Valid)
  49. return ((st0_sign ==
  50. SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B);
  51. if (st0_tag == TW_Denormal)
  52. return ((st0_sign ==
  53. SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B)
  54. | COMP_Denormal;
  55. }
  56. if (st0_tag == TW_Infinity) {
  57. if ((tagb == TAG_Valid) || (tagb == TAG_Zero))
  58. return ((st0_sign ==
  59. SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B);
  60. else if (tagb == TW_Denormal)
  61. return ((st0_sign ==
  62. SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B)
  63. | COMP_Denormal;
  64. else if (tagb == TW_Infinity) {
  65. /* The 80486 book says that infinities can be equal! */
  66. return (st0_sign == signb) ? COMP_A_eq_B :
  67. ((st0_sign ==
  68. SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B);
  69. }
  70. /* Fall through to the NaN code */
  71. } else if (tagb == TW_Infinity) {
  72. if ((st0_tag == TAG_Valid) || (st0_tag == TAG_Zero))
  73. return ((signb ==
  74. SIGN_POS) ? COMP_A_lt_B : COMP_A_gt_B);
  75. if (st0_tag == TW_Denormal)
  76. return ((signb ==
  77. SIGN_POS) ? COMP_A_lt_B : COMP_A_gt_B)
  78. | COMP_Denormal;
  79. /* Fall through to the NaN code */
  80. }
  81. /* The only possibility now should be that one of the arguments
  82. is a NaN */
  83. if ((st0_tag == TW_NaN) || (tagb == TW_NaN)) {
  84. int signalling = 0, unsupported = 0;
  85. if (st0_tag == TW_NaN) {
  86. signalling =
  87. (st0_ptr->sigh & 0xc0000000) == 0x80000000;
  88. unsupported = !((exponent(st0_ptr) == EXP_OVER)
  89. && (st0_ptr->
  90. sigh & 0x80000000));
  91. }
  92. if (tagb == TW_NaN) {
  93. signalling |=
  94. (b->sigh & 0xc0000000) == 0x80000000;
  95. unsupported |= !((exponent(b) == EXP_OVER)
  96. && (b->sigh & 0x80000000));
  97. }
  98. if (signalling || unsupported)
  99. return COMP_No_Comp | COMP_SNaN | COMP_NaN;
  100. else
  101. /* Neither is a signaling NaN */
  102. return COMP_No_Comp | COMP_NaN;
  103. }
  104. EXCEPTION(EX_Invalid);
  105. }
  106. if (st0_sign != signb) {
  107. return ((st0_sign == SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B)
  108. | (((st0_tag == TW_Denormal) || (tagb == TW_Denormal)) ?
  109. COMP_Denormal : 0);
  110. }
  111. if ((st0_tag == TW_Denormal) || (tagb == TW_Denormal)) {
  112. FPU_to_exp16(st0_ptr, &x);
  113. FPU_to_exp16(b, &y);
  114. st0_ptr = &x;
  115. b = &y;
  116. exp0 = exponent16(st0_ptr);
  117. expb = exponent16(b);
  118. } else {
  119. exp0 = exponent(st0_ptr);
  120. expb = exponent(b);
  121. }
  122. #ifdef PARANOID
  123. if (!(st0_ptr->sigh & 0x80000000))
  124. EXCEPTION(EX_Invalid);
  125. if (!(b->sigh & 0x80000000))
  126. EXCEPTION(EX_Invalid);
  127. #endif /* PARANOID */
  128. diff = exp0 - expb;
  129. if (diff == 0) {
  130. diff = st0_ptr->sigh - b->sigh; /* Works only if ms bits are
  131. identical */
  132. if (diff == 0) {
  133. diff = st0_ptr->sigl > b->sigl;
  134. if (diff == 0)
  135. diff = -(st0_ptr->sigl < b->sigl);
  136. }
  137. }
  138. if (diff > 0) {
  139. return ((st0_sign == SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B)
  140. | (((st0_tag == TW_Denormal) || (tagb == TW_Denormal)) ?
  141. COMP_Denormal : 0);
  142. }
  143. if (diff < 0) {
  144. return ((st0_sign == SIGN_POS) ? COMP_A_lt_B : COMP_A_gt_B)
  145. | (((st0_tag == TW_Denormal) || (tagb == TW_Denormal)) ?
  146. COMP_Denormal : 0);
  147. }
  148. return COMP_A_eq_B
  149. | (((st0_tag == TW_Denormal) || (tagb == TW_Denormal)) ?
  150. COMP_Denormal : 0);
  151. }
  152. /* This function requires that st(0) is not empty */
  153. int FPU_compare_st_data(FPU_REG const *loaded_data, u_char loaded_tag)
  154. {
  155. int f, c;
  156. c = compare(loaded_data, loaded_tag);
  157. if (c & COMP_NaN) {
  158. EXCEPTION(EX_Invalid);
  159. f = SW_C3 | SW_C2 | SW_C0;
  160. } else
  161. switch (c & 7) {
  162. case COMP_A_lt_B:
  163. f = SW_C0;
  164. break;
  165. case COMP_A_eq_B:
  166. f = SW_C3;
  167. break;
  168. case COMP_A_gt_B:
  169. f = 0;
  170. break;
  171. case COMP_No_Comp:
  172. f = SW_C3 | SW_C2 | SW_C0;
  173. break;
  174. default:
  175. #ifdef PARANOID
  176. EXCEPTION(EX_INTERNAL | 0x121);
  177. #endif /* PARANOID */
  178. f = SW_C3 | SW_C2 | SW_C0;
  179. break;
  180. }
  181. setcc(f);
  182. if (c & COMP_Denormal) {
  183. return denormal_operand() < 0;
  184. }
  185. return 0;
  186. }
  187. static int compare_st_st(int nr)
  188. {
  189. int f, c;
  190. FPU_REG *st_ptr;
  191. if (!NOT_EMPTY(0) || !NOT_EMPTY(nr)) {
  192. setcc(SW_C3 | SW_C2 | SW_C0);
  193. /* Stack fault */
  194. EXCEPTION(EX_StackUnder);
  195. return !(control_word & CW_Invalid);
  196. }
  197. st_ptr = &st(nr);
  198. c = compare(st_ptr, FPU_gettagi(nr));
  199. if (c & COMP_NaN) {
  200. setcc(SW_C3 | SW_C2 | SW_C0);
  201. EXCEPTION(EX_Invalid);
  202. return !(control_word & CW_Invalid);
  203. } else
  204. switch (c & 7) {
  205. case COMP_A_lt_B:
  206. f = SW_C0;
  207. break;
  208. case COMP_A_eq_B:
  209. f = SW_C3;
  210. break;
  211. case COMP_A_gt_B:
  212. f = 0;
  213. break;
  214. case COMP_No_Comp:
  215. f = SW_C3 | SW_C2 | SW_C0;
  216. break;
  217. default:
  218. #ifdef PARANOID
  219. EXCEPTION(EX_INTERNAL | 0x122);
  220. #endif /* PARANOID */
  221. f = SW_C3 | SW_C2 | SW_C0;
  222. break;
  223. }
  224. setcc(f);
  225. if (c & COMP_Denormal) {
  226. return denormal_operand() < 0;
  227. }
  228. return 0;
  229. }
  230. static int compare_i_st_st(int nr)
  231. {
  232. int f, c;
  233. FPU_REG *st_ptr;
  234. if (!NOT_EMPTY(0) || !NOT_EMPTY(nr)) {
  235. FPU_EFLAGS |= (X86_EFLAGS_ZF | X86_EFLAGS_PF | X86_EFLAGS_CF);
  236. /* Stack fault */
  237. EXCEPTION(EX_StackUnder);
  238. return !(control_word & CW_Invalid);
  239. }
  240. partial_status &= ~SW_C0;
  241. st_ptr = &st(nr);
  242. c = compare(st_ptr, FPU_gettagi(nr));
  243. if (c & COMP_NaN) {
  244. FPU_EFLAGS |= (X86_EFLAGS_ZF | X86_EFLAGS_PF | X86_EFLAGS_CF);
  245. EXCEPTION(EX_Invalid);
  246. return !(control_word & CW_Invalid);
  247. }
  248. switch (c & 7) {
  249. case COMP_A_lt_B:
  250. f = X86_EFLAGS_CF;
  251. break;
  252. case COMP_A_eq_B:
  253. f = X86_EFLAGS_ZF;
  254. break;
  255. case COMP_A_gt_B:
  256. f = 0;
  257. break;
  258. case COMP_No_Comp:
  259. f = X86_EFLAGS_ZF | X86_EFLAGS_PF | X86_EFLAGS_CF;
  260. break;
  261. default:
  262. #ifdef PARANOID
  263. EXCEPTION(EX_INTERNAL | 0x122);
  264. #endif /* PARANOID */
  265. f = 0;
  266. break;
  267. }
  268. FPU_EFLAGS = (FPU_EFLAGS & ~(X86_EFLAGS_ZF | X86_EFLAGS_PF | X86_EFLAGS_CF)) | f;
  269. if (c & COMP_Denormal) {
  270. return denormal_operand() < 0;
  271. }
  272. return 0;
  273. }
  274. static int compare_u_st_st(int nr)
  275. {
  276. int f = 0, c;
  277. FPU_REG *st_ptr;
  278. if (!NOT_EMPTY(0) || !NOT_EMPTY(nr)) {
  279. setcc(SW_C3 | SW_C2 | SW_C0);
  280. /* Stack fault */
  281. EXCEPTION(EX_StackUnder);
  282. return !(control_word & CW_Invalid);
  283. }
  284. st_ptr = &st(nr);
  285. c = compare(st_ptr, FPU_gettagi(nr));
  286. if (c & COMP_NaN) {
  287. setcc(SW_C3 | SW_C2 | SW_C0);
  288. if (c & COMP_SNaN) { /* This is the only difference between
  289. un-ordered and ordinary comparisons */
  290. EXCEPTION(EX_Invalid);
  291. return !(control_word & CW_Invalid);
  292. }
  293. return 0;
  294. } else
  295. switch (c & 7) {
  296. case COMP_A_lt_B:
  297. f = SW_C0;
  298. break;
  299. case COMP_A_eq_B:
  300. f = SW_C3;
  301. break;
  302. case COMP_A_gt_B:
  303. f = 0;
  304. break;
  305. case COMP_No_Comp:
  306. f = SW_C3 | SW_C2 | SW_C0;
  307. break;
  308. #ifdef PARANOID
  309. default:
  310. EXCEPTION(EX_INTERNAL | 0x123);
  311. f = SW_C3 | SW_C2 | SW_C0;
  312. break;
  313. #endif /* PARANOID */
  314. }
  315. setcc(f);
  316. if (c & COMP_Denormal) {
  317. return denormal_operand() < 0;
  318. }
  319. return 0;
  320. }
  321. static int compare_ui_st_st(int nr)
  322. {
  323. int f = 0, c;
  324. FPU_REG *st_ptr;
  325. if (!NOT_EMPTY(0) || !NOT_EMPTY(nr)) {
  326. FPU_EFLAGS |= (X86_EFLAGS_ZF | X86_EFLAGS_PF | X86_EFLAGS_CF);
  327. /* Stack fault */
  328. EXCEPTION(EX_StackUnder);
  329. return !(control_word & CW_Invalid);
  330. }
  331. partial_status &= ~SW_C0;
  332. st_ptr = &st(nr);
  333. c = compare(st_ptr, FPU_gettagi(nr));
  334. if (c & COMP_NaN) {
  335. FPU_EFLAGS |= (X86_EFLAGS_ZF | X86_EFLAGS_PF | X86_EFLAGS_CF);
  336. if (c & COMP_SNaN) { /* This is the only difference between
  337. un-ordered and ordinary comparisons */
  338. EXCEPTION(EX_Invalid);
  339. return !(control_word & CW_Invalid);
  340. }
  341. return 0;
  342. }
  343. switch (c & 7) {
  344. case COMP_A_lt_B:
  345. f = X86_EFLAGS_CF;
  346. break;
  347. case COMP_A_eq_B:
  348. f = X86_EFLAGS_ZF;
  349. break;
  350. case COMP_A_gt_B:
  351. f = 0;
  352. break;
  353. case COMP_No_Comp:
  354. f = X86_EFLAGS_ZF | X86_EFLAGS_PF | X86_EFLAGS_CF;
  355. break;
  356. #ifdef PARANOID
  357. default:
  358. EXCEPTION(EX_INTERNAL | 0x123);
  359. f = 0;
  360. break;
  361. #endif /* PARANOID */
  362. }
  363. FPU_EFLAGS = (FPU_EFLAGS & ~(X86_EFLAGS_ZF | X86_EFLAGS_PF | X86_EFLAGS_CF)) | f;
  364. if (c & COMP_Denormal) {
  365. return denormal_operand() < 0;
  366. }
  367. return 0;
  368. }
  369. /*---------------------------------------------------------------------------*/
  370. void fcom_st(void)
  371. {
  372. /* fcom st(i) */
  373. compare_st_st(FPU_rm);
  374. }
  375. void fcompst(void)
  376. {
  377. /* fcomp st(i) */
  378. if (!compare_st_st(FPU_rm))
  379. FPU_pop();
  380. }
  381. void fcompp(void)
  382. {
  383. /* fcompp */
  384. if (FPU_rm != 1) {
  385. FPU_illegal();
  386. return;
  387. }
  388. if (!compare_st_st(1))
  389. poppop();
  390. }
  391. void fucom_(void)
  392. {
  393. /* fucom st(i) */
  394. compare_u_st_st(FPU_rm);
  395. }
  396. void fucomp(void)
  397. {
  398. /* fucomp st(i) */
  399. if (!compare_u_st_st(FPU_rm))
  400. FPU_pop();
  401. }
  402. void fucompp(void)
  403. {
  404. /* fucompp */
  405. if (FPU_rm == 1) {
  406. if (!compare_u_st_st(1))
  407. poppop();
  408. } else
  409. FPU_illegal();
  410. }
  411. /* P6+ compare-to-EFLAGS ops */
  412. void fcomi_(void)
  413. {
  414. /* fcomi st(i) */
  415. compare_i_st_st(FPU_rm);
  416. }
  417. void fcomip(void)
  418. {
  419. /* fcomip st(i) */
  420. if (!compare_i_st_st(FPU_rm))
  421. FPU_pop();
  422. }
  423. void fucomi_(void)
  424. {
  425. /* fucomi st(i) */
  426. compare_ui_st_st(FPU_rm);
  427. }
  428. void fucomip(void)
  429. {
  430. /* fucomip st(i) */
  431. if (!compare_ui_st_st(FPU_rm))
  432. FPU_pop();
  433. }