extend range used for testing (larger range should work too but testing code overflows)
[libav.git] / libavutil / mathematics.c
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 /**
20 * @file mathematics.c
21 * Miscellaneous math routines and tables.
22 */
23
24 #include "common.h"
25 #include "mathematics.h"
26
27 const uint8_t ff_sqrt_tab[128]={
28 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,
29 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,
30 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,
31 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
32 };
33
34 const uint8_t ff_log2_tab[256]={
35 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,
36 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,
37 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,
38 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,
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 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,
41 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,
42 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
43 };
44
45 int64_t ff_gcd(int64_t a, int64_t b){
46 if(b) return ff_gcd(b, a%b);
47 else return a;
48 }
49
50 int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd){
51 int64_t r=0;
52 assert(c > 0);
53 assert(b >=0);
54 assert(rnd >=0 && rnd<=5 && rnd!=4);
55
56 if(a<0 && a != INT64_MIN) return -av_rescale_rnd(-a, b, c, rnd ^ ((rnd>>1)&1));
57
58 if(rnd==AV_ROUND_NEAR_INF) r= c/2;
59 else if(rnd&1) r= c-1;
60
61 if(b<=INT_MAX && c<=INT_MAX){
62 if(a<=INT_MAX)
63 return (a * b + r)/c;
64 else
65 return a/c*b + (a%c*b + r)/c;
66 }else{
67 #if 1
68 uint64_t a0= a&0xFFFFFFFF;
69 uint64_t a1= a>>32;
70 uint64_t b0= b&0xFFFFFFFF;
71 uint64_t b1= b>>32;
72 uint64_t t1= a0*b1 + a1*b0;
73 uint64_t t1a= t1<<32;
74 int i;
75
76 a0 = a0*b0 + t1a;
77 a1 = a1*b1 + (t1>>32) + (a0<t1a);
78 a0 += r;
79 a1 += a0<r;
80
81 for(i=63; i>=0; i--){
82 // int o= a1 & 0x8000000000000000ULL;
83 a1+= a1 + ((a0>>i)&1);
84 t1+=t1;
85 if(/*o || */c <= a1){
86 a1 -= c;
87 t1++;
88 }
89 }
90 return t1;
91 }
92 #else
93 AVInteger ai;
94 ai= av_mul_i(av_int2i(a), av_int2i(b));
95 ai= av_add_i(ai, av_int2i(r));
96
97 return av_i2int(av_div_i(ai, av_int2i(c)));
98 }
99 #endif
100 }
101
102 int64_t av_rescale(int64_t a, int64_t b, int64_t c){
103 return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
104 }
105
106 int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq){
107 int64_t b= bq.num * (int64_t)cq.den;
108 int64_t c= cq.num * (int64_t)bq.den;
109 return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
110 }
111 #if 0
112 #undef printf
113 main(){
114 int64_t a,b,c,d,e;
115
116 for(a=7; a<(1LL<<60); a=(a*3)+1){
117 for(b=3; b<(1LL<<60); b=(b*5)/4+1){
118 for(c=9; c<(1LL<<60); c=(c*7)/5+3){
119 int64_t r= c/2;
120 AVInteger ai;
121 ai= av_mul_i(av_int2i(a), av_int2i(b));
122 ai= av_add_i(ai, av_int2i(r));
123
124 d= av_i2int(av_div_i(ai, av_int2i(c)));
125
126 e= av_rescale(a,b,c);
127
128 if((double)a * (double)b / (double)c > (1LL<<63))
129 continue;
130
131 if(d!=e) printf("%Ld*%Ld/%Ld= %Ld=%Ld\n", a, b, c, d, e);
132 }
133 }
134 }
135 }
136 #endif