Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Restore some comment formatting for BigInteger #15726

Merged
merged 2 commits into from
Feb 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@

namespace System.Numerics
{
// ATTENTION: always pass BitsBuffer by reference,
// it's a structure for performance reasons. Furthermore
// it's a mutable one, so use it only with care!

internal static partial class BigIntegerCalculator
{
/// <summary>
/// To spare memory allocations a buffer helps reusing memory!
/// We just create the target array twice and switch between every
/// operation. In order to not compute unnecessarily with all those
/// leading zeros we take care of the current actual length.
/// </summary>
// To spare memory allocations a buffer helps reusing memory!
// We just create the target array twice and switch between every
// operation. In order to not compute unnecessarily with all those
// leading zeros we take care of the current actual length.

internal struct BitsBuffer
{
private uint[] _bits;
Expand Down Expand Up @@ -92,6 +95,7 @@ public void Reduce(ref FastReducer reducer)
{
// Executes a modulo operation using an optimized reducer.
// Thus, no need of any switching here, happens in-line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these blank lines being added?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I wrote these lines of code I put some extra space below comments with a more "general character". Means: if a comment is just related to the line below, there is no blank line. If a comment is an overall note to better understand the current function, it is an extra block. Same "problem" as when to group some lines of code using new-lines or not.

As I said, they got lost and I did an revert, re-added them. I can remove them, if the change is too peculiar.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Ok. Some of them to me personally seem like they'd be better without the blank line, but that's just a matter of style. What you have is fine. Thanks.

_length = reducer.Reduce(_bits, _length);
}

Expand Down Expand Up @@ -121,6 +125,7 @@ public unsafe void Reduce(ref BitsBuffer modulus)
{
// Executes a modulo operation using the divide operation.
// Thus, no need of any switching here, happens in-line.

if (_length >= modulus._length)
{
fixed (uint* b = _bits, m = modulus._bits)
Expand Down Expand Up @@ -201,6 +206,7 @@ private void Apply(ref BitsBuffer temp, int maxLength)

// Resets this and switches this and temp afterwards.
// The caller assumed an empty temp, the next will too.

Array.Clear(_bits, 0, _length);

uint[] t = temp._bits;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public static uint[] Divide(uint[] left, uint right)
Debug.Assert(left.Length >= 1);

// Same as above, but only computing the quotient.

uint[] quotient = new uint[left.Length];

ulong carry = 0UL;
Expand All @@ -60,6 +61,7 @@ public static uint Remainder(uint[] left, uint right)
Debug.Assert(left.Length >= 1);

// Same as above, but only computing the remainder.

ulong carry = 0UL;
for (int i = left.Length - 1; i >= 0; i--)
{
Expand Down Expand Up @@ -110,7 +112,9 @@ public static unsafe uint[] Divide(uint[] left, uint[] right)
Debug.Assert(left.Length >= right.Length);

// Same as above, but only returning the quotient.

// NOTE: left will get overwritten, we need a local copy

uint[] localLeft = CreateCopy(left);
uint[] bits = new uint[left.Length - right.Length + 1];

Expand All @@ -134,7 +138,9 @@ public static unsafe uint[] Remainder(uint[] left, uint[] right)
Debug.Assert(left.Length >= right.Length);

// Same as above, but only returning the remainder.

// NOTE: left will get overwritten, we need a local copy

uint[] localLeft = CreateCopy(left);

fixed (uint* l = localLeft, r = right)
Expand Down Expand Up @@ -243,6 +249,7 @@ private static unsafe uint AddDivisor(uint* left, int leftLength,
Debug.Assert(leftLength >= rightLength);

// Repairs the dividend, if the last subtract was too much

ulong carry = 0UL;

for (int i = 0; i < rightLength; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace System.Numerics
{
internal static partial class BigIntegerCalculator
{
/// <summary>
/// If we need to reduce by a certain modulus again and again, it's much
/// more efficient to do this with multiplication operations. This is
/// possible, if we do some pre-computations first...
/// see https://en.wikipedia.org/wiki/Barrett_reduction
/// </summary>
// If we need to reduce by a certain modulus again and again, it's much
// more efficient to do this with multiplication operations. This is
// possible, if we do some pre-computations first...

// see https://en.wikipedia.org/wiki/Barrett_reduction

internal struct FastReducer
{
private readonly uint[] _modulus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ internal static partial class BigIntegerCalculator
public static uint Gcd(uint left, uint right)
{
// Executes the classic Euclidean algorithm.

// https://en.wikipedia.org/wiki/Euclidean_algorithm

while (right != 0)
{
uint temp = left % right;
Expand All @@ -25,6 +27,7 @@ public static uint Gcd(uint left, uint right)
public static ulong Gcd(ulong left, ulong right)
{
// Same as above, but for 64-bit values.

while (right > 0xFFFFFFFF)
{
ulong temp = left % right;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace System.Numerics
{
internal static partial class BigIntegerCalculator
{
/// <summary>
/// Executes different exponentiation algorithms, which are
/// basically based on the classic square-and-multiply method.
/// https://en.wikipedia.org/wiki/Exponentiation_by_squaring
/// </summary>
// Executes different exponentiation algorithms, which are
// based on the classic square-and-multiply method.

// https://en.wikipedia.org/wiki/Exponentiation_by_squaring

public static uint[] Pow(uint value, uint power)
{
// The basic pow method for a 32-bit integer.
Expand All @@ -31,6 +31,7 @@ public static uint[] Pow(uint[] value, uint power)
// The basic pow method for a big integer.
// To spare memory allocations we first roughly
// estimate an upper bound for our buffers.

int size = PowBound(power, value.Length, 1);
BitsBuffer v = new BitsBuffer(size, value);
return PowCore(power, ref v);
Expand All @@ -39,6 +40,7 @@ public static uint[] Pow(uint[] value, uint power)
private static uint[] PowCore(uint power, ref BitsBuffer value)
{
// Executes the basic pow algorithm.

int size = value.GetSize();

BitsBuffer temp = new BitsBuffer(size, 0);
Expand All @@ -54,6 +56,7 @@ private static int PowBound(uint power, int valueLength,
{
// The basic pow algorithm, but instead of squaring
// and multiplying we just sum up the lengths.

while (power != 0)
{
checked
Expand All @@ -73,6 +76,7 @@ private static void PowCore(uint power, ref BitsBuffer value,
ref BitsBuffer result, ref BitsBuffer temp)
{
// The basic pow algorithm using square-and-multiply.

while (power != 0)
{
if ((power & 1) == 1)
Expand All @@ -87,6 +91,7 @@ public static uint Pow(uint value, uint power, uint modulus)
{
// The 32-bit modulus pow method for a 32-bit integer
// raised by a 32-bit integer...

return PowCore(power, modulus, value, 1);
}

Expand All @@ -96,6 +101,7 @@ public static uint Pow(uint[] value, uint power, uint modulus)

// The 32-bit modulus pow method for a big integer
// raised by a 32-bit integer...

uint v = Remainder(value, modulus);
return PowCore(power, modulus, v, 1);
}
Expand All @@ -106,6 +112,7 @@ public static uint Pow(uint value, uint[] power, uint modulus)

// The 32-bit modulus pow method for a 32-bit integer
// raised by a big integer...

return PowCore(power, modulus, value, 1);
}

Expand All @@ -116,6 +123,7 @@ public static uint Pow(uint[] value, uint[] power, uint modulus)

// The 32-bit modulus pow method for a big integer
// raised by a big integer...

uint v = Remainder(value, modulus);
return PowCore(power, modulus, v, 1);
}
Expand All @@ -125,6 +133,7 @@ private static uint PowCore(uint[] power, uint modulus,
{
// The 32-bit modulus pow algorithm for all but
// the last power limb using square-and-multiply.

for (int i = 0; i < power.Length - 1; i++)
{
uint p = power[i];
Expand All @@ -145,6 +154,7 @@ private static uint PowCore(uint power, uint modulus,
{
// The 32-bit modulus pow algorithm for the last or
// the only power limb using square-and-multiply.

while (power != 0)
{
if ((power & 1) == 1)
Expand All @@ -163,6 +173,7 @@ public static uint[] Pow(uint value, uint power, uint[] modulus)

// The big modulus pow method for a 32-bit integer
// raised by a 32-bit integer...

int size = modulus.Length + modulus.Length;
BitsBuffer v = new BitsBuffer(size, value);
return PowCore(power, modulus, ref v);
Expand Down Expand Up @@ -221,6 +232,7 @@ private static uint[] PowCore(uint[] power, uint[] modulus,
ref BitsBuffer value)
{
// Executes the big pow algorithm.

int size = value.GetSize();

BitsBuffer temp = new BitsBuffer(size, 0);
Expand All @@ -243,6 +255,7 @@ private static uint[] PowCore(uint power, uint[] modulus,
ref BitsBuffer value)
{
// Executes the big pow algorithm.

int size = value.GetSize();

BitsBuffer temp = new BitsBuffer(size, 0);
Expand All @@ -267,8 +280,10 @@ private static void PowCore(uint[] power, uint[] modulus,
{
// The big modulus pow algorithm for all but
// the last power limb using square-and-multiply.

// NOTE: we're using an ordinary remainder here,
// since the reducer overhead doesn't pay off.

for (int i = 0; i < power.Length - 1; i++)
{
uint p = power[i];
Expand All @@ -295,8 +310,10 @@ private static void PowCore(uint power, uint[] modulus,
{
// The big modulus pow algorithm for the last or
// the only power limb using square-and-multiply.

// NOTE: we're using an ordinary remainder here,
// since the reducer overhead doesn't pay off.

while (power != 0)
{
if ((power & 1) == 1)
Expand All @@ -313,12 +330,16 @@ private static void PowCore(uint power, uint[] modulus,
}
}

private static void PowCore(uint[] power, ref FastReducer reducer, ref BitsBuffer value, ref BitsBuffer result, ref BitsBuffer temp)
private static void PowCore(uint[] power, ref FastReducer reducer,
ref BitsBuffer value, ref BitsBuffer result,
ref BitsBuffer temp)
{
// The big modulus pow algorithm for all but
// the last power limb using square-and-multiply.

// NOTE: we're using a special reducer here,
// since it's additional overhead does pay off.

for (int i = 0; i < power.Length - 1; i++)
{
uint p = power[i];
Expand All @@ -345,8 +366,10 @@ private static void PowCore(uint power, ref FastReducer reducer,
{
// The big modulus pow algorithm for the last or
// the only power limb using square-and-multiply.

// NOTE: we're using a special reducer here,
// since it's additional overhead does pay off.

while (power != 0)
{
if ((power & 1) == 1)
Expand All @@ -367,6 +390,7 @@ private static int ActualLength(uint[] value)
{
// Since we're reusing memory here, the actual length
// of a given value may be less then the array's length

return ActualLength(value, value.Length);
}

Expand Down