Commit b8a5dfdc authored by Sébastien Villemot's avatar Sébastien Villemot
Browse files

Replace several usages of C macroprocessor with C++ constructs

parent 827fc494
......@@ -39,10 +39,10 @@
# endif
#endif
#define NEAR_ZERO (1e-12)
using namespace std;
const double near_zero{1e-12};
/**
* \enum Tags
* \brief The differents flags of the bytecode
......
......@@ -722,7 +722,7 @@ DataTree::writePowerDeriv(ostream &output) const
<< "#ifdef _MSC_VER" << endl
<< "# define nearbyint(x) (fabs((x)-floor(x)) < fabs((x)-ceil(x)) ? floor(x) : ceil(x))" << endl
<< "#endif" << endl
<< " if ( fabs(x) < " << NEAR_ZERO << " && p > 0 && k > p && fabs(p-nearbyint(p)) < " << NEAR_ZERO << " )" << endl
<< " if ( fabs(x) < " << near_zero << " && p > 0 && k > p && fabs(p-nearbyint(p)) < " << near_zero << " )" << endl
<< " return 0.0;" << endl
<< " else" << endl
<< " {" << endl
......
......@@ -35,8 +35,6 @@ using namespace std;
#include "ExternalFunctionsTable.hh"
#include "ExprNode.hh"
#define CONSTANTS_PRECISION 16
class DataTree
{
friend class ExprNode;
......@@ -118,6 +116,8 @@ protected:
static string packageDir(const string &package);
private:
const static int constants_precision{16};
using node_list_t = list<expr_t>;
//! The list of nodes
node_list_t node_list;
......@@ -329,7 +329,7 @@ DataTree::AddPossiblyNegativeConstant(double v)
neg = true;
}
ostringstream ost;
ost << setprecision(CONSTANTS_PRECISION) << v;
ost << setprecision(constants_precision) << v;
expr_t cnode = AddNonNegativeConstant(ost.str());
......
......@@ -4860,7 +4860,7 @@ DynamicModel::testTrendDerivativesEqualToZero(const eval_context_t &eval_context
equations[eq]->get_arg2());
// Do not run the test if the term inside the log is zero
if (fabs(homogeneq->eval(eval_context)) > ZERO_BAND)
if (fabs(homogeneq->eval(eval_context)) > zero_band)
{
expr_t testeq = AddLog(homogeneq); // F = log(lhs-rhs)
testeq = testeq->getDerivative(it->second); // d F / d Trend
......@@ -4869,7 +4869,7 @@ DynamicModel::testTrendDerivativesEqualToZero(const eval_context_t &eval_context
if (symbol_table.getType(endogit->first.first) == eEndogenous)
{
double nearZero = testeq->getDerivative(endogit->second)->eval(eval_context); // eval d F / d Trend d Endog
if (fabs(nearZero) > ZERO_BAND)
if (fabs(nearZero) > zero_band)
{
cerr << "WARNING: trends not compatible with balanced growth path; the second-order cross partial of equation " << eq + 1 << " (line "
<< equations_lineno[eq] << ") w.r.t. trend variable "
......@@ -5651,10 +5651,6 @@ DynamicModel::dynamicOnlyEquationsNbr() const
return eqs.size();
}
#ifndef PRIVATE_BUFFER_SIZE
# define PRIVATE_BUFFER_SIZE 1024
#endif
bool
DynamicModel::isChecksumMatching(const string &basename) const
{
......@@ -5712,10 +5708,11 @@ DynamicModel::isChecksumMatching(const string &basename) const
}
}
char private_buffer[PRIVATE_BUFFER_SIZE];
const size_t private_buffer_size{1024};
char private_buffer[private_buffer_size];
while (buffer)
{
buffer.get(private_buffer, PRIVATE_BUFFER_SIZE);
buffer.get(private_buffer, private_buffer_size);
result.process_bytes(private_buffer, strlen(private_buffer));
}
......
......@@ -21,7 +21,6 @@
#define _DYNAMICMODEL_HH
using namespace std;
#define ZERO_BAND 1e-8
#include <fstream>
#include <boost/crc.hpp>
......@@ -32,6 +31,8 @@ using namespace std;
class DynamicModel : public ModelTree
{
private:
constexpr static double zero_band{1e-8};
//! Stores equations declared as [static]
/*! They will be used in toStatic() to replace equations marked as [dynamic] */
vector<BinaryOpNode *> static_only_equations;
......
......@@ -2162,7 +2162,7 @@ UnaryOpNode::computeTemporaryTerms(map<expr_t, pair<int, NodeTreeReference>> &re
else
{
reference_count[this2] = { it->second.first + 1, it->second.second };
if (reference_count[this2].first * cost(temp_terms_map, is_matlab) > MIN_COST(is_matlab))
if (reference_count[this2].first * cost(temp_terms_map, is_matlab) > min_cost(is_matlab))
temp_terms_map[reference_count[this2].second].insert(this2);
}
}
......@@ -2186,7 +2186,7 @@ UnaryOpNode::computeTemporaryTerms(map<expr_t, int> &reference_count,
else
{
reference_count[this2]++;
if (reference_count[this2] * cost(temporary_terms, false) > MIN_COST_C)
if (reference_count[this2] * cost(temporary_terms, false) > min_cost_c)
{
temporary_terms.insert(this2);
v_temporary_terms[first_occurence[this2].first][first_occurence[this2].second].insert(this2);
......@@ -3763,7 +3763,7 @@ BinaryOpNode::cost(int cost, bool is_matlab) const
return cost + 990;
case oPower:
case oPowerDeriv:
return cost + (MIN_COST_MATLAB/2+1);
return cost + (min_cost_matlab/2+1);
case oEqual:
return cost;
}
......@@ -3790,7 +3790,7 @@ BinaryOpNode::cost(int cost, bool is_matlab) const
case oPower:
return cost + 520;
case oPowerDeriv:
return cost + (MIN_COST_C/2+1);;
return cost + (min_cost_c/2+1);;
case oEqual:
return cost;
}
......@@ -3819,7 +3819,7 @@ BinaryOpNode::computeTemporaryTerms(map<expr_t, pair<int, NodeTreeReference>> &r
and declare it as a temporary term if it is too costly (except if it is
an equal node: we don't want them as temporary terms) */
reference_count[this2] = { it->second.first + 1, it->second.second };;
if (reference_count[this2].first * cost(temp_terms_map, is_matlab) > MIN_COST(is_matlab)
if (reference_count[this2].first * cost(temp_terms_map, is_matlab) > min_cost(is_matlab)
&& op_code != oEqual)
temp_terms_map[reference_count[this2].second].insert(this2);
}
......@@ -3845,7 +3845,7 @@ BinaryOpNode::computeTemporaryTerms(map<expr_t, int> &reference_count,
else
{
reference_count[this2]++;
if (reference_count[this2] * cost(temporary_terms, false) > MIN_COST_C
if (reference_count[this2] * cost(temporary_terms, false) > min_cost_c
&& op_code != oEqual)
{
temporary_terms.insert(this2);
......@@ -3870,9 +3870,9 @@ BinaryOpNode::eval_opcode(double v1, BinaryOpcode op_code, double v2, int derivO
case oPower:
return (pow(v1, v2));
case oPowerDeriv:
if (fabs(v1) < NEAR_ZERO && v2 > 0
if (fabs(v1) < near_zero && v2 > 0
&& derivOrder > v2
&& fabs(v2-nearbyint(v2)) < NEAR_ZERO)
&& fabs(v2-nearbyint(v2)) < near_zero)
return 0.0;
else
{
......@@ -5320,7 +5320,7 @@ TrinaryOpNode::computeTemporaryTerms(map<expr_t, pair<int, NodeTreeReference>> &
// If the node has already been encountered, increment its ref count
// and declare it as a temporary term if it is too costly
reference_count[this2] = { it->second.first + 1, it->second.second };;
if (reference_count[this2].first * cost(temp_terms_map, is_matlab) > MIN_COST(is_matlab))
if (reference_count[this2].first * cost(temp_terms_map, is_matlab) > min_cost(is_matlab))
temp_terms_map[reference_count[this2].second].insert(this2);
}
}
......@@ -5346,7 +5346,7 @@ TrinaryOpNode::computeTemporaryTerms(map<expr_t, int> &reference_count,
else
{
reference_count[this2]++;
if (reference_count[this2] * cost(temporary_terms, false) > MIN_COST_C)
if (reference_count[this2] * cost(temporary_terms, false) > min_cost_c)
{
temporary_terms.insert(this2);
v_temporary_terms[first_occurence[this2].first][first_occurence[this2].second].insert(this2);
......
......@@ -129,11 +129,6 @@ enum ExprNodeOutputType
#define LEFT_PAR(output_type) (IS_LATEX(output_type) ? "\\left(" : "(")
#define RIGHT_PAR(output_type) (IS_LATEX(output_type) ? "\\right)" : ")")
// Computing cost above which a node can be declared a temporary term
#define MIN_COST_MATLAB (40*90)
#define MIN_COST_C (40*4)
#define MIN_COST(is_matlab) ((is_matlab) ? MIN_COST_MATLAB : MIN_COST_C)
//! Base class for expression nodes
class ExprNode
{
......@@ -171,6 +166,10 @@ class ExprNode
//! Used for caching of first order derivatives (when non-null)
map<int, expr_t> derivatives;
const static int min_cost_matlab{40*90};
const static int min_cost_c{40*4};
inline static int min_cost(bool is_matlab) { return(is_matlab ? min_cost_matlab : min_cost_c); };
//! Cost of computing current node
/*! Nodes included in temporary_terms are considered having a null cost */
virtual int cost(int cost, bool is_matlab) const;
......
......@@ -39,11 +39,6 @@ using namespace std;
#include "WarningConsolidation.hh"
#include "ExtendedPreprocessorTypes.hh"
// for checksum computation
#ifndef PRIVATE_BUFFER_SIZE
# define PRIVATE_BUFFER_SIZE 1024
#endif
//! The abstract representation of a "mod" file
class ModFile
{
......
......@@ -1126,7 +1126,7 @@ ModelTree::computeTemporaryTerms(bool is_matlab)
{
VariableNode *v = AddVariable(used_local_var);
temporary_terms_mlv[v] = local_variables_table.find(used_local_var)->second;
reference_count[v] = { MIN_COST(is_matlab)+1, eResiduals };
reference_count[v] = { ExprNode::min_cost(is_matlab)+1, eResiduals };
}
map<NodeTreeReference, temporary_terms_t> temp_terms_map;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment