RyuJIT - Bug with ushort and Equals override (64bit)
Asked Answered
A

1

1

While porting a 32bit managed application to 64bit I've observed a strange behavior by a Equals() override within a struct.

You find the a repro at github.

To reproduce the bug, you should compile the library with "optimize" flag on. This is default on the Release config. The consuming TestApp must be compiled without any optimization. Prefer 32 bit must be disabled to start as 64bit App. See notes on github!

The library contains a struct that implements the IEquatable interface which is implemented with a simple line of code.

    public bool Equals(StructWithValue other)
    {
        return value.Equals(other.value);
    }

This code calls the Equals method of the ushort/UInt16 type. If you build the solution with the proposed configuration, all values above 32767 will fail. You call Equal on a ushort value of 32768 and the value of 'other' is also 32768. But Equals() will return false for all values above 32767.

If you change the method to use the '==' operator the code will work. Also if you change the type from struct to class the the code runs as expected.

    public bool Equals(StructWithValue other)
    {
        return value == othervalue;
    }

I think this is a bug in the RyuJIT-Compiler. If I use the legacy JIT-Compiler the code works fine.

Tested with Visual Studio 2015 and TargetFramework 4.6.2 on different windows versions.

Arteriotomy answered 18/12, 2016 at 20:38 Comment(4)
Incidentally your implementation of object.Equals is very interesting. Had this been a reference type I would be reporting it is bugged.Stere
Your repro instructions are a bit misleading and got me on the wrong track, just recommend to use the Debug build. Its behavior is very similar to this bug, also a codegen problem and also related to sign extension and also disappears when the optimizer is enabled. Be sure to file it separately however.Trehala
Interesting and all, but this doesn't appear to be a question. Unless it's implicitly "am I right".Henigman
Yes, it's like the question "Am I right?"Arteriotomy
S
1

Bug confirmed by inspection.

I can't imagine what other answer could appear here. If the bug were unreal you would get an answer showing you where your code is wrong, but your code is not wrong.

Stere answered 18/12, 2016 at 21:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.