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. |
---|
4 | |
---|
5 | #include "nonstd_math.h" |
---|
6 | |
---|
7 | RandomGenerator& rndGetInstance() |
---|
8 | { |
---|
9 | static RandomGenerator rnd(0); |
---|
10 | return rnd; |
---|
11 | } |
---|
12 | |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | |
---|
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 | {} |
---|
21 | |
---|
22 | void fpExceptEnable() |
---|
23 | {} |
---|
24 | |
---|
25 | void fpExceptDisable() |
---|
26 | {} |
---|
27 | #endif |
---|
28 | |
---|
29 | #ifdef MACOS |
---|
30 | //TODO...? |
---|
31 | |
---|
32 | void fpExceptInit() |
---|
33 | {} |
---|
34 | |
---|
35 | void fpExceptEnable() |
---|
36 | {} |
---|
37 | |
---|
38 | void fpExceptDisable() |
---|
39 | {} |
---|
40 | #endif |
---|
41 | |
---|
42 | |
---|
43 | #if defined LINUX || defined TIZEN || defined __ANDROID__ |
---|
44 | |
---|
45 | #include <fenv.h> |
---|
46 | |
---|
47 | void fpExceptInit() |
---|
48 | {} |
---|
49 | |
---|
50 | void fpExceptEnable() |
---|
51 | { |
---|
52 | feclearexcept(FE_DIVBYZERO); |
---|
53 | feenableexcept(FE_DIVBYZERO); |
---|
54 | } |
---|
55 | |
---|
56 | void fpExceptDisable() |
---|
57 | { |
---|
58 | fedisableexcept(FE_DIVBYZERO); |
---|
59 | } |
---|
60 | |
---|
61 | #endif |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | #ifdef __BORLANDC__ |
---|
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) |
---|
75 | |
---|
76 | #include "log.h" |
---|
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(); |
---|
84 | //logPrintf("","fpExceptInit",LOG_INFO,"control87 status before clear was %08x", was); |
---|
85 | fp_control_word_std = _control87(0, 0); //4978 = 1001101110010 |
---|
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 | { |
---|
92 | unsigned int was = _clear87(); //trzeba czyscic zeby nie bylo exception... |
---|
93 | //logPrintf("","fpExceptEnable ",LOG_INFO,"control87 status before clear was %08x", was); |
---|
94 | _control87(fp_control_word_std, 0xffffffff); |
---|
95 | //logPrintf("","fpExceptEnable ",LOG_INFO,"control87 flags are %08x", _control87(0, 0)); //kontrola co sie ustawilo |
---|
96 | } |
---|
97 | |
---|
98 | void fpExceptDisable() |
---|
99 | { |
---|
100 | unsigned int was = _clear87(); //trzeba czyscic zeby nie bylo exception... |
---|
101 | //logPrintf("","fpExceptDisable",LOG_INFO,"control87 status before clear was %08x", was); |
---|
102 | _control87(fp_control_word_muted, 0xffffffff); |
---|
103 | //logPrintf("","fpExceptDisable",LOG_INFO,"control87 flags are %08x", _control87(0, 0)); //kontrola co sie ustawilo |
---|
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 |
---|