Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
inks/MsqFPE.cpp
Go to the documentation of this file.
1 /* *****************************************************************
2  MESQUITE -- The Mesh Quality Improvement Toolkit
3 
4  Copyright 2005 Lawrence Livermore National Laboratory. Under
5  the terms of Contract B545069 with the University of Wisconsin --
6  Madison, Lawrence Livermore National Laboratory retains certain
7  rights in this software.
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  (lgpl.txt) along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 
23  kraftche@cae.wisc.edu
24 
25  ***************************************************************** */
26 
27 #include "MsqFPE.hpp"
28 
29 
30 
31 
32 /* First check for BSD-style fpsetmask, which is the closest
33  to a standard across unix-like OSs */
34 
35 #if defined (HAVE_FPSETMASK)
36 
37 #include <ieeefp.h>
38 
40  { return true; }
41 
43  { return (int)fpgetmask(); }
44 
46  { fpsetmask( (fp_except)state ); }
47 
49  { fpsetmask( fpgetmask()|FP_X_INV|FP_X_OFL|FP_X_DZ ); }
50 
51 
52 
53 
54 
55 /* Next try GNU-C feenableexcept mechanism */
56 
57 #elif defined (HAVE_FEENABLEEXCEPT)
58 
59 #ifndef _GNU_SOURCE
60 # define MSQ_SET_GNU_SOURCE
61 # define _GNU_SOURCE
62 #endif
63 #include <fenv.h>
64 #ifdef MSQ_SET_GNU_SOURCE
65 # undef _GNU_SOURCE
66 # undef MSQ_SET_GNU_SOURCE
67 #endif
68 
69 
71  { return true; }
72 
74  { return fegetexcept(); }
75 
77  { feenableexcept( state ); }
78 
80 {
81  const int flags = FE_DIVBYZERO|FE_INVALID|FE_OVERFLOW;
82  feclearexcept( flags );
83  feenableexcept( flags );
84 }
85 
86 
87 
88 
89 
90 /* Next try Microsoft */
91 
92 #elif defined (_MSC_VER)
93 
94 #include <float.h>
95 
97  { return true; }
98 
100  { return _MCW_EM & ~_controlfp(0,0); }
101 
103  { _controlfp( state, _MCW_EM ); }
104 
106 {
107  const int flags = _EM_ZERODIVIDE|_EM_INVALID|_EM_OVERFLOW;
108  _controlfp( _controlfp(0,0) & ~flags, _MCW_EM );
109 }
110 
111 
112 
113 /* Unsupported platform */
114 #else
115 
117  { return false; }
118 
120  { return 0; }
121 
123  { }
124 
126  { }
127 
128 #endif
129 
130 
131 
132 
133 Mesquite::MsqFPE::MsqFPE( bool enabled ) : isEnabled( enabled )
134 {
135  if (isEnabled)
136  {
138  enable_trap_fpe();
139  }
140 }
141 
143 {
144  if (isEnabled)
145  {
146  set_current_fpe_state( prevState );
147  }
148 }
149 
150 
151 
static void enable_trap_fpe()
bool isEnabled
Saved constructor argument for use in destructor.
static void set_current_fpe_state(int state)
MsqFPE(bool enabled)
Set FPE state.
int prevState
Saved FPE state for use in destructor.
static bool fpe_trap_supported()
Check if FPE state manipulation is supported on this platform.
static int get_current_fpe_state()
~MsqFPE()
Restore FPE state.