Ignore:
Timestamp:
09/09/23 15:16:31 (21 months ago)
Author:
Maciej Komosinski
Message:

More unification of floating point exception handling across platforms

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/nonstd_math.cpp

    r1251 r1275  
    8888}
    8989
    90 
    91 
     90namespace fpExcept
     91{ //shared for all platform, using crossplatform constants, can be defined in build_config (but also conditionally changed from the code)
     92int wanted_exceptions =
     93#ifdef WANTED_FP_EXCEPTIONS
     94        WANTED_FP_EXCEPTIONS;
     95#else
     96        FPEX_DIV0; //default when 'WANTED_FP_EXCEPTIONS' is not defined (add more?)
     97#endif
     98}
    9299
    93100// Idea: enable selected floating point exceptions when the app starts and disable them temporarily when dividing values in ExtValue, so that we can directly handle problematic cases there.
     
    96103#ifdef IPHONE
    97104//TODO! -> ? http://stackoverflow.com/questions/12762418/how-to-enable-sigfpe-signal-on-division-by-zero-in-ios-app
    98 void fpExceptInit()
    99 {}
    100 
    101 void fpExceptEnable()
    102 {}
    103 
    104 void fpExceptDisable()
    105 {}
     105namespace fpExcept
     106{
     107        void init() {}
     108        void enable() {}
     109        void disable() {}
     110}
    106111#endif
    107112
    108113#ifdef MACOS
    109 //TODO...?
    110 
    111 void fpExceptInit()
    112 {}
    113 
    114 void fpExceptEnable()
    115 {}
    116 
    117 void fpExceptDisable()
    118 {}
     114//TODO...? (even less reasons to omit this in macos :-P)
     115namespace fpExcept
     116{
     117        void init() {}
     118        void enable() {}
     119        void disable() {}
     120}
    119121#endif
    120122
    121123
    122124#if defined LINUX || defined TIZEN || defined __ANDROID__
    123 
    124125#include <fenv.h>
    125126
    126 static constexpr int WANTED_FP_EXCEPTIONS = FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW;
    127 
    128 void fpExceptInit()
    129 {}
    130 
    131 void fpExceptEnable()
    132 {
    133         feclearexcept(WANTED_FP_EXCEPTIONS);
    134         feenableexcept(WANTED_FP_EXCEPTIONS);
    135 }
    136 
    137 void fpExceptDisable()
    138 {
    139         fedisableexcept(WANTED_FP_EXCEPTIONS);
    140 }
    141 
     127namespace fpExcept
     128{
     129        void init() {}
     130        void enable()
     131        {
     132                feclearexcept(wanted_exceptions);
     133                feenableexcept(wanted_exceptions);
     134        }
     135        void disable()
     136        {
     137                fedisableexcept(wanted_exceptions);
     138        }
     139};
    142140#endif
    143141
     
    146144#if defined(__BORLANDC__) || defined(_MSC_VER)
    147145
     146namespace fpExcept
     147{
    148148// in Borland, there was once a problem like this:
    149149// http://qc.embarcadero.com/wc/qcmain.aspx?d=5128
     
    177177unsigned int fp_control_word_muted;
    178178
    179 
    180 void fpExceptInit()
     179void init()
    181180{
    182181        _controlfp_s(&fp_control_word_std, 0, 0); //in Visual C++, the default value is exactly the masks listed below, and we have to turn them off to enable exceptions
    183182        // Make the new fp env same as the old one, except for the changes we're going to make
    184         fp_control_word_muted = fp_control_word_std & ~(EM_INVALID | /*EM_DENORMAL |*/ EM_ZERODIVIDE | EM_OVERFLOW /* | EM_UNDERFLOW | EM_INEXACT */); //commented out exceptions that occur during proper operation
    185 }
    186 
    187 void fpExceptEnable()
     183        fp_control_word_muted = fp_control_word_std & ~wanted_exceptions;
     184}
     185
     186void enable()
    188187{
    189188        //_fpreset(); //not needed since we just _clearfp()... mentioned in https://stackoverflow.com/questions/4282217/visual-c-weird-behavior-after-enabling-floating-point-exceptions-compiler-b
     
    193192}
    194193
    195 void fpExceptDisable()
     194void disable()
    196195{
    197196        //_fpreset(); //not needed since we just _clearfp()... mentioned in https://stackoverflow.com/questions/4282217/visual-c-weird-behavior-after-enabling-floating-point-exceptions-compiler-b
     
    200199        _controlfp_s(&was, fp_control_word_std, _MCW_EM);
    201200}
    202 #endif
     201       
     202};
     203
     204#endif
Note: See TracChangeset for help on using the changeset viewer.