-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
BigIntegerCalculator.Utils.cs
59 lines (47 loc) · 1.75 KB
/
BigIntegerCalculator.Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
namespace System.Numerics
{
internal static partial class BigIntegerCalculator
{
#if DEBUG
// Mutable for unit testing...
internal static
#else
internal const
#endif
int StackAllocThreshold = 64;
public static int Compare(ReadOnlySpan<uint> left, ReadOnlySpan<uint> right)
{
Debug.Assert(left.Length <= right.Length || left.Slice(right.Length).ContainsAnyExcept(0u));
Debug.Assert(left.Length >= right.Length || right.Slice(left.Length).ContainsAnyExcept(0u));
if (left.Length != right.Length)
return left.Length < right.Length ? -1 : 1;
int iv = left.Length;
while (--iv >= 0 && left[iv] == right[iv]) ;
if (iv < 0)
return 0;
return left[iv] < right[iv] ? -1 : 1;
}
private static int ActualLength(ReadOnlySpan<uint> value)
{
// Since we're reusing memory here, the actual length
// of a given value may be less then the array's length
int length = value.Length;
while (length > 0 && value[length - 1] == 0)
--length;
return length;
}
private static int Reduce(Span<uint> bits, ReadOnlySpan<uint> modulus)
{
// Executes a modulo operation using the divide operation.
if (bits.Length >= modulus.Length)
{
Divide(bits, modulus, default);
return ActualLength(bits.Slice(0, modulus.Length));
}
return bits.Length;
}
}
}