source: cpp/common/nonstd_math.cpp @ 145

Last change on this file since 145 was 145, checked in by sz, 7 years ago

Genetics reorganization (affects ALL applications!):

  • Converters/Validators? are now configured/initialized in a more verbose but also less confusing way
  • At the same time, the PreconfiguredGenetics? object will help you avoid the increased complexity by creating the ready-to-use environment that is sufficient in 99% of cases (see the demos)
  • Format F genetics updated (work in progress)
  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
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
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
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#ifdef IPHONE
48
49#include <fenv.h>
50
51void fpExceptInit()
52{}
53
54void fpExceptEnable()
55{
56feclearexcept(FE_DIVBYZERO | FE_OVERFLOW);
57//feenableexcept(FE_DIVBYZERO | FE_OVERFLOW);
58}
59
60void fpExceptDisable()
61{
62//fedisableexcept(FE_DIVBYZERO | FE_OVERFLOW);
63}
64#endif
65
66
67#ifdef __BORLANDC__
68// there was once a problem like this:
69// http://qc.embarcadero.com/wc/qcmain.aspx?d=5128
70// http://www.delorie.com/djgpp/doc/libc/libc_112.html
71// ? http://www.c-jump.com/CIS77/reference/Intel/CIS77_24319002/pg_0211.htm
72// ? 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
73// ? http://www.plantation-productions.com/Webster/www.artofasm.com/Linux/HTML/RealArithmetica2.html
74// http://blogs.msdn.com/b/oldnewthing/archive/2008/07/03/8682463.aspx
75// where each cast of a double into an int would cause an exception.
76// 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)
77
78#include "framsg.h"
79
80unsigned int fp_control_word_std;
81unsigned int fp_control_word_muted;
82
83void fpExceptInit()
84{
85        //unsigned int was=_clear87();
86        //FMprintf("","fpExceptInit",FMLV_INFO,"control87 status before clear was %08x", was);
87        fp_control_word_std=_control87(0, 0);             //4978 = 1001101110010
88        // Make the new fp env same as the old one, except for the changes we're going to make
89        fp_control_word_muted = fp_control_word_std | EM_INVALID | EM_DENORMAL | EM_ZERODIVIDE | EM_OVERFLOW | EM_UNDERFLOW | EM_INEXACT;  //4991 = 1001101111111
90}
91
92void fpExceptEnable()
93{
94        unsigned int was=_clear87(); //trzeba czyscic zeby nie bylo exception...
95        //FMprintf("","fpExceptEnable ",FMLV_INFO,"control87 status before clear was %08x", was);
96        _control87(fp_control_word_std, 0xffffffff);
97        //FMprintf("","fpExceptEnable ",FMLV_INFO,"control87 flags are %08x", _control87(0, 0)); //kontrola co sie ustawilo
98}
99
100void fpExceptDisable()
101{
102        unsigned int was=_clear87(); //trzeba czyscic zeby nie bylo exception...
103        //FMprintf("","fpExceptDisable",FMLV_INFO,"control87 status before clear was %08x", was);
104        _control87(fp_control_word_muted, 0xffffffff);
105        //FMprintf("","fpExceptDisable",FMLV_INFO,"control87 flags are %08x", _control87(0, 0)); //kontrola co sie ustawilo
106}
107
108#endif
109
110
111
112#ifdef _MSC_VER
113//Moznaby zrobic tak jak pod linuxem czyli wlaczyc exceptiony na poczatku i wylaczac na chwile przy dzieleniu w extvalue.
114//To by pozwoli³o na wy³apanie pod visualem z³ych sytuacji kiedy framsy licz¹ na NaN, INF itp.
115//http://stackoverflow.com/questions/2769814/how-do-i-use-try-catch-to-catch-floating-point-errors
116void fpExceptInit() {}
117void fpExceptEnable() {}
118void fpExceptDisable() {}
119#endif
120
Note: See TracBrowser for help on using the repository browser.