• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavutil/integer.c

Go to the documentation of this file.
00001 /*
00002  * arbitrary precision integers
00003  * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00028 #include "common.h"
00029 #include "integer.h"
00030 
00031 AVInteger av_add_i(AVInteger a, AVInteger b){
00032     int i, carry=0;
00033 
00034     for(i=0; i<AV_INTEGER_SIZE; i++){
00035         carry= (carry>>16) + a.v[i] + b.v[i];
00036         a.v[i]= carry;
00037     }
00038     return a;
00039 }
00040 
00041 AVInteger av_sub_i(AVInteger a, AVInteger b){
00042     int i, carry=0;
00043 
00044     for(i=0; i<AV_INTEGER_SIZE; i++){
00045         carry= (carry>>16) + a.v[i] - b.v[i];
00046         a.v[i]= carry;
00047     }
00048     return a;
00049 }
00050 
00051 int av_log2_i(AVInteger a){
00052     int i;
00053 
00054     for(i=AV_INTEGER_SIZE-1; i>=0; i--){
00055         if(a.v[i])
00056             return av_log2_16bit(a.v[i]) + 16*i;
00057     }
00058     return -1;
00059 }
00060 
00061 AVInteger av_mul_i(AVInteger a, AVInteger b){
00062     AVInteger out;
00063     int i, j;
00064     int na= (av_log2_i(a)+16) >> 4;
00065     int nb= (av_log2_i(b)+16) >> 4;
00066 
00067     memset(&out, 0, sizeof(out));
00068 
00069     for(i=0; i<na; i++){
00070         unsigned int carry=0;
00071 
00072         if(a.v[i])
00073             for(j=i; j<AV_INTEGER_SIZE && j-i<=nb; j++){
00074                 carry= (carry>>16) + out.v[j] + a.v[i]*b.v[j-i];
00075                 out.v[j]= carry;
00076             }
00077     }
00078 
00079     return out;
00080 }
00081 
00082 int av_cmp_i(AVInteger a, AVInteger b){
00083     int i;
00084     int v= (int16_t)a.v[AV_INTEGER_SIZE-1] - (int16_t)b.v[AV_INTEGER_SIZE-1];
00085     if(v) return (v>>16)|1;
00086 
00087     for(i=AV_INTEGER_SIZE-2; i>=0; i--){
00088         int v= a.v[i] - b.v[i];
00089         if(v) return (v>>16)|1;
00090     }
00091     return 0;
00092 }
00093 
00094 AVInteger av_shr_i(AVInteger a, int s){
00095     AVInteger out;
00096     int i;
00097 
00098     for(i=0; i<AV_INTEGER_SIZE; i++){
00099         unsigned int index= i + (s>>4);
00100         unsigned int v=0;
00101         if(index+1<AV_INTEGER_SIZE) v = a.v[index+1]<<16;
00102         if(index  <AV_INTEGER_SIZE) v+= a.v[index  ];
00103         out.v[i]= v >> (s&15);
00104     }
00105     return out;
00106 }
00107 
00108 AVInteger av_mod_i(AVInteger *quot, AVInteger a, AVInteger b){
00109     int i= av_log2_i(a) - av_log2_i(b);
00110     AVInteger quot_temp;
00111     if(!quot) quot = &quot_temp;
00112 
00113     assert((int16_t)a[AV_INTEGER_SIZE-1] >= 0 && (int16_t)b[AV_INTEGER_SIZE-1] >= 0);
00114     assert(av_log2(b)>=0);
00115 
00116     if(i > 0)
00117         b= av_shr_i(b, -i);
00118 
00119     memset(quot, 0, sizeof(AVInteger));
00120 
00121     while(i-- >= 0){
00122         *quot= av_shr_i(*quot, -1);
00123         if(av_cmp_i(a, b) >= 0){
00124             a= av_sub_i(a, b);
00125             quot->v[0] += 1;
00126         }
00127         b= av_shr_i(b, 1);
00128     }
00129     return a;
00130 }
00131 
00132 AVInteger av_div_i(AVInteger a, AVInteger b){
00133     AVInteger quot;
00134     av_mod_i(&quot, a, b);
00135     return quot;
00136 }
00137 
00138 AVInteger av_int2i(int64_t a){
00139     AVInteger out;
00140     int i;
00141 
00142     for(i=0; i<AV_INTEGER_SIZE; i++){
00143         out.v[i]= a;
00144         a>>=16;
00145     }
00146     return out;
00147 }
00148 
00149 int64_t av_i2int(AVInteger a){
00150     int i;
00151     int64_t out=(int8_t)a.v[AV_INTEGER_SIZE-1];
00152 
00153     for(i= AV_INTEGER_SIZE-2; i>=0; i--){
00154         out = (out<<16) + a.v[i];
00155     }
00156     return out;
00157 }
00158 
00159 #ifdef TEST
00160 #undef NDEBUG
00161 #include <assert.h>
00162 
00163 const uint8_t ff_log2_tab[256]={
00164         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,
00165         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,
00166         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,
00167         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,
00168         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,
00169         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,
00170         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,
00171         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
00172 };
00173 
00174 int main(void){
00175     int64_t a,b;
00176 
00177     for(a=7; a<256*256*256; a+=13215){
00178         for(b=3; b<256*256*256; b+=27118){
00179             AVInteger ai= av_int2i(a);
00180             AVInteger bi= av_int2i(b);
00181 
00182             assert(av_i2int(ai) == a);
00183             assert(av_i2int(bi) == b);
00184             assert(av_i2int(av_add_i(ai,bi)) == a+b);
00185             assert(av_i2int(av_sub_i(ai,bi)) == a-b);
00186             assert(av_i2int(av_mul_i(ai,bi)) == a*b);
00187             assert(av_i2int(av_shr_i(ai, 9)) == a>>9);
00188             assert(av_i2int(av_shr_i(ai,-9)) == a<<9);
00189             assert(av_i2int(av_shr_i(ai, 17)) == a>>17);
00190             assert(av_i2int(av_shr_i(ai,-17)) == a<<17);
00191             assert(av_log2_i(ai) == av_log2(a));
00192             assert(av_i2int(av_div_i(ai,bi)) == a/b);
00193         }
00194     }
00195     return 0;
00196 }
00197 #endif

Generated on Fri Sep 16 2011 17:17:51 for FFmpeg by  doxygen 1.7.1