forked from fredrikhr/dotnet-corefx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dotnet#1436 from axelheer/biginteger-performance
Improve performance of BigInteger.Multiply
- Loading branch information
Showing
8 changed files
with
505 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.AddSub.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System.Diagnostics; | ||
using System.Security; | ||
|
||
namespace System.Numerics | ||
{ | ||
internal static partial class BigIntegerCalculator | ||
{ | ||
[SecuritySafeCritical] | ||
private unsafe static void Add(uint* left, int leftLength, | ||
uint* right, int rightLength, | ||
uint* bits, int bitsLength) | ||
{ | ||
Debug.Assert(leftLength >= 0); | ||
Debug.Assert(rightLength >= 0); | ||
Debug.Assert(leftLength >= rightLength); | ||
Debug.Assert(bitsLength == leftLength + 1); | ||
|
||
// Executes the "grammar-school" algorithm for computing z = a + b. | ||
// While calculating z_i = a_i + b_i we take care of overflow: | ||
// Since a_i + b_i + c <= 2(2^32 - 1) + 1 = 2^33 - 1, our carry c | ||
// has always the value 1 or 0; hence, we're safe here. | ||
|
||
int i = 0; | ||
long carry = 0L; | ||
|
||
// adds the bits | ||
for (; i < rightLength; i++) | ||
{ | ||
long digit = (left[i] + carry) + right[i]; | ||
bits[i] = (uint)digit; | ||
carry = digit >> 32; | ||
} | ||
for (; i < leftLength; i++) | ||
{ | ||
long digit = left[i] + carry; | ||
bits[i] = (uint)digit; | ||
carry = digit >> 32; | ||
} | ||
bits[i] = (uint)carry; | ||
} | ||
|
||
[SecuritySafeCritical] | ||
private unsafe static void AddSelf(uint* left, int leftLength, | ||
uint* right, int rightLength) | ||
{ | ||
Debug.Assert(leftLength >= 0); | ||
Debug.Assert(rightLength >= 0); | ||
Debug.Assert(leftLength >= rightLength); | ||
|
||
// Executes the "grammar-school" algorithm for computing z = a + b. | ||
// Same as above, but we're writing the result directly to a and | ||
// stop execution, if we're out of b and c is already 0. | ||
|
||
int i = 0; | ||
long carry = 0L; | ||
|
||
// adds the bits | ||
for (; i < rightLength; i++) | ||
{ | ||
long digit = (left[i] + carry) + right[i]; | ||
left[i] = (uint)digit; | ||
carry = digit >> 32; | ||
} | ||
for (; carry != 0 && i < leftLength; i++) | ||
{ | ||
long digit = left[i] + carry; | ||
left[i] = (uint)digit; | ||
carry = digit >> 32; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.