test-fenv-sse.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Test floating-point environment includes SSE state (bug 16064).
  2. Copyright (C) 2014-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <cpuid.h>
  16. #include <fenv.h>
  17. #include <float.h>
  18. #include <stdbool.h>
  19. #include <stdio.h>
  20. static bool
  21. have_sse2 (void)
  22. {
  23. unsigned int eax, ebx, ecx, edx;
  24. if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx))
  25. return false;
  26. return (edx & bit_SSE2) != 0;
  27. }
  28. static __attribute__ ((noinline)) int
  29. sse_tests (void)
  30. {
  31. int ret = 0;
  32. fenv_t base_env;
  33. if (fegetenv (&base_env) != 0)
  34. {
  35. puts ("fegetenv (&base_env) failed");
  36. return 1;
  37. }
  38. if (fesetround (FE_UPWARD) != 0)
  39. {
  40. puts ("fesetround (FE_UPWARD) failed");
  41. return 1;
  42. }
  43. if (fesetenv (&base_env) != 0)
  44. {
  45. puts ("fesetenv (&base_env) failed");
  46. return 1;
  47. }
  48. volatile float a = 1.0f, b = FLT_MIN, c;
  49. c = a + b;
  50. if (c != 1.0f)
  51. {
  52. puts ("fesetenv did not restore rounding mode");
  53. ret = 1;
  54. }
  55. if (fesetround (FE_DOWNWARD) != 0)
  56. {
  57. puts ("fesetround (FE_DOWNWARD) failed");
  58. return 1;
  59. }
  60. if (feupdateenv (&base_env) != 0)
  61. {
  62. puts ("feupdateenv (&base_env) failed");
  63. return 1;
  64. }
  65. volatile float d = -FLT_MIN, e;
  66. e = a + d;
  67. if (e != 1.0f)
  68. {
  69. puts ("feupdateenv did not restore rounding mode");
  70. ret = 1;
  71. }
  72. if (fesetround (FE_UPWARD) != 0)
  73. {
  74. puts ("fesetround (FE_UPWARD) failed");
  75. return 1;
  76. }
  77. fenv_t upward_env;
  78. if (feholdexcept (&upward_env) != 0)
  79. {
  80. puts ("feholdexcept (&upward_env) failed");
  81. return 1;
  82. }
  83. if (fesetround (FE_DOWNWARD) != 0)
  84. {
  85. puts ("fesetround (FE_DOWNWARD) failed");
  86. return 1;
  87. }
  88. if (fesetenv (&upward_env) != 0)
  89. {
  90. puts ("fesetenv (&upward_env) failed");
  91. return 1;
  92. }
  93. e = a + d;
  94. if (e != 1.0f)
  95. {
  96. puts ("fesetenv did not restore rounding mode from feholdexcept");
  97. ret = 1;
  98. }
  99. if (fesetround (FE_UPWARD) != 0)
  100. {
  101. puts ("fesetround (FE_UPWARD) failed");
  102. return 1;
  103. }
  104. if (fesetenv (FE_DFL_ENV) != 0)
  105. {
  106. puts ("fesetenv (FE_DFL_ENV) failed");
  107. return 1;
  108. }
  109. c = a + b;
  110. if (c != 1.0f)
  111. {
  112. puts ("fesetenv (FE_DFL_ENV) did not restore rounding mode");
  113. ret = 1;
  114. }
  115. return ret;
  116. }
  117. static int
  118. do_test (void)
  119. {
  120. if (!have_sse2 ())
  121. {
  122. puts ("CPU does not support SSE2, cannot test");
  123. return 0;
  124. }
  125. return sse_tests ();
  126. }
  127. #define TEST_FUNCTION do_test ()
  128. #include <test-skeleton.c>