source: cpp/common/nonstd_math.cpp @ 714

Last change on this file since 714 was 492, checked in by Maciej Komosinski, 9 years ago

emscripten compatibility

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
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
7RandomGenerator& rndGetInstance()
8{
9static RandomGenerator rnd(0);
10return rnd;
11}
12
13
14
15
16#if (defined __BORLANDC__ || (_MSC_VER <= 1700)) & (!defined EMSCRIPTEN)
17double round(double val) //http://stackoverflow.com/questions/2170385/c-math-functions
18{   
19    return floor(val + 0.5);
20}
21#endif
22
23
24#ifdef IPHONE
25//TODO! -> ? http://stackoverflow.com/questions/12762418/how-to-enable-sigfpe-signal-on-division-by-zero-in-ios-app
26void fpExceptInit()
27{}
28
29void fpExceptEnable()
30{}
31
32void fpExceptDisable()
33{}
34#endif
35
36#ifdef MACOS
37//TODO...?
38
39void fpExceptInit()
40{}
41
42void fpExceptEnable()
43{}
44
45void fpExceptDisable()
46{}
47#endif
48
49
50#if defined LINUX || defined TIZEN || defined __ANDROID__
51
52#include <fenv.h>
53
54void fpExceptInit()
55{}
56
57void fpExceptEnable()
58{
59feclearexcept(FE_DIVBYZERO);
60feenableexcept(FE_DIVBYZERO);
61}
62
63void fpExceptDisable()
64{
65fedisableexcept(FE_DIVBYZERO);
66}
67
68#endif
69
70
71
72#ifdef __BORLANDC__
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)
82
83#include "log.h"
84
85unsigned int fp_control_word_std;
86unsigned int fp_control_word_muted;
87
88void fpExceptInit()
89{
90        //unsigned int was=_clear87();
91        //logPrintf("","fpExceptInit",LOG_INFO,"control87 status before clear was %08x", was);
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
97void fpExceptEnable()
98{
99        unsigned int was=_clear87(); //trzeba czyscic zeby nie bylo exception...
100        //logPrintf("","fpExceptEnable ",LOG_INFO,"control87 status before clear was %08x", was);
101        _control87(fp_control_word_std, 0xffffffff);
102        //logPrintf("","fpExceptEnable ",LOG_INFO,"control87 flags are %08x", _control87(0, 0)); //kontrola co sie ustawilo
103}
104
105void fpExceptDisable()
106{
107        unsigned int was=_clear87(); //trzeba czyscic zeby nie bylo exception...
108        //logPrintf("","fpExceptDisable",LOG_INFO,"control87 status before clear was %08x", was);
109        _control87(fp_control_word_muted, 0xffffffff);
110        //logPrintf("","fpExceptDisable",LOG_INFO,"control87 flags are %08x", _control87(0, 0)); //kontrola co sie ustawilo
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
121void fpExceptInit() {}
122void fpExceptEnable() {}
123void fpExceptDisable() {}
124#endif
125
Note: See TracBrowser for help on using the repository browser.