1 /*========================================================================= 2 3 Program: Visualization Toolkit 4 Module: vtkFloatingPointExceptions.cxx 5 6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 7 All rights reserved. 8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 9 10 This software is distributed WITHOUT ANY WARRANTY; without even 11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 12 PURPOSE. See the above copyright notice for more information. 13 14 =========================================================================*/ 15 16 #include "vtkFloatingPointExceptions.h" 17 18 #include "vtkFloatingPointExceptionsConfigure.h" 19 20 #if defined(VTK_USE_FENV) 21 #include <csignal> 22 #include <fenv.h> 23 #endif 24 25 #ifdef _MSC_VER 26 #include <float.h> 27 #endif 28 29 #if defined(VTK_USE_FENV) 30 //----------------------------------------------------------------------------- 31 // Signal handler for floating point exceptions in anonymous namespace 32 namespace { 33 signal_handler(int signal)34void signal_handler(int signal) 35 { 36 cerr << "Error: Floating point exception detected. Signal " << signal << endl; 37 // This should possibly throw an exception rather than abort, abort should 38 // at least give access to the stack when it fails here. 39 abort(); 40 } 41 42 } // End anonymous namespace 43 #endif 44 45 //----------------------------------------------------------------------------- 46 // Description: 47 // Enable floating point exceptions. Enable()48void vtkFloatingPointExceptions::Enable() 49 { 50 #ifdef _MSC_VER 51 // enable floating point exceptions on MSVC 52 _controlfp(_EM_DENORMAL | _EM_UNDERFLOW | _EM_INEXACT, _MCW_EM); 53 #endif //_MSC_VER 54 #if defined(VTK_USE_FENV) 55 // This should work on all platforms 56 feenableexcept(FE_DIVBYZERO | FE_INVALID); 57 // Set the signal handler 58 signal(SIGFPE, signal_handler); 59 #endif 60 } 61 62 //----------------------------------------------------------------------------- 63 // Description: 64 // Disable floating point exceptions. Disable()65void vtkFloatingPointExceptions::Disable() 66 { 67 #ifdef _MSC_VER 68 // disable floating point exceptions on MSVC 69 _controlfp(_EM_INVALID | _EM_DENORMAL | _EM_ZERODIVIDE | _EM_OVERFLOW | 70 _EM_UNDERFLOW | _EM_INEXACT, _MCW_EM); 71 #endif //_MSC_VER 72 #if defined(VTK_USE_FENV) 73 fedisableexcept(FE_DIVBYZERO | FE_INVALID); 74 #endif 75 } 76