Author: Gerd Isenberg
Date: 13:18:56 09/24/03
Go up one level in this thread
On September 24, 2003 at 07:43:33, Uri Blass wrote: >I was told that unsigned varaibles are better to use in every case that I can do >it. > >one of the varaibles that cannot get negative value is the ply of the search but >the problem is that this varaible is connected with beta because I tell movei >always that beta cannot be more than 9999-ply > >How do you solve that problem? > Hi Uri, if ply doesn't exceed 9999, is see no problem. 9999 is considerd signed int, so the expression may become negative. >practically I was too lazy to replace all my varaibles and decided only to >replace few varaibles and remember that rule for new varaibles. > >It was another varaible that I defined as unsigned and was connected with ply >and when I got the warning >signed/unsigned mismatch I tried to change ply to be unsigned and got more >warning of the same kind because beta is unsigned. > >I can get rid of the warning by adding signed. > >if (beta>9999-(signed)ply) > beta=9999-(signed)ply; > >I doubt if it is a good idea and it is possible that the conversion is going to >cost time for the computer. > >Uri So signed/unsigned integer casts don't to anything but except to satisfy the compiler. The problem with signed and unsigned is, that they have exclusive, disjoint ranges if MSB (sign bit for int) is set, but a common range, if MSB is not set. So at runtime with variables, assigning disjoint values from signed to unsigned or vice versa is a problem, which is simply ignored by the processor and reported by a compiler warning. It's up to you, the programmer, to be aware about the interpretation of the disjoint ranges. So for signed/unsigned char -128 becomes 128 and -1 becomes 255 or vice versa - the bit pattern are the same. For relational expressions with mixed signed/unsiged the same. Is signed char -128 == unsigned +128 or -1 == 255? Both signed/unsigned have different over- anderflow limits. Be aware of these overflows and use unsigned if values can't be negative per definition, e.g. array indices. If the value range is very small relatively to INTMAX and even some expressions with it fit for sure inside the common positive range, it don't cares so much. For ply-index, which is often computed in conjuncton with signed integers, i prefere to use signed 32-bit int too. One may even interprete negative plies as done halfmoves leading to current root position. Due to unsigned can't be less than zero, range checking is shorter and faster to implement with unsigned. Instead of if ( isq >= 0 && isq < 64 ) one may use unsigned if ( usq < 64 ) or this one: if ( isq >= 8 && isq < 56 ) to use the zero trick above, we subtract 8 on each side: if ( isq-8 >= 8-8 && isq-8 < 56-8 ) and with unsigned if ( usq-8 < 56-8 ) That may be an argument to use unsigned for square indizies, but this small range even fits in the common signed/unsigned char range. So you may use 32-bit signed and leave above optimizations to the compiler. Another point is mixing signed/unsigned with different word sizes. Avoid it, or be absolutely sure whether zero or sign extension should occur. Another aspect is mul/div and shifting right from MSB to LSB for power of two division. There is an "unsigned" logical shift and a "signed" arithmetical shift right. The first shift fills zeros from the left, the "signed" the MSB. Therefore for none simple number interpretation, like some character code or bitsets where you may shift some bits around, unsigned is required. With unsigned bitboards, to extract the LS1B, which is done with the bb & -bb trick based on two's complement, one has to cast to satisfy the compiler: ls1b = bb & (-(__int64)bb) Regards, Gerd Ps.: Interesting note in the signed/unsigned issue: Software Optimization Guide for AMD Athlon™ 64 and AMD Opteron™ Processors ------------------------------------------------------------------------------ 2.22 Using Unsigned Integers for 32-Bit Array Indices Optimization When using a 32-bit variable as an array index, declare the variable as an unsigned integer instead of a signed integer. Application This optimization applies to 64-bit software. Rationale When performing 64-bit address arithmetic, the compiler must insert an additional instruction into the object code to sign-extend a signed 32-bit integer, which reduces performance; no additional instruction is necessary to zero-extend an unsigned 32-bit integer because the processor performs zero-extension automatically. (There is no performance penalty for using 64-bit variables—either signed or unsigned—as array indices.)
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.