b2e480fca136f5744b6d6a856b485da583847ab1
2 * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
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.
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.
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
21 * Miscellaneous math routines and tables.
25 #include "mathematics.h"
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
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
45 int64_t ff_gcd(int64_t a
, int64_t b
){
46 if(b
) return ff_gcd(b
, a
%b
);
50 int64_t av_rescale_rnd(int64_t a
, int64_t b
, int64_t c
, enum AVRounding rnd
){
54 assert(rnd
>=0 && rnd
<=5 && rnd
!=4);
56 if(a
<0 && a
!= INT64_MIN
) return -av_rescale_rnd(-a
, b
, c
, rnd
^ ((rnd
>>1)&1));
58 if(rnd
==AV_ROUND_NEAR_INF
) r
= c
/2;
59 else if(rnd
&1) r
= c
-1;
61 if(b
<=INT_MAX
&& c
<=INT_MAX
){
65 return a
/c
*b
+ (a
%c
*b
+ r
)/c
;
68 uint64_t a0
= a
&0xFFFFFFFF;
70 uint64_t b0
= b
&0xFFFFFFFF;
72 uint64_t t1
= a0
*b1
+ a1
*b0
;
77 a1
= a1
*b1
+ (t1
>>32) + (a0
<t1a
);
82 // int o= a1 & 0x8000000000000000ULL;
83 a1
+= a1
+ ((a0
>>i
)&1);
94 ai
= av_mul_i(av_int2i(a
), av_int2i(b
));
95 ai
= av_add_i(ai
, av_int2i(r
));
97 return av_i2int(av_div_i(ai
, av_int2i(c
)));
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
);
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
);
116 for(a
=7; a
<256LL*256*256*256*128; a
=(a
*3)+1){
117 for(b
=3; b
<256LL*256*256*256*128; b
=(b
*5)/4+1){
118 for(c
=9; c
<256LL*256*256*256*128; c
=(c
*7)/5+3 ){
121 ai
= av_mul_i(av_int2i(a
), av_int2i(b
));
122 ai
= av_add_i(ai
, av_int2i(r
));
124 d
= av_i2int(av_div_i(ai
, av_int2i(c
)));
126 e
= av_rescale(a
,b
,c
);
128 if((double)a
* (double)b
/ (double)c
> (1LL<<63))
131 if(d
!=e
) printf("%Ld*%Ld/%Ld= %Ld=%Ld\n", a
, b
, c
, d
, e
);