/Languages/Ruby/Ruby/Compiler/UnsignedBigIntegerParser.cs

http://github.com/IronLanguages/main · C# · 201 lines · 145 code · 37 blank · 19 comment · 15 complexity · f558efc3b7b77793c44865376d8c445d MD5 · raw file

  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System;
  16. using System.Diagnostics;
  17. using Microsoft.Scripting.Utils;
  18. using Microsoft.Scripting.Math;
  19. namespace IronRuby.Compiler {
  20. /// <summary>
  21. /// TODO: move to DLR.
  22. /// </summary>
  23. public abstract class UnsignedBigIntegerParser {
  24. protected abstract int ReadDigit();
  25. protected UnsignedBigIntegerParser() {
  26. }
  27. public BigInteger/*!*/ ParseBinary(int digitCount) {
  28. return ParseBinarySuperPowers(digitCount, 1);
  29. }
  30. public BigInteger/*!*/ ParseHexadecimal(int digitCount) {
  31. return ParseBinarySuperPowers(digitCount, 4);
  32. }
  33. public BigInteger/*!*/ ParseDecimal(int digitCount) {
  34. const int DigitsPerWord = 9;
  35. const uint WordBase = 1000000000;
  36. return ParseAnyBase(digitCount, 10, WordBase, DigitsPerWord);
  37. }
  38. public BigInteger ParseOctal(int digitCount) {
  39. ContractUtils.Requires(digitCount > 0, "digitCount");
  40. const int BitsPerWord = 32;
  41. const int BitsPerDigit = 3;
  42. const int DigitsPerWord = BitsPerWord / BitsPerDigit;
  43. int remainingBits = (digitCount * BitsPerDigit) % (BitsPerWord * BitsPerDigit);
  44. int triwordCount = (digitCount * BitsPerDigit) / (BitsPerWord * BitsPerDigit);
  45. uint[] result = new uint[triwordCount * BitsPerDigit + (remainingBits + BitsPerWord - 1) / BitsPerWord];
  46. if (remainingBits <= BitsPerWord) {
  47. uint c = ReadBinaryWord(remainingBits / BitsPerDigit, BitsPerDigit);
  48. result[result.Length - 1] = c;
  49. } else if (remainingBits <= BitsPerWord * 2) {
  50. uint b = ReadBinaryWord((remainingBits - BitsPerWord) / BitsPerDigit, BitsPerDigit);
  51. uint bc = (uint)ReadDigit();
  52. uint c = ReadBinaryWord(DigitsPerWord, BitsPerDigit);
  53. result[result.Length - 1] = (b << 1) | (bc >> 2);
  54. result[result.Length - 2] = c | ((bc & 3) << 30);
  55. } else {
  56. ReadOctalTriword(result, result.Length - 1, (remainingBits - 2 * BitsPerWord) / BitsPerDigit);
  57. }
  58. for (int i = triwordCount * 3 - 1; i > 0; i -= 3) {
  59. ReadOctalTriword(result, i, DigitsPerWord);
  60. }
  61. return new BigInteger(+1, result);
  62. }
  63. private void ReadOctalTriword(uint[]/*!*/ result, int i, int digits) {
  64. const int BitsPerWord = 32;
  65. const int BitsPerDigit = 3;
  66. const int DigitsPerWord = BitsPerWord / BitsPerDigit;
  67. // [10 digits + 2 bits][1 bit + 10 digits + 1 bit][2 bits + 10 digits]
  68. uint a = ReadBinaryWord(digits, BitsPerDigit);
  69. uint ab = (uint)ReadDigit();
  70. uint b = ReadBinaryWord(DigitsPerWord, BitsPerDigit);
  71. uint bc = (uint)ReadDigit();
  72. uint c = ReadBinaryWord(DigitsPerWord, BitsPerDigit);
  73. result[i - 0] = (a << 2) | (ab >> 1);
  74. result[i - 1] = (b << 1) | ((ab & 1) << 31) | (bc >> 2);
  75. result[i - 2] = c | ((bc & 3) << 30);
  76. }
  77. public BigInteger/*!*/ Parse(int digitCount, int @base) {
  78. ContractUtils.Requires(@base > 1, "base");
  79. switch (@base) {
  80. case 2: return ParseBinary(digitCount);
  81. case 4: return ParseBinarySuperPowers(digitCount, 2);
  82. case 8: return ParseOctal(digitCount);
  83. case 16: return ParseHexadecimal(digitCount);
  84. case 10: return ParseDecimal(digitCount);
  85. default:
  86. return ParseDefault(digitCount, (uint)@base);
  87. }
  88. }
  89. internal BigInteger/*!*/ ParseDefault(int digitCount, uint @base) {
  90. uint wordBase = 1;
  91. int digitsPerWord = 0;
  92. while (true) {
  93. ulong newBase = (ulong)wordBase * @base;
  94. if (newBase > UInt32.MaxValue) break;
  95. wordBase = (uint)newBase;
  96. digitsPerWord++;
  97. }
  98. return ParseAnyBase(digitCount, @base, wordBase, digitsPerWord);
  99. }
  100. private BigInteger/*!*/ ParseAnyBase(int digitCount, uint @base, uint wordBase, int digitsPerWord) {
  101. ContractUtils.Requires(digitCount > 0, "digitCount");
  102. int resultSize = GetResultSize(digitCount, @base);
  103. int remainingDigits = digitCount % digitsPerWord;
  104. int wordCount = digitCount / digitsPerWord;
  105. uint[] result = new uint[resultSize];
  106. result[0] = ReadWord(remainingDigits, @base);
  107. int count = 1;
  108. for (int i = 0; i < wordCount; i++) {
  109. count = MultiplyAdd(result, count, wordBase, ReadWord(digitsPerWord, @base));
  110. }
  111. return new BigInteger(+1, result);
  112. }
  113. private int GetResultSize(int digitCount, uint @base) {
  114. int resultSize;
  115. try {
  116. resultSize = (int)System.Math.Ceiling(System.Math.Log(@base) * digitCount);
  117. } catch (OverflowException) {
  118. throw new ArgumentOutOfRangeException("Too many digits", "digitCount");
  119. }
  120. return resultSize;
  121. }
  122. private BigInteger/*!*/ ParseBinarySuperPowers(int digitCount, int bitsPerDigit) {
  123. ContractUtils.Requires(digitCount > 0, "digitCount");
  124. const int BitsPerWord = 32;
  125. Debug.Assert(BitsPerWord % bitsPerDigit == 0);
  126. int digitsPerWord = BitsPerWord / bitsPerDigit;
  127. int remainingDigits = digitCount % digitsPerWord;
  128. int wordCount = digitCount / digitsPerWord;
  129. uint[] result = new uint[wordCount + (remainingDigits + digitsPerWord - 1) / digitsPerWord];
  130. result[result.Length - 1] = ReadBinaryWord(remainingDigits, bitsPerDigit);
  131. for (int i = wordCount - 1; i >= 0; i--) {
  132. result[i] = ReadBinaryWord(digitsPerWord, bitsPerDigit);
  133. }
  134. return new BigInteger(+1, result);
  135. }
  136. // data = data * x + carry
  137. private int MultiplyAdd(uint[]/*!*/ data, int count, uint x, uint carry) {
  138. ulong m = 0;
  139. for (int i = 0; i < count + 1; i++) {
  140. m = (ulong)data[i] * x + carry;
  141. data[i] = (uint)(m & 0xffffffffL);
  142. carry = (uint)(m >> 32);
  143. }
  144. Debug.Assert(carry == 0);
  145. return (m > 0) ? count + 1 : count;
  146. }
  147. private uint ReadBinaryWord(int digitCount, int bitsPerDigit) {
  148. uint word = 0;
  149. while (digitCount > 0) {
  150. word = (word << bitsPerDigit) | (uint)ReadDigit();
  151. digitCount--;
  152. }
  153. return word;
  154. }
  155. private uint ReadWord(int digitCount, uint @base) {
  156. uint word = 0;
  157. while (digitCount > 0) {
  158. word = word * @base + (uint)ReadDigit();
  159. digitCount--;
  160. }
  161. return word;
  162. }
  163. }
  164. }