fenv.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (c) 2021, Mițca Dumitru <dumitru0mitca@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Types.h>
  27. #include <fenv.h>
  28. // This is the size of the floating point envinronment image in protected mode
  29. static_assert(sizeof(__x87_floating_point_environment) == 28);
  30. static u16 read_status_register()
  31. {
  32. u16 status_register;
  33. asm volatile("fstsw %0"
  34. : "=m"(status_register));
  35. return status_register;
  36. }
  37. static u16 read_control_word()
  38. {
  39. u16 control_word;
  40. asm volatile("fstcw %0"
  41. : "=m"(control_word));
  42. return control_word;
  43. }
  44. static void set_control_word(u16 new_control_word)
  45. {
  46. asm volatile("fldcw %0" ::"m"(new_control_word));
  47. }
  48. static u32 read_mxcsr()
  49. {
  50. u32 mxcsr;
  51. asm volatile("stmxcsr %0"
  52. : "=m"(mxcsr));
  53. return mxcsr;
  54. }
  55. static void set_mxcsr(u32 new_mxcsr)
  56. {
  57. asm volatile("ldmxcsr %0" ::"m"(new_mxcsr));
  58. }
  59. static constexpr u32 default_mxcsr_value = 0x1f80;
  60. extern "C" {
  61. int fegetenv(fenv_t* env)
  62. {
  63. if (!env)
  64. return 1;
  65. asm volatile("fstenv %0"
  66. : "=m"(env->__x87_fpu_env)::"memory");
  67. env->__mxcsr = read_mxcsr();
  68. return 0;
  69. }
  70. int fesetenv(const fenv_t* env)
  71. {
  72. if (!env)
  73. return 1;
  74. if (env == FE_DFL_ENV) {
  75. asm volatile("finit");
  76. set_mxcsr(default_mxcsr_value);
  77. return 0;
  78. }
  79. asm volatile("fldenv %0" ::"m"(env)
  80. : "memory");
  81. set_mxcsr(env->__mxcsr);
  82. return 0;
  83. }
  84. int feholdexcept(fenv_t* env)
  85. {
  86. fegetenv(env);
  87. fenv_t current_env;
  88. fegetenv(&current_env);
  89. current_env.__x87_fpu_env.__status_word &= ~FE_ALL_EXCEPT;
  90. current_env.__x87_fpu_env.__status_word &= ~(1 << 7); // Clear the "Exception Status Summary" bit
  91. current_env.__x87_fpu_env.__control_word &= FE_ALL_EXCEPT; // Masking these bits stops the corresponding exceptions from being generated according to the Intel Programmer's Manual
  92. fesetenv(&current_env);
  93. return 0;
  94. }
  95. int feupdateenv(const fenv_t* env)
  96. {
  97. auto currently_raised_exceptions = fetestexcept(FE_ALL_EXCEPT);
  98. fesetenv(env);
  99. feraiseexcept(currently_raised_exceptions);
  100. return 0;
  101. }
  102. int fegetexceptflag(fexcept_t* except, int exceptions)
  103. {
  104. if (!except)
  105. return 1;
  106. *except = (uint16_t)fetestexcept(exceptions);
  107. return 0;
  108. }
  109. int fesetexceptflag(const fexcept_t* except, int exceptions)
  110. {
  111. if (!except)
  112. return 1;
  113. fenv_t current_env;
  114. fegetenv(&current_env);
  115. exceptions &= FE_ALL_EXCEPT;
  116. current_env.__x87_fpu_env.__status_word &= exceptions;
  117. current_env.__x87_fpu_env.__status_word &= ~(1 << 7); // Make sure exceptions don't get raised
  118. fesetenv(&current_env);
  119. return 0;
  120. }
  121. int fegetround()
  122. {
  123. // There's no way to signal whether the SSE rounding mode and x87 ones are different, so we assume they're the same
  124. return (read_status_register() >> 10) & 3;
  125. }
  126. int fesetround(int rounding_mode)
  127. {
  128. if (rounding_mode < FE_TONEAREST || rounding_mode > FE_TOWARDSZERO)
  129. return 1;
  130. auto control_word = read_control_word();
  131. control_word &= ~(3 << 10);
  132. control_word |= rounding_mode << 10;
  133. set_control_word(control_word);
  134. auto mxcsr = read_mxcsr();
  135. mxcsr &= ~(3 << 13);
  136. mxcsr |= rounding_mode << 13;
  137. set_mxcsr(mxcsr);
  138. return 0;
  139. }
  140. int feclearexcepts(int exceptions)
  141. {
  142. exceptions &= FE_ALL_EXCEPT;
  143. fenv_t current_env;
  144. fegetenv(&current_env);
  145. current_env.__x87_fpu_env.__status_word &= ~exceptions;
  146. current_env.__x87_fpu_env.__status_word &= ~(1 << 7); // Clear the "Exception Status Summary" bit
  147. fesetenv(&current_env);
  148. return 0;
  149. }
  150. int fetestexcept(int exceptions)
  151. {
  152. u16 status_register = read_status_register() & FE_ALL_EXCEPT;
  153. exceptions &= FE_ALL_EXCEPT;
  154. return status_register & exceptions;
  155. }
  156. int feraiseexcept(int exceptions)
  157. {
  158. fenv_t env;
  159. fegetenv(&env);
  160. exceptions &= FE_ALL_EXCEPT;
  161. // While the order in which the exceptions is raised is unspecified, FE_OVERFLOW and FE_UNDERFLOW must be raised before FE_INEXACT, so handle that case in this branch
  162. if (exceptions & FE_INEXACT) {
  163. env.__x87_fpu_env.__status_word &= ((u16)exceptions & ~FE_INEXACT);
  164. fesetenv(&env);
  165. asm volatile("fwait"); // "raise" the exception by performing a floating point operation
  166. fegetenv(&env);
  167. env.__x87_fpu_env.__status_word &= FE_INEXACT;
  168. fesetenv(&env);
  169. asm volatile("fwait");
  170. return 0;
  171. }
  172. env.__x87_fpu_env.__status_word &= exceptions;
  173. fesetenv(&env);
  174. asm volatile("fwait");
  175. return 0;
  176. }
  177. }