Bug 58995 - Integer conversion inconsistent with .NET
Summary: Integer conversion inconsistent with .NET
Status: RESOLVED ANSWERED
Alias: None
Product: Runtime
Classification: Mono
Component: JIT ()
Version: master
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Alexis Christoforides
URL:
Depends on:
Blocks:
 
Reported: 2017-08-24 03:40 UTC by Happypig375
Modified: 2017-10-19 19:36 UTC (History)
8 users (show)

Tags: bugpool-archive
Is this bug a regression?: ---
Last known good build:


Attachments
mono_fconv_u4.c (352 bytes, text/plain)
2017-09-16 03:17 UTC, Alexis Christoforides
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Happypig375 2017-08-24 03:40:11 UTC
.NET Framework:
`var x = 1073741826.0; return (int)(uint)x;` returns 1073741826
`var x = -1073741826.0; return (int)(uint)x;` returns -1073741826
Xamarin.Android:
`var x = 1073741826.0; return (int)(uint)x;` returns 1073741826
`var x = -1073741826.0; return (int)(uint)x;` returns 0
Comment 1 Happypig375 2017-08-24 03:41:03 UTC
Linked from https://github.com/sebastienros/jint/issues/407.
Comment 2 Happypig375 2017-08-24 04:18:48 UTC
Please fix asap, I spent 3 days to find this bug
Comment 3 Jon Douglas [MSFT] 2017-08-24 16:05:04 UTC
I can CONFIRM the behavior between a .NET Core Console project and Xamarin.Android Project:

.NET Core Console:

            var x = 1073741826.0;
            Console.WriteLine((int)(uint)x); //returns 1073741826
            var y = -1073741826.0;
            Console.WriteLine((int)(uint)y); //returns -1073741826

Xamarin.Android:

            var x = 1073741826.0;
            Console.WriteLine((int)(uint)x); //returns 1073741826
            var y = -1073741826.0;
            Console.WriteLine((int)(uint)y); //returns 0

Xamarin.iOS:

            var x = 1073741826.0;
            Console.WriteLine((int)(uint)x); //returns 1073741826
            var y = -1073741826.0;
            Console.WriteLine((int)(uint)y); //returns -1073741826

Seems like this is specific to Xamarin.Android.
Comment 4 Jon Douglas [MSFT] 2017-09-12 21:46:36 UTC
This happens on both 15.3.3 and 15.4 Builds (7.4 and 7.5/8.0)

I have found this only occurs on a physical device. I have seen this on armv7, arm64 devices. On an emulator, this works as expected. Thus I would suspect something along the lines of x86 vs armv7/arm64 going on here. A reproduction project would be:

1. Create a file -> new Xamarin.Android project
2. Add the following code snippet in the OnCreate of the MainActivity:

            var x = 1073741826.0;
            Console.WriteLine((int)(uint)x); //returns 1073741826
            var y = -1073741826.0;
            Console.WriteLine((int)(uint)y); //returns 0
3. Run the code on a physical device and it should reproduce
Comment 5 Alexis Christoforides 2017-09-16 03:17:07 UTC
I've been able to reproduce this on an arm emulator, phone and jenkins device. This can also be reproduced using the attached C code. The JIT emits a jit icall to mono_fconv_u4() for the conv.u4 IL instruction. 

I'm tracking down a fix/which configurations are affected on https://github.com/mono/mono/pull/5595
Comment 6 Alexis Christoforides 2017-09-16 03:17:53 UTC
Created attachment 24788 [details]
mono_fconv_u4.c
Comment 7 Alexis Christoforides 2017-09-19 16:40:07 UTC
Update: We've tested .NET behavior on ARM (Windows Phone) and it matches our existing behavior (truncate negatives to 0). ECMA 335 also does not provide further details on converting floats to unsigned integers.  Therefore platform-specific cast behavior in this case is valid and should be expected.
Comment 8 Happypig375 2017-09-20 09:32:10 UTC
>Therefore platform-specific cast behavior in this case is valid and should be expected.

Are you sure leaving this pitfall behind? I guess my existing code needs to be rewritten to execute on Xamarin.Android... Might just as well write it in React Native instead.