source: cpp/common/nonstd_math.cpp @ 260

Last change on this file since 260 was 247, checked in by Maciej Komosinski, 10 years ago

Sources support both 32-bit and 64-bit, and more compilers

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