[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 | { |
---|
| 9 | static RandomGenerator rnd(0); |
---|
| 10 | return rnd; |
---|
| 11 | } |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | |
---|
[135] | 16 | #if defined __BORLANDC__ || (_MSC_VER <= 1700) |
---|
| 17 | double round(double val) //http://stackoverflow.com/questions/2170385/c-math-functions |
---|
[109] | 18 | { |
---|
| 19 | return floor(val + 0.5); |
---|
| 20 | } |
---|
| 21 | #endif |
---|
| 22 | |
---|
| 23 | |
---|
[247] | 24 | #ifdef IPHONE |
---|
| 25 | //TODO! -> ? http://stackoverflow.com/questions/12762418/how-to-enable-sigfpe-signal-on-division-by-zero-in-ios-app |
---|
| 26 | void fpExceptInit() |
---|
| 27 | {} |
---|
[109] | 28 | |
---|
[247] | 29 | void fpExceptEnable() |
---|
| 30 | {} |
---|
[109] | 31 | |
---|
[247] | 32 | void fpExceptDisable() |
---|
| 33 | {} |
---|
| 34 | #endif |
---|
[109] | 35 | |
---|
[285] | 36 | #ifdef MACOS |
---|
| 37 | //TODO...? |
---|
[247] | 38 | |
---|
[285] | 39 | void fpExceptInit() |
---|
| 40 | {} |
---|
| 41 | |
---|
| 42 | void fpExceptEnable() |
---|
| 43 | {} |
---|
| 44 | |
---|
| 45 | void fpExceptDisable() |
---|
| 46 | {} |
---|
| 47 | #endif |
---|
| 48 | |
---|
| 49 | |
---|
[247] | 50 | #if defined LINUX || defined TIZEN || defined __ANDROID__ |
---|
| 51 | |
---|
[109] | 52 | #include <fenv.h> |
---|
| 53 | |
---|
| 54 | void fpExceptInit() |
---|
| 55 | {} |
---|
| 56 | |
---|
| 57 | void fpExceptEnable() |
---|
| 58 | { |
---|
| 59 | feclearexcept(FE_DIVBYZERO | FE_OVERFLOW); |
---|
| 60 | feenableexcept(FE_DIVBYZERO | FE_OVERFLOW); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | void fpExceptDisable() |
---|
| 64 | { |
---|
| 65 | fedisableexcept(FE_DIVBYZERO | FE_OVERFLOW); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | #endif |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | #ifdef __BORLANDC__ |
---|
[145] | 73 | // there was once a problem like this: |
---|
| 74 | // http://qc.embarcadero.com/wc/qcmain.aspx?d=5128 |
---|
| 75 | // http://www.delorie.com/djgpp/doc/libc/libc_112.html |
---|
| 76 | // ? http://www.c-jump.com/CIS77/reference/Intel/CIS77_24319002/pg_0211.htm |
---|
| 77 | // ? 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 |
---|
| 78 | // ? http://www.plantation-productions.com/Webster/www.artofasm.com/Linux/HTML/RealArithmetica2.html |
---|
| 79 | // http://blogs.msdn.com/b/oldnewthing/archive/2008/07/03/8682463.aspx |
---|
| 80 | // where each cast of a double into an int would cause an exception. |
---|
| 81 | // 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] | 82 | |
---|
[375] | 83 | #include "log.h" |
---|
[109] | 84 | |
---|
| 85 | unsigned int fp_control_word_std; |
---|
| 86 | unsigned int fp_control_word_muted; |
---|
| 87 | |
---|
| 88 | void fpExceptInit() |
---|
| 89 | { |
---|
| 90 | //unsigned int was=_clear87(); |
---|
[375] | 91 | //logPrintf("","fpExceptInit",LOG_INFO,"control87 status before clear was %08x", was); |
---|
[109] | 92 | fp_control_word_std=_control87(0, 0); //4978 = 1001101110010 |
---|
| 93 | // Make the new fp env same as the old one, except for the changes we're going to make |
---|
| 94 | fp_control_word_muted = fp_control_word_std | EM_INVALID | EM_DENORMAL | EM_ZERODIVIDE | EM_OVERFLOW | EM_UNDERFLOW | EM_INEXACT; //4991 = 1001101111111 |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void fpExceptEnable() |
---|
| 98 | { |
---|
| 99 | unsigned int was=_clear87(); //trzeba czyscic zeby nie bylo exception... |
---|
[375] | 100 | //logPrintf("","fpExceptEnable ",LOG_INFO,"control87 status before clear was %08x", was); |
---|
[109] | 101 | _control87(fp_control_word_std, 0xffffffff); |
---|
[375] | 102 | //logPrintf("","fpExceptEnable ",LOG_INFO,"control87 flags are %08x", _control87(0, 0)); //kontrola co sie ustawilo |
---|
[109] | 103 | } |
---|
| 104 | |
---|
| 105 | void fpExceptDisable() |
---|
| 106 | { |
---|
| 107 | unsigned int was=_clear87(); //trzeba czyscic zeby nie bylo exception... |
---|
[375] | 108 | //logPrintf("","fpExceptDisable",LOG_INFO,"control87 status before clear was %08x", was); |
---|
[109] | 109 | _control87(fp_control_word_muted, 0xffffffff); |
---|
[375] | 110 | //logPrintf("","fpExceptDisable",LOG_INFO,"control87 flags are %08x", _control87(0, 0)); //kontrola co sie ustawilo |
---|
[109] | 111 | } |
---|
| 112 | |
---|
| 113 | #endif |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | #ifdef _MSC_VER |
---|
| 118 | //Moznaby zrobic tak jak pod linuxem czyli wlaczyc exceptiony na poczatku i wylaczac na chwile przy dzieleniu w extvalue. |
---|
| 119 | //To by pozwoli³o na wy³apanie pod visualem z³ych sytuacji kiedy framsy licz¹ na NaN, INF itp. |
---|
| 120 | //http://stackoverflow.com/questions/2769814/how-do-i-use-try-catch-to-catch-floating-point-errors |
---|
| 121 | void fpExceptInit() {} |
---|
| 122 | void fpExceptEnable() {} |
---|
| 123 | void fpExceptDisable() {} |
---|
| 124 | #endif |
---|
| 125 | |
---|