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
23 #include "libavutil/libm.h"
24 #include "libavutil/timer.h"
25 #include "libavutil/eval.h"
27 static const double const_values[] = {
33 static const char *const const_names[] = {
39 int main(int argc, char **argv)
43 const char *const *expr;
44 static const char *const exprs[] = {
50 "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
71 "bar + PI + E + 100f*2 + foo",
72 "13k + 12f - foo(1, 2)",
85 /* compute 1+2+...+N */
86 "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
88 "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)",
90 "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
119 "ifnot(1, NaN) + if(0, 1)",
123 "taylor(eq(mod(ld(1),4),1)-eq(mod(ld(1),4),3), PI/2, 1)",
124 "root(sin(ld(0))-1, 2)",
125 "root(sin(ld(0))+6+sin(ld(0)/12)-log(ld(0)), 100)",
126 "7000000B*random(0)",
130 "gcd(30,55)*print(min(9,1))",
134 "between(10, -3, 10)",
135 "between(-4, -2, -1)",
144 for (expr = exprs; *expr; expr++) {
145 printf("Evaluating '%s'\n", *expr);
146 ret = av_expr_parse_and_eval(&d, *expr,
147 const_names, const_values,
148 NULL, NULL, NULL, NULL, NULL, 0, NULL);
150 printf("'%s' -> nan\n\n", *expr);
152 printf("'%s' -> %f\n\n", *expr, d);
154 printf("av_expr_parse_and_eval failed\n");
157 ret = av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
158 const_names, const_values,
159 NULL, NULL, NULL, NULL, NULL, 0, NULL);
160 printf("%f == 12.7\n", d);
162 printf("av_expr_parse_and_eval failed\n");
163 ret = av_expr_parse_and_eval(&d, "80G/80Gi",
164 const_names, const_values,
165 NULL, NULL, NULL, NULL, NULL, 0, NULL);
166 printf("%f == 0.931322575\n", d);
168 printf("av_expr_parse_and_eval failed\n");
170 if (argc > 1 && !strcmp(argv[1], "-t")) {
171 for (i = 0; i < 1050; i++) {
173 ret = av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
174 const_names, const_values,
175 NULL, NULL, NULL, NULL, NULL, 0, NULL);
177 printf("av_expr_parse_and_eval failed\n");
178 STOP_TIMER("av_expr_parse_and_eval");