[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
| 2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
| 3 | // See LICENSE.txt for details. |
---|
[122] | 4 | |
---|
[109] | 5 | #include "nonstd_math.h" |
---|
| 6 | |
---|
| 7 | RandomGenerator& rndGetInstance() |
---|
| 8 | { |
---|
[867] | 9 | static RandomGenerator rnd(0); |
---|
| 10 | return rnd; |
---|
[109] | 11 | } |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | |
---|
[247] | 17 | #ifdef IPHONE |
---|
| 18 | //TODO! -> ? http://stackoverflow.com/questions/12762418/how-to-enable-sigfpe-signal-on-division-by-zero-in-ios-app |
---|
| 19 | void fpExceptInit() |
---|
| 20 | {} |
---|
[109] | 21 | |
---|
[247] | 22 | void fpExceptEnable() |
---|
| 23 | {} |
---|
[109] | 24 | |
---|
[247] | 25 | void fpExceptDisable() |
---|
| 26 | {} |
---|
| 27 | #endif |
---|
[109] | 28 | |
---|
[285] | 29 | #ifdef MACOS |
---|
| 30 | //TODO...? |
---|
[247] | 31 | |
---|
[285] | 32 | void fpExceptInit() |
---|
| 33 | {} |
---|
| 34 | |
---|
| 35 | void fpExceptEnable() |
---|
| 36 | {} |
---|
| 37 | |
---|
| 38 | void fpExceptDisable() |
---|
| 39 | {} |
---|
| 40 | #endif |
---|
| 41 | |
---|
| 42 | |
---|
[247] | 43 | #if defined LINUX || defined TIZEN || defined __ANDROID__ |
---|
| 44 | |
---|
[109] | 45 | #include <fenv.h> |
---|
| 46 | |
---|
| 47 | void fpExceptInit() |
---|
| 48 | {} |
---|
| 49 | |
---|
| 50 | void fpExceptEnable() |
---|
| 51 | { |
---|
[867] | 52 | feclearexcept(FE_DIVBYZERO); |
---|
| 53 | feenableexcept(FE_DIVBYZERO); |
---|
[109] | 54 | } |
---|
| 55 | |
---|
| 56 | void fpExceptDisable() |
---|
| 57 | { |
---|
[867] | 58 | fedisableexcept(FE_DIVBYZERO); |
---|
[109] | 59 | } |
---|
| 60 | |
---|
| 61 | #endif |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | #ifdef __BORLANDC__ |
---|
[145] | 66 | // there was once a problem like this: |
---|
| 67 | // http://qc.embarcadero.com/wc/qcmain.aspx?d=5128 |
---|
| 68 | // http://www.delorie.com/djgpp/doc/libc/libc_112.html |
---|
| 69 | // ? http://www.c-jump.com/CIS77/reference/Intel/CIS77_24319002/pg_0211.htm |
---|
| 70 | // ? http://www.jaist.ac.jp/iscenter-new/mpc/altix/altixdata/opt/intel/vtune/doc/users_guide/mergedProjects/analyzer_ec/mergedProjects/reference_olh/mergedProjects/instructions/instruct32_hh/vc100.htm |
---|
| 71 | // ? http://www.plantation-productions.com/Webster/www.artofasm.com/Linux/HTML/RealArithmetica2.html |
---|
| 72 | // http://blogs.msdn.com/b/oldnewthing/archive/2008/07/03/8682463.aspx |
---|
| 73 | // where each cast of a double into an int would cause an exception. |
---|
| 74 | // But it was resolved by restarting windows and cleaning all intermediate compilation files :o (restarting windows was the key element! restarting BC++Builder and deleting files would not help) |
---|
[109] | 75 | |
---|
[375] | 76 | #include "log.h" |
---|
[109] | 77 | |
---|
| 78 | unsigned int fp_control_word_std; |
---|
| 79 | unsigned int fp_control_word_muted; |
---|
| 80 | |
---|
| 81 | void fpExceptInit() |
---|
| 82 | { |
---|
| 83 | //unsigned int was=_clear87(); |
---|
[375] | 84 | //logPrintf("","fpExceptInit",LOG_INFO,"control87 status before clear was %08x", was); |
---|
[867] | 85 | fp_control_word_std = _control87(0, 0); //4978 = 1001101110010 |
---|
[109] | 86 | // Make the new fp env same as the old one, except for the changes we're going to make |
---|
| 87 | fp_control_word_muted = fp_control_word_std | EM_INVALID | EM_DENORMAL | EM_ZERODIVIDE | EM_OVERFLOW | EM_UNDERFLOW | EM_INEXACT; //4991 = 1001101111111 |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | void fpExceptEnable() |
---|
| 91 | { |
---|
[867] | 92 | unsigned int was = _clear87(); //trzeba czyscic zeby nie bylo exception... |
---|
[375] | 93 | //logPrintf("","fpExceptEnable ",LOG_INFO,"control87 status before clear was %08x", was); |
---|
[109] | 94 | _control87(fp_control_word_std, 0xffffffff); |
---|
[375] | 95 | //logPrintf("","fpExceptEnable ",LOG_INFO,"control87 flags are %08x", _control87(0, 0)); //kontrola co sie ustawilo |
---|
[109] | 96 | } |
---|
| 97 | |
---|
| 98 | void fpExceptDisable() |
---|
| 99 | { |
---|
[867] | 100 | unsigned int was = _clear87(); //trzeba czyscic zeby nie bylo exception... |
---|
[375] | 101 | //logPrintf("","fpExceptDisable",LOG_INFO,"control87 status before clear was %08x", was); |
---|
[109] | 102 | _control87(fp_control_word_muted, 0xffffffff); |
---|
[375] | 103 | //logPrintf("","fpExceptDisable",LOG_INFO,"control87 flags are %08x", _control87(0, 0)); //kontrola co sie ustawilo |
---|
[109] | 104 | } |
---|
| 105 | |
---|
| 106 | #endif |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | #ifdef _MSC_VER |
---|
| 111 | //Moznaby zrobic tak jak pod linuxem czyli wlaczyc exceptiony na poczatku i wylaczac na chwile przy dzieleniu w extvalue. |
---|
| 112 | //To by pozwoli³o na wy³apanie pod visualem z³ych sytuacji kiedy framsy licz¹ na NaN, INF itp. |
---|
| 113 | //http://stackoverflow.com/questions/2769814/how-do-i-use-try-catch-to-catch-floating-point-errors |
---|
| 114 | void fpExceptInit() {} |
---|
| 115 | void fpExceptEnable() {} |
---|
| 116 | void fpExceptDisable() {} |
---|
| 117 | #endif |
---|