Author: Gerd Isenberg
Date: 13:18:35 03/11/03
Go up one level in this thread
On March 11, 2003 at 14:46:11, Russell Reagan wrote:
>I got some warnings yesterday from my compiler about mixing data types. One
>warning said something like, "converting bool to int, performance warning", or
>something similar.
Hi Russell,
wasn't it int to bool? Instead of
bool expIsTrue = pointerExpression;
bool expIsTrue = intExpression;
you should use an additional compare to give the compiler some hint:
bool expIsTrue = (pointerExpression) != NULL;
bool expIsTrue = (intExpression) != 0;
Relational expressions are bool.
Remember bool is 8-bit in most (all?) C++ implementations. Therefore it is often
seen the use of typedefed int-type like BOOL (MFC) or Bool as locals and
parameter to avoid an unaligned stack.
>What is good practice regarding the use of different sized
>variables together? One example from my program is that I store the move as a
>struct with several byte sized fields. My thinking is that since I will need to
>save the move everytime I make a move, I can copy those 4 bytes once, instead of
>having to do 4 copies of 32-bit values.
>
>typedef struct {
> unsigned char from;
> unsigned char to;
> unsigned char type;
> unsigned char whatever;
>} Move;
I would prefere this shorter one, specially if you have huge arrays of them.
One Move is 32-bit, that is fine. Also passing Moves to functions by value or
return values is simply passing a 32-bit int.
I use a move-class with sizeof(SMove) == 32-bit and a lot of usefull mostly
inlined functions including several overloaded operators. In this
class-namespace i also enum my kind of moves.
>
>Instead of:
>
>typedef struct {
> unsigned int from;
> unsigned int to;
> unsigned int type;
> unsigned int whatever;
>} Move;
If you have similar structures/classes allocated rarely but accessed often, this
second one may be favourable. You should pass objects > 64-bits per (const)
reference to avoid copying to many data.
>
>I got another warning when using an unsigned char as my board index, and using a
>signed char as the offset from that index (it might have been a different
>situation, but it was something similar, when I was mixing unsigned with
>signed).
>
>I got various warnings about mixing char with int, unsigned with signed, and so
>on.
You shouldn't ignore signed/unsigned warnings or possible loss of information
while assigning a larger datatype to a shorter, eg. 32-bit to 8-bit. If you are
sure about the value range (eg. board coordinates from 0-63) so that no overflow
could occur, you may use casts. Better to get rid of all these warnings.
>They were not all performance warnings, but I would like to know if it is
>better to use the smaller structure and save time copying it, or if it is better
>to use the larger structure and work with "faster" variables.
It depends. Packed structures, if allocated much but used "rarely". Otherwise,
if allocated less but used often consider int types.
Cheers,
Gerd
> It seems like you
>could keep more stuff in the cache if you used smaller sized variables when
>suitable. I'd also like to know if there are ever performance issues when mixing
>variables of different types (mixing char w/ short w/ long, unsigned w/ signed,
>etc.).
>
>Thanks,
>Russell
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.