test_FISTTP.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. unsigned long long res64 = -1;
  16. unsigned int res32 = -1;
  17. unsigned short res16 = -1;
  18. int test(void)
  19. {
  20. int ex;
  21. feclearexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
  22. asm volatile ("\n"
  23. " fld1""\n"
  24. " fisttp res16""\n"
  25. " fld1""\n"
  26. " fisttpl res32""\n"
  27. " fld1""\n"
  28. " fisttpll res64""\n"
  29. : : : "memory"
  30. );
  31. if (res16 != 1 || res32 != 1 || res64 != 1) {
  32. printf("[BAD]\tfisttp 1\n");
  33. return 1;
  34. }
  35. ex = fetestexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
  36. if (ex != 0) {
  37. printf("[BAD]\tfisttp 1: wrong exception state\n");
  38. return 1;
  39. }
  40. feclearexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
  41. asm volatile ("\n"
  42. " fldpi""\n"
  43. " fisttp res16""\n"
  44. " fldpi""\n"
  45. " fisttpl res32""\n"
  46. " fldpi""\n"
  47. " fisttpll res64""\n"
  48. : : : "memory"
  49. );
  50. if (res16 != 3 || res32 != 3 || res64 != 3) {
  51. printf("[BAD]\tfisttp pi\n");
  52. return 1;
  53. }
  54. ex = fetestexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
  55. if (ex != FE_INEXACT) {
  56. printf("[BAD]\tfisttp pi: wrong exception state\n");
  57. return 1;
  58. }
  59. feclearexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
  60. asm volatile ("\n"
  61. " fldpi""\n"
  62. " fchs""\n"
  63. " fisttp res16""\n"
  64. " fldpi""\n"
  65. " fchs""\n"
  66. " fisttpl res32""\n"
  67. " fldpi""\n"
  68. " fchs""\n"
  69. " fisttpll res64""\n"
  70. : : : "memory"
  71. );
  72. if (res16 != 0xfffd || res32 != 0xfffffffd || res64 != 0xfffffffffffffffdULL) {
  73. printf("[BAD]\tfisttp -pi\n");
  74. return 1;
  75. }
  76. ex = fetestexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
  77. if (ex != FE_INEXACT) {
  78. printf("[BAD]\tfisttp -pi: wrong exception state\n");
  79. return 1;
  80. }
  81. feclearexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
  82. asm volatile ("\n"
  83. " fldln2""\n"
  84. " fisttp res16""\n"
  85. " fldln2""\n"
  86. " fisttpl res32""\n"
  87. " fldln2""\n"
  88. " fisttpll res64""\n"
  89. : : : "memory"
  90. );
  91. /* Test truncation to zero (round-to-nearest would give 1 here) */
  92. if (res16 != 0 || res32 != 0 || res64 != 0) {
  93. printf("[BAD]\tfisttp ln2\n");
  94. return 1;
  95. }
  96. ex = fetestexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
  97. if (ex != FE_INEXACT) {
  98. printf("[BAD]\tfisttp ln2: wrong exception state\n");
  99. return 1;
  100. }
  101. return 0;
  102. }
  103. void sighandler(int sig)
  104. {
  105. printf("[FAIL]\tGot signal %d, exiting\n", sig);
  106. exit(1);
  107. }
  108. int main(int argc, char **argv, char **envp)
  109. {
  110. int err = 0;
  111. /* SIGILL triggers on 32-bit kernels w/o fisttp emulation
  112. * when run with "no387 nofxsr". Other signals are caught
  113. * just in case.
  114. */
  115. signal(SIGILL, sighandler);
  116. signal(SIGFPE, sighandler);
  117. signal(SIGSEGV, sighandler);
  118. printf("[RUN]\tTesting fisttp instructions\n");
  119. err |= test();
  120. if (!err)
  121. printf("[OK]\tfisttp\n");
  122. else
  123. printf("[FAIL]\tfisttp errors: %d\n", err);
  124. return err;
  125. }