source: cpp/common/nonstd_math.cpp @ 238

Last change on this file since 238 was 227, checked in by Maciej Komosinski, 11 years ago

Android compilation and access to RESOURCES and HOME files

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