Branch data Line data Source code
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 : : [email protected]
24 : :
25 : : ***************************************************************** */
26 : :
27 : : #include "MsqFPE.hpp"
28 : :
29 : : /* First check for BSD-style fpsetmask, which is the closest
30 : : to a standard across unix-like OSs */
31 : :
32 : : #if defined( HAVE_FPSETMASK )
33 : :
34 : : #include <ieeefp.h>
35 : :
36 : : bool MBMesquite::MsqFPE::fpe_trap_supported()
37 : : {
38 : : return true;
39 : : }
40 : :
41 : : int MBMesquite::MsqFPE::get_current_fpe_state()
42 : : {
43 : : return fpgetmask();
44 : : }
45 : :
46 : : void MBMesquite::MsqFPE::set_current_fpe_state( int state )
47 : : {
48 : : fpsetmask( state );
49 : : }
50 : :
51 : : void MBMesquite::MsqFPE::enable_trap_fpe()
52 : : {
53 : : fpsetmask( fpgetmask() | FP_X_INV | FP_X_OFL | FP_X_DZ );
54 : : }
55 : :
56 : : /* Next try GNU-C feenableexcept mechanism */
57 : :
58 : : #elif defined( HAVE_FEENABLEEXCEPT )
59 : :
60 : : #ifndef _GNU_SOURCE
61 : : #define MSQ_SET_GNU_SOURCE
62 : : #define _GNU_SOURCE
63 : : #endif
64 : : #include <fenv.h>
65 : : #ifdef MSQ_SET_GNU_SOURCE
66 : : #undef _GNU_SOURCE
67 : : #undef MSQ_SET_GNU_SOURCE
68 : : #endif
69 : :
70 : : bool MBMesquite::MsqFPE::fpe_trap_supported()
71 : : {
72 : : return true;
73 : : }
74 : :
75 : : int MBMesquite::MsqFPE::get_current_fpe_state()
76 : : {
77 : : return fegetexcept();
78 : : }
79 : :
80 : : void MBMesquite::MsqFPE::set_current_fpe_state( int state )
81 : : {
82 : : feenableexcept( state );
83 : : }
84 : :
85 : : void MBMesquite::MsqFPE::enable_trap_fpe()
86 : : {
87 : : const int flags = FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW;
88 : : feclearexcept( flags );
89 : : feenableexcept( flags );
90 : : }
91 : :
92 : : /* Next try Microsoft */
93 : :
94 : : #elif defined( _WIN32 )
95 : :
96 : : #include <float.h>
97 : :
98 : : bool MBMesquite::MsqFPE::fpe_trap_supported()
99 : : {
100 : : return true;
101 : : }
102 : :
103 : : int MBMesquite::MsqFPE::get_current_fpe_state()
104 : : {
105 : : return _controlfp( 0, 0 );
106 : : }
107 : :
108 : : void MBMesquite::MsqFPE::set_current_fpe_state( int state )
109 : : {
110 : : _controlfp( state, _MCW_EM );
111 : : }
112 : :
113 : : void MBMesquite::MsqFPE::enable_trap_fpe()
114 : : {
115 : : const int flags = _EM_ZERODIVIDE | _EM_INVALID | _EM_OVERFLOW;
116 : : _controlfp( ~flags, _MCW_EM );
117 : : }
118 : :
119 : : /* Unsupported platform */
120 : : #else
121 : :
122 : 0 : bool MBMesquite::MsqFPE::fpe_trap_supported()
123 : : {
124 : 0 : return false;
125 : : }
126 : :
127 : 0 : int MBMesquite::MsqFPE::get_current_fpe_state()
128 : : {
129 : 0 : return 0;
130 : : }
131 : :
132 : 0 : void MBMesquite::MsqFPE::set_current_fpe_state( int ) {}
133 : :
134 : 0 : void MBMesquite::MsqFPE::enable_trap_fpe() {}
135 : :
136 : : #endif
137 : :
138 : 138 : MBMesquite::MsqFPE::MsqFPE( bool enabled ) : isEnabled( enabled )
139 : : {
140 [ - + ]: 138 : if( isEnabled )
141 : : {
142 : 0 : prevState = get_current_fpe_state();
143 : 0 : enable_trap_fpe();
144 : : }
145 : 138 : }
146 : :
147 : 138 : MBMesquite::MsqFPE::~MsqFPE()
148 : : {
149 [ - + ]: 138 : if( isEnabled ) { set_current_fpe_state( prevState ); }
150 : 138 : }
|