Author: Matt Taylor
Date: 12:44:53 12/05/02
Go up one level in this thread
On December 05, 2002 at 15:41:02, Gerd Isenberg wrote: >On December 05, 2002 at 12:48:02, Dieter Buerssner wrote: > >>On December 05, 2002 at 11:25:27, Gerd Isenberg wrote: >> >>>Most (All?) C-compilers have no problem with unary minus and constants: >>> >>>unsigned int A = -CONST; >> >>Assume 32 bit integers, and const is -2^31. >>I believe, the above is undefined behaviour. In this case a cast will make it >>defined behaviour: >> >> unsigned int A = -(unsigned)CONST. >> > >Hi Dieter, > >I guess it's null, because 2^31 is INT_MIN. I had small constants in mind. >So -1 is very common to to set all bits in an unsigned variable, don't care >about the word size. > >unsigned int A = -1; >if ( popCount(A) == 64) >{ > // ah, nice native 64 bit ints >} > >Where i have some problems with sometimes, is the implicite type of a direct >constant expressions, calculated by the compiler, specially with propritary >64 bit integers types. > >Eg.: MSC6.0 > >unsigned __int64 FFFF = -1; // that' fine, all 64 bits set >unsigned __int64 H8BB = 1<<63; // oups zero > >produces an internal unsigned int overflow and assigns zero( without any >warning, even with warning level 4). Why isn't the compiler able to cast the 1 >implicitly to the 64bit type? > >unsigned __int64 H8BB = (unsigned __int64)1<<63; // that's ok >unsigned __int64 H8BB = (-1)<<63; // also wrong > > >>Totally unrelated to chess. If you want to write a function to convert int to >>ASCII (without using sprintf) the typical way may be: >> >> int toconvert; >> unsigned uval; >> if (toconvert < 0) >> uval = -toconvert; /* Oops, may not work for toconvert = INT_MIN */ >> /* and go on to convert the unsigned value */ >> >>>Because -CONST is a kind synonym for the compiler which means implicitly >>> (2**wordLengthInBits) - CONST ==> 0 - CONST >> >>All unsigned arithmetics is guaranteed to yield results mod 2 ^ bits. > >except imul and idiv ;-) > >> >>Cheers, >>Dieter > >See you, >Gerd Actually, I noticed an interesting "feature" of the VC compiler when I converted an atoi assembly routine into C (before I realized it was just atoi). I was using unsigned numbers, but the VC compiler generated a signed multiply. I first thought it was a bug, but later I worked it on paper and came to the conclusion that a signed 32x32 -> 32-bits is identical to unsigned 32x32 -> 32-bits. I concluded that this was not really a bug, then, but a feature since the 32x32 -> 32 is probably a bit faster than a full 32x32 -> 64 on most chips. Athlon sustains a rate of 1 32x32 -> 32 multiplies every 2 cycles, and Opteron is supposed to do a multiply per cycle. I haven't tested the 32x32 -> 64 on my Athlon, but I suppose it's worth looking into.
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.