crl_winapi_fp_exceptions.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #include <crl/crl_time.h>
  8. #ifdef CRL_USE_WINAPI_TIME
  9. #ifdef CRL_THROW_FP_EXCEPTIONS
  10. #include <float.h>
  11. #pragma fenv_access (on)
  12. namespace crl {
  13. void toggle_fp_exceptions(bool throwing) {
  14. // Allow throwing (and reporting) floating point exceptions.
  15. //
  16. // Otherwise x86 build behaves unpredictably on old hardware,
  17. // after an fp-error it may fail some benign operations, like
  18. // std::round(1.) giving 'nan' or double -> int64 giving int64_min.
  19. //
  20. // This results in unexpected assertion violations.
  21. auto state = (unsigned int)0;
  22. // Right now catch only division by zero and invalid operations.
  23. const auto bits = (throwing ? 0 : (_EM_ZERODIVIDE | _EM_INVALID))
  24. | _EM_DENORMAL | _EM_INEXACT | _EM_UNDERFLOW | _EM_OVERFLOW;
  25. _controlfp_s(&state, bits, _MCW_EM);
  26. }
  27. } // namespace crl
  28. #else // CRL_THROW_FP_EXCEPTIONS
  29. namespace crl {
  30. void toggle_fp_exceptions(bool throwing) {
  31. }
  32. } // namespace crl
  33. #endif // CRL_THROW_FP_EXCEPTIONS
  34. #endif // CRL_USE_WINAPI_TIME