[Mono-list] Mono and .Net Floating Point Inconsistencies

ddambro ddambro at gmail.com
Sat Jan 17 18:20:13 EST 2009

I tried the FPU flags as follows:
1.Compiled the following file to a dll using gcc on a test linux system:
#include <fpu_control.h>

void setFloats()
        fpu_control_t desired = _FPU_MASK_DM | _FPU_MASK_UM | _FPU_MASK_PM
        _FPU_SETCW( desired);

2.Added the following code to my main class in the C# code:
public static extern void setFloats();

3.Called setFloats() before any other code.

The end results were the exact same as all my other mono runs and thus
different than my .NET runs.  The C code without the masks caused my program
to throw divided by zero exceptions from Console.Writeline() and
File.Exists().  Are there other flags I can try or does the fact that this
happens on Windows mono and Linux mono point to something other than the FPU
setup?  I'll keep looking for the exact spot where inconsistencies start to
happen and just to try it, I'm compiling mono 2.2 on a test machine to see
if that changes anything (everything so far has been on 2.0.2).


Kornél Pál wrote:
> Hi,
> Please try the FPU flags suggested John to see if that solves your 
> problem, modifying the control flags could be added to x86 versions of 
> Mono so you wouldn't have to modify them manually.
> Kornél
> ddambro wrote:
>> The code is compiled for x86 in Windows, and the problems I describe
>> persist
>> even using mono for Windows.  That is, on every computer I've tried so
>> far
>> regardless of OS and processor type, all of the mono runs produce the
>> same
>> results and all of the .NET runs produce the same results, but these
>> results
>> are different from each other.  This includes a 64-bit Vista machine
>> running
>> the x86 code in .NET and mono (installed as x86).  How would this affect
>> your suggested fix?  Also I noticed a fairly significant speed up when I
>> converted all my doubles to floats in this code a few years ago, would
>> your
>> fix undo that?  mono already runs the code MUCH slower than .NET so I'd
>> hate
>> to get another performance hit.
>> Dallman, John-2 wrote:
>>>> when I take the same binary and run it with the same inputs it 
>>>> produces different outputs if it is run on mono and .Net.
>>> This is with Mono on Linux, and .NET on Windows? The executable 
>>> is 32-bit .NET code? 
>>> I suspect that you've hit a misfeature that exists for most 
>>> floating-point code on 32-bit x86 Linux.
>>> The code to be run in the C function is:
>>> #include <fpu_control.h>    /* Mask the Denormal, Underflow and Inexact
>>> exceptions,
>>>         				leaving Invalid, Overflow and
>>> Zero-divide active.
>>> 				      Set precision to standard doubles,
>>> and round-to-nearest. */    
>>> fpu_control_t desired = _FPU_MASK_DM | _FPU_MASK_UM | _FPU_MASK_PM |
>>> _FPU_SETCW( desired);
>>> This needs to be a C function because everything in uppercase in 
>>> that code is a macro, from fpu_control.h. You may want to leave out 
>>> enabling floating point traps, in which case the code becomes:
>>> #include <fpu_control.h>    /* Set precision to standard doubles, and
>>> round-to-nearest. */    
>>> fpu_control_t desired =	_FPU_DOUBLE | _FPU_RC_NEAREST ;    
>>> _FPU_SETCW( desired);
>>> It would be good, really, if Mono had a standard call for setting 
>>> up consistent floating-point on all its platforms. 
>>> -- 
>>> John Dallman
>>> Parasolid Porting Engineer
>>> Siemens PLM Software
>>> 46 Regent Street, Cambridge, CB2 1DP
>>> United Kingdom
>>> Tel: +44-1223-371554
>>> john.dallman at siemens.com
>>> www.siemens.com/plm
>>> _______________________________________________
>>> Mono-list maillist  -  Mono-list at lists.ximian.com
>>> http://lists.ximian.com/mailman/listinfo/mono-list
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list

View this message in context: http://www.nabble.com/Mono-and-.Net-Floating-Point-Inconsistencies-tp21428695p21522156.html
Sent from the Mono - General mailing list archive at Nabble.com.

More information about the Mono-list mailing list