eval.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*
  2. * Copyright (c) 2002-2006 Michael Niedermayer <michaelni@gmx.at>
  3. * Copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * simple arithmetic expression evaluator.
  24. *
  25. * see http://joe.hotchkiss.com/programming/eval/eval.html
  26. */
  27. #include "avutil.h"
  28. #include "eval.h"
  29. typedef struct Parser {
  30. const AVClass *class;
  31. int stack_index;
  32. char *s;
  33. const double *const_values;
  34. const char * const *const_names; // NULL terminated
  35. double (* const *funcs1)(void *, double a); // NULL terminated
  36. const char * const *func1_names; // NULL terminated
  37. double (* const *funcs2)(void *, double a, double b); // NULL terminated
  38. const char * const *func2_names; // NULL terminated
  39. void *opaque;
  40. int log_offset;
  41. void *log_ctx;
  42. #define VARS 10
  43. double var[VARS];
  44. } Parser;
  45. static const AVClass class = { "Eval", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(Parser,log_offset), offsetof(Parser,log_ctx) };
  46. static const int8_t si_prefixes['z' - 'E' + 1] = {
  47. ['y'-'E']= -24,
  48. ['z'-'E']= -21,
  49. ['a'-'E']= -18,
  50. ['f'-'E']= -15,
  51. ['p'-'E']= -12,
  52. ['n'-'E']= - 9,
  53. ['u'-'E']= - 6,
  54. ['m'-'E']= - 3,
  55. ['c'-'E']= - 2,
  56. ['d'-'E']= - 1,
  57. ['h'-'E']= 2,
  58. ['k'-'E']= 3,
  59. ['K'-'E']= 3,
  60. ['M'-'E']= 6,
  61. ['G'-'E']= 9,
  62. ['T'-'E']= 12,
  63. ['P'-'E']= 15,
  64. ['E'-'E']= 18,
  65. ['Z'-'E']= 21,
  66. ['Y'-'E']= 24,
  67. };
  68. double av_strtod(const char *numstr, char **tail)
  69. {
  70. double d;
  71. char *next;
  72. if(numstr[0]=='0' && (numstr[1]|0x20)=='x') {
  73. d = strtoul(numstr, &next, 16);
  74. } else
  75. d = strtod(numstr, &next);
  76. /* if parsing succeeded, check for and interpret postfixes */
  77. if (next!=numstr) {
  78. if (*next >= 'E' && *next <= 'z') {
  79. int e= si_prefixes[*next - 'E'];
  80. if (e) {
  81. if (next[1] == 'i') {
  82. d*= pow( 2, e/0.3);
  83. next+=2;
  84. } else {
  85. d*= pow(10, e);
  86. next++;
  87. }
  88. }
  89. }
  90. if (*next=='B') {
  91. d*=8;
  92. next++;
  93. }
  94. }
  95. /* if requested, fill in tail with the position after the last parsed
  96. character */
  97. if (tail)
  98. *tail = next;
  99. return d;
  100. }
  101. #define IS_IDENTIFIER_CHAR(c) ((c) - '0' <= 9U || (c) - 'a' <= 25U || (c) - 'A' <= 25U || (c) == '_')
  102. static int strmatch(const char *s, const char *prefix)
  103. {
  104. int i;
  105. for (i=0; prefix[i]; i++) {
  106. if (prefix[i] != s[i]) return 0;
  107. }
  108. /* return 1 only if the s identifier is terminated */
  109. return !IS_IDENTIFIER_CHAR(s[i]);
  110. }
  111. struct AVExpr {
  112. enum {
  113. e_value, e_const, e_func0, e_func1, e_func2,
  114. e_squish, e_gauss, e_ld, e_isnan,
  115. e_mod, e_max, e_min, e_eq, e_gt, e_gte,
  116. e_pow, e_mul, e_div, e_add,
  117. e_last, e_st, e_while, e_floor, e_ceil, e_trunc,
  118. e_sqrt, e_not,
  119. } type;
  120. double value; // is sign in other types
  121. union {
  122. int const_index;
  123. double (*func0)(double);
  124. double (*func1)(void *, double);
  125. double (*func2)(void *, double, double);
  126. } a;
  127. struct AVExpr *param[2];
  128. };
  129. static double eval_expr(Parser *p, AVExpr *e)
  130. {
  131. switch (e->type) {
  132. case e_value: return e->value;
  133. case e_const: return e->value * p->const_values[e->a.const_index];
  134. case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0]));
  135. case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
  136. case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
  137. case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
  138. case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
  139. case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
  140. case e_isnan: return e->value * !!isnan(eval_expr(p, e->param[0]));
  141. case e_floor: return e->value * floor(eval_expr(p, e->param[0]));
  142. case e_ceil : return e->value * ceil (eval_expr(p, e->param[0]));
  143. case e_trunc: return e->value * trunc(eval_expr(p, e->param[0]));
  144. case e_sqrt: return e->value * sqrt (eval_expr(p, e->param[0]));
  145. case e_not: return e->value * eval_expr(p, e->param[0]) == 0;
  146. case e_while: {
  147. double d = NAN;
  148. while (eval_expr(p, e->param[0]))
  149. d=eval_expr(p, e->param[1]);
  150. return d;
  151. }
  152. default: {
  153. double d = eval_expr(p, e->param[0]);
  154. double d2 = eval_expr(p, e->param[1]);
  155. switch (e->type) {
  156. case e_mod: return e->value * (d - floor(d/d2)*d2);
  157. case e_max: return e->value * (d > d2 ? d : d2);
  158. case e_min: return e->value * (d < d2 ? d : d2);
  159. case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
  160. case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
  161. case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
  162. case e_pow: return e->value * pow(d, d2);
  163. case e_mul: return e->value * (d * d2);
  164. case e_div: return e->value * (d / d2);
  165. case e_add: return e->value * (d + d2);
  166. case e_last:return e->value * d2;
  167. case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
  168. }
  169. }
  170. }
  171. return NAN;
  172. }
  173. static int parse_expr(AVExpr **e, Parser *p);
  174. void av_expr_free(AVExpr *e)
  175. {
  176. if (!e) return;
  177. av_expr_free(e->param[0]);
  178. av_expr_free(e->param[1]);
  179. av_freep(&e);
  180. }
  181. static int parse_primary(AVExpr **e, Parser *p)
  182. {
  183. AVExpr *d = av_mallocz(sizeof(AVExpr));
  184. char *next = p->s, *s0 = p->s;
  185. int ret, i;
  186. if (!d)
  187. return AVERROR(ENOMEM);
  188. /* number */
  189. d->value = av_strtod(p->s, &next);
  190. if (next != p->s) {
  191. d->type = e_value;
  192. p->s= next;
  193. *e = d;
  194. return 0;
  195. }
  196. d->value = 1;
  197. /* named constants */
  198. for (i=0; p->const_names && p->const_names[i]; i++) {
  199. if (strmatch(p->s, p->const_names[i])) {
  200. p->s+= strlen(p->const_names[i]);
  201. d->type = e_const;
  202. d->a.const_index = i;
  203. *e = d;
  204. return 0;
  205. }
  206. }
  207. p->s= strchr(p->s, '(');
  208. if (p->s==NULL) {
  209. av_log(p, AV_LOG_ERROR, "Undefined constant or missing '(' in '%s'\n", s0);
  210. p->s= next;
  211. av_expr_free(d);
  212. return AVERROR(EINVAL);
  213. }
  214. p->s++; // "("
  215. if (*next == '(') { // special case do-nothing
  216. av_freep(&d);
  217. if ((ret = parse_expr(&d, p)) < 0)
  218. return ret;
  219. if (p->s[0] != ')') {
  220. av_log(p, AV_LOG_ERROR, "Missing ')' in '%s'\n", s0);
  221. av_expr_free(d);
  222. return AVERROR(EINVAL);
  223. }
  224. p->s++; // ")"
  225. *e = d;
  226. return 0;
  227. }
  228. if ((ret = parse_expr(&(d->param[0]), p)) < 0) {
  229. av_expr_free(d);
  230. return ret;
  231. }
  232. if (p->s[0]== ',') {
  233. p->s++; // ","
  234. parse_expr(&d->param[1], p);
  235. }
  236. if (p->s[0] != ')') {
  237. av_log(p, AV_LOG_ERROR, "Missing ')' or too many args in '%s'\n", s0);
  238. av_expr_free(d);
  239. return AVERROR(EINVAL);
  240. }
  241. p->s++; // ")"
  242. d->type = e_func0;
  243. if (strmatch(next, "sinh" )) d->a.func0 = sinh;
  244. else if (strmatch(next, "cosh" )) d->a.func0 = cosh;
  245. else if (strmatch(next, "tanh" )) d->a.func0 = tanh;
  246. else if (strmatch(next, "sin" )) d->a.func0 = sin;
  247. else if (strmatch(next, "cos" )) d->a.func0 = cos;
  248. else if (strmatch(next, "tan" )) d->a.func0 = tan;
  249. else if (strmatch(next, "atan" )) d->a.func0 = atan;
  250. else if (strmatch(next, "asin" )) d->a.func0 = asin;
  251. else if (strmatch(next, "acos" )) d->a.func0 = acos;
  252. else if (strmatch(next, "exp" )) d->a.func0 = exp;
  253. else if (strmatch(next, "log" )) d->a.func0 = log;
  254. else if (strmatch(next, "abs" )) d->a.func0 = fabs;
  255. else if (strmatch(next, "squish")) d->type = e_squish;
  256. else if (strmatch(next, "gauss" )) d->type = e_gauss;
  257. else if (strmatch(next, "mod" )) d->type = e_mod;
  258. else if (strmatch(next, "max" )) d->type = e_max;
  259. else if (strmatch(next, "min" )) d->type = e_min;
  260. else if (strmatch(next, "eq" )) d->type = e_eq;
  261. else if (strmatch(next, "gte" )) d->type = e_gte;
  262. else if (strmatch(next, "gt" )) d->type = e_gt;
  263. else if (strmatch(next, "lte" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
  264. else if (strmatch(next, "lt" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
  265. else if (strmatch(next, "ld" )) d->type = e_ld;
  266. else if (strmatch(next, "isnan" )) d->type = e_isnan;
  267. else if (strmatch(next, "st" )) d->type = e_st;
  268. else if (strmatch(next, "while" )) d->type = e_while;
  269. else if (strmatch(next, "floor" )) d->type = e_floor;
  270. else if (strmatch(next, "ceil" )) d->type = e_ceil;
  271. else if (strmatch(next, "trunc" )) d->type = e_trunc;
  272. else if (strmatch(next, "sqrt" )) d->type = e_sqrt;
  273. else if (strmatch(next, "not" )) d->type = e_not;
  274. else if (strmatch(next, "pow" )) d->type = e_pow;
  275. else {
  276. for (i=0; p->func1_names && p->func1_names[i]; i++) {
  277. if (strmatch(next, p->func1_names[i])) {
  278. d->a.func1 = p->funcs1[i];
  279. d->type = e_func1;
  280. *e = d;
  281. return 0;
  282. }
  283. }
  284. for (i=0; p->func2_names && p->func2_names[i]; i++) {
  285. if (strmatch(next, p->func2_names[i])) {
  286. d->a.func2 = p->funcs2[i];
  287. d->type = e_func2;
  288. *e = d;
  289. return 0;
  290. }
  291. }
  292. av_log(p, AV_LOG_ERROR, "Unknown function in '%s'\n", s0);
  293. av_expr_free(d);
  294. return AVERROR(EINVAL);
  295. }
  296. *e = d;
  297. return 0;
  298. }
  299. static AVExpr *new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
  300. {
  301. AVExpr *e = av_mallocz(sizeof(AVExpr));
  302. if (!e)
  303. return NULL;
  304. e->type =type ;
  305. e->value =value ;
  306. e->param[0] =p0 ;
  307. e->param[1] =p1 ;
  308. return e;
  309. }
  310. static int parse_pow(AVExpr **e, Parser *p, int *sign)
  311. {
  312. *sign= (*p->s == '+') - (*p->s == '-');
  313. p->s += *sign&1;
  314. return parse_primary(e, p);
  315. }
  316. static int parse_factor(AVExpr **e, Parser *p)
  317. {
  318. int sign, sign2, ret;
  319. AVExpr *e0, *e1, *e2;
  320. if ((ret = parse_pow(&e0, p, &sign)) < 0)
  321. return ret;
  322. while(p->s[0]=='^'){
  323. e1 = e0;
  324. p->s++;
  325. if ((ret = parse_pow(&e2, p, &sign2)) < 0) {
  326. av_expr_free(e1);
  327. return ret;
  328. }
  329. e0 = new_eval_expr(e_pow, 1, e1, e2);
  330. if (!e0) {
  331. av_expr_free(e1);
  332. av_expr_free(e2);
  333. return AVERROR(ENOMEM);
  334. }
  335. if (e0->param[1]) e0->param[1]->value *= (sign2|1);
  336. }
  337. if (e0) e0->value *= (sign|1);
  338. *e = e0;
  339. return 0;
  340. }
  341. static int parse_term(AVExpr **e, Parser *p)
  342. {
  343. int ret;
  344. AVExpr *e0, *e1, *e2;
  345. if ((ret = parse_factor(&e0, p)) < 0)
  346. return ret;
  347. while (p->s[0]=='*' || p->s[0]=='/') {
  348. int c= *p->s++;
  349. e1 = e0;
  350. if ((ret = parse_factor(&e2, p)) < 0) {
  351. av_expr_free(e1);
  352. return ret;
  353. }
  354. e0 = new_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2);
  355. if (!e0) {
  356. av_expr_free(e1);
  357. av_expr_free(e2);
  358. return AVERROR(ENOMEM);
  359. }
  360. }
  361. *e = e0;
  362. return 0;
  363. }
  364. static int parse_subexpr(AVExpr **e, Parser *p)
  365. {
  366. int ret;
  367. AVExpr *e0, *e1, *e2;
  368. if ((ret = parse_term(&e0, p)) < 0)
  369. return ret;
  370. while (*p->s == '+' || *p->s == '-') {
  371. e1 = e0;
  372. if ((ret = parse_term(&e2, p)) < 0) {
  373. av_expr_free(e1);
  374. return ret;
  375. }
  376. e0 = new_eval_expr(e_add, 1, e1, e2);
  377. if (!e0) {
  378. av_expr_free(e1);
  379. av_expr_free(e2);
  380. return AVERROR(ENOMEM);
  381. }
  382. };
  383. *e = e0;
  384. return 0;
  385. }
  386. static int parse_expr(AVExpr **e, Parser *p)
  387. {
  388. int ret;
  389. AVExpr *e0, *e1, *e2;
  390. if (p->stack_index <= 0) //protect against stack overflows
  391. return AVERROR(EINVAL);
  392. p->stack_index--;
  393. if ((ret = parse_subexpr(&e0, p)) < 0)
  394. return ret;
  395. while (*p->s == ';') {
  396. p->s++;
  397. e1 = e0;
  398. if ((ret = parse_subexpr(&e2, p)) < 0) {
  399. av_expr_free(e1);
  400. return ret;
  401. }
  402. e0 = new_eval_expr(e_last, 1, e1, e2);
  403. if (!e0) {
  404. av_expr_free(e1);
  405. av_expr_free(e2);
  406. return AVERROR(ENOMEM);
  407. }
  408. };
  409. p->stack_index++;
  410. *e = e0;
  411. return 0;
  412. }
  413. static int verify_expr(AVExpr *e)
  414. {
  415. if (!e) return 0;
  416. switch (e->type) {
  417. case e_value:
  418. case e_const: return 1;
  419. case e_func0:
  420. case e_func1:
  421. case e_squish:
  422. case e_ld:
  423. case e_gauss:
  424. case e_isnan:
  425. case e_floor:
  426. case e_ceil:
  427. case e_trunc:
  428. case e_sqrt:
  429. case e_not:
  430. return verify_expr(e->param[0]);
  431. default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
  432. }
  433. }
  434. int av_expr_parse(AVExpr **expr, const char *s,
  435. const char * const *const_names,
  436. const char * const *func1_names, double (* const *funcs1)(void *, double),
  437. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  438. int log_offset, void *log_ctx)
  439. {
  440. Parser p;
  441. AVExpr *e = NULL;
  442. char *w = av_malloc(strlen(s) + 1);
  443. char *wp = w;
  444. const char *s0 = s;
  445. int ret = 0;
  446. if (!w)
  447. return AVERROR(ENOMEM);
  448. while (*s)
  449. if (!isspace(*s++)) *wp++ = s[-1];
  450. *wp++ = 0;
  451. p.class = &class;
  452. p.stack_index=100;
  453. p.s= w;
  454. p.const_names = const_names;
  455. p.funcs1 = funcs1;
  456. p.func1_names = func1_names;
  457. p.funcs2 = funcs2;
  458. p.func2_names = func2_names;
  459. p.log_offset = log_offset;
  460. p.log_ctx = log_ctx;
  461. if ((ret = parse_expr(&e, &p)) < 0)
  462. goto end;
  463. if (*p.s) {
  464. av_expr_free(e);
  465. av_log(&p, AV_LOG_ERROR, "Invalid chars '%s' at the end of expression '%s'\n", p.s, s0);
  466. ret = AVERROR(EINVAL);
  467. goto end;
  468. }
  469. if (!verify_expr(e)) {
  470. av_expr_free(e);
  471. ret = AVERROR(EINVAL);
  472. goto end;
  473. }
  474. *expr = e;
  475. end:
  476. av_free(w);
  477. return ret;
  478. }
  479. double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
  480. {
  481. Parser p;
  482. p.const_values = const_values;
  483. p.opaque = opaque;
  484. return eval_expr(&p, e);
  485. }
  486. int av_expr_parse_and_eval(double *d, const char *s,
  487. const char * const *const_names, const double *const_values,
  488. const char * const *func1_names, double (* const *funcs1)(void *, double),
  489. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  490. void *opaque, int log_offset, void *log_ctx)
  491. {
  492. AVExpr *e = NULL;
  493. int ret = av_expr_parse(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx);
  494. if (ret < 0) {
  495. *d = NAN;
  496. return ret;
  497. }
  498. *d = av_expr_eval(e, const_values, opaque);
  499. av_expr_free(e);
  500. return isnan(*d) ? AVERROR(EINVAL) : 0;
  501. }
  502. #if FF_API_OLD_EVAL_NAMES
  503. int av_parse_expr(AVExpr **expr, const char *s,
  504. const char * const *const_names,
  505. const char * const *func1_names, double (* const *funcs1)(void *, double),
  506. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  507. int log_offset, void *log_ctx)
  508. {
  509. return av_expr_parse(expr, s, const_names, func1_names, funcs1, func2_names, funcs2,
  510. log_offset, log_ctx);
  511. }
  512. double av_eval_expr(AVExpr *e, const double *const_values, void *opaque)
  513. {
  514. return av_expr_eval(e, const_values, opaque);
  515. }
  516. int av_parse_and_eval_expr(double *res, const char *s,
  517. const char * const *const_names, const double *const_values,
  518. const char * const *func1_names, double (* const *funcs1)(void *, double),
  519. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  520. void *opaque, int log_offset, void *log_ctx)
  521. {
  522. return av_expr_parse_and_eval(res, s, const_names, const_values, func1_names, funcs1, func2_names, funcs2,
  523. opaque, log_offset, log_ctx);
  524. }
  525. void av_free_expr(AVExpr *e)
  526. {
  527. av_expr_free(e);
  528. }
  529. #endif /* FF_API_OLD_EVAL_NAMES */
  530. #ifdef TEST
  531. #undef printf
  532. static double const_values[] = {
  533. M_PI,
  534. M_E,
  535. 0
  536. };
  537. static const char *const_names[] = {
  538. "PI",
  539. "E",
  540. 0
  541. };
  542. int main(void)
  543. {
  544. int i;
  545. double d;
  546. const char **expr, *exprs[] = {
  547. "",
  548. "1;2",
  549. "-20",
  550. "-PI",
  551. "+PI",
  552. "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  553. "80G/80Gi"
  554. "1k",
  555. "1Gi",
  556. "1gi",
  557. "1GiFoo",
  558. "1k+1k",
  559. "1Gi*3foo",
  560. "foo",
  561. "foo(",
  562. "foo()",
  563. "foo)",
  564. "sin",
  565. "sin(",
  566. "sin()",
  567. "sin)",
  568. "sin 10",
  569. "sin(1,2,3)",
  570. "sin(1 )",
  571. "1",
  572. "1foo",
  573. "bar + PI + E + 100f*2 + foo",
  574. "13k + 12f - foo(1, 2)",
  575. "1gi",
  576. "1Gi",
  577. "st(0, 123)",
  578. "st(1, 123); ld(1)",
  579. /* compute 1+2+...+N */
  580. "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
  581. /* compute Fib(N) */
  582. "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)",
  583. "while(0, 10)",
  584. "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
  585. "isnan(1)",
  586. "isnan(NAN)",
  587. "floor(NAN)",
  588. "floor(123.123)",
  589. "floor(-123.123)",
  590. "trunc(123.123)",
  591. "trunc(-123.123)",
  592. "ceil(123.123)",
  593. "ceil(-123.123)",
  594. "sqrt(1764)",
  595. "sqrt(-1)",
  596. "not(1)",
  597. "not(NAN)",
  598. "not(0)",
  599. "pow(0,1.23)",
  600. "pow(PI,1.23)",
  601. "PI^1.23",
  602. "pow(-1,1.23)",
  603. NULL
  604. };
  605. for (expr = exprs; *expr; expr++) {
  606. printf("Evaluating '%s'\n", *expr);
  607. av_expr_parse_and_eval(&d, *expr,
  608. const_names, const_values,
  609. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  610. printf("'%s' -> %f\n\n", *expr, d);
  611. }
  612. av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  613. const_names, const_values,
  614. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  615. printf("%f == 12.7\n", d);
  616. av_expr_parse_and_eval(&d, "80G/80Gi",
  617. const_names, const_values,
  618. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  619. printf("%f == 0.931322575\n", d);
  620. for (i=0; i<1050; i++) {
  621. START_TIMER
  622. av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  623. const_names, const_values,
  624. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  625. STOP_TIMER("av_expr_parse_and_eval")
  626. }
  627. return 0;
  628. }
  629. #endif