yading@11: /* yading@11: * arbitrary precision integers yading@11: * Copyright (c) 2004 Michael Niedermayer yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: /** yading@11: * @file yading@11: * arbitrary precision integers yading@11: * @author Michael Niedermayer yading@11: */ yading@11: yading@11: #include "common.h" yading@11: #include "integer.h" yading@11: #include "avassert.h" yading@11: yading@11: AVInteger av_add_i(AVInteger a, AVInteger b){ yading@11: int i, carry=0; yading@11: yading@11: for(i=0; i>16) + a.v[i] + b.v[i]; yading@11: a.v[i]= carry; yading@11: } yading@11: return a; yading@11: } yading@11: yading@11: AVInteger av_sub_i(AVInteger a, AVInteger b){ yading@11: int i, carry=0; yading@11: yading@11: for(i=0; i>16) + a.v[i] - b.v[i]; yading@11: a.v[i]= carry; yading@11: } yading@11: return a; yading@11: } yading@11: yading@11: int av_log2_i(AVInteger a){ yading@11: int i; yading@11: yading@11: for(i=AV_INTEGER_SIZE-1; i>=0; i--){ yading@11: if(a.v[i]) yading@11: return av_log2_16bit(a.v[i]) + 16*i; yading@11: } yading@11: return -1; yading@11: } yading@11: yading@11: AVInteger av_mul_i(AVInteger a, AVInteger b){ yading@11: AVInteger out; yading@11: int i, j; yading@11: int na= (av_log2_i(a)+16) >> 4; yading@11: int nb= (av_log2_i(b)+16) >> 4; yading@11: yading@11: memset(&out, 0, sizeof(out)); yading@11: yading@11: for(i=0; i>16) + out.v[j] + a.v[i]*b.v[j-i]; yading@11: out.v[j]= carry; yading@11: } yading@11: } yading@11: yading@11: return out; yading@11: } yading@11: yading@11: int av_cmp_i(AVInteger a, AVInteger b){ yading@11: int i; yading@11: int v= (int16_t)a.v[AV_INTEGER_SIZE-1] - (int16_t)b.v[AV_INTEGER_SIZE-1]; yading@11: if(v) return (v>>16)|1; yading@11: yading@11: for(i=AV_INTEGER_SIZE-2; i>=0; i--){ yading@11: int v= a.v[i] - b.v[i]; yading@11: if(v) return (v>>16)|1; yading@11: } yading@11: return 0; yading@11: } yading@11: yading@11: AVInteger av_shr_i(AVInteger a, int s){ yading@11: AVInteger out; yading@11: int i; yading@11: yading@11: for(i=0; i>4); yading@11: unsigned int v=0; yading@11: if(index+1> (s&15); yading@11: } yading@11: return out; yading@11: } yading@11: yading@11: AVInteger av_mod_i(AVInteger *quot, AVInteger a, AVInteger b){ yading@11: int i= av_log2_i(a) - av_log2_i(b); yading@11: AVInteger quot_temp; yading@11: if(!quot) quot = "_temp; yading@11: yading@11: av_assert2((int16_t)a.v[AV_INTEGER_SIZE-1] >= 0 && (int16_t)b.v[AV_INTEGER_SIZE-1] >= 0); yading@11: av_assert2(av_log2_i(b)>=0); yading@11: yading@11: if(i > 0) yading@11: b= av_shr_i(b, -i); yading@11: yading@11: memset(quot, 0, sizeof(AVInteger)); yading@11: yading@11: while(i-- >= 0){ yading@11: *quot= av_shr_i(*quot, -1); yading@11: if(av_cmp_i(a, b) >= 0){ yading@11: a= av_sub_i(a, b); yading@11: quot->v[0] += 1; yading@11: } yading@11: b= av_shr_i(b, 1); yading@11: } yading@11: return a; yading@11: } yading@11: yading@11: AVInteger av_div_i(AVInteger a, AVInteger b){ yading@11: AVInteger quot; yading@11: av_mod_i(", a, b); yading@11: return quot; yading@11: } yading@11: yading@11: AVInteger av_int2i(int64_t a){ yading@11: AVInteger out; yading@11: int i; yading@11: yading@11: for(i=0; i>=16; yading@11: } yading@11: return out; yading@11: } yading@11: yading@11: int64_t av_i2int(AVInteger a){ yading@11: int i; yading@11: int64_t out=(int8_t)a.v[AV_INTEGER_SIZE-1]; yading@11: yading@11: for(i= AV_INTEGER_SIZE-2; i>=0; i--){ yading@11: out = (out<<16) + a.v[i]; yading@11: } yading@11: return out; yading@11: } yading@11: yading@11: #ifdef TEST yading@11: yading@11: const uint8_t ff_log2_tab[256]={ yading@11: 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, yading@11: 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, yading@11: 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, yading@11: 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, yading@11: 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, yading@11: 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, yading@11: 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, yading@11: 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 yading@11: }; yading@11: yading@11: int main(void){ yading@11: int64_t a,b; yading@11: yading@11: for(a=7; a<256*256*256; a+=13215){ yading@11: for(b=3; b<256*256*256; b+=27118){ yading@11: AVInteger ai= av_int2i(a); yading@11: AVInteger bi= av_int2i(b); yading@11: yading@11: av_assert0(av_i2int(ai) == a); yading@11: av_assert0(av_i2int(bi) == b); yading@11: av_assert0(av_i2int(av_add_i(ai,bi)) == a+b); yading@11: av_assert0(av_i2int(av_sub_i(ai,bi)) == a-b); yading@11: av_assert0(av_i2int(av_mul_i(ai,bi)) == a*b); yading@11: av_assert0(av_i2int(av_shr_i(ai, 9)) == a>>9); yading@11: av_assert0(av_i2int(av_shr_i(ai,-9)) == a<<9); yading@11: av_assert0(av_i2int(av_shr_i(ai, 17)) == a>>17); yading@11: av_assert0(av_i2int(av_shr_i(ai,-17)) == a<<17); yading@11: av_assert0(av_log2_i(ai) == av_log2(a)); yading@11: av_assert0(av_i2int(av_div_i(ai,bi)) == a/b); yading@11: } yading@11: } yading@11: return 0; yading@11: } yading@11: #endif