annotate eval.c @ 3730:aae4aed137ea libavcodec

K prefix add SI prefix selftest
author michael
date Sun, 17 Sep 2006 10:22:01 +0000
parents 58483364f021
children 8b8773577dd9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
1 /*
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
2 * simple arithmetic expression evaluator
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
3 *
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
4 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
5 *
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
6 * This library is free software; you can redistribute it and/or
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
7 * modify it under the terms of the GNU Lesser General Public
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
8 * License as published by the Free Software Foundation; either
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
9 * version 2 of the License, or (at your option) any later version.
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
10 *
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
11 * This library is distributed in the hope that it will be useful,
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
14 * Lesser General Public License for more details.
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
15 *
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
16 * You should have received a copy of the GNU Lesser General Public
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
17 * License along with this library; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 2967
diff changeset
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
19 *
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
20 */
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
21
1106
1e39f273ecd6 per file doxy
michaelni
parents: 1057
diff changeset
22 /**
1e39f273ecd6 per file doxy
michaelni
parents: 1057
diff changeset
23 * @file eval.c
1e39f273ecd6 per file doxy
michaelni
parents: 1057
diff changeset
24 * simple arithmetic expression evaluator.
1e39f273ecd6 per file doxy
michaelni
parents: 1057
diff changeset
25 *
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
26 * see http://joe.hotchkiss.com/programming/eval/eval.html
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
27 */
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
28
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
29 #include "avcodec.h"
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
30 #include "mpegvideo.h"
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
31
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
32 #include <stdio.h>
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
33 #include <stdlib.h>
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
34 #include <string.h>
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
35 #include <math.h>
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
36
614
b786f15df503 NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents: 612
diff changeset
37 #ifndef NAN
b786f15df503 NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents: 612
diff changeset
38 #define NAN 0
b786f15df503 NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents: 612
diff changeset
39 #endif
b786f15df503 NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents: 612
diff changeset
40
627
79c43f519d02 undefined M_PI / M_E fix
michaelni
parents: 614
diff changeset
41 #ifndef M_PI
79c43f519d02 undefined M_PI / M_E fix
michaelni
parents: 614
diff changeset
42 #define M_PI 3.14159265358979323846
79c43f519d02 undefined M_PI / M_E fix
michaelni
parents: 614
diff changeset
43 #endif
79c43f519d02 undefined M_PI / M_E fix
michaelni
parents: 614
diff changeset
44
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
45 typedef struct Parser{
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
46 int stack_index;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
47 char *s;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
48 double *const_value;
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
49 const char **const_name; // NULL terminated
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
50 double (**func1)(void *, double a); // NULL terminated
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
51 const char **func1_name; // NULL terminated
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
52 double (**func2)(void *, double a, double b); // NULL terminated
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
53 char **func2_name; // NULL terminated
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
54 void *opaque;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
55 } Parser;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
56
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
57 static double evalExpression(Parser *p);
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
58
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
59 static int strmatch(const char *s, const char *prefix){
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
60 int i;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
61 for(i=0; prefix[i]; i++){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
62 if(prefix[i] != s[i]) return 0;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
63 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
64 return 1;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
65 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
66
3729
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
67 static int8_t si_prefixes['z' - 'E' + 1]={
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
68 ['y'-'E']= -24,
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
69 ['z'-'E']= -21,
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
70 ['a'-'E']= -18,
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
71 ['f'-'E']= -15,
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
72 ['p'-'E']= -12,
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
73 ['n'-'E']= - 9,
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
74 ['u'-'E']= - 6,
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
75 ['m'-'E']= - 3,
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
76 ['c'-'E']= - 2,
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
77 ['d'-'E']= - 1,
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
78 ['h'-'E']= 2,
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
79 ['k'-'E']= 3,
3730
aae4aed137ea K prefix
michael
parents: 3729
diff changeset
80 ['K'-'E']= 3,
3729
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
81 ['M'-'E']= 6,
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
82 ['G'-'E']= 9,
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
83 ['T'-'E']= 12,
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
84 ['P'-'E']= 15,
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
85 ['E'-'E']= 18,
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
86 ['Z'-'E']= 21,
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
87 ['Y'-'E']= 24,
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
88 };
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
89
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
90 static double evalPrimary(Parser *p){
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
91 double d, d2=NAN;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
92 char *next= p->s;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
93 int i;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
94
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
95 /* number */
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
96 d= strtod(p->s, &next);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
97 if(next != p->s){
3729
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
98 if(*next >= 'E' && *next <= 'z'){
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
99 int e= si_prefixes[*next - 'E'];
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
100 if(e){
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
101 if(next[1] == 'i'){
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
102 d*= pow( 2, e/0.3);
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
103 next+=2;
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
104 }else{
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
105 d*= pow(10, e);
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
106 next++;
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
107 }
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
108 }
58483364f021 support SI and some non SI prefixes
michael
parents: 3036
diff changeset
109 }
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
110 p->s= next;
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
111 return d;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
112 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
113
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
114 /* named constants */
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
115 for(i=0; p->const_name && p->const_name[i]; i++){
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
116 if(strmatch(p->s, p->const_name[i])){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
117 p->s+= strlen(p->const_name[i]);
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
118 return p->const_value[i];
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
119 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
120 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
121
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
122 p->s= strchr(p->s, '(');
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
123 if(p->s==NULL){
1598
932d306bf1dc av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents: 1106
diff changeset
124 av_log(NULL, AV_LOG_ERROR, "Parser: missing ( in \"%s\"\n", next);
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
125 return NAN;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
126 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
127 p->s++; // "("
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
128 d= evalExpression(p);
1815
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
129 if(p->s[0]== ','){
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
130 p->s++; // ","
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
131 d2= evalExpression(p);
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
132 }
1815
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
133 if(p->s[0] != ')'){
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
134 av_log(NULL, AV_LOG_ERROR, "Parser: missing ) in \"%s\"\n", next);
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
135 return NAN;
1815
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
136 }
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
137 p->s++; // ")"
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
138
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
139 if( strmatch(next, "sinh" ) ) d= sinh(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
140 else if( strmatch(next, "cosh" ) ) d= cosh(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
141 else if( strmatch(next, "tanh" ) ) d= tanh(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
142 else if( strmatch(next, "sin" ) ) d= sin(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
143 else if( strmatch(next, "cos" ) ) d= cos(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
144 else if( strmatch(next, "tan" ) ) d= tan(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
145 else if( strmatch(next, "exp" ) ) d= exp(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
146 else if( strmatch(next, "log" ) ) d= log(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
147 else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
148 else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
149 else if( strmatch(next, "abs" ) ) d= fabs(d);
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
150 else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
151 else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
152 else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
1815
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
153 else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
154 else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
1815
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
155 else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
156 else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
157 else if( strmatch(next, "(" ) ) d= d;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
158 // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
159 // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
160 else{
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
161 for(i=0; p->func1_name && p->func1_name[i]; i++){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
162 if(strmatch(next, p->func1_name[i])){
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
163 return p->func1[i](p->opaque, d);
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
164 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
165 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
166
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
167 for(i=0; p->func2_name && p->func2_name[i]; i++){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
168 if(strmatch(next, p->func2_name[i])){
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
169 return p->func2[i](p->opaque, d, d2);
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
170 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
171 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
172
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
173 av_log(NULL, AV_LOG_ERROR, "Parser: unknown function in \"%s\"\n", next);
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
174 return NAN;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
175 }
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
176
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
177 return d;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
178 }
2436
86d14aebd527 simplify
michael
parents: 2434
diff changeset
179
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
180 static double evalPow(Parser *p){
2436
86d14aebd527 simplify
michael
parents: 2434
diff changeset
181 int sign= (*p->s == '+') - (*p->s == '-');
86d14aebd527 simplify
michael
parents: 2434
diff changeset
182 p->s += sign&1;
86d14aebd527 simplify
michael
parents: 2434
diff changeset
183 return (sign|1) * evalPrimary(p);
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
184 }
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
185
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
186 static double evalFactor(Parser *p){
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
187 double ret= evalPow(p);
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
188 while(p->s[0]=='^'){
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
189 p->s++;
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
190 ret= pow(ret, evalPow(p));
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
191 }
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
192 return ret;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
193 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
194
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
195 static double evalTerm(Parser *p){
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
196 double ret= evalFactor(p);
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
197 while(p->s[0]=='*' || p->s[0]=='/'){
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
198 if(*p->s++ == '*') ret*= evalFactor(p);
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
199 else ret/= evalFactor(p);
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
200 }
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
201 return ret;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
202 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
203
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
204 static double evalExpression(Parser *p){
2436
86d14aebd527 simplify
michael
parents: 2434
diff changeset
205 double ret= 0;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
206
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
207 if(p->stack_index <= 0) //protect against stack overflows
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
208 return NAN;
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
209 p->stack_index--;
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
210
2436
86d14aebd527 simplify
michael
parents: 2434
diff changeset
211 do{
86d14aebd527 simplify
michael
parents: 2434
diff changeset
212 ret += evalTerm(p);
86d14aebd527 simplify
michael
parents: 2434
diff changeset
213 }while(*p->s == '+' || *p->s == '-');
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
214
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
215 p->stack_index++;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
216
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
217 return ret;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
218 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
219
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
220 double ff_eval(char *s, double *const_value, const char **const_name,
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
221 double (**func1)(void *, double), const char **func1_name,
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
222 double (**func2)(void *, double, double), char **func2_name,
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
223 void *opaque){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
224 Parser p;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
225
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
226 p.stack_index=100;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
227 p.s= s;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
228 p.const_value= const_value;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
229 p.const_name = const_name;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
230 p.func1 = func1;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
231 p.func1_name = func1_name;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
232 p.func2 = func2;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
233 p.func2_name = func2_name;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
234 p.opaque = opaque;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
235
2434
24aa9209e8b0 simplify
michael
parents: 2433
diff changeset
236 return evalExpression(&p);
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
237 }
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
238
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
239 #ifdef TEST
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
240 #undef printf
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
241 static double const_values[]={
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
242 M_PI,
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
243 M_E,
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
244 0
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
245 };
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
246 static const char *const_names[]={
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
247 "PI",
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
248 "E",
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
249 0
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
250 };
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
251 main(){
2436
86d14aebd527 simplify
michael
parents: 2434
diff changeset
252 int i;
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
253 printf("%f == 12.7\n", ff_eval("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
3730
aae4aed137ea K prefix
michael
parents: 3729
diff changeset
254 printf("%f == 0.931322575\n", ff_eval("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2436
diff changeset
255
2436
86d14aebd527 simplify
michael
parents: 2434
diff changeset
256 for(i=0; i<1050; i++){
86d14aebd527 simplify
michael
parents: 2434
diff changeset
257 START_TIMER
86d14aebd527 simplify
michael
parents: 2434
diff changeset
258 ff_eval("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL);
86d14aebd527 simplify
michael
parents: 2434
diff changeset
259 STOP_TIMER("ff_eval")
86d14aebd527 simplify
michael
parents: 2434
diff changeset
260 }
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
261 }
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
262 #endif