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.

122 lines
3.1KB

  1. /*
  2. * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * miscellaneous math routines and tables
  23. */
  24. #include <stdint.h>
  25. #include <limits.h>
  26. #include "mathematics.h"
  27. #include "version.h"
  28. int64_t av_gcd(int64_t a, int64_t b)
  29. {
  30. if (b)
  31. return av_gcd(b, a % b);
  32. else
  33. return a;
  34. }
  35. int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
  36. {
  37. int64_t r = 0;
  38. if (c <= 0 || b < 0 || rnd == 4 || rnd > 5)
  39. return INT64_MIN;
  40. if (a < 0 && a != INT64_MIN)
  41. return -av_rescale_rnd(-a, b, c, rnd ^ ((rnd >> 1) & 1));
  42. if (rnd == AV_ROUND_NEAR_INF)
  43. r = c / 2;
  44. else if (rnd & 1)
  45. r = c - 1;
  46. if (b <= INT_MAX && c <= INT_MAX) {
  47. if (a <= INT_MAX)
  48. return (a * b + r) / c;
  49. else
  50. return a / c * b + (a % c * b + r) / c;
  51. } else {
  52. uint64_t a0 = a & 0xFFFFFFFF;
  53. uint64_t a1 = a >> 32;
  54. uint64_t b0 = b & 0xFFFFFFFF;
  55. uint64_t b1 = b >> 32;
  56. uint64_t t1 = a0 * b1 + a1 * b0;
  57. uint64_t t1a = t1 << 32;
  58. int i;
  59. a0 = a0 * b0 + t1a;
  60. a1 = a1 * b1 + (t1 >> 32) + (a0 < t1a);
  61. a0 += r;
  62. a1 += a0 < r;
  63. for (i = 63; i >= 0; i--) {
  64. a1 += a1 + ((a0 >> i) & 1);
  65. t1 += t1;
  66. if (c <= a1) {
  67. a1 -= c;
  68. t1++;
  69. }
  70. }
  71. return t1;
  72. }
  73. }
  74. int64_t av_rescale(int64_t a, int64_t b, int64_t c)
  75. {
  76. return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
  77. }
  78. int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq,
  79. enum AVRounding rnd)
  80. {
  81. int64_t b = bq.num * (int64_t)cq.den;
  82. int64_t c = cq.num * (int64_t)bq.den;
  83. return av_rescale_rnd(a, b, c, rnd);
  84. }
  85. int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
  86. {
  87. return av_rescale_q_rnd(a, bq, cq, AV_ROUND_NEAR_INF);
  88. }
  89. int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
  90. {
  91. int64_t a = tb_a.num * (int64_t)tb_b.den;
  92. int64_t b = tb_b.num * (int64_t)tb_a.den;
  93. if (av_rescale_rnd(ts_a, a, b, AV_ROUND_DOWN) < ts_b)
  94. return -1;
  95. if (av_rescale_rnd(ts_b, b, a, AV_ROUND_DOWN) < ts_a)
  96. return 1;
  97. return 0;
  98. }
  99. int64_t av_compare_mod(uint64_t a, uint64_t b, uint64_t mod)
  100. {
  101. int64_t c = (a - b) & (mod - 1);
  102. if (c > (mod >> 1))
  103. c -= mod;
  104. return c;
  105. }