3aa102be |
/* |
3cedeeca |
* Copyright (c) 2002-2006 Michael Niedermayer <michaelni@gmx.at> |
b5c5a86b |
* Copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org> |
3aa102be |
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
3aa102be |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
3aa102be |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
3aa102be |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
3aa102be |
*/
|
983e3246 |
/** |
ba87f080 |
* @file |
983e3246 |
* simple arithmetic expression evaluator.
* |
3aa102be |
* see http://joe.hotchkiss.com/programming/eval/eval.html
*/
|
dc25d79f |
#include "avutil.h" |
9a88c1df |
#include "eval.h"
|
0cd28d94 |
typedef struct Parser { |
2b65bb45 |
const AVClass *class; |
3aa102be |
int stack_index;
char *s; |
9711439b |
const double *const_values;
const char * const *const_names; // NULL terminated
double (* const *funcs1)(void *, double a); // NULL terminated
const char * const *func1_names; // NULL terminated
double (* const *funcs2)(void *, double a, double b); // NULL terminated
const char * const *func2_names; // NULL terminated |
3aa102be |
void *opaque; |
2b65bb45 |
int log_offset;
void *log_ctx; |
835954e3 |
#define VARS 10
double var[VARS]; |
3aa102be |
} Parser;
|
2b65bb45 |
static const AVClass class = { "Eval", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(Parser,log_offset), offsetof(Parser,log_ctx) };
|
0cd28d94 |
static const int8_t si_prefixes['z' - 'E' + 1] = { |
95c99430 |
['y'-'E']= -24,
['z'-'E']= -21,
['a'-'E']= -18,
['f'-'E']= -15,
['p'-'E']= -12,
['n'-'E']= - 9,
['u'-'E']= - 6,
['m'-'E']= - 3,
['c'-'E']= - 2,
['d'-'E']= - 1,
['h'-'E']= 2,
['k'-'E']= 3,
['K'-'E']= 3,
['M'-'E']= 6,
['G'-'E']= 9,
['T'-'E']= 12,
['P'-'E']= 15,
['E'-'E']= 18,
['Z'-'E']= 21,
['Y'-'E']= 24,
};
|
0cd28d94 |
double av_strtod(const char *numstr, char **tail)
{ |
95c99430 |
double d;
char *next; |
9132f2ad |
if(numstr[0]=='0' && (numstr[1]|0x20)=='x') { |
940a55cc |
d = strtoul(numstr, &next, 16); |
9132f2ad |
} else
d = strtod(numstr, &next); |
95c99430 |
/* if parsing succeeded, check for and interpret postfixes */ |
e877eaac |
if (next!=numstr) { |
0cd28d94 |
if (*next >= 'E' && *next <= 'z') { |
95c99430 |
int e= si_prefixes[*next - 'E']; |
0cd28d94 |
if (e) {
if (next[1] == 'i') { |
95c99430 |
d*= pow( 2, e/0.3);
next+=2; |
0cd28d94 |
} else { |
95c99430 |
d*= pow(10, e);
next++;
}
}
}
|
0cd28d94 |
if (*next=='B') { |
95c99430 |
d*=8; |
a02142a5 |
next++; |
95c99430 |
}
}
/* if requested, fill in tail with the position after the last parsed
character */
if (tail)
*tail = next;
return d;
}
|
4cabef0a |
#define IS_IDENTIFIER_CHAR(c) ((c) - '0' <= 9U || (c) - 'a' <= 25U || (c) - 'A' <= 25U || (c) == '_')
|
0cd28d94 |
static int strmatch(const char *s, const char *prefix)
{ |
3aa102be |
int i; |
0cd28d94 |
for (i=0; prefix[i]; i++) {
if (prefix[i] != s[i]) return 0; |
3aa102be |
} |
4cabef0a |
/* return 1 only if the s identifier is terminated */
return !IS_IDENTIFIER_CHAR(s[i]); |
3aa102be |
}
|
a3731cad |
struct AVExpr { |
85b4eb08 |
enum {
e_value, e_const, e_func0, e_func1, e_func2, |
20fcd079 |
e_squish, e_gauss, e_ld, e_isnan, |
85b4eb08 |
e_mod, e_max, e_min, e_eq, e_gt, e_gte,
e_pow, e_mul, e_div, e_add, |
25601bc5 |
e_last, e_st, e_while, e_floor, e_ceil, e_trunc, |
3c2c52ba |
e_sqrt, e_not, |
85b4eb08 |
} type;
double value; // is sign in other types
union {
int const_index;
double (*func0)(double);
double (*func1)(void *, double);
double (*func2)(void *, double, double);
} a; |
a3731cad |
struct AVExpr *param[2]; |
85b4eb08 |
};
|
0cd28d94 |
static double eval_expr(Parser *p, AVExpr *e)
{ |
85b4eb08 |
switch (e->type) {
case e_value: return e->value; |
9711439b |
case e_const: return e->value * p->const_values[e->a.const_index]; |
85b4eb08 |
case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0]));
case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); } |
f66e4f5f |
case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)]; |
20fcd079 |
case e_isnan: return e->value * !!isnan(eval_expr(p, e->param[0])); |
25601bc5 |
case e_floor: return e->value * floor(eval_expr(p, e->param[0]));
case e_ceil : return e->value * ceil (eval_expr(p, e->param[0]));
case e_trunc: return e->value * trunc(eval_expr(p, e->param[0])); |
e5272e72 |
case e_sqrt: return e->value * sqrt (eval_expr(p, e->param[0])); |
3c2c52ba |
case e_not: return e->value * eval_expr(p, e->param[0]) == 0; |
45ee0e32 |
case e_while: { |
63a547ea |
double d = NAN; |
0cd28d94 |
while (eval_expr(p, e->param[0])) |
45ee0e32 |
d=eval_expr(p, e->param[1]);
return d;
} |
85b4eb08 |
default: {
double d = eval_expr(p, e->param[0]);
double d2 = eval_expr(p, e->param[1]);
switch (e->type) {
case e_mod: return e->value * (d - floor(d/d2)*d2);
case e_max: return e->value * (d > d2 ? d : d2);
case e_min: return e->value * (d < d2 ? d : d2);
case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
case e_pow: return e->value * pow(d, d2);
case e_mul: return e->value * (d * d2);
case e_div: return e->value * (d / d2);
case e_add: return e->value * (d + d2); |
72523c7a |
case e_last:return e->value * d2; |
f66e4f5f |
case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2); |
85b4eb08 |
}
}
}
return NAN;
}
|
9ace13b4 |
static int parse_expr(AVExpr **e, Parser *p); |
85b4eb08 |
|
d2af7205 |
void av_expr_free(AVExpr *e) |
0cd28d94 |
{ |
85b4eb08 |
if (!e) return; |
d2af7205 |
av_expr_free(e->param[0]);
av_expr_free(e->param[1]); |
85b4eb08 |
av_freep(&e);
}
|
9ace13b4 |
static int parse_primary(AVExpr **e, Parser *p)
{ |
0cd28d94 |
AVExpr *d = av_mallocz(sizeof(AVExpr)); |
2f62224b |
char *next = p->s, *s0 = p->s; |
9ace13b4 |
int ret, i; |
3aa102be |
|
0345f36b |
if (!d) |
9ace13b4 |
return AVERROR(ENOMEM); |
0345f36b |
|
3aa102be |
/* number */ |
85b4eb08 |
d->value = av_strtod(p->s, &next); |
0cd28d94 |
if (next != p->s) { |
85b4eb08 |
d->type = e_value; |
3aa102be |
p->s= next; |
9ace13b4 |
*e = d;
return 0; |
3aa102be |
} |
85b4eb08 |
d->value = 1; |
115329f1 |
|
3aa102be |
/* named constants */ |
0cd28d94 |
for (i=0; p->const_names && p->const_names[i]; i++) {
if (strmatch(p->s, p->const_names[i])) { |
9711439b |
p->s+= strlen(p->const_names[i]); |
85b4eb08 |
d->type = e_const;
d->a.const_index = i; |
9ace13b4 |
*e = d;
return 0; |
3aa102be |
}
} |
115329f1 |
|
3aa102be |
p->s= strchr(p->s, '('); |
0cd28d94 |
if (p->s==NULL) { |
2f62224b |
av_log(p, AV_LOG_ERROR, "Undefined constant or missing '(' in '%s'\n", s0); |
e6b12001 |
p->s= next; |
d2af7205 |
av_expr_free(d); |
9ace13b4 |
return AVERROR(EINVAL); |
3aa102be |
}
p->s++; // "(" |
85b4eb08 |
if (*next == '(') { // special case do-nothing
av_freep(&d); |
9ace13b4 |
if ((ret = parse_expr(&d, p)) < 0)
return ret; |
0cd28d94 |
if (p->s[0] != ')') { |
2f62224b |
av_log(p, AV_LOG_ERROR, "Missing ')' in '%s'\n", s0); |
d2af7205 |
av_expr_free(d); |
9ace13b4 |
return AVERROR(EINVAL); |
85b4eb08 |
}
p->s++; // ")" |
9ace13b4 |
*e = d;
return 0;
}
if ((ret = parse_expr(&(d->param[0]), p)) < 0) { |
d2af7205 |
av_expr_free(d); |
9ace13b4 |
return ret; |
85b4eb08 |
} |
0cd28d94 |
if (p->s[0]== ',') { |
69f5de18 |
p->s++; // "," |
9ace13b4 |
parse_expr(&d->param[1], p); |
3aa102be |
} |
0cd28d94 |
if (p->s[0] != ')') { |
2f62224b |
av_log(p, AV_LOG_ERROR, "Missing ')' or too many args in '%s'\n", s0); |
d2af7205 |
av_expr_free(d); |
9ace13b4 |
return AVERROR(EINVAL); |
69f5de18 |
}
p->s++; // ")" |
115329f1 |
|
85b4eb08 |
d->type = e_func0; |
0cd28d94 |
if (strmatch(next, "sinh" )) d->a.func0 = sinh;
else if (strmatch(next, "cosh" )) d->a.func0 = cosh;
else if (strmatch(next, "tanh" )) d->a.func0 = tanh;
else if (strmatch(next, "sin" )) d->a.func0 = sin;
else if (strmatch(next, "cos" )) d->a.func0 = cos;
else if (strmatch(next, "tan" )) d->a.func0 = tan;
else if (strmatch(next, "atan" )) d->a.func0 = atan;
else if (strmatch(next, "asin" )) d->a.func0 = asin;
else if (strmatch(next, "acos" )) d->a.func0 = acos;
else if (strmatch(next, "exp" )) d->a.func0 = exp;
else if (strmatch(next, "log" )) d->a.func0 = log;
else if (strmatch(next, "abs" )) d->a.func0 = fabs;
else if (strmatch(next, "squish")) d->type = e_squish;
else if (strmatch(next, "gauss" )) d->type = e_gauss;
else if (strmatch(next, "mod" )) d->type = e_mod;
else if (strmatch(next, "max" )) d->type = e_max;
else if (strmatch(next, "min" )) d->type = e_min;
else if (strmatch(next, "eq" )) d->type = e_eq;
else if (strmatch(next, "gte" )) d->type = e_gte;
else if (strmatch(next, "gt" )) d->type = e_gt;
else if (strmatch(next, "lte" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
else if (strmatch(next, "lt" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
else if (strmatch(next, "ld" )) d->type = e_ld; |
20fcd079 |
else if (strmatch(next, "isnan" )) d->type = e_isnan; |
0cd28d94 |
else if (strmatch(next, "st" )) d->type = e_st;
else if (strmatch(next, "while" )) d->type = e_while; |
25601bc5 |
else if (strmatch(next, "floor" )) d->type = e_floor;
else if (strmatch(next, "ceil" )) d->type = e_ceil;
else if (strmatch(next, "trunc" )) d->type = e_trunc; |
e5272e72 |
else if (strmatch(next, "sqrt" )) d->type = e_sqrt; |
3c2c52ba |
else if (strmatch(next, "not" )) d->type = e_not; |
d58ed64a |
else if (strmatch(next, "pow" )) d->type = e_pow; |
85b4eb08 |
else { |
0cd28d94 |
for (i=0; p->func1_names && p->func1_names[i]; i++) {
if (strmatch(next, p->func1_names[i])) { |
9711439b |
d->a.func1 = p->funcs1[i]; |
85b4eb08 |
d->type = e_func1; |
9ace13b4 |
*e = d;
return 0; |
3aa102be |
}
}
|
0cd28d94 |
for (i=0; p->func2_names && p->func2_names[i]; i++) {
if (strmatch(next, p->func2_names[i])) { |
9711439b |
d->a.func2 = p->funcs2[i]; |
85b4eb08 |
d->type = e_func2; |
9ace13b4 |
*e = d;
return 0; |
3aa102be |
}
}
|
2f62224b |
av_log(p, AV_LOG_ERROR, "Unknown function in '%s'\n", s0); |
d2af7205 |
av_expr_free(d); |
9ace13b4 |
return AVERROR(EINVAL); |
3aa102be |
} |
b349fde1 |
|
9ace13b4 |
*e = d;
return 0; |
115329f1 |
} |
80a49958 |
|
0cd28d94 |
static AVExpr *new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
{
AVExpr *e = av_mallocz(sizeof(AVExpr)); |
0345f36b |
if (!e)
return NULL; |
a98f4515 |
e->type =type ;
e->value =value ;
e->param[0] =p0 ;
e->param[1] =p1 ;
return e;
}
|
9ace13b4 |
static int parse_pow(AVExpr **e, Parser *p, int *sign)
{ |
2c409cc7 |
*sign= (*p->s == '+') - (*p->s == '-');
p->s += *sign&1; |
9ace13b4 |
return parse_primary(e, p); |
3aa102be |
}
|
9ace13b4 |
static int parse_factor(AVExpr **e, Parser *p)
{
int sign, sign2, ret;
AVExpr *e0, *e1, *e2;
if ((ret = parse_pow(&e0, p, &sign)) < 0)
return ret; |
3aa102be |
while(p->s[0]=='^'){ |
9ace13b4 |
e1 = e0; |
3aa102be |
p->s++; |
9ace13b4 |
if ((ret = parse_pow(&e2, p, &sign2)) < 0) { |
d2af7205 |
av_expr_free(e1); |
9ace13b4 |
return ret;
}
e0 = new_eval_expr(e_pow, 1, e1, e2);
if (!e0) { |
d2af7205 |
av_expr_free(e1);
av_expr_free(e2); |
9ace13b4 |
return AVERROR(ENOMEM);
}
if (e0->param[1]) e0->param[1]->value *= (sign2|1); |
3aa102be |
} |
9ace13b4 |
if (e0) e0->value *= (sign|1);
*e = e0;
return 0; |
3aa102be |
}
|
9ace13b4 |
static int parse_term(AVExpr **e, Parser *p)
{
int ret;
AVExpr *e0, *e1, *e2;
if ((ret = parse_factor(&e0, p)) < 0)
return ret; |
0cd28d94 |
while (p->s[0]=='*' || p->s[0]=='/') { |
a98f4515 |
int c= *p->s++; |
9ace13b4 |
e1 = e0;
if ((ret = parse_factor(&e2, p)) < 0) { |
d2af7205 |
av_expr_free(e1); |
9ace13b4 |
return ret;
}
e0 = new_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2);
if (!e0) { |
d2af7205 |
av_expr_free(e1);
av_expr_free(e2); |
9ace13b4 |
return AVERROR(ENOMEM);
} |
3aa102be |
} |
9ace13b4 |
*e = e0;
return 0; |
3aa102be |
}
|
9ace13b4 |
static int parse_subexpr(AVExpr **e, Parser *p)
{
int ret;
AVExpr *e0, *e1, *e2;
if ((ret = parse_term(&e0, p)) < 0)
return ret; |
0cd28d94 |
while (*p->s == '+' || *p->s == '-') { |
9ace13b4 |
e1 = e0;
if ((ret = parse_term(&e2, p)) < 0) { |
d2af7205 |
av_expr_free(e1); |
9ace13b4 |
return ret;
}
e0 = new_eval_expr(e_add, 1, e1, e2);
if (!e0) { |
d2af7205 |
av_expr_free(e1);
av_expr_free(e2); |
9ace13b4 |
return AVERROR(ENOMEM);
} |
835954e3 |
};
|
9ace13b4 |
*e = e0;
return 0; |
835954e3 |
}
|
9ace13b4 |
static int parse_expr(AVExpr **e, Parser *p)
{
int ret;
AVExpr *e0, *e1, *e2; |
0cd28d94 |
if (p->stack_index <= 0) //protect against stack overflows |
9ace13b4 |
return AVERROR(EINVAL); |
67e11730 |
p->stack_index--;
|
9ace13b4 |
if ((ret = parse_subexpr(&e0, p)) < 0)
return ret; |
0cd28d94 |
while (*p->s == ';') { |
63b16303 |
p->s++; |
9ace13b4 |
e1 = e0;
if ((ret = parse_subexpr(&e2, p)) < 0) { |
d2af7205 |
av_expr_free(e1); |
9ace13b4 |
return ret;
}
e0 = new_eval_expr(e_last, 1, e1, e2);
if (!e0) { |
d2af7205 |
av_expr_free(e1);
av_expr_free(e2); |
9ace13b4 |
return AVERROR(ENOMEM);
} |
85b4eb08 |
}; |
67e11730 |
p->stack_index++; |
9ace13b4 |
*e = e0;
return 0; |
3aa102be |
}
|
0cd28d94 |
static int verify_expr(AVExpr *e)
{ |
85b4eb08 |
if (!e) return 0;
switch (e->type) {
case e_value:
case e_const: return 1;
case e_func0:
case e_func1:
case e_squish: |
835954e3 |
case e_ld: |
20fcd079 |
case e_gauss: |
25601bc5 |
case e_isnan:
case e_floor:
case e_ceil:
case e_trunc: |
e5272e72 |
case e_sqrt: |
3c2c52ba |
case e_not: |
25601bc5 |
return verify_expr(e->param[0]); |
85b4eb08 |
default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
}
}
|
d2af7205 |
int av_expr_parse(AVExpr **expr, const char *s, |
9711439b |
const char * const *const_names,
const char * const *func1_names, double (* const *funcs1)(void *, double),
const char * const *func2_names, double (* const *funcs2)(void *, double, double), |
b851dd9e |
int log_offset, void *log_ctx) |
2b65bb45 |
{ |
3aa102be |
Parser p; |
073f6d5b |
AVExpr *e = NULL; |
0314dead |
char *w = av_malloc(strlen(s) + 1);
char *wp = w; |
7ec9b9be |
const char *s0 = s; |
9ace13b4 |
int ret = 0; |
0314dead |
if (!w) |
9ace13b4 |
return AVERROR(ENOMEM); |
8cd68d80 |
while (*s)
if (!isspace(*s++)) *wp++ = s[-1];
*wp++ = 0; |
115329f1 |
|
2b65bb45 |
p.class = &class; |
67e11730 |
p.stack_index=100; |
8cd68d80 |
p.s= w; |
9711439b |
p.const_names = const_names;
p.funcs1 = funcs1;
p.func1_names = func1_names;
p.funcs2 = funcs2;
p.func2_names = func2_names; |
2b65bb45 |
p.log_offset = log_offset;
p.log_ctx = log_ctx; |
115329f1 |
|
9ace13b4 |
if ((ret = parse_expr(&e, &p)) < 0)
goto end; |
7ec9b9be |
if (*p.s) { |
2649439b |
av_expr_free(e); |
7ec9b9be |
av_log(&p, AV_LOG_ERROR, "Invalid chars '%s' at the end of expression '%s'\n", p.s, s0);
ret = AVERROR(EINVAL);
goto end;
} |
85b4eb08 |
if (!verify_expr(e)) { |
d2af7205 |
av_expr_free(e); |
9ace13b4 |
ret = AVERROR(EINVAL);
goto end; |
85b4eb08 |
} |
9ace13b4 |
*expr = e; |
0314dead |
end:
av_free(w); |
9ace13b4 |
return ret; |
85b4eb08 |
}
|
d2af7205 |
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque) |
0cd28d94 |
{ |
85b4eb08 |
Parser p;
|
9711439b |
p.const_values = const_values; |
85b4eb08 |
p.opaque = opaque;
return eval_expr(&p, e);
}
|
d2af7205 |
int av_expr_parse_and_eval(double *d, const char *s, |
9711439b |
const char * const *const_names, const double *const_values,
const char * const *func1_names, double (* const *funcs1)(void *, double),
const char * const *func2_names, double (* const *funcs2)(void *, double, double), |
b851dd9e |
void *opaque, int log_offset, void *log_ctx) |
2b65bb45 |
{ |
9ace13b4 |
AVExpr *e = NULL; |
d2af7205 |
int ret = av_expr_parse(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx); |
9ace13b4 |
if (ret < 0) {
*d = NAN;
return ret;
} |
d2af7205 |
*d = av_expr_eval(e, const_values, opaque);
av_expr_free(e); |
9ace13b4 |
return isnan(*d) ? AVERROR(EINVAL) : 0; |
3aa102be |
} |
b349fde1 |
|
d2af7205 |
#if FF_API_OLD_EVAL_NAMES
int av_parse_expr(AVExpr **expr, const char *s,
const char * const *const_names,
const char * const *func1_names, double (* const *funcs1)(void *, double),
const char * const *func2_names, double (* const *funcs2)(void *, double, double),
int log_offset, void *log_ctx)
{
return av_expr_parse(expr, s, const_names, func1_names, funcs1, func2_names, funcs2,
log_offset, log_ctx);
}
double av_eval_expr(AVExpr *e, const double *const_values, void *opaque)
{
return av_expr_eval(e, const_values, opaque);
}
int av_parse_and_eval_expr(double *res, const char *s,
const char * const *const_names, const double *const_values,
const char * const *func1_names, double (* const *funcs1)(void *, double),
const char * const *func2_names, double (* const *funcs2)(void *, double, double),
void *opaque, int log_offset, void *log_ctx)
{
return av_expr_parse_and_eval(res, s, const_names, const_values, func1_names, funcs1, func2_names, funcs2,
opaque, log_offset, log_ctx);
}
void av_free_expr(AVExpr *e)
{
av_expr_free(e);
}
#endif /* FF_API_OLD_EVAL_NAMES */
|
b349fde1 |
#ifdef TEST |
115329f1 |
#undef printf |
0cd28d94 |
static double const_values[] = { |
b349fde1 |
M_PI,
M_E,
0
}; |
b851dd9e |
|
0cd28d94 |
static const char *const_names[] = { |
b349fde1 |
"PI",
"E",
0
}; |
b851dd9e |
|
0cd28d94 |
int main(void)
{ |
80a49958 |
int i; |
9ace13b4 |
double d; |
eb928286 |
const char **expr, *exprs[] = {
"", |
63b16303 |
"1;2", |
8bb12e71 |
"-20",
"-PI",
"+PI", |
eb928286 |
"1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
"80G/80Gi"
"1k",
"1Gi",
"1gi",
"1GiFoo",
"1k+1k",
"1Gi*3foo",
"foo",
"foo(",
"foo()",
"foo)",
"sin",
"sin(",
"sin()",
"sin)",
"sin 10",
"sin(1,2,3)",
"sin(1 )",
"1",
"1foo",
"bar + PI + E + 100f*2 + foo",
"13k + 12f - foo(1, 2)",
"1gi",
"1Gi", |
2b59fbe9 |
"st(0, 123)",
"st(1, 123); ld(1)",
/* compute 1+2+...+N */
"st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
/* compute Fib(N) */
"st(1, 1); st(2, 2); st(0, 1); while(lte(ld(0),10), st(3, ld(1)+ld(2)); st(1, ld(2)); st(2, ld(3)); st(0, ld(0)+1)); ld(3)",
"while(0, 10)",
"st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))", |
20fcd079 |
"isnan(1)",
"isnan(NAN)", |
25601bc5 |
"floor(NAN)",
"floor(123.123)",
"floor(-123.123)",
"trunc(123.123)",
"trunc(-123.123)",
"ceil(123.123)",
"ceil(-123.123)", |
e5272e72 |
"sqrt(1764)",
"sqrt(-1)", |
3c2c52ba |
"not(1)",
"not(NAN)",
"not(0)", |
d58ed64a |
"pow(0,1.23)",
"pow(PI,1.23)",
"PI^1.23",
"pow(-1,1.23)", |
eb928286 |
NULL
};
for (expr = exprs; *expr; expr++) {
printf("Evaluating '%s'\n", *expr); |
d2af7205 |
av_expr_parse_and_eval(&d, *expr, |
eb928286 |
const_names, const_values,
NULL, NULL, NULL, NULL, NULL, 0, NULL);
printf("'%s' -> %f\n\n", *expr, d);
}
|
d2af7205 |
av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", |
b851dd9e |
const_names, const_values,
NULL, NULL, NULL, NULL, NULL, 0, NULL); |
9ace13b4 |
printf("%f == 12.7\n", d); |
d2af7205 |
av_expr_parse_and_eval(&d, "80G/80Gi", |
b851dd9e |
const_names, const_values, |
79513857 |
NULL, NULL, NULL, NULL, NULL, 0, NULL); |
9ace13b4 |
printf("%f == 0.931322575\n", d); |
115329f1 |
|
0cd28d94 |
for (i=0; i<1050; i++) { |
80a49958 |
START_TIMER |
d2af7205 |
av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", |
b851dd9e |
const_names, const_values,
NULL, NULL, NULL, NULL, NULL, 0, NULL); |
d2af7205 |
STOP_TIMER("av_expr_parse_and_eval") |
80a49958 |
} |
e5b10e31 |
return 0; |
b349fde1 |
}
#endif |