Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Compiler bug (OT)

Author: Christophe Theron

Date: 07:44:54 12/08/01

Go up one level in this thread


On December 08, 2001 at 04:16:46, Lex Loep wrote:

>Altough a bit off topic I still post it as many here use the MSVC compiler.
>This bug makes me feel a bit uneasy as this is just a simple constuction
>and makes you wander how many more of these faults are in there.
>
>Lex
>
>
>When compiling the code fragment below with optimization for minimum size
>the wrong code is produced.
>
>The Good and Bad function should return the same result (0x11000)
>but does not, Bad returns 0. Works fine in debug builds.
>VS6SP5
>
>// compilerbug.cpp : Defines the entry point for the console application.
>//
>
>#include <stdio.h>
>#include <conio.h>
>
>
>typedef unsigned long	DWORD;
>typedef unsigned char   BYTE;
>
>DWORD Good(BYTE P1,BYTE P2);
>DWORD Bad(BYTE P1,BYTE P2);
>
>int main(int argc, char* argv[])
>{
>	BYTE Para1=0x10;
>	BYTE Para2=0;
>
>	DWORD ResultGood=Good(Para1,Para2);
>	DWORD ResultBad=Bad(Para1,Para2);
>
>	printf("Result good:%lx\n",ResultGood);
>	printf("Result bad:%lx\n",ResultBad);
>
>	_getch();
>	return 0;
>}
>
>
>DWORD Good(BYTE P1,BYTE P2)
>{
>	if ( P1 == 0 )
>		return 0;
>
>	DWORD Result=((DWORD)P1<<8) | (P2);
>
>	if ( P2 == 0)
>		Result |= 0x10000;
>
>	return Result;
>}
>
>DWORD Bad(BYTE P1,BYTE P2)
>{
>	DWORD Result=((DWORD)P1<<8) | (P2);
>
>	if (P1 ==0 )
>		return 0;
>
>	if ( P2 == 0)
>		Result |= 0x10000;
>
>	return Result;
>}



I have found this bug a while ago.

I had a bit of code that exchanged the higher and lower bytes in a word. It was
programmed in a single expression and the compiler generated wrong code for it
when some optimizations were turned on.

Downloading the latest service pack (SP5) did not solve the problem. 13 hours of
download for nothing, I was happy as you can imagine.

Splitting the code in two lines solved the problem.

The common point between your code and mine is that we are both using a shift
operation combined with an or operation.

This is a common thing in any C program, so I feel very uncomfortable with this
compiler bug.



    Christophe



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.