2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "libavutil/timer.h"
25 #include "libavutil/libm.h"
26 #include "libavutil/eval.h"
28 static const double const_values[] = {
34 static const char *const const_names[] = {
40 int main(int argc, char **argv)
44 const char *const *expr;
45 static const char *const exprs[] = {
51 "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
72 "bar + PI + E + 100f*2 + foo",
73 "13k + 12f - foo(1, 2)",
86 /* compute 1+2+...+N */
87 "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
89 "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)",
91 "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
120 "ifnot(1, NaN) + if(0, 1)",
124 "taylor(eq(mod(ld(1),4),1)-eq(mod(ld(1),4),3), PI/2, 1)",
125 "root(sin(ld(0))-1, 2)",
126 "root(sin(ld(0))+6+sin(ld(0)/12)-log(ld(0)), 100)",
127 "7000000B*random(0)",
131 "gcd(30,55)*print(min(9,1))",
135 "between(10, -3, 10)",
136 "between(-4, -2, -1)",
145 for (expr = exprs; *expr; expr++) {
146 printf("Evaluating '%s'\n", *expr);
147 ret = av_expr_parse_and_eval(&d, *expr,
148 const_names, const_values,
149 NULL, NULL, NULL, NULL, NULL, 0, NULL);
151 printf("'%s' -> nan\n\n", *expr);
153 printf("'%s' -> %f\n\n", *expr, d);
155 printf("av_expr_parse_and_eval failed\n");
158 ret = av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
159 const_names, const_values,
160 NULL, NULL, NULL, NULL, NULL, 0, NULL);
161 printf("%f == 12.7\n", d);
163 printf("av_expr_parse_and_eval failed\n");
164 ret = av_expr_parse_and_eval(&d, "80G/80Gi",
165 const_names, const_values,
166 NULL, NULL, NULL, NULL, NULL, 0, NULL);
167 printf("%f == 0.931322575\n", d);
169 printf("av_expr_parse_and_eval failed\n");
171 if (argc > 1 && !strcmp(argv[1], "-t")) {
172 for (i = 0; i < 1050; i++) {
174 ret = av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
175 const_names, const_values,
176 NULL, NULL, NULL, NULL, NULL, 0, NULL);
178 printf("av_expr_parse_and_eval failed\n");
179 STOP_TIMER("av_expr_parse_and_eval");