1 | // This file is a part of the Framsticks GDK. |
---|
2 | // Copyright (C) 2002-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
3 | // Refer to http://www.framsticks.com/ for further information. |
---|
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 | #if defined __BORLANDC__ || (_MSC_VER <= 1700) |
---|
17 | double round(double val) //http://stackoverflow.com/questions/2170385/c-math-functions |
---|
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__ |
---|
68 | |
---|
69 | #include "framsg.h" |
---|
70 | |
---|
71 | unsigned int fp_control_word_std; |
---|
72 | unsigned int fp_control_word_muted; |
---|
73 | |
---|
74 | void fpExceptInit() |
---|
75 | { |
---|
76 | //unsigned int was=_clear87(); |
---|
77 | //FMprintf("","fpExceptInit",FMLV_INFO,"control87 status before clear was %08x", was); |
---|
78 | fp_control_word_std=_control87(0, 0); //4978 = 1001101110010 |
---|
79 | // Make the new fp env same as the old one, except for the changes we're going to make |
---|
80 | fp_control_word_muted = fp_control_word_std | EM_INVALID | EM_DENORMAL | EM_ZERODIVIDE | EM_OVERFLOW | EM_UNDERFLOW | EM_INEXACT; //4991 = 1001101111111 |
---|
81 | } |
---|
82 | |
---|
83 | void fpExceptEnable() |
---|
84 | { |
---|
85 | unsigned int was=_clear87(); //trzeba czyscic zeby nie bylo exception... |
---|
86 | //FMprintf("","fpExceptEnable ",FMLV_INFO,"control87 status before clear was %08x", was); |
---|
87 | _control87(fp_control_word_std, 0xffffffff); |
---|
88 | //FMprintf("","fpExceptEnable ",FMLV_INFO,"control87 flags are %08x", _control87(0, 0)); //kontrola co sie ustawilo |
---|
89 | } |
---|
90 | |
---|
91 | void fpExceptDisable() |
---|
92 | { |
---|
93 | unsigned int was=_clear87(); //trzeba czyscic zeby nie bylo exception... |
---|
94 | //FMprintf("","fpExceptDisable",FMLV_INFO,"control87 status before clear was %08x", was); |
---|
95 | _control87(fp_control_word_muted, 0xffffffff); |
---|
96 | //FMprintf("","fpExceptDisable",FMLV_INFO,"control87 flags are %08x", _control87(0, 0)); //kontrola co sie ustawilo |
---|
97 | } |
---|
98 | |
---|
99 | #endif |
---|
100 | |
---|
101 | |
---|
102 | |
---|
103 | #ifdef _MSC_VER |
---|
104 | //Moznaby zrobic tak jak pod linuxem czyli wlaczyc exceptiony na poczatku i wylaczac na chwile przy dzieleniu w extvalue. |
---|
105 | //To by pozwoli³o na wy³apanie pod visualem z³ych sytuacji kiedy framsy licz¹ na NaN, INF itp. |
---|
106 | //http://stackoverflow.com/questions/2769814/how-do-i-use-try-catch-to-catch-floating-point-errors |
---|
107 | void fpExceptInit() {} |
---|
108 | void fpExceptEnable() {} |
---|
109 | void fpExceptDisable() {} |
---|
110 | #endif |
---|
111 | |
---|