News for hackers. Stuff that matters.
Re:Fast integer overflow detection (2012) by UnoriginalGuy on Sunday December 14, 2014 @03:43PM (#8749495)
It seems like a lot of problems in C/C++ is caused by "this code might be removed duration optimisation." Makes the whole environment undefined.

Why has nobody pushed for marking key sections of code with "don't optimise away" flags?

In C# you have this:

    [MethodImplAttribute(MethodImplOptions.NoOptimization)] 
    public static string GetCalendarName(Calendar cal)
    {
       return cal.ToString().Replace("System.Globalization.", "").
                 Replace("Calendar", "");
    }
Re:Fast integer overflow detection (2012) by Someone on Sunday December 14, 2014 @04:20PM (#8749604)
In practice, there is no such thing as "no optimization" in C.

If you map every variable read to a memory read and every variable written to a memory store, your code will run incredibly slow. At the least, you will want to do register allocation and constant folding.

That's not so bad w.r.t. undefined behavior, but you also will want to do dead code elimination. If you don't, program size will blow up (think of macro definitions for DEBUG) That dead code elimination is what causes many of those portability issues.

In C, undefined behaviour is the price we pay for small code size and fast execution.

Also, are you sure C# doesn't optimize code marked DontOptimize? I'm asking because the documentation at http://msdn.microsoft.com/en-us/library/system.runtime.compi... says "The method is not optimized by the just-in-time (JIT) compiler or by native code generation (see Ngen.exe) when debugging possible code generation problems.", but doesn't tell what that last phrase means.

Re:Fast integer overflow detection (2012) by Moral_ on Sunday December 14, 2014 @03:53PM (#8749520)
In gcc there are some pragmas you can use to to this:

for instance `#pragma gcc optimize("O0")` should work.

https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/Function-Specif...

But the whole "this code might be removed during optimization" isn't exactly true. The compiler has to keep the meaning of your code the same. But once you introduce undefined behavior into the code, everything is meaningless from then on and gcc can do tons of wacky stuff.

Re:Fast integer overflow detection (2012) by AgentME on Sunday December 14, 2014 @03:54PM (#8749526)
>Why has nobody pushed for marking key sections of code with "don't optimise away" flags?

The issue isn't that certain code is optimized away. That's just a symptom of the fact that it's incorrect C code relying on the way a specific system treats undefined behavior. Checking for overflow can be done with correct C code that doesn't rely on undefined behavior, so putting in a hack that lets incorrect C work in some situations does not seem like the right answer.

Re:Fast integer overflow detection (2012) by TheLoneWolfling on Sunday December 14, 2014 @03:50PM (#8749510)
(Most) things that are undefined behavior are so for a reason.

In this case - you don't know if the system you're on even stores numbers as twos complement. And if it doesn't, it may not have the same behavior. So even the unoptimized version may fail.

Re:Fast integer overflow detection (2012) by to3m on Sunday December 14, 2014 @04:08PM (#8749566)
I think in most cases, people writing code to check for overflow do know whether the system they're on stores integers as twos complement, and they'll have a pretty good idea what will happen when overflow occurs. A portable approach for this sort of thing would be very useful, but in the mean time, perhaps we could settle for compilers that don't strip out reasonable code?

I know, I know - I'm asking a lot.

My favourite two links on the subject: http://blog.metaobject.com/2014/04/cc-osmartass.html, http://robertoconcerto.blogspot.co.uk/2010/10/strict-aliasin...

Re:Fast integer overflow detection (2012) by heinrich5991 on Sunday December 14, 2014 @05:18PM (#8749774)
The good thing is that this allows to optimize out in reasonable places too.