2014-04-20

Demonstrating GCC's signed int overflow optimization bug is non-conforming

// When compiled in GCC with -O2, this causes an infinite loop

#include <limits>
int main()
{
  if( std::numeric_limits<signed int>::is_modulo )
  {
  for( int ii=1 ; ii<0 ; ++ii ) { }
    }
  return 0;
}


/*
This has been heavily debated as to whether it's actually a defect, with many claiming it is not a defect due to the following line in the C++ spec:

"If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined."

However, std::numeric_limits<signed int>::is_modulo clearly changes the argument, and is defined in the spec as:

True if the type is modulo. A type is modulo if, for any operation involving +, -, or * on values of that type whose result would fall outside the range [min(),max()], the value returned differs from the true value by an integer multiple of max() – min() + 1.

By signed int being a modulo type in gcc's implementation of C++, it has a clear mathmatical definition and is required to wrap on overflow as most developers expect.

I tested the following compilers (all targeting x86):
- g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
- Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final) (based on LLVM 3.0)
- Visual Studio 2013

GCC and Clang tests were compiled using standard 'safe' optimization level of -O2. Visual Studio test was compiled using the default optimization options for a Release build.

All three compilers report signed int to be modulo in their implementation. Only GCC causes an infinite loop in the test code. (It is unknown whether Clang or VisualStudio can be made to exhibit similar bugs with non-trivial code). Visual Studio optimized out the entire loop initially; a printf was added in the loop for the Visual Studio test to prevent the loop from being removed in entirety.

I believe this clearly demonstrates a bug in GCC. Either the oft quoted "signed int overflow is undefined" is incorrect due to signed int being modulo and GCC's optimizer is exhibiting incorrect behavior, or GCC's std::numeric_limits is incorrectly reporting signed int as being modulo. */

4 comments:

  1. merit casino | Shootercasino
    Play 메리트카지노 the best casino games at Shootercasino. Best Games, No Deposit Bonuses! Rating: 4.4 · ‎32 샌즈카지노 votes · ‎Free · ‎Game 온카지노

    ReplyDelete
  2. Planet Win 365 Casino Review 2021 & Rating
    Planet 퍼스트 카지노 Win 365 Casino Review with Overview, Games, Bonus & Software ✓ Full review of this 메리트카지노 online casino.Casino type: Live Dealer, Mobile Casino Rating: planet win 365 3 · ‎Review by David Jest

    ReplyDelete
  3. Casino - DrmCD
    › nba-deposit-online-gens › nba-deposit-online-gens Drmcd has the largest 평택 출장샵 collection of real money video poker machines in 광양 출장안마 the state, 충청남도 출장마사지 so 광주광역 출장안마 you can play games online with real money players. Use our mobile 서귀포 출장마사지

    ReplyDelete
  4. It makes it a hell lot simpler to carry out excessive volume CNC machining as we enter a set of numerical instructions. Before designing a prototype using CNC machines, several of} factors have to be considered. Most prototypes are designed by the product specs considered on the production floor. However, Stockings our sales organisation isn't only about machines, but also about the processes that make it potential to produce demanding elements effectively.

    ReplyDelete