define vs function for optimization

Programming Topics (Computer Chess) and technical aspects as test techniques, book building, program tuning etc

Moderator: Andres Valverde

define vs function for optimization

Postby netiad » 04 Oct 2010, 01:11

I have a rather simple question related to my Move Generation and wanted to know where you guys are drawing the line with your #defines. I know in chess engines speed is preferred over readability. I used defines for my north, south, .... moves. Now that I'm getting into more of the moves the readability / good code practice side of me is fighting with the optimized side. And I'm wondering what you guys are doing related to this topic.

MoveGeneration.h :
extern BitBoard getWhitePawnPushTargets(BitBoard whitePawns, BitBoard emptySquares);

MoveGeneration.c
BitBoard getWhitePawnPushTargets(BitBoard whitePawns, BitBoard emptySquares) {
return NORTH_ONE(whitePawns) & emptySquares;
}

or

MoveGeneration.h
#define GET_WHITE_PAWN_PUSH_TARGETS(whitePawns, emptySquares) (NORTH_ONE(whitePawns) & emptySquares)
netiad
 
Posts: 16
Joined: 16 Sep 2010, 03:02

Re: define vs function for optimization

Postby Ron Murawski » 04 Oct 2010, 07:21

netiad wrote:I have a rather simple question related to my Move Generation and wanted to know where you guys are drawing the line with your #defines. I know in chess engines speed is preferred over readability. I used defines for my north, south, .... moves. Now that I'm getting into more of the moves the readability / good code practice side of me is fighting with the optimized side. And I'm wondering what you guys are doing related to this topic.

MoveGeneration.h :
extern BitBoard getWhitePawnPushTargets(BitBoard whitePawns, BitBoard emptySquares);

MoveGeneration.c
BitBoard getWhitePawnPushTargets(BitBoard whitePawns, BitBoard emptySquares) {
return NORTH_ONE(whitePawns) & emptySquares;
}

or

MoveGeneration.h
#define GET_WHITE_PAWN_PUSH_TARGETS(whitePawns, emptySquares) (NORTH_ONE(whitePawns) & emptySquares)



I prefer using functions, but I make them inline functions so, in debug mode, the parameters can be type-checked. I don't believe there's any difference in speed of #define vs inline function as long as the compiler is instructed to maximize optimizations.

Ron
User avatar
Ron Murawski
 
Posts: 352
Joined: 26 Sep 2004, 21:50
Location: Schenectady, NY, USA

Re: define vs function for optimization

Postby Dann Corbit » 04 Oct 2010, 20:49

Macros have other nasty behavior.
For instance:
#define SQUARE(x) ((x) * (x))
seems sensible enough.
However:
dp1sq = SQUARE(d++);
introduces undefined behavior.
With a function call, there is a sequence point. Even with inline functions, it must perform as-if it were a function call so there is no undefined behavior.
Dann Corbit
 


Return to Programming and Technical Discussions

Who is online

Users browsing this forum: No registered users and 18 guests