test_FCOMI.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #undef _GNU_SOURCE
  2. #define _GNU_SOURCE 1
  3. #undef __USE_GNU
  4. #define __USE_GNU 1
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <signal.h>
  10. #include <sys/types.h>
  11. #include <sys/select.h>
  12. #include <sys/time.h>
  13. #include <sys/wait.h>
  14. #include <fenv.h>
  15. enum {
  16. CF = 1 << 0,
  17. PF = 1 << 2,
  18. ZF = 1 << 6,
  19. ARITH = CF | PF | ZF,
  20. };
  21. long res_fcomi_pi_1;
  22. long res_fcomi_1_pi;
  23. long res_fcomi_1_1;
  24. long res_fcomi_nan_1;
  25. /* sNaN is s|111 1111 1|1xx xxxx xxxx xxxx xxxx xxxx */
  26. /* qNaN is s|111 1111 1|0xx xxxx xxxx xxxx xxxx xxxx (some x must be nonzero) */
  27. int snan = 0x7fc11111;
  28. int qnan = 0x7f811111;
  29. unsigned short snan1[5];
  30. /* sNaN80 is s|111 1111 1111 1111 |10xx xx...xx (some x must be nonzero) */
  31. unsigned short snan80[5] = { 0x1111, 0x1111, 0x1111, 0x8111, 0x7fff };
  32. int test(long flags)
  33. {
  34. feclearexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
  35. asm ("\n"
  36. " push %0""\n"
  37. " popf""\n"
  38. " fld1""\n"
  39. " fldpi""\n"
  40. " fcomi %%st(1), %%st" "\n"
  41. " ffree %%st(0)" "\n"
  42. " ffree %%st(1)" "\n"
  43. " pushf""\n"
  44. " pop res_fcomi_1_pi""\n"
  45. " push %0""\n"
  46. " popf""\n"
  47. " fldpi""\n"
  48. " fld1""\n"
  49. " fcomi %%st(1), %%st" "\n"
  50. " ffree %%st(0)" "\n"
  51. " ffree %%st(1)" "\n"
  52. " pushf""\n"
  53. " pop res_fcomi_pi_1""\n"
  54. " push %0""\n"
  55. " popf""\n"
  56. " fld1""\n"
  57. " fld1""\n"
  58. " fcomi %%st(1), %%st" "\n"
  59. " ffree %%st(0)" "\n"
  60. " ffree %%st(1)" "\n"
  61. " pushf""\n"
  62. " pop res_fcomi_1_1""\n"
  63. :
  64. : "r" (flags)
  65. );
  66. if ((res_fcomi_1_pi & ARITH) != (0)) {
  67. printf("[BAD]\tfcomi_1_pi with flags:%lx\n", flags);
  68. return 1;
  69. }
  70. if ((res_fcomi_pi_1 & ARITH) != (CF)) {
  71. printf("[BAD]\tfcomi_pi_1 with flags:%lx->%lx\n", flags, res_fcomi_pi_1 & ARITH);
  72. return 1;
  73. }
  74. if ((res_fcomi_1_1 & ARITH) != (ZF)) {
  75. printf("[BAD]\tfcomi_1_1 with flags:%lx\n", flags);
  76. return 1;
  77. }
  78. if (fetestexcept(FE_INVALID) != 0) {
  79. printf("[BAD]\tFE_INVALID is set in %s\n", __func__);
  80. return 1;
  81. }
  82. return 0;
  83. }
  84. int test_qnan(long flags)
  85. {
  86. feclearexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
  87. asm ("\n"
  88. " push %0""\n"
  89. " popf""\n"
  90. " flds qnan""\n"
  91. " fld1""\n"
  92. " fnclex""\n" // fld of a qnan raised FE_INVALID, clear it
  93. " fcomi %%st(1), %%st" "\n"
  94. " ffree %%st(0)" "\n"
  95. " ffree %%st(1)" "\n"
  96. " pushf""\n"
  97. " pop res_fcomi_nan_1""\n"
  98. :
  99. : "r" (flags)
  100. );
  101. if ((res_fcomi_nan_1 & ARITH) != (ZF|CF|PF)) {
  102. printf("[BAD]\tfcomi_qnan_1 with flags:%lx\n", flags);
  103. return 1;
  104. }
  105. if (fetestexcept(FE_INVALID) != FE_INVALID) {
  106. printf("[BAD]\tFE_INVALID is not set in %s\n", __func__);
  107. return 1;
  108. }
  109. return 0;
  110. }
  111. int testu_qnan(long flags)
  112. {
  113. feclearexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
  114. asm ("\n"
  115. " push %0""\n"
  116. " popf""\n"
  117. " flds qnan""\n"
  118. " fld1""\n"
  119. " fnclex""\n" // fld of a qnan raised FE_INVALID, clear it
  120. " fucomi %%st(1), %%st" "\n"
  121. " ffree %%st(0)" "\n"
  122. " ffree %%st(1)" "\n"
  123. " pushf""\n"
  124. " pop res_fcomi_nan_1""\n"
  125. :
  126. : "r" (flags)
  127. );
  128. if ((res_fcomi_nan_1 & ARITH) != (ZF|CF|PF)) {
  129. printf("[BAD]\tfcomi_qnan_1 with flags:%lx\n", flags);
  130. return 1;
  131. }
  132. if (fetestexcept(FE_INVALID) != 0) {
  133. printf("[BAD]\tFE_INVALID is set in %s\n", __func__);
  134. return 1;
  135. }
  136. return 0;
  137. }
  138. int testu_snan(long flags)
  139. {
  140. feclearexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
  141. asm ("\n"
  142. " push %0""\n"
  143. " popf""\n"
  144. // " flds snan""\n" // WRONG, this will convert 32-bit fp snan to a *qnan* in 80-bit fp register!
  145. // " fstpt snan1""\n" // if uncommented, it prints "snan1:7fff c111 1100 0000 0000" - c111, not 8111!
  146. // " fnclex""\n" // flds of a snan raised FE_INVALID, clear it
  147. " fldt snan80""\n" // fldt never raise FE_INVALID
  148. " fld1""\n"
  149. " fucomi %%st(1), %%st" "\n"
  150. " ffree %%st(0)" "\n"
  151. " ffree %%st(1)" "\n"
  152. " pushf""\n"
  153. " pop res_fcomi_nan_1""\n"
  154. :
  155. : "r" (flags)
  156. );
  157. if ((res_fcomi_nan_1 & ARITH) != (ZF|CF|PF)) {
  158. printf("[BAD]\tfcomi_qnan_1 with flags:%lx\n", flags);
  159. return 1;
  160. }
  161. // printf("snan:%x snan1:%04x %04x %04x %04x %04x\n", snan, snan1[4], snan1[3], snan1[2], snan1[1], snan1[0]);
  162. if (fetestexcept(FE_INVALID) != FE_INVALID) {
  163. printf("[BAD]\tFE_INVALID is not set in %s\n", __func__);
  164. return 1;
  165. }
  166. return 0;
  167. }
  168. int testp(long flags)
  169. {
  170. feclearexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
  171. asm ("\n"
  172. " push %0""\n"
  173. " popf""\n"
  174. " fld1""\n"
  175. " fldpi""\n"
  176. " fcomip %%st(1), %%st" "\n"
  177. " ffree %%st(0)" "\n"
  178. " pushf""\n"
  179. " pop res_fcomi_1_pi""\n"
  180. " push %0""\n"
  181. " popf""\n"
  182. " fldpi""\n"
  183. " fld1""\n"
  184. " fcomip %%st(1), %%st" "\n"
  185. " ffree %%st(0)" "\n"
  186. " pushf""\n"
  187. " pop res_fcomi_pi_1""\n"
  188. " push %0""\n"
  189. " popf""\n"
  190. " fld1""\n"
  191. " fld1""\n"
  192. " fcomip %%st(1), %%st" "\n"
  193. " ffree %%st(0)" "\n"
  194. " pushf""\n"
  195. " pop res_fcomi_1_1""\n"
  196. :
  197. : "r" (flags)
  198. );
  199. if ((res_fcomi_1_pi & ARITH) != (0)) {
  200. printf("[BAD]\tfcomi_1_pi with flags:%lx\n", flags);
  201. return 1;
  202. }
  203. if ((res_fcomi_pi_1 & ARITH) != (CF)) {
  204. printf("[BAD]\tfcomi_pi_1 with flags:%lx->%lx\n", flags, res_fcomi_pi_1 & ARITH);
  205. return 1;
  206. }
  207. if ((res_fcomi_1_1 & ARITH) != (ZF)) {
  208. printf("[BAD]\tfcomi_1_1 with flags:%lx\n", flags);
  209. return 1;
  210. }
  211. if (fetestexcept(FE_INVALID) != 0) {
  212. printf("[BAD]\tFE_INVALID is set in %s\n", __func__);
  213. return 1;
  214. }
  215. return 0;
  216. }
  217. int testp_qnan(long flags)
  218. {
  219. feclearexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
  220. asm ("\n"
  221. " push %0""\n"
  222. " popf""\n"
  223. " flds qnan""\n"
  224. " fld1""\n"
  225. " fnclex""\n" // fld of a qnan raised FE_INVALID, clear it
  226. " fcomip %%st(1), %%st" "\n"
  227. " ffree %%st(0)" "\n"
  228. " pushf""\n"
  229. " pop res_fcomi_nan_1""\n"
  230. :
  231. : "r" (flags)
  232. );
  233. if ((res_fcomi_nan_1 & ARITH) != (ZF|CF|PF)) {
  234. printf("[BAD]\tfcomi_qnan_1 with flags:%lx\n", flags);
  235. return 1;
  236. }
  237. if (fetestexcept(FE_INVALID) != FE_INVALID) {
  238. printf("[BAD]\tFE_INVALID is not set in %s\n", __func__);
  239. return 1;
  240. }
  241. return 0;
  242. }
  243. int testup_qnan(long flags)
  244. {
  245. feclearexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
  246. asm ("\n"
  247. " push %0""\n"
  248. " popf""\n"
  249. " flds qnan""\n"
  250. " fld1""\n"
  251. " fnclex""\n" // fld of a qnan raised FE_INVALID, clear it
  252. " fucomip %%st(1), %%st" "\n"
  253. " ffree %%st(0)" "\n"
  254. " pushf""\n"
  255. " pop res_fcomi_nan_1""\n"
  256. :
  257. : "r" (flags)
  258. );
  259. if ((res_fcomi_nan_1 & ARITH) != (ZF|CF|PF)) {
  260. printf("[BAD]\tfcomi_qnan_1 with flags:%lx\n", flags);
  261. return 1;
  262. }
  263. if (fetestexcept(FE_INVALID) != 0) {
  264. printf("[BAD]\tFE_INVALID is set in %s\n", __func__);
  265. return 1;
  266. }
  267. return 0;
  268. }
  269. void sighandler(int sig)
  270. {
  271. printf("[FAIL]\tGot signal %d, exiting\n", sig);
  272. exit(1);
  273. }
  274. int main(int argc, char **argv, char **envp)
  275. {
  276. int err = 0;
  277. /* SIGILL triggers on 32-bit kernels w/o fcomi emulation
  278. * when run with "no387 nofxsr". Other signals are caught
  279. * just in case.
  280. */
  281. signal(SIGILL, sighandler);
  282. signal(SIGFPE, sighandler);
  283. signal(SIGSEGV, sighandler);
  284. printf("[RUN]\tTesting f[u]comi[p] instructions\n");
  285. err |= test(0);
  286. err |= test_qnan(0);
  287. err |= testu_qnan(0);
  288. err |= testu_snan(0);
  289. err |= test(CF|ZF|PF);
  290. err |= test_qnan(CF|ZF|PF);
  291. err |= testu_qnan(CF|ZF|PF);
  292. err |= testu_snan(CF|ZF|PF);
  293. err |= testp(0);
  294. err |= testp_qnan(0);
  295. err |= testup_qnan(0);
  296. err |= testp(CF|ZF|PF);
  297. err |= testp_qnan(CF|ZF|PF);
  298. err |= testup_qnan(CF|ZF|PF);
  299. if (!err)
  300. printf("[OK]\tf[u]comi[p]\n");
  301. else
  302. printf("[FAIL]\tf[u]comi[p] errors: %d\n", err);
  303. return err;
  304. }