eval.c 19 KB

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