Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/9.0-staging] Add a missing = in BigInteger.cs #109732

Merged
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 @@ -544,9 +544,9 @@ private BigInteger(Span<uint> value)
isNegative = true;
length = value.LastIndexOfAnyExcept(uint.MaxValue) + 1;

if ((length == 0) || ((int)value[length - 1] > 0))
if ((length == 0) || ((int)value[length - 1] >= 0))
{
// We ne need to preserve the sign bit
// We need to preserve the sign bit
length++;
}
Debug.Assert((int)value[length - 1] < 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,11 @@ public static List<byte> GetBytes(BitArray ba)
}

public static string Print(byte[] bytes)
{
return Print(bytes.AsSpan());
}

public static string Print(ReadOnlySpan<byte> bytes)
{
string ret = "make ";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.Numerics.Tests
{
public static partial class SampleGeneration
{
public static IEnumerable<ReadOnlyMemory<T>> EnumerateSequence<T>(IEnumerable<T> elementSource, int minLength, int maxLengthExclusive)
{
return EnumerateSequence(elementSource.ToArray(), minLength, maxLengthExclusive);
}

public static IEnumerable<ReadOnlyMemory<T>> EnumerateSequence<T>(T[] elementSource, int minLength, int maxLengthExclusive)
{
for (var i = minLength; maxLengthExclusive > i; ++i)
{
foreach (var item in EnumerateSequence(elementSource, i))
{
yield return item;
}
}
}

public static IEnumerable<ReadOnlyMemory<T>> EnumerateSequence<T>(IEnumerable<T> elementSource, int length)
{
return EnumerateSequence(elementSource.ToArray(), length);
}

public static IEnumerable<ReadOnlyMemory<T>> EnumerateSequence<T>(T[] elementSource, int length)
{
var a = new T[length];
var r = new ReadOnlyMemory<T>(a);
foreach (var _ in EnumerateSequenceYieldsCurrentCount(elementSource, a))
{
yield return r;
}
}

private static IEnumerable<long> EnumerateSequenceYieldsCurrentCount<T>(T[] elementSource, T[] buffer)
{
var c = 0L;
var b = elementSource.Length;
if (b != 0)
{
var stack = new int[buffer.Length];
for (var i = 0; i < buffer.Length; ++i)
{
buffer[i] = elementSource[0];
}
{
L:;
yield return c++;
for (var i = 0; stack.Length != i; ++i)
{
var en = ++stack[i];
if (b == en)
{
}
else
{
buffer[i] = elementSource[en];
for (; 0 <= --i;)
{
buffer[i] = elementSource[0];
stack[i] = 0;
}
goto L;
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Xunit;

namespace System.Numerics.Tests
{

public static partial class UInt32Samples
{
private static readonly uint[] set1 = new uint[] {
0B00000000000000000000000000000000U,
0B00000000000000000000000000000001U,
0B00111111111111111111111111111110U,
0B00111111111111111111111111111111U,

0B01000000000000000000000000000000U,
0B01000000000000000000000000000001U,
0B01111111111111111111111111111110U,
0B01111111111111111111111111111111U,

0B10000000000000000000000000000000U,
0B10000000000000000000000000000001U,
0B10111111111111111111111111111110U,
0B10111111111111111111111111111111U,

0B11000000000000000000000000000000U,
0B11000000000000000000000000000001U,
0B11111111111111111111111111111110U,
0B11111111111111111111111111111111U,
};

private static IEnumerable<uint> GetSet1()
{
foreach (var item in set1)
{
yield return item;
}
}

public static readonly IEnumerable<uint> Set1 = GetSet1();

private static readonly uint[] set2 = new uint[] {
0B00000000000000000000000000000000U,
0B00000000000000000000000000000001U,
0B00000000000000000100000000000000U,
0B00000000000000000100000000000001U,

0B00000000000000010000000000000000U,
0B00000000000000010000000000000001U,
0B00000000000000010100000000000000U,
0B00000000000000010100000000000001U,

0B00111111111111111111111111111110U,
0B00111111111111111111111111111111U,
0B00111111111111111011111111111110U,
0B00111111111111111011111111111111U,

0B00111111111111101111111111111110U,
0B00111111111111101111111111111111U,
0B00111111111111101011111111111110U,
0B00111111111111101011111111111111U,

0B01000000000000000000000000000000U,
0B01000000000000000000000000000001U,
0B01000000000000000100000000000000U,
0B01000000000000000100000000000001U,

0B01000000000000010000000000000000U,
0B01000000000000010000000000000001U,
0B01000000000000010100000000000000U,
0B01000000000000010100000000000001U,

0B01111111111111111111111111111110U,
0B01111111111111111111111111111111U,
0B01111111111111111011111111111110U,
0B01111111111111111011111111111111U,

0B01111111111111101111111111111110U,
0B01111111111111101111111111111111U,
0B01111111111111101011111111111110U,
0B01111111111111101011111111111111U,

0B10000000000000000000000000000000U,
0B10000000000000000000000000000001U,
0B10000000000000000100000000000000U,
0B10000000000000000100000000000001U,

0B10000000000000010000000000000000U,
0B10000000000000010000000000000001U,
0B10000000000000010100000000000000U,
0B10000000000000010100000000000001U,

0B10111111111111111111111111111110U,
0B10111111111111111111111111111111U,
0B10111111111111111011111111111110U,
0B10111111111111111011111111111111U,

0B10111111111111101111111111111110U,
0B10111111111111101111111111111111U,
0B10111111111111101011111111111110U,
0B10111111111111101011111111111111U,

0B11000000000000000000000000000000U,
0B11000000000000000000000000000001U,
0B11000000000000000100000000000000U,
0B11000000000000000100000000000001U,

0B11000000000000010000000000000000U,
0B11000000000000010000000000000001U,
0B11000000000000010100000000000000U,
0B11000000000000010100000000000001U,

0B11111111111111111111111111111110U,
0B11111111111111111111111111111111U,
0B11111111111111111011111111111110U,
0B11111111111111111011111111111111U,

0B11111111111111101111111111111110U,
0B11111111111111101111111111111111U,
0B11111111111111101011111111111110U,
0B11111111111111101011111111111111U,
};

private static IEnumerable<uint> GetSet2()
{
foreach (var item in set2)
{
yield return item;
}
}

public static readonly IEnumerable<uint> Set2 = GetSet2();
}
}
44 changes: 44 additions & 0 deletions src/libraries/System.Runtime.Numerics/tests/BigInteger/op_and.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Xunit;

namespace System.Numerics.Tests
Expand Down Expand Up @@ -117,6 +120,42 @@ public static void RunAndTests()
}
}

[Fact]
public void Issue109669()
{
// Operations on numbers whose result is of the form 0xFFFFFFFF 00000000 ... 00000000
// in two's complement.

Assert.Equal(-4294967296, new BigInteger(-4294967296) & new BigInteger(-1919810));
Assert.Equal(-4294967296, new BigInteger(-4042322161) & new BigInteger(-252645136));
Assert.Equal(-4294967296, new BigInteger(-8589934592) | new BigInteger(-21474836480));

BigInteger a = new BigInteger(MemoryMarshal.AsBytes([uint.MaxValue, 0u, 0u]), isBigEndian: true);
Assert.Equal(a, a & a);
Assert.Equal(a, a | a);
Assert.Equal(a, a ^ 0);
}

[Fact]
public static void RunAndTestsForSampleSet1()
{
var s = SampleGeneration.EnumerateSequence(UInt32Samples.Set1, 2);
var t = SampleGeneration.EnumerateSequence(UInt32Samples.Set1, 2);

foreach (var i in s)
{
foreach (var j in t)
{
var a = MemoryMarshal.AsBytes(i.Span);
var b = MemoryMarshal.AsBytes(j.Span);

VerifyAndString(Print(a) + Print(b) + "b&");

VerifyAndString(Print(b) + Print(a) + "b&");
}
}
}

private static void VerifyAndString(string opstring)
{
StackCalc sc = new StackCalc(opstring);
Expand All @@ -139,5 +178,10 @@ private static string Print(byte[] bytes)
{
return MyBigIntImp.Print(bytes);
}

private static string Print(ReadOnlySpan<byte> bytes)
{
return MyBigIntImp.Print(bytes);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@
<Compile Include="BigInteger\pow.cs" />
<Compile Include="BigInteger\properties.cs" />
<Compile Include="BigInteger\remainder.cs" />
<Compile Include="BigInteger\SampleGeneration.cs" />
<Compile Include="BigInteger\sign.cs" />
<Compile Include="BigInteger\stackcalculator.cs" />
<Compile Include="BigInteger\ToByteArray.cs" />
<Compile Include="BigInteger\TryWriteBytes.cs" />
<Compile Include="BigInteger\UInt32Samples.cs" />
<Compile Include="ComplexTests.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Loading