eval.c 20 KB

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