test_FISTTP.c 3.0 KB

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