You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
5.4KB

  1. /*
  2. * arbitrary precision integers
  3. * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. /**
  23. * @file integer.c
  24. * arbitrary precision integers.
  25. * @author Michael Niedermayer <michaelni@gmx.at>
  26. */
  27. #include "common.h"
  28. #include "integer.h"
  29. AVInteger av_add_i(AVInteger a, AVInteger b){
  30. int i, carry=0;
  31. for(i=0; i<AV_INTEGER_SIZE; i++){
  32. carry= (carry>>16) + a.v[i] + b.v[i];
  33. a.v[i]= carry;
  34. }
  35. return a;
  36. }
  37. AVInteger av_sub_i(AVInteger a, AVInteger b){
  38. int i, carry=0;
  39. for(i=0; i<AV_INTEGER_SIZE; i++){
  40. carry= (carry>>16) + a.v[i] - b.v[i];
  41. a.v[i]= carry;
  42. }
  43. return a;
  44. }
  45. /**
  46. * returns the rounded down value of the logarithm of base 2 of the given AVInteger.
  47. * this is simply the index of the most significant bit which is 1. Or 0 of all bits are 0
  48. */
  49. int av_log2_i(AVInteger a){
  50. int i;
  51. for(i=AV_INTEGER_SIZE-1; i>=0; i--){
  52. if(a.v[i])
  53. return av_log2_16bit(a.v[i]) + 16*i;
  54. }
  55. return -1;
  56. }
  57. AVInteger av_mul_i(AVInteger a, AVInteger b){
  58. AVInteger out;
  59. int i, j;
  60. int na= (av_log2_i(a)+16) >> 4;
  61. int nb= (av_log2_i(b)+16) >> 4;
  62. memset(&out, 0, sizeof(out));
  63. for(i=0; i<na; i++){
  64. unsigned int carry=0;
  65. if(a.v[i])
  66. for(j=i; j<AV_INTEGER_SIZE && j-i<=nb; j++){
  67. carry= (carry>>16) + out.v[j] + a.v[i]*b.v[j-i];
  68. out.v[j]= carry;
  69. }
  70. }
  71. return out;
  72. }
  73. int av_cmp_i(AVInteger a, AVInteger b){
  74. int i;
  75. int v= (int16_t)a.v[AV_INTEGER_SIZE-1] - (int16_t)b.v[AV_INTEGER_SIZE-1];
  76. if(v) return (v>>16)|1;
  77. for(i=AV_INTEGER_SIZE-2; i>=0; i--){
  78. int v= a.v[i] - b.v[i];
  79. if(v) return (v>>16)|1;
  80. }
  81. return 0;
  82. }
  83. AVInteger av_shr_i(AVInteger a, int s){
  84. AVInteger out;
  85. int i;
  86. for(i=0; i<AV_INTEGER_SIZE; i++){
  87. int index= i + (s>>4);
  88. unsigned int v=0;
  89. if(index+1<AV_INTEGER_SIZE && index+1>=0) v = a.v[index+1]<<16;
  90. if(index <AV_INTEGER_SIZE && index >=0) v+= a.v[index ];
  91. out.v[i]= v >> (s&15);
  92. }
  93. return out;
  94. }
  95. AVInteger av_mod_i(AVInteger *quot, AVInteger a, AVInteger b){
  96. int i= av_log2_i(a) - av_log2_i(b);
  97. AVInteger quot_temp;
  98. if(!quot) quot = &quot_temp;
  99. assert((int16_t)a[AV_INTEGER_SIZE-1] >= 0 && (int16_t)b[AV_INTEGER_SIZE-1] >= 0);
  100. assert(av_log2(b)>=0);
  101. if(i > 0)
  102. b= av_shr_i(b, -i);
  103. memset(quot, 0, sizeof(AVInteger));
  104. while(i-- >= 0){
  105. *quot= av_shr_i(*quot, -1);
  106. if(av_cmp_i(a, b) >= 0){
  107. a= av_sub_i(a, b);
  108. quot->v[0] += 1;
  109. }
  110. b= av_shr_i(b, 1);
  111. }
  112. return a;
  113. }
  114. AVInteger av_div_i(AVInteger a, AVInteger b){
  115. AVInteger quot;
  116. av_mod_i(&quot, a, b);
  117. return quot;
  118. }
  119. /**
  120. * converts the given int64_t to an AVInteger.
  121. */
  122. AVInteger av_int2i(int64_t a){
  123. AVInteger out;
  124. int i;
  125. for(i=0; i<AV_INTEGER_SIZE; i++){
  126. out.v[i]= a;
  127. a>>=16;
  128. }
  129. return out;
  130. }
  131. /**
  132. * converts the given AVInteger to an int64_t.
  133. * if the AVInteger is too large to fit into an int64_t,
  134. * then only the least significant 64bit will be used
  135. */
  136. int64_t av_i2int(AVInteger a){
  137. int i;
  138. int64_t out=(int8_t)a.v[AV_INTEGER_SIZE-1];
  139. for(i= AV_INTEGER_SIZE-2; i>=0; i--){
  140. out = (out<<16) + a.v[i];
  141. }
  142. return out;
  143. }
  144. #if 0
  145. #undef NDEBUG
  146. #include <assert.h>
  147. const uint8_t ff_log2_tab[256]={
  148. 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,
  149. 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,
  150. 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,
  151. 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,
  152. 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,
  153. 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,
  154. 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,
  155. 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
  156. };
  157. main(){
  158. int64_t a,b;
  159. for(a=7; a<256*256*256; a+=13215){
  160. for(b=3; b<256*256*256; b+=27118){
  161. AVInteger ai= av_int2i(a);
  162. AVInteger bi= av_int2i(b);
  163. assert(av_i2int(ai) == a);
  164. assert(av_i2int(bi) == b);
  165. assert(av_i2int(av_add_i(ai,bi)) == a+b);
  166. assert(av_i2int(av_sub_i(ai,bi)) == a-b);
  167. assert(av_i2int(av_mul_i(ai,bi)) == a*b);
  168. assert(av_i2int(av_shr_i(ai, 9)) == a>>9);
  169. assert(av_i2int(av_shr_i(ai,-9)) == a<<9);
  170. assert(av_i2int(av_shr_i(ai, 17)) == a>>17);
  171. assert(av_i2int(av_shr_i(ai,-17)) == a<<17);
  172. assert(av_log2_i(ai) == av_log2(a));
  173. assert(av_i2int(av_div_i(ai,bi)) == a/b);
  174. }
  175. }
  176. }
  177. #endif