Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a VC compiler bug? About unsigned integer wrapping

I think the C program below shall output 1:

#include <stdio.h>
int main()
{
    unsigned int n=18u;
    while ((n+17u)>=17u) n-=17u;
    printf("%u\n",n+17u);
    return 0;
}

But compiled in VC6, Visual Studio 2010, or Visual Studio 2012, all in release mode, the program doesn't output anything and doesn't quit.

This is the assembly code generated by VS2012:

00BD1000  mov         eax,12h
00BD1005  lea         eax,[eax-11h]
00BD1008  jmp         main+5h (0BD1005h)

It seems that the compiler did some optimization and generated an infinite loop.

I think that ((n+17u)>=17u) is not always true, because if n==0xFFFF..FF, n+17u would wrap to 16u.

Am I wrong, or are the compilers wrong?

like image 931
zwhconst Avatar asked Nov 10 '13 17:11

zwhconst


1 Answers

gcc and clang both compile away that loop, replacing it with a printf of the constant 1 (using -O3.)

I think the VC behaviour you observe is a bug: unsigned arithmetic is well-defined, and you're right that the overflow should wrap around to an integer less than 17. So gcc and clang get it right.

like image 165
rici Avatar answered Sep 21 '22 07:09

rici