[XviD-devel] Don't do " signed /= unsigned "

Christoph Lampert xvid-devel@xvid.org
Sat, 11 Jan 2003 14:45:20 +0100 (CET)


Hi,

I just found out a nasty side effect and wanted to warn those of you who
didn't know about it (like me):

Don't divide signed values by unsigned! At least in gcc 2.95 and 3.2 
the division will be done as unsigned and the result will be wrong! 
I don't know if this is what ANSI C demands, but it's annoying! 
Please check yourself with the test program. 

In branch motion_comp.c there is a line 

dx /= (1+quarterpel) 

and this gives me wrong results if dx is signed int (which it is)
and quarterpel is unsigned (which I declared that way, since it's a 0 or 
1 flag). Division is then done unsigned!

So dx=-2, quarterpel=1   leads to  

dx = 2 Billion  (dx= 0x7FFFFFFF)

Boy, it took me a while to find this, because in previous version of
motion_comp the lines were 

if (quarterpel) dx/=2;

which works flawlessly with unsigned quarterpel, too. 

gruel 

----------------------
void main()
{
        int dx=-2;
        const unsigned int q=1;

        dx /= 1+q;

        printf("dx=%d\n",dx);
}


Output: 

dx=2147483647