Pointer to functions

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
michiguel
Posts: 6401
Joined: Thu Mar 09, 2006 8:30 pm
Location: Chicago, Illinois, USA

Re: Pointer to functions

Post by michiguel »

Dann Corbit wrote:
michiguel wrote:They are supposed to be slower. However, in certain situations the alternative is to have a switch or a chain of "if else" that involves lots of branches. When would you choose one approach over the other?

I know I have to try, but having an educated guess at the beginning helps a lot.

Miguel
Unless it is in a very time critical place, just write which is the most clear to you. On some compilers one technique will be faster than another, but it can change even with different compiler settings.

[snip]
Thanks all for your answers. They were very informative.
I a just rewrote my move ordering data structure and I am using pointer to functions. I do not believe it is in a critical spot, because the function itself spends quite a bit of time in generating and ordering the moves.

The idea is that now I can experiment with different schemes in a much easier way. In each position I initialize an array of pointer to functions that will generate and sort moves in phases (job done in function call pool_init()). A crude example is Function[0] generates and order captures, Function[1] pick killers from the table, Function[2] generates non-captures. Every time I have to pick a move (pool_pick()), I loop through the moves available and when I run out, I call the next function to generate moves from the next phase. When I run out moves and functions, I am done (pool_done()). So, in case of quiescence, I can initialize

Function[0]=load_captures;
Function[1]=NULL;

But if I want to generate checks in the first ply of quiescence(), I do

Function[0]=load_captures;
Function[1]=load_checks;
Function[2]=NULL;

The rest of the code remains intact. To do this, I need a switch at the begining of pool_init() :-) but is much cleaner.

The concept is that I want to experiment with different ordering schemes that depend on the position (why endgames should have move ordering different from middle games?). But I am digressing...

Anyway, I was always curious about the efficiency. For instance, everytime I evaluate a piece (mobility, positions etc) I call a pointer to a function that deals with a specific piece. I am not sure if this efficient, but I cleaned up my code very much!! I need to test it thoroughly, but I am wiling to sacrifice a little bit of speed for clarity.

Thanks again,
Miguel
Dann Corbit
Posts: 12803
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Pointer to functions

Post by Dann Corbit »

michiguel wrote:
Dann Corbit wrote:
michiguel wrote:They are supposed to be slower. However, in certain situations the alternative is to have a switch or a chain of "if else" that involves lots of branches. When would you choose one approach over the other?

I know I have to try, but having an educated guess at the beginning helps a lot.

Miguel
Unless it is in a very time critical place, just write which is the most clear to you. On some compilers one technique will be faster than another, but it can change even with different compiler settings.

[snip]
Thanks all for your answers. They were very informative.
I a just rewrote my move ordering data structure and I am using pointer to functions. I do not believe it is in a critical spot, because the function itself spends quite a bit of time in generating and ordering the moves.

The idea is that now I can experiment with different schemes in a much easier way. In each position I initialize an array of pointer to functions that will generate and sort moves in phases (job done in function call pool_init()). A crude example is Function[0] generates and order captures, Function[1] pick killers from the table, Function[2] generates non-captures. Every time I have to pick a move (pool_pick()), I loop through the moves available and when I run out, I call the next function to generate moves from the next phase. When I run out moves and functions, I am done (pool_done()). So, in case of quiescence, I can initialize

Function[0]=load_captures;
Function[1]=NULL;

But if I want to generate checks in the first ply of quiescence(), I do

Function[0]=load_captures;
Function[1]=load_checks;
Function[2]=NULL;

The rest of the code remains intact. To do this, I need a switch at the begining of pool_init() :-) but is much cleaner.

The concept is that I want to experiment with different ordering schemes that depend on the position (why endgames should have move ordering different from middle games?). But I am digressing...

Anyway, I was always curious about the efficiency. For instance, everytime I evaluate a piece (mobility, positions etc) I call a pointer to a function that deals with a specific piece. I am not sure if this efficient, but I cleaned up my code very much!! I need to test it thoroughly, but I am wiling to sacrifice a little bit of speed for clarity.

Thanks again,
Miguel
Your idea is similar to DarkThought's evaluation scheme:
http://people.csail.mit.edu/heinz/dt/node15.html

I guess that since it is published in a popular chess programming book, quite a few people have tried ideas similar to this.
Marimar
Posts: 100
Joined: Fri Mar 10, 2006 11:23 pm

Re: Pointer to functions

Post by Marimar »

michiguel wrote:They are supposed to be slower. However, in certain situations the alternative is to have a switch or a chain of "if else" that involves lots of branches. When would you choose one approach over the other?

I know I have to try, but having an educated guess at the beginning helps a lot.

Miguel
It is the first time I read that pointers to functions are "slow". In my experience (non-chess, however), they are actually faster.
José.