Skip to content

.net 10 regression: Different output for algorithm using IBinaryInteger Zero and One #121572

@rmja

Description

@rmja

Description

In the process of upgrading from .net 9 to .net 10 I have seen one of my tests fail. It utilizes the IBinaryInteger<T> interface with known values of One and Zero to generate a polynomial string for a crc polynomial.
The generated string now differs in .net 9 and .net 10.

I can of cause simply change the algorithm now that I have updated to .net 10, but I wanted to let you know that this is different across the two versions.

Reproduction Steps

Consider this small C# program:

using System.Numerics;
using System.Text;

var xmodem = new CrcAlgorithm<ushort>(16, 0x1021);

Console.WriteLine(xmodem.GetPolynomialString());

record CrcAlgorithm<T>(int Width, T Polynomial)
    where T : IBinaryInteger<T>
{
    public string GetPolynomialString()
    {
        var sb = new StringBuilder();
        AppendTerm(sb, Width);
        for (var i = Width; i >= 0; i--)
        {
            var term = Polynomial >> i;
            if ((term & T.One) != T.Zero)
            {
                AppendTerm(sb, i);
            }
        }
        if (sb.Length == 0)
        {
            sb.Append('0');
        }
        return sb.ToString();

        static void AppendTerm(StringBuilder sb, int degree)
        {
            if (sb.Length > 0)
            {
                sb.Append(" + ");
            }
            if (degree == 0)
            {
                sb.Append(1);
            }
            else
            {
                sb.Append('x');
                if (degree > 1)
                {
                    sb.Append('^').Append(degree);
                }
            }
        }
    }
}

In .net 9 this produced (as expected):

dotnet run -f net9.0
x^16 + x^12 + x^5 + 1

In .net 10 this now produces:

dotnet run -f net10.0
x^16 + x^16 + x^12 + x^5 + 1

It contains an extra inclusion of the x^16 term.

Expected behavior

I expect that the generated string for both .net version produce the same result.

Actual behavior

The output is not the same for the two versions.

Regression?

Yes, this produces the desired output in .net 9.

Known Workarounds

Rewrite the code for generating the polynomial string.

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions