You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.9KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <math.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include "libavutil/libm.h"
  22. #include "libavutil/timer.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. NULL
  107. };
  108. for (expr = exprs; *expr; expr++) {
  109. printf("Evaluating '%s'\n", *expr);
  110. av_expr_parse_and_eval(&d, *expr,
  111. const_names, const_values,
  112. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  113. if (isnan(d))
  114. printf("'%s' -> nan\n\n", *expr);
  115. else
  116. printf("'%s' -> %f\n\n", *expr, d);
  117. }
  118. av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  119. const_names, const_values,
  120. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  121. printf("%f == 12.7\n", d);
  122. av_expr_parse_and_eval(&d, "80G/80Gi",
  123. const_names, const_values,
  124. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  125. printf("%f == 0.931322575\n", d);
  126. if (argc > 1 && !strcmp(argv[1], "-t")) {
  127. for (i = 0; i < 1050; i++) {
  128. START_TIMER;
  129. av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  130. const_names, const_values,
  131. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  132. STOP_TIMER("av_expr_parse_and_eval");
  133. }
  134. }
  135. return 0;
  136. }