MOAB: Mesh Oriented datABase  (version 5.2.1)
MsqFPE.cpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2005 Lawrence Livermore National Laboratory.  Under
00005     the terms of Contract B545069 with the University of Wisconsin --
00006     Madison, Lawrence Livermore National Laboratory retains certain
00007     rights in this software.
00008 
00009     This library is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU Lesser General Public
00011     License as published by the Free Software Foundation; either
00012     version 2.1 of the License, or (at your option) any later version.
00013 
00014     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     (lgpl.txt) along with this library; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023     kraftche@cae.wisc.edu
00024 
00025   ***************************************************************** */
00026 
00027 #include "MsqFPE.hpp"
00028 
00029 /* First check for BSD-style fpsetmask, which is the closest
00030    to a standard across unix-like OSs */
00031 
00032 #if defined( HAVE_FPSETMASK )
00033 
00034 #include <ieeefp.h>
00035 
00036 bool MBMesquite::MsqFPE::fpe_trap_supported()
00037 {
00038     return true;
00039 }
00040 
00041 int MBMesquite::MsqFPE::get_current_fpe_state()
00042 {
00043     return fpgetmask();
00044 }
00045 
00046 void MBMesquite::MsqFPE::set_current_fpe_state( int state )
00047 {
00048     fpsetmask( state );
00049 }
00050 
00051 void MBMesquite::MsqFPE::enable_trap_fpe()
00052 {
00053     fpsetmask( fpgetmask() | FP_X_INV | FP_X_OFL | FP_X_DZ );
00054 }
00055 
00056 /* Next try GNU-C feenableexcept mechanism */
00057 
00058 #elif defined( HAVE_FEENABLEEXCEPT )
00059 
00060 #ifndef _GNU_SOURCE
00061 #define MSQ_SET_GNU_SOURCE
00062 #define _GNU_SOURCE
00063 #endif
00064 #include <fenv.h>
00065 #ifdef MSQ_SET_GNU_SOURCE
00066 #undef _GNU_SOURCE
00067 #undef MSQ_SET_GNU_SOURCE
00068 #endif
00069 
00070 bool MBMesquite::MsqFPE::fpe_trap_supported()
00071 {
00072     return true;
00073 }
00074 
00075 int MBMesquite::MsqFPE::get_current_fpe_state()
00076 {
00077     return fegetexcept();
00078 }
00079 
00080 void MBMesquite::MsqFPE::set_current_fpe_state( int state )
00081 {
00082     feenableexcept( state );
00083 }
00084 
00085 void MBMesquite::MsqFPE::enable_trap_fpe()
00086 {
00087     const int flags = FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW;
00088     feclearexcept( flags );
00089     feenableexcept( flags );
00090 }
00091 
00092 /* Next try Microsoft */
00093 
00094 #elif defined( _WIN32 )
00095 
00096 #include <float.h>
00097 
00098 bool MBMesquite::MsqFPE::fpe_trap_supported()
00099 {
00100     return true;
00101 }
00102 
00103 int MBMesquite::MsqFPE::get_current_fpe_state()
00104 {
00105     return _controlfp( 0, 0 );
00106 }
00107 
00108 void MBMesquite::MsqFPE::set_current_fpe_state( int state )
00109 {
00110     _controlfp( state, _MCW_EM );
00111 }
00112 
00113 void MBMesquite::MsqFPE::enable_trap_fpe()
00114 {
00115     const int flags = _EM_ZERODIVIDE | _EM_INVALID | _EM_OVERFLOW;
00116     _controlfp( ~flags, _MCW_EM );
00117 }
00118 
00119 /* Unsupported platform */
00120 #else
00121 
00122 bool MBMesquite::MsqFPE::fpe_trap_supported()
00123 {
00124     return false;
00125 }
00126 
00127 int MBMesquite::MsqFPE::get_current_fpe_state()
00128 {
00129     return 0;
00130 }
00131 
00132 void MBMesquite::MsqFPE::set_current_fpe_state( int ) {}
00133 
00134 void MBMesquite::MsqFPE::enable_trap_fpe() {}
00135 
00136 #endif
00137 
00138 MBMesquite::MsqFPE::MsqFPE( bool enabled ) : isEnabled( enabled )
00139 {
00140     if( isEnabled )
00141     {
00142         prevState = get_current_fpe_state();
00143         enable_trap_fpe();
00144     }
00145 }
00146 
00147 MBMesquite::MsqFPE::~MsqFPE()
00148 {
00149     if( isEnabled ) { set_current_fpe_state( prevState ); }
00150 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines