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.

196 lines
5.1KB

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