IexMathFloatExc.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright (c) Contributors to the OpenEXR Project.
  4. //
  5. #ifndef INCLUDED_IEXMATHFLOATEXC_H
  6. #define INCLUDED_IEXMATHFLOATEXC_H
  7. #include "IexExport.h"
  8. #include "IexNamespace.h"
  9. #include "IexMathIeeeExc.h"
  10. IEX_INTERNAL_NAMESPACE_HEADER_ENTER
  11. //-------------------------------------------------------------
  12. // Function mathExcOn() defines which floating point exceptions
  13. // will be trapped and converted to C++ exceptions.
  14. //-------------------------------------------------------------
  15. IEX_EXPORT
  16. void mathExcOn (int when = (IEEE_OVERFLOW | IEEE_DIVZERO | IEEE_INVALID));
  17. //----------------------------------------------------------------------
  18. // Function getMathExcOn() tells you for which floating point exceptions
  19. // trapping and conversion to C++ exceptions is currently enabled.
  20. //----------------------------------------------------------------------
  21. IEX_EXPORT
  22. int getMathExcOn();
  23. //------------------------------------------------------------------------
  24. // A class that temporarily sets floating point exception trapping
  25. // and conversion, and later restores the previous settings.
  26. //
  27. // Example:
  28. //
  29. // float
  30. // trickyComputation (float x)
  31. // {
  32. // MathExcOn meo (0); // temporarily disable floating
  33. // // point exception trapping
  34. //
  35. // float result = ...; // computation which may cause
  36. // // floating point exceptions
  37. //
  38. // return result; // destruction of meo restores
  39. // } // the program's previous floating
  40. // // point exception settings
  41. //------------------------------------------------------------------------
  42. class IEX_EXPORT_TYPE MathExcOn
  43. {
  44. public:
  45. IEX_EXPORT MathExcOn (int when);
  46. IEX_EXPORT ~MathExcOn ();
  47. MathExcOn (const MathExcOn&) = delete;
  48. MathExcOn& operator= (const MathExcOn&) = delete;
  49. MathExcOn (MathExcOn&&) = delete;
  50. MathExcOn& operator= (MathExcOn&&) = delete;
  51. // It is possible for functions to set the exception registers
  52. // yet not trigger a SIGFPE. Specifically, the implementation
  53. // of pow(x, y) we're using can generates a NaN from a negative x
  54. // and fractional y but a SIGFPE is not generated.
  55. // This function examimes the exception registers and calls the
  56. // fpHandler if those registers modulo the exception mask are set.
  57. // It should be called wherever this class is commonly used where it has
  58. // been found that certain floating point exceptions are not being thrown.
  59. IEX_EXPORT void handleOutstandingExceptions();
  60. private:
  61. bool _changed;
  62. int _saved;
  63. };
  64. IEX_INTERNAL_NAMESPACE_HEADER_EXIT
  65. #endif