eval.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  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.
  8. *
  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.
  13. *
  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
  17. */
  18. #include "libavutil/timer.h"
  19. #include <math.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include "libavutil/libm.h"
  23. #include "libavutil/eval.h"
  24. static const double const_values[] = {
  25. M_PI,
  26. M_E,
  27. 0
  28. };
  29. static const char *const const_names[] = {
  30. "PI",
  31. "E",
  32. 0
  33. };
  34. int main(int argc, char **argv)
  35. {
  36. int i;
  37. double d;
  38. const char *const *expr;
  39. static const char *const exprs[] = {
  40. "",
  41. "1;2",
  42. "-20",
  43. "-PI",
  44. "+PI",
  45. "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  46. "80G/80Gi",
  47. "1k",
  48. "1Gi",
  49. "1gi",
  50. "1GiFoo",
  51. "1k+1k",
  52. "1Gi*3foo",
  53. "foo",
  54. "foo(",
  55. "foo()",
  56. "foo)",
  57. "sin",
  58. "sin(",
  59. "sin()",
  60. "sin)",
  61. "sin 10",
  62. "sin(1,2,3)",
  63. "sin(1 )",
  64. "1",
  65. "1foo",
  66. "bar + PI + E + 100f*2 + foo",
  67. "13k + 12f - foo(1, 2)",
  68. "1gi",
  69. "1Gi",
  70. "st(0, 123)",
  71. "st(1, 123); ld(1)",
  72. "lte(0, 1)",
  73. "lte(1, 1)",
  74. "lte(1, 0)",
  75. "lt(0, 1)",
  76. "lt(1, 1)",
  77. "gt(1, 0)",
  78. "gt(2, 7)",
  79. "gte(122, 122)",
  80. /* compute 1+2+...+N */
  81. "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
  82. /* compute Fib(N) */
  83. "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)",
  84. "while(0, 10)",
  85. "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
  86. "isnan(1)",
  87. "isnan(NAN)",
  88. "isnan(INF)",
  89. "isinf(1)",
  90. "isinf(NAN)",
  91. "isinf(INF)",
  92. "floor(NAN)",
  93. "floor(123.123)",
  94. "floor(-123.123)",
  95. "trunc(123.123)",
  96. "trunc(-123.123)",
  97. "ceil(123.123)",
  98. "ceil(-123.123)",
  99. "sqrt(1764)",
  100. "isnan(sqrt(-1))",
  101. "not(1)",
  102. "not(NAN)",
  103. "not(0)",
  104. "6.0206dB",
  105. "-3.0103dB",
  106. "pow(0,1.23)",
  107. "pow(PI,1.23)",
  108. "PI^1.23",
  109. "pow(-1,1.23)",
  110. "if(1, 2)",
  111. "if(1, 1, 2)",
  112. "if(0, 1, 2)",
  113. "ifnot(0, 23)",
  114. "ifnot(1, NaN) + if(0, 1)",
  115. "ifnot(1, 1, 2)",
  116. "ifnot(0, 1, 2)",
  117. "taylor(1, 1)",
  118. "taylor(eq(mod(ld(1),4),1)-eq(mod(ld(1),4),3), PI/2, 1)",
  119. "root(sin(ld(0))-1, 2)",
  120. "root(sin(ld(0))+6+sin(ld(0)/12)-log(ld(0)), 100)",
  121. "7000000B*random(0)",
  122. "squish(2)",
  123. "gauss(0.1)",
  124. "hypot(4,3)",
  125. "gcd(30,55)*print(min(9,1))",
  126. "bitor(42, 12)",
  127. "bitand(42, 12)",
  128. "bitand(NAN, 1)",
  129. "between(10, -3, 10)",
  130. "between(-4, -2, -1)",
  131. "between(1,2)",
  132. "clip(0, 2, 1)",
  133. "clip(0/0, 1, 2)",
  134. "clip(0, 0/0, 1)",
  135. NULL
  136. };
  137. int ret;
  138. for (expr = exprs; *expr; expr++) {
  139. printf("Evaluating '%s'\n", *expr);
  140. ret = av_expr_parse_and_eval(&d, *expr,
  141. const_names, const_values,
  142. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  143. if (isnan(d))
  144. printf("'%s' -> nan\n\n", *expr);
  145. else
  146. printf("'%s' -> %f\n\n", *expr, d);
  147. if (ret < 0)
  148. printf("av_expr_parse_and_eval failed\n");
  149. }
  150. ret = av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  151. const_names, const_values,
  152. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  153. printf("%f == 12.7\n", d);
  154. if (ret < 0)
  155. printf("av_expr_parse_and_eval failed\n");
  156. ret = av_expr_parse_and_eval(&d, "80G/80Gi",
  157. const_names, const_values,
  158. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  159. printf("%f == 0.931322575\n", d);
  160. if (ret < 0)
  161. printf("av_expr_parse_and_eval failed\n");
  162. if (argc > 1 && !strcmp(argv[1], "-t")) {
  163. for (i = 0; i < 1050; i++) {
  164. START_TIMER;
  165. ret = av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  166. const_names, const_values,
  167. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  168. if (ret < 0)
  169. printf("av_expr_parse_and_eval failed\n");
  170. STOP_TIMER("av_expr_parse_and_eval");
  171. }
  172. }
  173. return 0;
  174. }