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.

85 lines
3.0KB

  1. /*
  2. * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This library 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 of the License, or (at your option) any later version.
  8. *
  9. * This library 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 this library; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /**
  19. * @file mathematics.c
  20. * Miscellaneous math routines and tables.
  21. */
  22. #include "common.h"
  23. #include "integer.h"
  24. #include "mathematics.h"
  25. const uint8_t ff_sqrt_tab[128]={
  26. 0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
  27. 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  28. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  29. 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11
  30. };
  31. const uint8_t ff_log2_tab[256]={
  32. 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  33. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  34. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  35. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  36. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  37. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  38. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  39. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
  40. };
  41. int64_t ff_gcd(int64_t a, int64_t b){
  42. if(b) return ff_gcd(b, a%b);
  43. else return a;
  44. }
  45. int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd){
  46. AVInteger ai;
  47. int64_t r=0;
  48. assert(c > 0);
  49. assert(b >=0);
  50. assert(rnd >=0 && rnd<=5 && rnd!=4);
  51. if(a<0 && a != INT64_MIN) return -av_rescale_rnd(-a, b, c, rnd ^ ((rnd>>1)&1));
  52. if(rnd==AV_ROUND_NEAR_INF) r= c/2;
  53. else if(rnd&1) r= c-1;
  54. if(b<=INT_MAX && c<=INT_MAX){
  55. if(a<=INT_MAX)
  56. return (a * b + r)/c;
  57. else
  58. return a/c*b + (a%c*b + r)/c;
  59. }
  60. ai= av_mul_i(av_int2i(a), av_int2i(b));
  61. ai= av_add_i(ai, av_int2i(r));
  62. return av_i2int(av_div_i(ai, av_int2i(c)));
  63. }
  64. int64_t av_rescale(int64_t a, int64_t b, int64_t c){
  65. return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
  66. }
  67. int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq){
  68. int64_t b= bq.num * (int64_t)cq.den;
  69. int64_t c= cq.num * (int64_t)bq.den;
  70. return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
  71. }