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.

138 lines
4.2KB

  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file mathematics.c
  20. * Miscellaneous math routines and tables.
  21. */
  22. #include "common.h"
  23. #include "mathematics.h"
  24. const uint8_t ff_sqrt_tab[128]={
  25. 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,
  26. 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,
  27. 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,
  28. 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
  29. };
  30. const uint8_t ff_log2_tab[256]={
  31. 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,
  32. 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,
  33. 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,
  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. 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,
  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. };
  40. int64_t ff_gcd(int64_t a, int64_t b){
  41. if(b) return ff_gcd(b, a%b);
  42. else return a;
  43. }
  44. int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd){
  45. int64_t r=0;
  46. assert(c > 0);
  47. assert(b >=0);
  48. assert(rnd >=0 && rnd<=5 && rnd!=4);
  49. if(a<0 && a != INT64_MIN) return -av_rescale_rnd(-a, b, c, rnd ^ ((rnd>>1)&1));
  50. if(rnd==AV_ROUND_NEAR_INF) r= c/2;
  51. else if(rnd&1) r= c-1;
  52. if(b<=INT_MAX && c<=INT_MAX){
  53. if(a<=INT_MAX)
  54. return (a * b + r)/c;
  55. else
  56. return a/c*b + (a%c*b + r)/c;
  57. }else{
  58. #if 1
  59. uint64_t a0= a&0xFFFFFFFF;
  60. uint64_t a1= a>>32;
  61. uint64_t b0= b&0xFFFFFFFF;
  62. uint64_t b1= b>>32;
  63. uint64_t t1= a0*b1 + a1*b0;
  64. uint64_t t1a= t1<<32;
  65. int i;
  66. a0 = a0*b0 + t1a;
  67. a1 = a1*b1 + (t1>>32) + (a0<t1a);
  68. a0 += r;
  69. a1 += a0<r;
  70. for(i=63; i>=0; i--){
  71. // int o= a1 & 0x8000000000000000ULL;
  72. a1+= a1 + ((a0>>i)&1);
  73. t1+=t1;
  74. if(/*o || */c <= a1){
  75. a1 -= c;
  76. t1++;
  77. }
  78. }
  79. return t1;
  80. }
  81. #else
  82. AVInteger ai;
  83. ai= av_mul_i(av_int2i(a), av_int2i(b));
  84. ai= av_add_i(ai, av_int2i(r));
  85. return av_i2int(av_div_i(ai, av_int2i(c)));
  86. }
  87. #endif
  88. }
  89. int64_t av_rescale(int64_t a, int64_t b, int64_t c){
  90. return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
  91. }
  92. int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq){
  93. int64_t b= bq.num * (int64_t)cq.den;
  94. int64_t c= cq.num * (int64_t)bq.den;
  95. return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
  96. }
  97. #if 0
  98. #include "integer.h"
  99. #undef printf
  100. main(){
  101. int64_t a,b,c,d,e;
  102. for(a=7; a<(1LL<<62); a+=a/3+1){
  103. for(b=3; b<(1LL<<62); b+=b/4+1){
  104. for(c=9; c<(1LL<<62); c+=(c*2)/5+3){
  105. int64_t r= c/2;
  106. AVInteger ai;
  107. ai= av_mul_i(av_int2i(a), av_int2i(b));
  108. ai= av_add_i(ai, av_int2i(r));
  109. d= av_i2int(av_div_i(ai, av_int2i(c)));
  110. e= av_rescale(a,b,c);
  111. if((double)a * (double)b / (double)c > (1LL<<63))
  112. continue;
  113. if(d!=e) printf("%Ld*%Ld/%Ld= %Ld=%Ld\n", a, b, c, d, e);
  114. }
  115. }
  116. }
  117. }
  118. #endif