🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.4 KiB

  1. #include "config.h"
  2. #include "fpu_ctrl.h"
  3. #ifdef HAVE_INTRIN_H
  4. #include <intrin.h>
  5. #endif
  6. #ifdef HAVE_SSE_INTRINSICS
  7. #include <emmintrin.h>
  8. #ifndef _MM_DENORMALS_ZERO_MASK
  9. /* Some headers seem to be missing these? */
  10. #define _MM_DENORMALS_ZERO_MASK 0x0040u
  11. #define _MM_DENORMALS_ZERO_ON 0x0040u
  12. #endif
  13. #endif
  14. #include "cpu_caps.h"
  15. void FPUCtl::enter() noexcept
  16. {
  17. if(this->in_mode) return;
  18. #if defined(HAVE_SSE_INTRINSICS)
  19. this->sse_state = _mm_getcsr();
  20. unsigned int sseState{this->sse_state};
  21. sseState &= ~(_MM_FLUSH_ZERO_MASK | _MM_DENORMALS_ZERO_MASK);
  22. sseState |= _MM_FLUSH_ZERO_ON | _MM_DENORMALS_ZERO_ON;
  23. _mm_setcsr(sseState);
  24. #elif defined(__GNUC__) && defined(HAVE_SSE)
  25. if((CPUCapFlags&CPU_CAP_SSE))
  26. {
  27. __asm__ __volatile__("stmxcsr %0" : "=m" (*&this->sse_state));
  28. unsigned int sseState{this->sse_state};
  29. sseState |= 0x8000; /* set flush-to-zero */
  30. if((CPUCapFlags&CPU_CAP_SSE2))
  31. sseState |= 0x0040; /* set denormals-are-zero */
  32. __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
  33. }
  34. #endif
  35. this->in_mode = true;
  36. }
  37. void FPUCtl::leave() noexcept
  38. {
  39. if(!this->in_mode) return;
  40. #if defined(HAVE_SSE_INTRINSICS)
  41. _mm_setcsr(this->sse_state);
  42. #elif defined(__GNUC__) && defined(HAVE_SSE)
  43. if((CPUCapFlags&CPU_CAP_SSE))
  44. __asm__ __volatile__("ldmxcsr %0" : : "m" (*&this->sse_state));
  45. #endif
  46. this->in_mode = false;
  47. }