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