Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: C++ class design question

Author: Oliver Roese

Date: 09:55:14 10/10/03

Go up one level in this thread


On October 09, 2003 at 17:05:41, Gerd Isenberg wrote:

>Hi all,
>especially low level C++ experts,
>
>I introduced this SSE2-intrinsic wrapper class with sizeof(2*8):
>
>http://www.talkchess.com/forums/1/message.html?320110
>
>It encapsulates the intrinsic data type __m128i.
>
>Now i like to write a compatible class with standard C++ 64-bit types,
>two bitboards.
>
>Both classes have the same public interface, the same size,
>the same memory image, but disjoint register representation.
>
>Due to all constructors and operators are forced to be inlined,
>a abstract, pure virtual base class makes no sense. And late binding
>at runtime is no issue here.
>
>The issue is portability and the possibility to mix SSE2 (or other 128-bit
>register files on other processors) and gp-algorithms with independent
>instruction chains. Performance tuning by changing some datatype of connected
>(register) variables (but probably not the type of incarnations in memory,
>e.g. members of some source and target structs).
>
>What is the best way to implement that,
>with respect to the different register files?
>
>1. Two none related classes with coincidentally have
>   the same operators, size and memory image.
>
>2. One "protected" base class with a union of both types, but no operators,
>   and two derived classes with inlined operators and constructors?
>
>  union
>  {
>    __m128i dbl;
>    BitBoard sgl[2];
>  }
>
>3. A portable full implemented base class and a derived class with
>   overloaded operators and constructors implemented with
>   intrinsic functions and data type.
>
>One problem is assignment from one type to the other and casting.
>Memory-memory/register or vice versa is fine, but register-register
>is rather inperformant and should be avoided.
>
>Thanks in advance,
>Gerd

Hello,

to implement polymorphism in C++ you have basically 2 options:
--use virtual functions
--use templates.

Your situation leaves you with the template-approach, since virtual functions
cant get you the high performance you are aming for. They are out of question,
since you would get then a hidden vm-pointer added to your datastructure.
So you should go with point 1 on your list.
The 2 classes wont be templates, the routines using them would be.

It may be advisable to derive both class from a common base class to capture
common types and constants.
You can even share routines which operate on the public interface, this is
slightly advanced. Search for "Barton-Nackman"-Trick or go to
http://osl.iu.edu/~tveldhui/papers/techniques/
this is very good reading.
On the other hand templates add considerably complexity to your code! You should
have a reason using them. Dont blame me later!

Oliver Roese
Software Developer (www.forseason.de)










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.