12 #ifndef ONCE_FPARSER_TYPES_H_
13 #define ONCE_FPARSER_TYPES_H_
15 #include "fpconfig.hh"
26 cAtan, cAtan2, cAtanh,
28 cCos, cCosh, cCot, cCsc,
30 cExp, cExp2, cFloor, cIf, cInt, cLog, cLog10, cLog2, cMax, cMin,
31 cPow, cSec, cSin, cSinh, cSqrt, cTan, cTanh,
35 cNeg, cAdd, cSub, cMul, cDiv, cMod,
36 cEqual, cNEqual, cLess, cLessOrEq, cGreater, cGreaterOrEq,
45 #ifdef FP_SUPPORT_OPTIMIZER
65 #ifdef ONCE_FPARSER_H_
76 bool operator<(
const FuncDefinition& rhs)
const
78 for(
unsigned i = 0; i < nameLength; ++i)
80 if(i == rhs.nameLength)
return false;
81 const char c1 = name[i], c2 = rhs.name[i];
82 if(c1 < c2)
return true;
83 if(c2 < c1)
return false;
85 return nameLength < rhs.nameLength;
89 #ifndef FP_DISABLE_EVAL
90 #define FP_EVAL_FUNCTION_ENABLED true
92 #define FP_EVAL_FUNCTION_ENABLED false
96 const FuncDefinition Functions[]=
98 {
"abs", 3, cAbs, 1,
true },
99 {
"acos", 4, cAcos, 1,
true },
100 {
"acosh", 5, cAcosh, 1,
true },
101 {
"asin", 4, cAsin, 1,
true },
102 {
"asinh", 5, cAsinh, 1,
true },
103 {
"atan", 4, cAtan, 1,
true },
104 {
"atan2", 5, cAtan2, 2,
true },
105 {
"atanh", 5, cAtanh, 1,
true },
106 {
"ceil", 4, cCeil, 1,
true },
107 {
"cos", 3, cCos, 1,
true },
108 {
"cosh", 4, cCosh, 1,
true },
109 {
"cot", 3, cCot, 1,
true },
110 {
"csc", 3, cCsc, 1,
true },
111 {
"eval", 4, cEval, 0, FP_EVAL_FUNCTION_ENABLED },
112 {
"exp", 3, cExp, 1,
true },
113 {
"exp2", 4, cExp2, 1,
true },
114 {
"floor", 5, cFloor, 1,
true },
115 {
"if", 2, cIf, 0,
true },
116 {
"int", 3, cInt, 1,
true },
117 {
"log", 3, cLog, 1,
true },
118 {
"log10", 5, cLog10, 1,
true },
119 {
"log2", 4, cLog2, 1,
true },
120 {
"max", 3, cMax, 2,
true },
121 {
"min", 3, cMin, 2,
true },
122 {
"pow", 3, cPow, 2,
true },
123 {
"sec", 3, cSec, 1,
true },
124 {
"sin", 3, cSin, 1,
true },
125 {
"sinh", 4, cSinh, 1,
true },
126 {
"sqrt", 4, cSqrt, 1,
true },
127 {
"tan", 3, cTan, 1,
true },
128 {
"tanh", 4, cTanh, 1,
true }
136 NamePtr(
const char* n,
unsigned l): name(n), nameLength(l) {}
138 inline bool operator<(
const NamePtr& rhs)
const
140 for(
unsigned i = 0; i < nameLength; ++i)
142 if(i == rhs.nameLength)
return false;
143 const char c1 = name[i], c2 = rhs.name[i];
144 if(c1 < c2)
return true;
145 if(c2 < c1)
return false;
147 return nameLength < rhs.nameLength;
153 enum DataType { CONSTANT, UNIT, FUNC_PTR, PARSER_PTR };
164 NameData(DataType t,
const std::string& n): type(t), name(n) {}
166 inline bool operator<(
const NameData& rhs)
const
168 return name < rhs.name;
172 const unsigned FUNC_AMOUNT =
sizeof(Functions)/
sizeof(Functions[0]);
175 inline int compare(
const FuncDefinition& lhs,
const NamePtr& rhs)
177 for(
unsigned i = 0; i < lhs.nameLength; ++i)
179 if(i == rhs.nameLength)
return 1;
180 const char c1 = lhs.name[i], c2 = rhs.name[i];
181 if(c1 < c2)
return -1;
182 if(c2 < c1)
return 1;
184 return lhs.nameLength < rhs.nameLength ? -1 : 0;
187 inline const FuncDefinition* findFunction(
const NamePtr& functionName)
189 const FuncDefinition* first = Functions;
190 const FuncDefinition* last = Functions + FUNC_AMOUNT;
194 const FuncDefinition* middle = first+(last-first)/2;
195 const int comp = compare(*middle, functionName);
196 if(comp == 0)
return middle;
197 if(comp < 0) first = middle+1;
203 #ifndef FP_SUPPORT_ASINH
204 inline double fp_asinh(
double x) {
return log(x + sqrt(x*x + 1)); }
205 inline double fp_acosh(
double x) {
return log(x + sqrt(x*x - 1)); }
206 inline double fp_atanh(
double x) {
return log( (1+x) / (1-x) ) * 0.5; }
208 inline double fp_asinh(
double x) {
return asinh(x); }
209 inline double fp_acosh(
double x) {
return acosh(x); }
210 inline double fp_atanh(
double x) {
return atanh(x); }
211 #endif // FP_SUPPORT_ASINH
214 inline bool FloatEqual(
double a,
double b)
215 {
return fabs(a - b) <= FP_EPSILON; }
217 inline bool FloatEqual(
double a,
double b)
221 inline bool IsIntegerConst(
double a)
222 {
return FloatEqual(a, (
double)(
long)a); }
224 #endif // ONCE_FPARSER_H_
227 #ifdef ONCE_FPARSER_H_
232 struct FunctionParser::Data
234 unsigned referenceCounter;
236 std::string variablesString;
237 std::map<FUNCTIONPARSERTYPES::NamePtr, unsigned> variableRefs;
239 std::set<FUNCTIONPARSERTYPES::NameData> nameData;
240 std::map<FUNCTIONPARSERTYPES::NamePtr,
241 const FUNCTIONPARSERTYPES::NameData*> namePtrs;
249 std::vector<FuncPtrData> FuncPtrs;
250 std::vector<FuncPtrData> FuncParsers;
252 std::vector<unsigned> ByteCode;
253 std::vector<double> Immed;
254 std::vector<double> Stack;
257 Data(): referenceCounter(1),
265 Immed(), Stack(), StackSize(0) {}
267 Data& operator=(
const Data&);
Definition: fpoptimizer.cc:45
Definition: fparser.hh:21