Advertisement
KvanTTT

.NET BigInteger Multiplication

Feb 15th, 2014
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1. public static BigInteger operator *(BigInteger left, BigInteger right)
  2. {
  3.     int sign = 1;
  4.     BigIntegerBuilder bigIntegerBuilder = new BigIntegerBuilder(left, ref sign);
  5.     BigIntegerBuilder bigIntegerBuilder2 = new BigIntegerBuilder(right, ref sign);
  6.     bigIntegerBuilder.Mul(ref bigIntegerBuilder2);
  7.     return bigIntegerBuilder.GetInteger(sign);
  8. }
  9.  
  10. ...
  11.  
  12. public void Mul(ref BigIntegerBuilder regMul)
  13. {
  14.     if (regMul._iuLast == 0)
  15.     {
  16.         this.Mul(regMul._uSmall);
  17.         return;
  18.     }
  19.     if (this._iuLast == 0)
  20.     {
  21.         uint uSmall = this._uSmall;
  22.         if (uSmall == 1u)
  23.         {
  24.             this = new BigIntegerBuilder(ref regMul);
  25.             return;
  26.         }
  27.         if (uSmall != 0u)
  28.         {
  29.             this.Load(ref regMul, 1);
  30.             this.Mul(uSmall);
  31.             return;
  32.         }
  33.     }
  34.     else
  35.     {
  36.         int num = this._iuLast + 1;
  37.         this.SetSizeKeep(num + regMul._iuLast, 1);
  38.         int num2 = num;
  39.         while (--num2 >= 0)
  40.         {
  41.             uint uMul = this._rgu[num2];
  42.             this._rgu[num2] = 0u;
  43.             uint num3 = 0u;
  44.             for (int i = 0; i <= regMul._iuLast; i++)
  45.             {
  46.                 num3 = BigIntegerBuilder.AddMulCarry(ref this._rgu[num2 + i], regMul._rgu[i], uMul, num3);
  47.             }
  48.             if (num3 != 0u)
  49.             {
  50.                 int num4 = num2 + regMul._iuLast + 1;
  51.                 while (num3 != 0u && num4 <= this._iuLast)
  52.                 {
  53.                     num3 = BigIntegerBuilder.AddCarry(ref this._rgu[num4], 0u, num3);
  54.                     num4++;
  55.                 }
  56.                 if (num3 != 0u)
  57.                 {
  58.                     this.SetSizeKeep(this._iuLast + 2, 0);
  59.                     this._rgu[this._iuLast] = num3;
  60.                 }
  61.             }
  62.         }
  63.     }
  64. }
  65.  
  66. ...
  67.  
  68. private static uint AddMulCarry(ref uint uAdd, uint uMul1, uint uMul2, uint uCarry)
  69. {
  70.     ulong num = (ulong)uMul1 * (ulong)uMul2 + (ulong)uAdd + (ulong)uCarry;
  71.     uAdd = (uint)num;
  72.     return (uint)(num >> 32);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement