[833] | 1 | // |
---|
| 2 | // ******************************************************************** |
---|
| 3 | // * License and Disclaimer * |
---|
| 4 | // * * |
---|
| 5 | // * The Geant4 software is copyright of the Copyright Holders of * |
---|
| 6 | // * the Geant4 Collaboration. It is provided under the terms and * |
---|
| 7 | // * conditions of the Geant4 Software License, included in the file * |
---|
| 8 | // * LICENSE and available at http://cern.ch/geant4/license . These * |
---|
| 9 | // * include a list of copyright holders. * |
---|
| 10 | // * * |
---|
| 11 | // * Neither the authors of this software system, nor their employing * |
---|
| 12 | // * institutes,nor the agencies providing financial support for this * |
---|
| 13 | // * work make any representation or warranty, express or implied, * |
---|
| 14 | // * regarding this software system or assume any liability for its * |
---|
| 15 | // * use. Please see the license in the file LICENSE and URL above * |
---|
| 16 | // * for the full disclaimer and the limitation of liability. * |
---|
| 17 | // * * |
---|
| 18 | // * This code implementation is the result of the scientific and * |
---|
| 19 | // * technical work of the GEANT4 collaboration. * |
---|
| 20 | // * By using, copying, modifying or distributing the software (or * |
---|
| 21 | // * any work based on the software) you agree to acknowledge its * |
---|
| 22 | // * use in resulting scientific publications, and indicate your * |
---|
| 23 | // * acceptance of all terms of the Geant4 Software license. * |
---|
| 24 | // ******************************************************************** |
---|
| 25 | // |
---|
| 26 | // |
---|
| 27 | // $Id: G4FPEDetection.hh,v 1.2 2006/11/15 16:00:18 gcosmo Exp $ |
---|
| 28 | // GEANT4 tag $Name: $ |
---|
| 29 | // |
---|
| 30 | // |
---|
| 31 | // -*- C++ -*- |
---|
| 32 | // |
---|
| 33 | // ----------------------------------------------------------------------- |
---|
| 34 | // This global method should be used on LINUX platforms with gcc compiler |
---|
| 35 | // for activating NaN detection and FPE signals, and forcing abortion of |
---|
| 36 | // the application at the time these are detected. |
---|
| 37 | // Meant to be used for debug purposes, can be activated by compiling the |
---|
| 38 | // "run" module with the flag G4FPE_DEBUG set in the environment. |
---|
| 39 | // ----------------------------------------------------------------------- |
---|
| 40 | |
---|
| 41 | #ifndef G4FPEDetection_h |
---|
| 42 | #define G4FPEDetection_h 1 |
---|
| 43 | |
---|
| 44 | #ifdef __linux__ |
---|
| 45 | #ifdef __GNUC__ |
---|
| 46 | #include <features.h> |
---|
| 47 | #include <fenv.h> |
---|
| 48 | #include <csignal> |
---|
| 49 | |
---|
| 50 | #include <iostream> |
---|
| 51 | |
---|
| 52 | struct sigaction termaction, oldaction; |
---|
| 53 | |
---|
| 54 | void TerminationSignalHandler(int sig) |
---|
| 55 | { |
---|
| 56 | std::cerr << "ERROR: " << sig; |
---|
| 57 | std::string message; |
---|
| 58 | switch (SIGFPE) |
---|
| 59 | { |
---|
| 60 | case FPE_INTDIV: |
---|
| 61 | message = "Integer divide by zero."; |
---|
| 62 | break; |
---|
| 63 | case FPE_INTOVF: |
---|
| 64 | message = "Integer overflow."; |
---|
| 65 | break; |
---|
| 66 | case FPE_FLTDIV: |
---|
| 67 | message = "Floating point divide by zero."; |
---|
| 68 | break; |
---|
| 69 | case FPE_FLTOVF: |
---|
| 70 | message = "Floating point overflow."; |
---|
| 71 | break; |
---|
| 72 | case FPE_FLTUND: |
---|
| 73 | message = "Floating point underflow."; |
---|
| 74 | break; |
---|
| 75 | case FPE_FLTRES: |
---|
| 76 | message = "Floating point inexact result."; |
---|
| 77 | break; |
---|
| 78 | case FPE_FLTINV: |
---|
| 79 | message = "Floating point invalid operation."; |
---|
| 80 | break; |
---|
| 81 | case FPE_FLTSUB: |
---|
| 82 | message = "Subscript out of range."; |
---|
| 83 | break; |
---|
| 84 | default: |
---|
| 85 | message = "Unknown error."; |
---|
| 86 | break; |
---|
| 87 | } |
---|
| 88 | std::cerr << " - " << message << std::endl; |
---|
| 89 | |
---|
| 90 | ::abort(); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | void InvalidOperationDetection() |
---|
| 94 | { |
---|
| 95 | std::cout << std::endl |
---|
| 96 | << " " |
---|
| 97 | << "############################################" << std::endl |
---|
| 98 | << " " |
---|
| 99 | << "!!! WARNING - FPE detection is activated !!!" << std::endl |
---|
| 100 | << " " |
---|
| 101 | << "############################################" << std::endl; |
---|
| 102 | |
---|
| 103 | (void) feenableexcept( FE_DIVBYZERO ); |
---|
| 104 | (void) feenableexcept( FE_INVALID ); |
---|
| 105 | //(void) feenableexcept( FE_OVERFLOW ); |
---|
| 106 | //(void) feenableexcept( FE_UNDERFLOW ); |
---|
| 107 | |
---|
| 108 | sigset_t *def_set; |
---|
| 109 | def_set=&termaction.sa_mask; |
---|
| 110 | sigfillset(def_set); |
---|
| 111 | sigdelset(def_set,SIGFPE); |
---|
| 112 | termaction.sa_handler=TerminationSignalHandler; |
---|
| 113 | termaction.sa_flags=0; |
---|
| 114 | sigaction(SIGFPE, &termaction,&oldaction); |
---|
| 115 | } |
---|
| 116 | #endif |
---|
| 117 | #else |
---|
| 118 | void InvalidOperationDetection() {;} |
---|
| 119 | #endif |
---|
| 120 | |
---|
| 121 | #endif |
---|