File: System\Numerics\BigIntegerCalculator.Utils.cs
Web Access
Project: src\src\libraries\System.Runtime.Numerics\src\System.Runtime.Numerics.csproj (System.Runtime.Numerics)
// 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;
        }
 
        public 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;
        }
    }
}