Bug 26219

Summary: Result of cast from double to int is different on Mono amd64 than on .NET and Mono x86
Product: [Mono] Runtime Reporter: Alexander Köplinger <alex.koeplinger>
Component: JITAssignee: Bugzilla <bugzilla>
Status: RESOLVED FIXED    
Severity: normal CC: joao.matos, mono-bugs+mono, mono-bugs+runtime, ramc, sebastien, vargaz
Priority: ---    
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: All   
Tags: Is this bug a regression?: ---
Last known good build:

Description Alexander Köplinger 2015-01-20 20:02:04 UTC
The following code in a console app:

>unchecked {
>	Console.WriteLine (IntPtr.Size + " " + (int)double.MaxValue);
>	Console.WriteLine (IntPtr.Size + " " + (int)Math.Floor (double.MaxValue));
>}

outputs this:

>.NET:
>
>8 0
>8 -2147483648
>4 0
>4 -2147483648
>
>Mono:
>
>8 -2147483648
>8 0
>4 -2147483648
>4 -2147483648

I noticed this after the System.Timers.Timer source from MS referencesource was imported, as a test started failing on amd64 (see http://wrench.mono-project.com/Wrench/WebServices/Download.aspx?workfile_id=3679509 for an example on Wrench).

Thanks to @davidkarlas and @alexrp for their help.
Comment 1 João Matos 2015-01-21 09:59:13 UTC
I can also reproduce on OSX and Mono for Windows.

> Mono:
> C:\dev\mono\msvc\x64\bin>mono.exe Program.exe
> 8 -2147483648
> 8 0
Comment 2 Zoltan Varga 2015-01-21 15:01:11 UTC
Fixed in mono master 6cf56cbb2f3a95bcacf23f891830ae45a0ebdfa3.
Comment 3 João Matos 2015-01-22 09:39:38 UTC
I can confirm the runtime bug is fixed but I still see some different behaviour with mcs vs. csc, even when running mcs with a Mono with this fix applied.

Looking into it.
Comment 4 João Matos 2015-01-22 10:26:31 UTC
So after some research, me and @akoeplinger concluded that there's a bug in csc.exe when doing double-to-int narrowing conversions. mcs is doing the right thing and Roslyn also seems to have fixed it since:

> <akoeplinger> I tested with roslyn 0.6.4 as used on dotnetfiddle and it does ldc.i4.0, but the roslyn used on another site generates the same as mcs
Comment 6 João Matos 2015-01-30 08:21:34 UTC
I don't think we are shipping 64-bit X86 runtimes in either Windows or OSX so that's why you can't see the option under OSX. And under Windows it must be using the .NET one.

You'll have to compile a 64-bit Mono if you really want to reproduce the fix since it's 64-bit specific.
Comment 8 Ram Chandra 2015-01-30 12:26:14 UTC
Thank you very much Sebastien.

With the help of comment 7 I am able to reproduce this issue.

To verify this issue, I have checked this issue with following builds:

Mac OS X 10.10.0
Xamarin Studio: 5.8 (build 624)
Installation UUID: 6ea47b0d-1852-4aaf-808d-373ff0a5002b
Runtime:
Mono 3.12.0 ((detached/a813491)
GTK+ 2.24.23 (Raleigh theme)
Package version: 312000068
Xcode 6.1 (6604)
Build 6A1052d
Xamarin.iOS: 8.6.1.24 (Business Edition)
=== Build Information ===
Release ID: 508000624
Git revision: fbedf37530cbbf01a75c4cc95557944aa8996aaa
Build date: 2015-01-30 07:52:28-05
Xamarin addins: cb32b8c2c23d061713c5abe206b8ad07723aa77e

Observations: When  I run the test case mentioned in bug description with XI. The output is :

8 -2147483648
8 -2147483648
4 -2147483648
4 -2147483648

This issue has been fixed. Hence I am closing this issue.