Bug 7981 - Mono 3.0 double type conversion to long type fails on MIPS hardware
Summary: Mono 3.0 double type conversion to long type fails on MIPS hardware
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: JIT ()
Version: unspecified
Hardware: Other Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-10-25 00:43 UTC by MFang
Modified: 2017-07-13 00:17 UTC (History)
8 users (show)

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


Attachments
tracelog made with --trace=all (554.58 KB, text/plain)
2013-06-12 17:39 UTC, mr.zoltan.gyarmati
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 FIXED

Description MFang 2012-10-25 00:43:39 UTC
cpu:mips bcm4706 

my code:
     double num1 = 100000D;
     long aa = (long)num1;
     Console.WriteLine("double->long:{0}->{1}", num1, aa);

resule:
    double->long:100000->0

    This bug will affect DateTime.AddDay () does not work, because of the need to use (long)double.
                                     I hope as soon as possible.thanks!
Comment 1 Zoltan Varga 2012-10-26 14:00:25 UTC
Note that MIPS is not a supported mono architecture. That being said, I can't reproduce this on
a Broadcom BMIPS5000. The problem is probably that your cpu is big-endian, and mono does not seem to work on big-endian mips.
Comment 2 MFang 2012-10-26 22:09:26 UTC
broadcom 4706 is little endian.
After tracking (long) double processing will call the jit-calls.c file gint64 mono_fconv_i8 (double v) method.
Debugging code:
gint64
mono_fconv_i8 (double v)
{
     + printf ("mono_fconv_i8 double:% lf \ n", v);
     - return (gint64) v;
     + return -1;
}

result:
mono_fconv_i8 double: 0.000000
double-> long: 100000 -> -1

I think that the double parameter is passed as 0 handling problems, so the conversion of long type is 0.
Comment 3 MFang 2012-10-27 01:22:10 UTC
I use the cross-compiler, configuration is as follows:

export PATH=$STAGING_DIR/bin:$PATH
_prefix=mipsel-linux
CSRC_ROOT=/root/src/buildroot-2009.11/output/staging/usr/bin
export CC=$CSRC_ROOT/${_prefix}-gcc
export CXX=$CSRC_ROOT/${_prefix}-g++
export CPP=$CSRC_ROOT/${_prefix}-cpp
export AR=$CSRC_ROOT/${_prefix}-ar
export AS=$CSRC_ROOT/${_prefix}-as
export LD=$CSRC_ROOT/${_prefix}-ld
export CPPFLAGS="-EL -O2"
export CPPFLAGS="-EL -O2"
export CXXFLAGS="-EL -O2"

./configure --prefix=/opt/usr/mono --host=mipsel-linux-uclibc --disable-mcs-build --with-mcs-docs=no --with-sigaltstack=no --with-tls=pthread mono_cv_uscore=no mono_cv_clang=no
Comment 4 Suchiman 2013-03-14 19:51:14 UTC
I confirm this bug. I have encountered this bug on a little endian mips processor ( "MIPS 4KEc V6.8" UR8 on FritzBox router ). Following testcode did i use:
static void Main(string[] args)
{
    checked
    {
        Double dvalue = 100;
        Int64 IValue = (Int64)dvalue;
        Console.WriteLine("IValue: " + IValue);
    }
}
which resulted in:
System.OverflowException: Number overflow.
  at (wrapper managed-to-native) object:__icall_wrapper___emul_fconv_to_ovf_i8 (double)
  at monotest.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0

so i added some debugging code like MFang but to the method named by the exception:
gint64
mono_fconv_ovf_i8 (double v)
{
        gint64 res;
        printf ("mono_fconv_ovf_i8:% lf", v);
        MONO_ARCH_SAVE_REGS;
        res = (gint64)v;
        printf ("mono_fconv_ovf_i8_res:% lld", res);
        if (isnan(v) || trunc (v) != res) {
                mono_raise_exception (mono_get_exception_overflow ());
        }
        return res;
}
after i run the testcode again i got the following results:
mono_fconv_ovf_i8: 3281902282778674900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,000000
mono_fconv_ovf_i8_res:-1
everything else seems to run fine on mips but i got stuck with this bug.
./configure --build=i386-linux-gnu --target=mipsel-linux --host=mipsel-linux CC="mipsel-linux-gcc" CFLAGS="-pipe -march=4kc -Wa,--trap" --disable-mcs-build --with-mcs-docs=no --with-sigaltstack=no --with-tls=pthread --prefix=/home/robin/mono_freetz --enable-minimal=aot,profiler
Comment 5 MFang 2013-04-01 23:07:40 UTC
Why nobody deal with this BUG?
Comment 6 Zoltan Varga 2013-04-02 00:32:56 UTC
MIPS is not a supported mono architecture, and we can't reproduce the problem.
Comment 7 mr.zoltan.gyarmati 2013-06-12 17:39:07 UTC
Created attachment 4109 [details]
tracelog made with --trace=all
Comment 8 mr.zoltan.gyarmati 2013-06-12 17:39:39 UTC
  I have a similar and most likely related issue on MIPS architecture, where i got a  System.OverflowException exception while simple TimeSpan is being created (which involves float number conversions as well).

 Please see attachment for the whole trace, the problematic part starts around line #163. The c# code available here:
http://veritas-vos-liberabit.com/monogatari/2009/12/mono-wcf-advent-day-2-hello-wcf.html

  The arch is mips32r2, mono version 3.0.10, kernel 3.3.8, with enabled MIPS_FPU_EMU. Please let me know what other useful information i can provide (if any).

Zoltan, what do you mean by "MIPS is not a supported Mono architecture"?
It's listed here: http://www.mono-project.com/Supported_Platforms, and mentioned on the release notes for example here: http://www.mono-project.com/Release_Notes_Mono_3.0#MIPS_port , not to mention that the code in the  mono source tree seems to be working (but apparently with broken floating point support...)
Comment 9 Zoltan Varga 2013-06-12 18:44:42 UTC
The problem is that there are a lot of MIPS variants, little-endian/big-endian, 32bit/64 bit, cpu-s without hw division, different ABIs etc., so its hard to support all of them.
Comment 10 kerschl.alexander 2014-04-14 15:52:11 UTC
I'm trying similar things on a Fritzbox 7390. I had success compiling the latest mono sources version 3.4

my configuration:
mono-sgen: ELF 32-bit MSB executable, MIPS, MIPS32 rel2 version 1, dynamically linked (uses shared libs), with unknown capability 0x41000000 = 0xf676e75, with unknown capability 0x10000 = 0x70403, not stripped


a simple Console.WriteLine(DateTime.Now.ToString());
results in a similar error:


Unhandled Exception:
System.TypeInitializationException: An exception was thrown by the type initializer for System.DateTime ---> System.ArgumentOutOfRangeException: Value 734668917 is outside the valid range [0,734668917].
Parameter name: ticks
  at System.DateTime.InvalidTickValue (Int64 ticks) [0x00000] in <filename unknown>:0
  at System.DateTime..ctor (Int64 ticks) [0x00000] in <filename unknown>:0
  at System.DateTime..cctor () [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
  at ConsoleApplication2.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: An exception was thrown by the type initializer for System.DateTime ---> System.ArgumentOutOfRangeException: Value 734668917 is outside the valid range [0,734668917].
Parameter name: ticks
  at System.DateTime.InvalidTickValue (Int64 ticks) [0x00000] in <filename unknown>:0
  at System.DateTime..ctor (Int64 ticks) [0x00000] in <filename unknown>:0
  at System.DateTime..cctor () [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
  at ConsoleApplication2.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0


I hope somebody could have a look at this. Maybe it is only a little thing for somebody who knows what is going on in the background. Thanks a lot!
Comment 11 neolithos 2014-05-13 10:26:32 UTC
The arguments a stored in the wrong order.

I changed it, and it worked fine for my mips vr9 34kc Big Endian.

--- mono-3.4.0/mono/mini/mini-mips_2.c	Di 13. Mai 16:20:41 2014
+++ mono-3.4.0/mono/mini/mini-mips.c	Mi 19. Feb 19:48:46 2014
@@ -5165,8 +5165,8 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 					break;
 				case 8:
 #if (SIZEOF_REGISTER == 4)
-					mips_sw (code, ainfo->reg, inst->inst_basereg, basereg_offset + ms_word_offset);
-					mips_sw (code, ainfo->reg + 1, inst->inst_basereg, basereg_offset + ls_word_offset);
+					mips_sw (code, ainfo->reg, inst->inst_basereg, basereg_offset + ls_word_offset);
+					mips_sw (code, ainfo->reg + 1, inst->inst_basereg, basereg_offset + ms_word_offset);
 #elif (SIZEOF_REGISTER == 8)
 					mips_sd (code, ainfo->reg, inst->inst_basereg, basereg_offset);
 #endif
Comment 12 neolithos 2014-05-13 11:13:59 UTC
The previous patch was not right.

Thx, to Ralf:

--- mono-3.4.0/mono/mini/mini-mips.new.c	Di 13. Mai 16:20:41 2014
+++ mono-3.4.0/mono/mini/mini-mips.c	Mi 19. Feb 19:48:46 2014
@@ -5165,8 +5165,8 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 					break;
 				case 8:
 #if (SIZEOF_REGISTER == 4)
-					mips_sw (code, ainfo->reg, inst->inst_basereg, basereg_offset + ms_word_offset);
-					mips_sw (code, ainfo->reg + 1, inst->inst_basereg, basereg_offset + ls_word_offset);
+					mips_sw (code, ainfo->reg + ls_word_idx, inst->inst_basereg, basereg_offset + ls_word_offset);
+					mips_sw (code, ainfo->reg + ms_word_idx, inst->inst_basereg, basereg_offset + ms_word_offset);
 #elif (SIZEOF_REGISTER == 8)
 					mips_sd (code, ainfo->reg, inst->inst_basereg, basereg_offset);
 #endif
Comment 13 kerschl.alexander 2014-05-31 06:54:20 UTC
FYI: Unfortunately - the above fix does not fix the issues with mono on MIPS CPUs...

e.g.

TimeSpan ts = TimeSpan.FromMilliseconds(1000);
Console.WriteLine(ts.Seconds.ToString());

----> 
 Unhandled Exception:
 System.OverflowException: Outside range [MinValue,MaxValue]
 at System.TimeSpan.From (Double value, Int64 tickMultiplicator) [0x00000] in <filename unknown>:0
 at System.TimeSpan.FromMilliseconds (Double value) [0x00000] in <filename unknown>:0
 at ConsoleApplication2.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0
 [ERROR] FATAL UNHANDLED EXCEPTION: System.OverflowException: Outside range [MinValue,MaxValue]
 at System.TimeSpan.From (Double value, Int64 tickMultiplicator) [0x00000] in <filename unknown>:0
 at System.TimeSpan.FromMilliseconds (Double value) [0x00000] in <filename unknown>:0
 at ConsoleApplication2.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0
Comment 14 neolithos 2014-05-31 08:46:59 UTC
Correct, there are also problems with the ABI and the double/float conversation.
Comment 15 Rodrigo Kumpera 2017-07-13 00:17:09 UTC
If this still happening with mono 5.2, please reopen this bug.