Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

advise: add "NoOptimization" flag #2

Open
HenryHo2006 opened this issue Mar 8, 2016 · 1 comment
Open

advise: add "NoOptimization" flag #2

HenryHo2006 opened this issue Mar 8, 2016 · 1 comment

Comments

@HenryHo2006
Copy link

some unit test code like var result = a + b - a, it will be fault in release mode,
add [MethodImpl(MethodImplOptions.NoOptimization)] to avoid fault.

@govert
Copy link
Owner

govert commented Apr 20, 2016

Are you sure about this?
I would expect the compiler to never simplify a+b-a to b, since the semantics would change.
Could you help me reproduce the difference?

I could get no difference with and without the NoOptimization using this code
(in both cases I get infinity - as I'd expect):

using System;
using System.Runtime.CompilerServices;

namespace TestFloatOptimization
{
    class Program
    {
        static void Main(string[] args)
        {
            double a = double.MaxValue;
            double b = double.MaxValue;
            double withOpt = WithOpt(a, b);
            double noOpt = NoOpt(a, b);

            Console.WriteLine("WithOpt: " + withOpt.ToString("R"));
            Console.WriteLine("NoOpt:   " + noOpt.ToString("R"));
            Console.ReadLine();
        }

        static double WithOpt(double a, double b)
        {
            return a+b-a;
        }

        [MethodImpl(MethodImplOptions.NoOptimization)]
        static double NoOpt(double a, double b)
        {
            return a+b-a;
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants