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