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 //
3 // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
4 // Digital Ltd. LLC
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
10 // met:
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 // * Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 ///////////////////////////////////////////////////////////////////////////
34 
35 
36 
37 #ifndef INCLUDED_IEXMATHFLOATEXC_H
38 #define INCLUDED_IEXMATHFLOATEXC_H
39 
40 #include "IexMathExport.h"
41 #include "IexNamespace.h"
42 #include "IexMathExc.h"
43 #include "IexMathIeeeExc.h"
44 
46 
47 
48 //-------------------------------------------------------------
49 // Function mathExcOn() defines which floating point exceptions
50 // will be trapped and converted to C++ exceptions.
51 //-------------------------------------------------------------
52 
54 void mathExcOn (int when = (IEEE_OVERFLOW | IEEE_DIVZERO | IEEE_INVALID));
55 
56 
57 //----------------------------------------------------------------------
58 // Function getMathExcOn() tells you for which floating point exceptions
59 // trapping and conversion to C++ exceptions is currently enabled.
60 //----------------------------------------------------------------------
61 
63 int getMathExcOn();
64 
65 
66 //------------------------------------------------------------------------
67 // A classs that temporarily sets floating point exception trapping
68 // and conversion, and later restores the previous settings.
69 //
70 // Example:
71 //
72 // float
73 // trickyComputation (float x)
74 // {
75 // MathExcOn meo (0); // temporarily disable floating
76 // // point exception trapping
77 //
78 // float result = ...; // computation which may cause
79 // // floating point exceptions
80 //
81 // return result; // destruction of meo restores
82 // } // the program's previous floating
83 // // point exception settings
84 //------------------------------------------------------------------------
85 
87 {
88  public:
89 
90  MathExcOn (int when)
91  :
92  _changed (false)
93  {
94  _saved = getMathExcOn();
95 
96  if (_saved != when)
97  {
98  _changed = true;
99  mathExcOn (when);
100  }
101  }
102 
104  {
105  if (_changed)
106  mathExcOn (_saved);
107  }
108 
109  // It is possible for functions to set the exception registers
110  // yet not trigger a SIGFPE. Specifically, the implementation
111  // of pow(x, y) we're using can generates a NaN from a negative x
112  // and fractional y but a SIGFPE is not generated.
113  // This function examimes the exception registers and calls the
114  // fpHandler if those registers modulo the exception mask are set.
115  // It should be called wherever this class is commonly used where it has
116  // been found that certain floating point exceptions are not being thrown.
117 
118  void handleOutstandingExceptions();
119 
120  private:
121 
122  bool _changed;
123  int _saved;
124 };
125 
126 
128 
129 #endif
IEX_INTERNAL_NAMESPACE_HEADER_ENTER IEXMATH_EXPORT void mathExcOn(int when=(IEEE_OVERFLOW|IEEE_DIVZERO|IEEE_INVALID))
IEXMATH_EXPORT int getMathExcOn()
MathExcOn(int when)
#define IEX_INTERNAL_NAMESPACE_HEADER_ENTER
Definition: IexNamespace.h:106
#define IEXMATH_EXPORT
Definition: IexMathExport.h:60
#define IEX_INTERNAL_NAMESPACE_HEADER_EXIT
Definition: IexNamespace.h:107