[122] | 1 | // This file is a part of the Framsticks GDK. |
---|
[197] | 2 | // Copyright (C) 1999-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
[122] | 3 | // Refer to http://www.framsticks.com/ for further information. |
---|
| 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 | |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | #if defined LINUX || defined TIZEN |
---|
| 27 | |
---|
| 28 | #include <fenv.h> |
---|
| 29 | |
---|
| 30 | void fpExceptInit() |
---|
| 31 | {} |
---|
| 32 | |
---|
| 33 | void fpExceptEnable() |
---|
| 34 | { |
---|
| 35 | feclearexcept(FE_DIVBYZERO | FE_OVERFLOW); |
---|
| 36 | feenableexcept(FE_DIVBYZERO | FE_OVERFLOW); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | void fpExceptDisable() |
---|
| 40 | { |
---|
| 41 | fedisableexcept(FE_DIVBYZERO | FE_OVERFLOW); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | #endif |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | #ifdef IPHONE |
---|
| 48 | |
---|
| 49 | #include <fenv.h> |
---|
| 50 | |
---|
| 51 | void fpExceptInit() |
---|
| 52 | {} |
---|
| 53 | |
---|
| 54 | void fpExceptEnable() |
---|
| 55 | { |
---|
| 56 | feclearexcept(FE_DIVBYZERO | FE_OVERFLOW); |
---|
| 57 | //feenableexcept(FE_DIVBYZERO | FE_OVERFLOW); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | void fpExceptDisable() |
---|
| 61 | { |
---|
| 62 | //fedisableexcept(FE_DIVBYZERO | FE_OVERFLOW); |
---|
| 63 | } |
---|
| 64 | #endif |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | #ifdef __BORLANDC__ |
---|
[145] | 68 | // there was once a problem like this: |
---|
| 69 | // http://qc.embarcadero.com/wc/qcmain.aspx?d=5128 |
---|
| 70 | // http://www.delorie.com/djgpp/doc/libc/libc_112.html |
---|
| 71 | // ? http://www.c-jump.com/CIS77/reference/Intel/CIS77_24319002/pg_0211.htm |
---|
| 72 | // ? 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 |
---|
| 73 | // ? http://www.plantation-productions.com/Webster/www.artofasm.com/Linux/HTML/RealArithmetica2.html |
---|
| 74 | // http://blogs.msdn.com/b/oldnewthing/archive/2008/07/03/8682463.aspx |
---|
| 75 | // where each cast of a double into an int would cause an exception. |
---|
| 76 | // 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] | 77 | |
---|
| 78 | #include "framsg.h" |
---|
| 79 | |
---|
| 80 | unsigned int fp_control_word_std; |
---|
| 81 | unsigned int fp_control_word_muted; |
---|
| 82 | |
---|
| 83 | void fpExceptInit() |
---|
| 84 | { |
---|
| 85 | //unsigned int was=_clear87(); |
---|
| 86 | //FMprintf("","fpExceptInit",FMLV_INFO,"control87 status before clear was %08x", was); |
---|
| 87 | fp_control_word_std=_control87(0, 0); //4978 = 1001101110010 |
---|
| 88 | // Make the new fp env same as the old one, except for the changes we're going to make |
---|
| 89 | fp_control_word_muted = fp_control_word_std | EM_INVALID | EM_DENORMAL | EM_ZERODIVIDE | EM_OVERFLOW | EM_UNDERFLOW | EM_INEXACT; //4991 = 1001101111111 |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | void fpExceptEnable() |
---|
| 93 | { |
---|
| 94 | unsigned int was=_clear87(); //trzeba czyscic zeby nie bylo exception... |
---|
| 95 | //FMprintf("","fpExceptEnable ",FMLV_INFO,"control87 status before clear was %08x", was); |
---|
| 96 | _control87(fp_control_word_std, 0xffffffff); |
---|
| 97 | //FMprintf("","fpExceptEnable ",FMLV_INFO,"control87 flags are %08x", _control87(0, 0)); //kontrola co sie ustawilo |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void fpExceptDisable() |
---|
| 101 | { |
---|
| 102 | unsigned int was=_clear87(); //trzeba czyscic zeby nie bylo exception... |
---|
| 103 | //FMprintf("","fpExceptDisable",FMLV_INFO,"control87 status before clear was %08x", was); |
---|
| 104 | _control87(fp_control_word_muted, 0xffffffff); |
---|
| 105 | //FMprintf("","fpExceptDisable",FMLV_INFO,"control87 flags are %08x", _control87(0, 0)); //kontrola co sie ustawilo |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | #endif |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | #ifdef _MSC_VER |
---|
| 113 | //Moznaby zrobic tak jak pod linuxem czyli wlaczyc exceptiony na poczatku i wylaczac na chwile przy dzieleniu w extvalue. |
---|
| 114 | //To by pozwoli³o na wy³apanie pod visualem z³ych sytuacji kiedy framsy licz¹ na NaN, INF itp. |
---|
| 115 | //http://stackoverflow.com/questions/2769814/how-do-i-use-try-catch-to-catch-floating-point-errors |
---|
| 116 | void fpExceptInit() {} |
---|
| 117 | void fpExceptEnable() {} |
---|
| 118 | void fpExceptDisable() {} |
---|
| 119 | #endif |
---|
| 120 | |
---|