Number.BigInteger.cs 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. using System.Runtime.InteropServices;
  6. using Internal.Runtime.CompilerServices;
  7. namespace System
  8. {
  9. internal static partial class Number
  10. {
  11. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  12. internal unsafe ref struct BigInteger
  13. {
  14. // The longest binary mantissa requires: explicit mantissa bits + abs(min exponent)
  15. // * Half: 10 + 14 = 24
  16. // * Single: 23 + 126 = 149
  17. // * Double: 52 + 1022 = 1074
  18. // * Quad: 112 + 16382 = 16494
  19. private const int BitsForLongestBinaryMantissa = 1074;
  20. // The longest digit sequence requires: ceil(log2(pow(10, max significant digits + 1 rounding digit)))
  21. // * Half: ceil(log2(pow(10, 21 + 1))) = 74
  22. // * Single: ceil(log2(pow(10, 112 + 1))) = 376
  23. // * Double: ceil(log2(pow(10, 767 + 1))) = 2552
  24. // * Quad: ceil(log2(pow(10, 11563 + 1))) = 38415
  25. private const int BitsForLongestDigitSequence = 2552;
  26. // We require BitsPerBlock additional bits for shift space used during the pre-division preparation
  27. private const int MaxBits = BitsForLongestBinaryMantissa + BitsForLongestDigitSequence + BitsPerBlock;
  28. private const int BitsPerBlock = sizeof(int) * 8;
  29. private const int MaxBlockCount = (MaxBits + (BitsPerBlock - 1)) / BitsPerBlock;
  30. private static readonly uint[] s_Pow10UInt32Table = new uint[]
  31. {
  32. 1, // 10^0
  33. 10, // 10^1
  34. 100, // 10^2
  35. 1000, // 10^3
  36. 10000, // 10^4
  37. 100000, // 10^5
  38. 1000000, // 10^6
  39. 10000000, // 10^7
  40. };
  41. private static readonly int[] s_Pow10BigNumTableIndices = new int[]
  42. {
  43. 0, // 10^8
  44. 2, // 10^16
  45. 5, // 10^32
  46. 10, // 10^64
  47. 18, // 10^128
  48. 33, // 10^256
  49. 61, // 10^512
  50. 116, // 10^1024
  51. };
  52. private static readonly uint[] s_Pow10BigNumTable = new uint[]
  53. {
  54. // 10^8
  55. 1, // _length
  56. 100000000, // _blocks
  57. // 10^16
  58. 2, // _length
  59. 0x6FC10000, // _blocks
  60. 0x002386F2,
  61. // 10^32
  62. 4, // _length
  63. 0x00000000, // _blocks
  64. 0x85ACEF81,
  65. 0x2D6D415B,
  66. 0x000004EE,
  67. // 10^64
  68. 7, // _length
  69. 0x00000000, // _blocks
  70. 0x00000000,
  71. 0xBF6A1F01,
  72. 0x6E38ED64,
  73. 0xDAA797ED,
  74. 0xE93FF9F4,
  75. 0x00184F03,
  76. // 10^128
  77. 14, // _length
  78. 0x00000000, // _blocks
  79. 0x00000000,
  80. 0x00000000,
  81. 0x00000000,
  82. 0x2E953E01,
  83. 0x03DF9909,
  84. 0x0F1538FD,
  85. 0x2374E42F,
  86. 0xD3CFF5EC,
  87. 0xC404DC08,
  88. 0xBCCDB0DA,
  89. 0xA6337F19,
  90. 0xE91F2603,
  91. 0x0000024E,
  92. // 10^256
  93. 27, // _length
  94. 0x00000000, // _blocks
  95. 0x00000000,
  96. 0x00000000,
  97. 0x00000000,
  98. 0x00000000,
  99. 0x00000000,
  100. 0x00000000,
  101. 0x00000000,
  102. 0x982E7C01,
  103. 0xBED3875B,
  104. 0xD8D99F72,
  105. 0x12152F87,
  106. 0x6BDE50C6,
  107. 0xCF4A6E70,
  108. 0xD595D80F,
  109. 0x26B2716E,
  110. 0xADC666B0,
  111. 0x1D153624,
  112. 0x3C42D35A,
  113. 0x63FF540E,
  114. 0xCC5573C0,
  115. 0x65F9EF17,
  116. 0x55BC28F2,
  117. 0x80DCC7F7,
  118. 0xF46EEDDC,
  119. 0x5FDCEFCE,
  120. 0x000553F7,
  121. // 10^512
  122. 54, // _length
  123. 0x00000000, // _blocks
  124. 0x00000000,
  125. 0x00000000,
  126. 0x00000000,
  127. 0x00000000,
  128. 0x00000000,
  129. 0x00000000,
  130. 0x00000000,
  131. 0x00000000,
  132. 0x00000000,
  133. 0x00000000,
  134. 0x00000000,
  135. 0x00000000,
  136. 0x00000000,
  137. 0x00000000,
  138. 0x00000000,
  139. 0xFC6CF801,
  140. 0x77F27267,
  141. 0x8F9546DC,
  142. 0x5D96976F,
  143. 0xB83A8A97,
  144. 0xC31E1AD9,
  145. 0x46C40513,
  146. 0x94E65747,
  147. 0xC88976C1,
  148. 0x4475B579,
  149. 0x28F8733B,
  150. 0xAA1DA1BF,
  151. 0x703ED321,
  152. 0x1E25CFEA,
  153. 0xB21A2F22,
  154. 0xBC51FB2E,
  155. 0x96E14F5D,
  156. 0xBFA3EDAC,
  157. 0x329C57AE,
  158. 0xE7FC7153,
  159. 0xC3FC0695,
  160. 0x85A91924,
  161. 0xF95F635E,
  162. 0xB2908EE0,
  163. 0x93ABADE4,
  164. 0x1366732A,
  165. 0x9449775C,
  166. 0x69BE5B0E,
  167. 0x7343AFAC,
  168. 0xB099BC81,
  169. 0x45A71D46,
  170. 0xA2699748,
  171. 0x8CB07303,
  172. 0x8A0B1F13,
  173. 0x8CAB8A97,
  174. 0xC1D238D9,
  175. 0x633415D4,
  176. 0x0000001C,
  177. // 10^1024
  178. 107, // _length
  179. 0x00000000, // _blocks
  180. 0x00000000,
  181. 0x00000000,
  182. 0x00000000,
  183. 0x00000000,
  184. 0x00000000,
  185. 0x00000000,
  186. 0x00000000,
  187. 0x00000000,
  188. 0x00000000,
  189. 0x00000000,
  190. 0x00000000,
  191. 0x00000000,
  192. 0x00000000,
  193. 0x00000000,
  194. 0x00000000,
  195. 0x00000000,
  196. 0x00000000,
  197. 0x00000000,
  198. 0x00000000,
  199. 0x00000000,
  200. 0x00000000,
  201. 0x00000000,
  202. 0x00000000,
  203. 0x00000000,
  204. 0x00000000,
  205. 0x00000000,
  206. 0x00000000,
  207. 0x00000000,
  208. 0x00000000,
  209. 0x00000000,
  210. 0x00000000,
  211. 0x2919F001,
  212. 0xF55B2B72,
  213. 0x6E7C215B,
  214. 0x1EC29F86,
  215. 0x991C4E87,
  216. 0x15C51A88,
  217. 0x140AC535,
  218. 0x4C7D1E1A,
  219. 0xCC2CD819,
  220. 0x0ED1440E,
  221. 0x896634EE,
  222. 0x7DE16CFB,
  223. 0x1E43F61F,
  224. 0x9FCE837D,
  225. 0x231D2B9C,
  226. 0x233E55C7,
  227. 0x65DC60D7,
  228. 0xF451218B,
  229. 0x1C5CD134,
  230. 0xC9635986,
  231. 0x922BBB9F,
  232. 0xA7E89431,
  233. 0x9F9F2A07,
  234. 0x62BE695A,
  235. 0x8E1042C4,
  236. 0x045B7A74,
  237. 0x1ABE1DE3,
  238. 0x8AD822A5,
  239. 0xBA34C411,
  240. 0xD814B505,
  241. 0xBF3FDEB3,
  242. 0x8FC51A16,
  243. 0xB1B896BC,
  244. 0xF56DEEEC,
  245. 0x31FB6BFD,
  246. 0xB6F4654B,
  247. 0x101A3616,
  248. 0x6B7595FB,
  249. 0xDC1A47FE,
  250. 0x80D98089,
  251. 0x80BDA5A5,
  252. 0x9A202882,
  253. 0x31EB0F66,
  254. 0xFC8F1F90,
  255. 0x976A3310,
  256. 0xE26A7B7E,
  257. 0xDF68368A,
  258. 0x3CE3A0B8,
  259. 0x8E4262CE,
  260. 0x75A351A2,
  261. 0x6CB0B6C9,
  262. 0x44597583,
  263. 0x31B5653F,
  264. 0xC356E38A,
  265. 0x35FAABA6,
  266. 0x0190FBA0,
  267. 0x9FC4ED52,
  268. 0x88BC491B,
  269. 0x1640114A,
  270. 0x005B8041,
  271. 0xF4F3235E,
  272. 0x1E8D4649,
  273. 0x36A8DE06,
  274. 0x73C55349,
  275. 0xA7E6BD2A,
  276. 0xC1A6970C,
  277. 0x47187094,
  278. 0xD2DB49EF,
  279. 0x926C3F5B,
  280. 0xAE6209D4,
  281. 0x2D433949,
  282. 0x34F4A3C6,
  283. 0xD4305D94,
  284. 0xD9D61A05,
  285. 0x00000325,
  286. // 9 Trailing blocks to ensure MaxBlockCount
  287. 0x00000000,
  288. 0x00000000,
  289. 0x00000000,
  290. 0x00000000,
  291. 0x00000000,
  292. 0x00000000,
  293. 0x00000000,
  294. 0x00000000,
  295. 0x00000000,
  296. };
  297. private int _length;
  298. private fixed uint _blocks[MaxBlockCount];
  299. public BigInteger(uint value)
  300. {
  301. _blocks[0] = value;
  302. _length = (value == 0) ? 0 : 1;
  303. }
  304. public BigInteger(ulong value)
  305. {
  306. var lower = (uint)(value);
  307. var upper = (uint)(value >> 32);
  308. _blocks[0] = lower;
  309. _blocks[1] = upper;
  310. _length = (upper == 0) ? 1 : 2;
  311. }
  312. public static void Add(ref BigInteger lhs, uint value, ref BigInteger result)
  313. {
  314. if (lhs.IsZero())
  315. {
  316. result.SetUInt32(value);
  317. return;
  318. }
  319. if (value == 0)
  320. {
  321. result.SetValue(ref lhs);
  322. return;
  323. }
  324. int lhsLength = lhs._length;
  325. int index = 0;
  326. uint carry = value;
  327. while (index < lhsLength)
  328. {
  329. ulong sum = (ulong)(lhs._blocks[index]) + carry;
  330. lhs._blocks[index] = (uint)(sum);
  331. carry = (uint)(sum >> 32);
  332. index++;
  333. }
  334. if (carry != 0)
  335. {
  336. Debug.Assert(unchecked((uint)(lhsLength)) + 1 <= MaxBlockCount);
  337. result._blocks[index] = carry;
  338. result._length = (lhsLength + 1);
  339. }
  340. }
  341. public static void Add(ref BigInteger lhs, ref BigInteger rhs, out BigInteger result)
  342. {
  343. // determine which operand has the smaller length
  344. ref BigInteger large = ref (lhs._length < rhs._length) ? ref rhs : ref lhs;
  345. ref BigInteger small = ref (lhs._length < rhs._length) ? ref lhs : ref rhs;
  346. int largeLength = large._length;
  347. int smallLength = small._length;
  348. // The output will be at least as long as the largest input
  349. result = new BigInteger(0);
  350. result._length = largeLength;
  351. // Add each block and add carry the overflow to the next block
  352. ulong carry = 0;
  353. int largeIndex = 0;
  354. int smallIndex = 0;
  355. int resultIndex = 0;
  356. while (smallIndex < smallLength)
  357. {
  358. ulong sum = carry + large._blocks[largeIndex] + small._blocks[smallIndex];
  359. carry = sum >> 32;
  360. result._blocks[resultIndex] = (uint)(sum);
  361. largeIndex++;
  362. smallIndex++;
  363. resultIndex++;
  364. }
  365. // Add the carry to any blocks that only exist in the large operand
  366. while (largeIndex < largeLength)
  367. {
  368. ulong sum = carry + large._blocks[largeIndex];
  369. carry = sum >> 32;
  370. result._blocks[resultIndex] = (uint)(sum);
  371. largeIndex++;
  372. resultIndex++;
  373. }
  374. // If there's still a carry, append a new block
  375. if (carry != 0)
  376. {
  377. Debug.Assert(carry == 1);
  378. Debug.Assert((resultIndex == largeLength) && (largeLength < MaxBlockCount));
  379. result._blocks[resultIndex] = 1;
  380. result._length += 1;
  381. }
  382. }
  383. public static int Compare(ref BigInteger lhs, ref BigInteger rhs)
  384. {
  385. Debug.Assert(unchecked((uint)(lhs._length)) <= MaxBlockCount);
  386. Debug.Assert(unchecked((uint)(rhs._length)) <= MaxBlockCount);
  387. int lhsLength = lhs._length;
  388. int rhsLength = rhs._length;
  389. int lengthDelta = (lhsLength - rhsLength);
  390. if (lengthDelta != 0)
  391. {
  392. return lengthDelta;
  393. }
  394. if (lhsLength == 0)
  395. {
  396. Debug.Assert(rhsLength == 0);
  397. return 0;
  398. }
  399. for (int index = (lhsLength - 1); index >= 0; index--)
  400. {
  401. long delta = (long)(lhs._blocks[index]) - rhs._blocks[index];
  402. if (delta != 0)
  403. {
  404. return delta > 0 ? 1 : -1;
  405. }
  406. }
  407. return 0;
  408. }
  409. public static uint CountSignificantBits(uint value)
  410. {
  411. return 32 - (uint)BitOps.LeadingZeroCount(value);
  412. }
  413. public static uint CountSignificantBits(ulong value)
  414. {
  415. uint upper = (uint)(value >> 32);
  416. if (upper != 0)
  417. {
  418. return 32 + CountSignificantBits(upper);
  419. }
  420. return CountSignificantBits((uint)value);
  421. }
  422. public static uint CountSignificantBits(ref BigInteger value)
  423. {
  424. if (value.IsZero())
  425. {
  426. return 0;
  427. }
  428. // We don't track any unused blocks, so we only need to do a BSR on the
  429. // last index and add that to the number of bits we skipped.
  430. uint lastIndex = (uint)(value._length - 1);
  431. return (lastIndex * BitsPerBlock) + CountSignificantBits(value._blocks[lastIndex]);
  432. }
  433. public static void DivRem(ref BigInteger lhs, ref BigInteger rhs, out BigInteger quo, out BigInteger rem)
  434. {
  435. // This is modified from the CoreFX BigIntegerCalculator.DivRem.cs implementation:
  436. // https://github.com/dotnet/corefx/blob/0bb106232745aedfc0d0c5a84ff2b244bf190317/src/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.DivRem.cs
  437. Debug.Assert(!rhs.IsZero());
  438. quo = new BigInteger(0);
  439. rem = new BigInteger(0);
  440. if (lhs.IsZero())
  441. {
  442. return;
  443. }
  444. int lhsLength = lhs._length;
  445. int rhsLength = rhs._length;
  446. if ((lhsLength == 1) && (rhsLength == 1))
  447. {
  448. uint quotient = Math.DivRem(lhs._blocks[0], rhs._blocks[0], out uint remainder);
  449. quo = new BigInteger(quotient);
  450. rem = new BigInteger(remainder);
  451. return;
  452. }
  453. if (rhsLength == 1)
  454. {
  455. // We can make the computation much simpler if the rhs is only one block
  456. int quoLength = lhsLength;
  457. ulong rhsValue = rhs._blocks[0];
  458. ulong carry = 0;
  459. for (int i = quoLength - 1; i >= 0; i--)
  460. {
  461. ulong value = (carry << 32) | lhs._blocks[i];
  462. ulong digit = Math.DivRem(value, rhsValue, out carry);
  463. if ((digit == 0) && (i == (quoLength - 1)))
  464. {
  465. quoLength--;
  466. }
  467. else
  468. {
  469. quo._blocks[i] = (uint)(digit);
  470. }
  471. }
  472. quo._length = quoLength;
  473. rem.SetUInt32((uint)(carry));
  474. return;
  475. }
  476. else if (rhsLength > lhsLength)
  477. {
  478. // Handle the case where we have no quotient
  479. rem.SetValue(ref lhs);
  480. return;
  481. }
  482. else
  483. {
  484. int quoLength = lhsLength - rhsLength + 1;
  485. rem.SetValue(ref lhs);
  486. int remLength = lhsLength;
  487. // Executes the "grammar-school" algorithm for computing q = a / b.
  488. // Before calculating q_i, we get more bits into the highest bit
  489. // block of the divisor. Thus, guessing digits of the quotient
  490. // will be more precise. Additionally we'll get r = a % b.
  491. uint divHi = rhs._blocks[rhsLength - 1];
  492. uint divLo = rhs._blocks[rhsLength - 2];
  493. // We measure the leading zeros of the divisor
  494. int shiftLeft = BitOps.LeadingZeroCount(divHi);
  495. int shiftRight = 32 - shiftLeft;
  496. // And, we make sure the most significant bit is set
  497. if (shiftLeft > 0)
  498. {
  499. divHi = (divHi << shiftLeft) | (divLo >> shiftRight);
  500. divLo = (divLo << shiftLeft);
  501. if (rhsLength > 2)
  502. {
  503. divLo |= (rhs._blocks[rhsLength - 3] >> shiftRight);
  504. }
  505. }
  506. // Then, we divide all of the bits as we would do it using
  507. // pen and paper: guessing the next digit, subtracting, ...
  508. for (int i = lhsLength; i >= rhsLength; i--)
  509. {
  510. int n = i - rhsLength;
  511. uint t = i < lhsLength ? rem._blocks[i] : 0;
  512. ulong valHi = ((ulong)(t) << 32) | rem._blocks[i - 1];
  513. uint valLo = i > 1 ? rem._blocks[i - 2] : 0;
  514. // We shifted the divisor, we shift the dividend too
  515. if (shiftLeft > 0)
  516. {
  517. valHi = (valHi << shiftLeft) | (valLo >> shiftRight);
  518. valLo = (valLo << shiftLeft);
  519. if (i > 2)
  520. {
  521. valLo |= (rem._blocks[i - 3] >> shiftRight);
  522. }
  523. }
  524. // First guess for the current digit of the quotient,
  525. // which naturally must have only 32 bits...
  526. ulong digit = valHi / divHi;
  527. if (digit > uint.MaxValue)
  528. {
  529. digit = uint.MaxValue;
  530. }
  531. // Our first guess may be a little bit to big
  532. while (DivideGuessTooBig(digit, valHi, valLo, divHi, divLo))
  533. {
  534. digit--;
  535. }
  536. if (digit > 0)
  537. {
  538. // Now it's time to subtract our current quotient
  539. uint carry = SubtractDivisor(ref rem, n, ref rhs, digit);
  540. if (carry != t)
  541. {
  542. Debug.Assert(carry == t + 1);
  543. // Our guess was still exactly one too high
  544. carry = AddDivisor(ref rem, n, ref rhs);
  545. digit--;
  546. Debug.Assert(carry == 1);
  547. }
  548. }
  549. // We have the digit!
  550. if (quoLength != 0)
  551. {
  552. if ((digit == 0) && (n == (quoLength - 1)))
  553. {
  554. quoLength--;
  555. }
  556. else
  557. {
  558. quo._blocks[n] = (uint)(digit);
  559. }
  560. }
  561. if (i < remLength)
  562. {
  563. remLength--;
  564. }
  565. }
  566. quo._length = quoLength;
  567. // We need to check for the case where remainder is zero
  568. for (int i = remLength - 1; i >= 0; i--)
  569. {
  570. if (rem._blocks[i] == 0)
  571. {
  572. remLength--;
  573. }
  574. }
  575. rem._length = remLength;
  576. }
  577. }
  578. public static uint HeuristicDivide(ref BigInteger dividend, ref BigInteger divisor)
  579. {
  580. int divisorLength = divisor._length;
  581. if (dividend._length < divisorLength)
  582. {
  583. return 0;
  584. }
  585. // This is an estimated quotient. Its error should be less than 2.
  586. // Reference inequality:
  587. // a/b - floor(floor(a)/(floor(b) + 1)) < 2
  588. int lastIndex = (divisorLength - 1);
  589. uint quotient = dividend._blocks[lastIndex] / (divisor._blocks[lastIndex] + 1);
  590. if (quotient != 0)
  591. {
  592. // Now we use our estimated quotient to update each block of dividend.
  593. // dividend = dividend - divisor * quotient
  594. int index = 0;
  595. ulong borrow = 0;
  596. ulong carry = 0;
  597. do
  598. {
  599. ulong product = ((ulong)(divisor._blocks[index]) * quotient) + carry;
  600. carry = product >> 32;
  601. ulong difference = (ulong)(dividend._blocks[index]) - (uint)(product) - borrow;
  602. borrow = (difference >> 32) & 1;
  603. dividend._blocks[index] = (uint)(difference);
  604. index++;
  605. }
  606. while (index < divisorLength);
  607. // Remove all leading zero blocks from dividend
  608. while ((divisorLength > 0) && (dividend._blocks[divisorLength - 1] == 0))
  609. {
  610. divisorLength--;
  611. }
  612. dividend._length = divisorLength;
  613. }
  614. // If the dividend is still larger than the divisor, we overshot our estimate quotient. To correct,
  615. // we increment the quotient and subtract one more divisor from the dividend (Because we guaranteed the error range).
  616. if (Compare(ref dividend, ref divisor) >= 0)
  617. {
  618. quotient++;
  619. // dividend = dividend - divisor
  620. int index = 0;
  621. ulong borrow = 0;
  622. do
  623. {
  624. ulong difference = (ulong)(dividend._blocks[index]) - divisor._blocks[index] - borrow;
  625. borrow = (difference >> 32) & 1;
  626. dividend._blocks[index] = (uint)(difference);
  627. index++;
  628. }
  629. while (index < divisorLength);
  630. // Remove all leading zero blocks from dividend
  631. while ((divisorLength > 0) && (dividend._blocks[divisorLength - 1] == 0))
  632. {
  633. divisorLength--;
  634. }
  635. dividend._length = divisorLength;
  636. }
  637. return quotient;
  638. }
  639. public static void Multiply(ref BigInteger lhs, uint value, ref BigInteger result)
  640. {
  641. if (lhs.IsZero() || (value == 1))
  642. {
  643. result.SetValue(ref lhs);
  644. return;
  645. }
  646. if (value == 0)
  647. {
  648. result.SetZero();
  649. return;
  650. }
  651. int lhsLength = lhs._length;
  652. int index = 0;
  653. uint carry = 0;
  654. while (index < lhsLength)
  655. {
  656. ulong product = ((ulong)(lhs._blocks[index]) * value) + carry;
  657. result._blocks[index] = (uint)(product);
  658. carry = (uint)(product >> 32);
  659. index++;
  660. }
  661. if (carry != 0)
  662. {
  663. Debug.Assert(unchecked((uint)(lhsLength)) + 1 <= MaxBlockCount);
  664. result._blocks[index] = carry;
  665. result._length = (lhsLength + 1);
  666. }
  667. }
  668. public static void Multiply(ref BigInteger lhs, ref BigInteger rhs, ref BigInteger result)
  669. {
  670. if (lhs.IsZero() || rhs.IsOne())
  671. {
  672. result.SetValue(ref lhs);
  673. return;
  674. }
  675. if (rhs.IsZero())
  676. {
  677. result.SetZero();
  678. return;
  679. }
  680. ref readonly BigInteger large = ref lhs;
  681. int largeLength = lhs._length;
  682. ref readonly BigInteger small = ref rhs;
  683. int smallLength = rhs._length;
  684. if (largeLength < smallLength)
  685. {
  686. large = ref rhs;
  687. largeLength = rhs._length;
  688. small = ref lhs;
  689. smallLength = lhs._length;
  690. }
  691. int maxResultLength = smallLength + largeLength;
  692. Debug.Assert(unchecked((uint)(maxResultLength)) <= MaxBlockCount);
  693. // Zero out result internal blocks.
  694. Buffer.ZeroMemory((byte*)(result.GetBlocksPointer()), (maxResultLength * sizeof(uint)));
  695. int smallIndex = 0;
  696. int resultStartIndex = 0;
  697. while (smallIndex < smallLength)
  698. {
  699. // Multiply each block of large BigNum.
  700. if (small._blocks[smallIndex] != 0)
  701. {
  702. int largeIndex = 0;
  703. int resultIndex = resultStartIndex;
  704. ulong carry = 0;
  705. do
  706. {
  707. ulong product = result._blocks[resultIndex] + ((ulong)(small._blocks[smallIndex]) * large._blocks[largeIndex]) + carry;
  708. carry = product >> 32;
  709. result._blocks[resultIndex] = (uint)(product);
  710. resultIndex++;
  711. largeIndex++;
  712. }
  713. while (largeIndex < largeLength);
  714. result._blocks[resultIndex] = (uint)(carry);
  715. }
  716. smallIndex++;
  717. resultStartIndex++;
  718. }
  719. if ((maxResultLength > 0) && (result._blocks[maxResultLength - 1] == 0))
  720. {
  721. result._length = (maxResultLength - 1);
  722. }
  723. else
  724. {
  725. result._length = maxResultLength;
  726. }
  727. }
  728. public static void Pow2(uint exponent, out BigInteger result)
  729. {
  730. result = new BigInteger(0);
  731. ShiftLeft(1, exponent, ref result);
  732. }
  733. public static void Pow10(uint exponent, out BigInteger result)
  734. {
  735. // We leverage two arrays - s_Pow10UInt32Table and s_Pow10BigNumTable to speed up the Pow10 calculation.
  736. //
  737. // s_Pow10UInt32Table stores the results of 10^0 to 10^7.
  738. // s_Pow10BigNumTable stores the results of 10^8, 10^16, 10^32, 10^64, 10^128, 10^256, and 10^512
  739. //
  740. // For example, let's say exp = 0b111111. We can split the exp to two parts, one is small exp,
  741. // which 10^smallExp can be represented as uint, another part is 10^bigExp, which must be represented as BigNum.
  742. // So the result should be 10^smallExp * 10^bigExp.
  743. //
  744. // Calculating 10^smallExp is simple, we just lookup the 10^smallExp from s_Pow10UInt32Table.
  745. // But here's a bad news: although uint can represent 10^9, exp 9's binary representation is 1001.
  746. // That means 10^(1011), 10^(1101), 10^(1111) all cannot be stored as uint, we cannot easily say something like:
  747. // "Any bits <= 3 is small exp, any bits > 3 is big exp". So instead of involving 10^8, 10^9 to s_Pow10UInt32Table,
  748. // consider 10^8 and 10^9 as a bigNum, so they fall into s_Pow10BigNumTable. Now we can have a simple rule:
  749. // "Any bits <= 3 is small exp, any bits > 3 is big exp".
  750. //
  751. // For 0b111111, we first calculate 10^(smallExp), which is 10^(7), now we can shift right 3 bits, prepare to calculate the bigExp part,
  752. // the exp now becomes 0b000111.
  753. //
  754. // Apparently the lowest bit of bigExp should represent 10^8 because we have already shifted 3 bits for smallExp, so s_Pow10BigNumTable[0] = 10^8.
  755. // Now let's shift exp right 1 bit, the lowest bit should represent 10^(8 * 2) = 10^16, and so on...
  756. //
  757. // That's why we just need the values of s_Pow10BigNumTable be power of 2.
  758. //
  759. // More details of this implementation can be found at: https://github.com/dotnet/coreclr/pull/12894#discussion_r128890596
  760. // Validate that `s_Pow10BigNumTable` has exactly enough trailing elements to fill a BigInteger (which contains MaxBlockCount + 1 elements)
  761. // We validate here, since this is the only current consumer of the array
  762. Debug.Assert((s_Pow10BigNumTableIndices[s_Pow10BigNumTableIndices.Length - 1] + MaxBlockCount + 2) == s_Pow10BigNumTable.Length);
  763. BigInteger temp1 = new BigInteger(s_Pow10UInt32Table[exponent & 0x7]);
  764. ref BigInteger lhs = ref temp1;
  765. BigInteger temp2 = new BigInteger(0);
  766. ref BigInteger product = ref temp2;
  767. exponent >>= 3;
  768. uint index = 0;
  769. while (exponent != 0)
  770. {
  771. // If the current bit is set, multiply it with the corresponding power of 10
  772. if ((exponent & 1) != 0)
  773. {
  774. // Multiply into the next temporary
  775. fixed (uint* pBigNumEntry = &s_Pow10BigNumTable[s_Pow10BigNumTableIndices[index]])
  776. {
  777. ref BigInteger rhs = ref *(BigInteger*)(pBigNumEntry);
  778. Multiply(ref lhs, ref rhs, ref product);
  779. }
  780. // Swap to the next temporary
  781. ref BigInteger temp = ref product;
  782. product = ref lhs;
  783. lhs = ref temp;
  784. }
  785. // Advance to the next bit
  786. ++index;
  787. exponent >>= 1;
  788. }
  789. result = new BigInteger(0);
  790. result.SetValue(ref lhs);
  791. }
  792. public static void ShiftLeft(ulong input, uint shift, ref BigInteger output)
  793. {
  794. if (shift == 0)
  795. {
  796. return;
  797. }
  798. uint blocksToShift = Math.DivRem(shift, 32, out uint remainingBitsToShift);
  799. if (blocksToShift > 0)
  800. {
  801. // If blocks shifted, we should fill the corresponding blocks with zero.
  802. output.ExtendBlocks(0, blocksToShift);
  803. }
  804. if (remainingBitsToShift == 0)
  805. {
  806. // We shift 32 * n (n >= 1) bits. No remaining bits.
  807. output.ExtendBlock((uint)(input));
  808. uint highBits = (uint)(input >> 32);
  809. if (highBits != 0)
  810. {
  811. output.ExtendBlock(highBits);
  812. }
  813. }
  814. else
  815. {
  816. // Extract the high position bits which would be shifted out of range.
  817. uint highPositionBits = (uint)(input) >> (int)(64 - remainingBitsToShift);
  818. // Shift the input. The result should be stored to current block.
  819. ulong shiftedInput = input << (int)(remainingBitsToShift);
  820. output.ExtendBlock((uint)(shiftedInput));
  821. uint highBits = (uint)(input >> 32);
  822. if (highBits != 0)
  823. {
  824. output.ExtendBlock(highBits);
  825. }
  826. if (highPositionBits != 0)
  827. {
  828. // If the high position bits is not 0, we should store them to next block.
  829. output.ExtendBlock(highPositionBits);
  830. }
  831. }
  832. }
  833. private static unsafe uint AddDivisor(ref BigInteger lhs, int lhsStartIndex, ref BigInteger rhs)
  834. {
  835. int lhsLength = lhs._length;
  836. int rhsLength = rhs._length;
  837. Debug.Assert(lhsLength >= 0);
  838. Debug.Assert(rhsLength >= 0);
  839. Debug.Assert(lhsLength >= rhsLength);
  840. // Repairs the dividend, if the last subtract was too much
  841. ulong carry = 0UL;
  842. for (int i = 0; i < rhsLength; i++)
  843. {
  844. ref uint lhsValue = ref lhs._blocks[lhsStartIndex + i];
  845. ulong digit = (lhsValue + carry) + rhs._blocks[i];
  846. lhsValue = unchecked((uint)digit);
  847. carry = digit >> 32;
  848. }
  849. return (uint)(carry);
  850. }
  851. private static bool DivideGuessTooBig(ulong q, ulong valHi, uint valLo, uint divHi, uint divLo)
  852. {
  853. Debug.Assert(q <= 0xFFFFFFFF);
  854. // We multiply the two most significant limbs of the divisor
  855. // with the current guess for the quotient. If those are bigger
  856. // than the three most significant limbs of the current dividend
  857. // we return true, which means the current guess is still too big.
  858. ulong chkHi = divHi * q;
  859. ulong chkLo = divLo * q;
  860. chkHi = chkHi + (chkLo >> 32);
  861. chkLo = chkLo & uint.MaxValue;
  862. if (chkHi < valHi)
  863. return false;
  864. if (chkHi > valHi)
  865. return true;
  866. if (chkLo < valLo)
  867. return false;
  868. if (chkLo > valLo)
  869. return true;
  870. return false;
  871. }
  872. private static unsafe uint SubtractDivisor(ref BigInteger lhs, int lhsStartIndex, ref BigInteger rhs, ulong q)
  873. {
  874. int lhsLength = lhs._length - lhsStartIndex;
  875. int rhsLength = rhs._length;
  876. Debug.Assert((lhsLength) >= 0);
  877. Debug.Assert(rhsLength >= 0);
  878. Debug.Assert(lhsLength >= rhsLength);
  879. Debug.Assert(q <= uint.MaxValue);
  880. // Combines a subtract and a multiply operation, which is naturally
  881. // more efficient than multiplying and then subtracting...
  882. ulong carry = 0;
  883. for (int i = 0; i < rhsLength; i++)
  884. {
  885. carry += rhs._blocks[i] * q;
  886. uint digit = unchecked((uint)carry);
  887. carry = carry >> 32;
  888. ref uint lhsValue = ref lhs._blocks[lhsStartIndex + i];
  889. if (lhsValue < digit)
  890. {
  891. carry++;
  892. }
  893. lhsValue = unchecked(lhsValue - digit);
  894. }
  895. return (uint)(carry);
  896. }
  897. public void Add(uint value)
  898. {
  899. Add(ref this, value, ref this);
  900. }
  901. public void ExtendBlock(uint blockValue)
  902. {
  903. _blocks[_length] = blockValue;
  904. _length++;
  905. }
  906. public void ExtendBlocks(uint blockValue, uint blockCount)
  907. {
  908. Debug.Assert(blockCount > 0);
  909. if (blockCount == 1)
  910. {
  911. ExtendBlock(blockValue);
  912. return;
  913. }
  914. Buffer.ZeroMemory((byte*)(GetBlocksPointer() + _length), ((blockCount - 1) * sizeof(uint)));
  915. _length += (int)(blockCount);
  916. _blocks[_length - 1] = blockValue;
  917. }
  918. public uint GetBlock(uint index)
  919. {
  920. Debug.Assert(index < _length);
  921. return _blocks[index];
  922. }
  923. public int GetLength()
  924. {
  925. return _length;
  926. }
  927. public bool IsOne()
  928. {
  929. return (_length == 1)
  930. && (_blocks[0] == 1);
  931. }
  932. public bool IsZero()
  933. {
  934. return _length == 0;
  935. }
  936. public void Multiply(uint value)
  937. {
  938. Multiply(ref this, value, ref this);
  939. }
  940. public void Multiply(ref BigInteger value)
  941. {
  942. var result = new BigInteger(0);
  943. Multiply(ref this, ref value, ref result);
  944. Buffer.Memcpy((byte*)(GetBlocksPointer()), (byte*)(result.GetBlocksPointer()), (result._length) * sizeof(uint));
  945. _length = result._length;
  946. }
  947. public void Multiply10()
  948. {
  949. if (IsZero())
  950. {
  951. return;
  952. }
  953. int index = 0;
  954. int length = _length;
  955. ulong carry = 0;
  956. while (index < length)
  957. {
  958. var block = (ulong)(_blocks[index]);
  959. ulong product = (block << 3) + (block << 1) + carry;
  960. carry = product >> 32;
  961. _blocks[index] = (uint)(product);
  962. index++;
  963. }
  964. if (carry != 0)
  965. {
  966. Debug.Assert(unchecked((uint)(_length)) + 1 <= MaxBlockCount);
  967. _blocks[index] = (uint)(carry);
  968. _length += 1;
  969. }
  970. }
  971. public void MultiplyPow10(uint exponent)
  972. {
  973. Pow10(exponent, out BigInteger poweredValue);
  974. if (poweredValue._length == 1)
  975. {
  976. Multiply(poweredValue._blocks[0]);
  977. }
  978. else
  979. {
  980. Multiply(ref poweredValue);
  981. }
  982. }
  983. public void SetUInt32(uint value)
  984. {
  985. _blocks[0] = value;
  986. _length = 1;
  987. }
  988. public void SetUInt64(ulong value)
  989. {
  990. var lower = (uint)(value);
  991. var upper = (uint)(value >> 32);
  992. _blocks[0] = lower;
  993. _blocks[1] = upper;
  994. _length = (upper == 0) ? 1 : 2;
  995. }
  996. public void SetValue(ref BigInteger rhs)
  997. {
  998. int rhsLength = rhs._length;
  999. Buffer.Memcpy((byte*)(GetBlocksPointer()), (byte*)(rhs.GetBlocksPointer()), (rhsLength * sizeof(uint)));
  1000. _length = rhsLength;
  1001. }
  1002. public void SetZero()
  1003. {
  1004. _length = 0;
  1005. }
  1006. public void ShiftLeft(uint shift)
  1007. {
  1008. // Process blocks high to low so that we can safely process in place
  1009. var length = _length;
  1010. if ((length == 0) || (shift == 0))
  1011. {
  1012. return;
  1013. }
  1014. uint blocksToShift = Math.DivRem(shift, 32, out uint remainingBitsToShift);
  1015. // Copy blocks from high to low
  1016. int readIndex = (length - 1);
  1017. int writeIndex = readIndex + (int)(blocksToShift);
  1018. // Check if the shift is block aligned
  1019. if (remainingBitsToShift == 0)
  1020. {
  1021. Debug.Assert(writeIndex < MaxBlockCount);
  1022. while (readIndex >= 0)
  1023. {
  1024. _blocks[writeIndex] = _blocks[readIndex];
  1025. readIndex--;
  1026. writeIndex--;
  1027. }
  1028. _length += (int)(blocksToShift);
  1029. // Zero the remaining low blocks
  1030. Buffer.ZeroMemory((byte*)(GetBlocksPointer()), (blocksToShift * sizeof(uint)));
  1031. }
  1032. else
  1033. {
  1034. // We need an extra block for the partial shift
  1035. writeIndex++;
  1036. Debug.Assert(writeIndex < MaxBlockCount);
  1037. // Set the length to hold the shifted blocks
  1038. _length = writeIndex + 1;
  1039. // Output the initial blocks
  1040. uint lowBitsShift = (32 - remainingBitsToShift);
  1041. uint highBits = 0;
  1042. uint block = _blocks[readIndex];
  1043. uint lowBits = block >> (int)(lowBitsShift);
  1044. while (readIndex > 0)
  1045. {
  1046. _blocks[writeIndex] = highBits | lowBits;
  1047. highBits = block << (int)(remainingBitsToShift);
  1048. --readIndex;
  1049. --writeIndex;
  1050. block = _blocks[readIndex];
  1051. lowBits = block >> (int)lowBitsShift;
  1052. }
  1053. // Output the final blocks
  1054. _blocks[writeIndex] = highBits | lowBits;
  1055. _blocks[writeIndex - 1] = block << (int)(remainingBitsToShift);
  1056. // Zero the remaining low blocks
  1057. Buffer.ZeroMemory((byte*)(GetBlocksPointer()), (blocksToShift * sizeof(uint)));
  1058. // Check if the terminating block has no set bits
  1059. if (_blocks[_length - 1] == 0)
  1060. {
  1061. _length--;
  1062. }
  1063. }
  1064. }
  1065. public ulong ToUInt64()
  1066. {
  1067. if (_length > 1)
  1068. {
  1069. return ((ulong)(_blocks[1]) << 32) + _blocks[0];
  1070. }
  1071. if (_length > 0)
  1072. {
  1073. return _blocks[0];
  1074. }
  1075. return 0;
  1076. }
  1077. private uint* GetBlocksPointer()
  1078. {
  1079. // This is safe to do since we are a ref struct
  1080. return (uint*)(Unsafe.AsPointer(ref _blocks[0]));
  1081. }
  1082. }
  1083. }
  1084. }