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.

224 lines
5.7KB

  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. /**
  74. * returns 0 if a==b, 1 if a>b and -1 if a<b.
  75. */
  76. int av_cmp_i(AVInteger a, AVInteger b){
  77. int i;
  78. int v= (int16_t)a.v[AV_INTEGER_SIZE-1] - (int16_t)b.v[AV_INTEGER_SIZE-1];
  79. if(v) return (v>>16)|1;
  80. for(i=AV_INTEGER_SIZE-2; i>=0; i--){
  81. int v= a.v[i] - b.v[i];
  82. if(v) return (v>>16)|1;
  83. }
  84. return 0;
  85. }
  86. /**
  87. * bitwise shift.
  88. * @param s the number of bits by which the value should be shifted right, may be negative for shifting left
  89. */
  90. AVInteger av_shr_i(AVInteger a, int s){
  91. AVInteger out;
  92. int i;
  93. for(i=0; i<AV_INTEGER_SIZE; i++){
  94. int index= i + (s>>4);
  95. unsigned int v=0;
  96. if(index+1<AV_INTEGER_SIZE && index+1>=0) v = a.v[index+1]<<16;
  97. if(index <AV_INTEGER_SIZE && index >=0) v+= a.v[index ];
  98. out.v[i]= v >> (s&15);
  99. }
  100. return out;
  101. }
  102. /**
  103. * returns a % b.
  104. * @param quot a/b will be stored here
  105. */
  106. AVInteger av_mod_i(AVInteger *quot, AVInteger a, AVInteger b){
  107. int i= av_log2_i(a) - av_log2_i(b);
  108. AVInteger quot_temp;
  109. if(!quot) quot = &quot_temp;
  110. assert((int16_t)a[AV_INTEGER_SIZE-1] >= 0 && (int16_t)b[AV_INTEGER_SIZE-1] >= 0);
  111. assert(av_log2(b)>=0);
  112. if(i > 0)
  113. b= av_shr_i(b, -i);
  114. memset(quot, 0, sizeof(AVInteger));
  115. while(i-- >= 0){
  116. *quot= av_shr_i(*quot, -1);
  117. if(av_cmp_i(a, b) >= 0){
  118. a= av_sub_i(a, b);
  119. quot->v[0] += 1;
  120. }
  121. b= av_shr_i(b, 1);
  122. }
  123. return a;
  124. }
  125. /**
  126. * returns a/b.
  127. */
  128. AVInteger av_div_i(AVInteger a, AVInteger b){
  129. AVInteger quot;
  130. av_mod_i(&quot, a, b);
  131. return quot;
  132. }
  133. /**
  134. * converts the given int64_t to an AVInteger.
  135. */
  136. AVInteger av_int2i(int64_t a){
  137. AVInteger out;
  138. int i;
  139. for(i=0; i<AV_INTEGER_SIZE; i++){
  140. out.v[i]= a;
  141. a>>=16;
  142. }
  143. return out;
  144. }
  145. /**
  146. * converts the given AVInteger to an int64_t.
  147. * if the AVInteger is too large to fit into an int64_t,
  148. * then only the least significant 64bit will be used
  149. */
  150. int64_t av_i2int(AVInteger a){
  151. int i;
  152. int64_t out=(int8_t)a.v[AV_INTEGER_SIZE-1];
  153. for(i= AV_INTEGER_SIZE-2; i>=0; i--){
  154. out = (out<<16) + a.v[i];
  155. }
  156. return out;
  157. }
  158. #if 0
  159. #undef NDEBUG
  160. #include <assert.h>
  161. const uint8_t ff_log2_tab[256]={
  162. 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,
  163. 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,
  164. 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,
  165. 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,
  166. 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,
  167. 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,
  168. 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,
  169. 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
  170. };
  171. main(){
  172. int64_t a,b;
  173. for(a=7; a<256*256*256; a+=13215){
  174. for(b=3; b<256*256*256; b+=27118){
  175. AVInteger ai= av_int2i(a);
  176. AVInteger bi= av_int2i(b);
  177. assert(av_i2int(ai) == a);
  178. assert(av_i2int(bi) == b);
  179. assert(av_i2int(av_add_i(ai,bi)) == a+b);
  180. assert(av_i2int(av_sub_i(ai,bi)) == a-b);
  181. assert(av_i2int(av_mul_i(ai,bi)) == a*b);
  182. assert(av_i2int(av_shr_i(ai, 9)) == a>>9);
  183. assert(av_i2int(av_shr_i(ai,-9)) == a<<9);
  184. assert(av_i2int(av_shr_i(ai, 17)) == a>>17);
  185. assert(av_i2int(av_shr_i(ai,-17)) == a<<17);
  186. assert(av_log2_i(ai) == av_log2(a));
  187. assert(av_i2int(av_div_i(ai,bi)) == a/b);
  188. }
  189. }
  190. }
  191. #endif