00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "common.h"
00027 #include "mathematics.h"
00028
00029 const uint8_t ff_sqrt_tab[128]={
00030 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,
00031 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,
00032 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,
00033 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
00034 };
00035
00036 const uint8_t ff_log2_tab[256]={
00037 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,
00038 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,
00039 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,
00040 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,
00041 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,
00042 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,
00043 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,
00044 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
00045 };
00046
00047 int64_t ff_gcd(int64_t a, int64_t b){
00048 if(b) return ff_gcd(b, a%b);
00049 else return a;
00050 }
00051
00052 int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd){
00053 int64_t r=0;
00054 assert(c > 0);
00055 assert(b >=0);
00056 assert(rnd >=0 && rnd<=5 && rnd!=4);
00057
00058 if(a<0 && a != INT64_MIN) return -av_rescale_rnd(-a, b, c, rnd ^ ((rnd>>1)&1));
00059
00060 if(rnd==AV_ROUND_NEAR_INF) r= c/2;
00061 else if(rnd&1) r= c-1;
00062
00063 if(b<=INT_MAX && c<=INT_MAX){
00064 if(a<=INT_MAX)
00065 return (a * b + r)/c;
00066 else
00067 return a/c*b + (a%c*b + r)/c;
00068 }else{
00069 #if 1
00070 uint64_t a0= a&0xFFFFFFFF;
00071 uint64_t a1= a>>32;
00072 uint64_t b0= b&0xFFFFFFFF;
00073 uint64_t b1= b>>32;
00074 uint64_t t1= a0*b1 + a1*b0;
00075 uint64_t t1a= t1<<32;
00076 int i;
00077
00078 a0 = a0*b0 + t1a;
00079 a1 = a1*b1 + (t1>>32) + (a0<t1a);
00080 a0 += r;
00081 a1 += a0<r;
00082
00083 for(i=63; i>=0; i--){
00084
00085 a1+= a1 + ((a0>>i)&1);
00086 t1+=t1;
00087 if(c <= a1){
00088 a1 -= c;
00089 t1++;
00090 }
00091 }
00092 return t1;
00093 }
00094 #else
00095 AVInteger ai;
00096 ai= av_mul_i(av_int2i(a), av_int2i(b));
00097 ai= av_add_i(ai, av_int2i(r));
00098
00099 return av_i2int(av_div_i(ai, av_int2i(c)));
00100 }
00101 #endif
00102 }
00103
00104 int64_t av_rescale(int64_t a, int64_t b, int64_t c){
00105 return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
00106 }
00107
00108 int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq){
00109 int64_t b= bq.num * (int64_t)cq.den;
00110 int64_t c= cq.num * (int64_t)bq.den;
00111 return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
00112 }
00113 #if 0
00114 #include "integer.h"
00115 #undef printf
00116 main(){
00117 int64_t a,b,c,d,e;
00118
00119 for(a=7; a<(1LL<<62); a+=a/3+1){
00120 for(b=3; b<(1LL<<62); b+=b/4+1){
00121 for(c=9; c<(1LL<<62); c+=(c*2)/5+3){
00122 int64_t r= c/2;
00123 AVInteger ai;
00124 ai= av_mul_i(av_int2i(a), av_int2i(b));
00125 ai= av_add_i(ai, av_int2i(r));
00126
00127 d= av_i2int(av_div_i(ai, av_int2i(c)));
00128
00129 e= av_rescale(a,b,c);
00130
00131 if((double)a * (double)b / (double)c > (1LL<<63))
00132 continue;
00133
00134 if(d!=e) printf("%"PRId64"*%"PRId64"/%"PRId64"= %"PRId64"=%"PRId64"\n", a, b, c, d, e);
00135 }
00136 }
00137 }
00138 }
00139 #endif