Question about the way to use bitboards.

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
Luis Babboni
Posts: 464
Joined: Sat Feb 28, 2015 4:37 pm
Location: Argentina

Re: Question about the way to use bitboards.

Post by Luis Babboni »

Hi!
I bother you, anyone that could help me, again.

I redid the Pawn Movements for the Move Generator of the BitBoard version of Soberango.

I think this part of the code is understandable (I translated it from spanish to english)

It works, but could someone see if the idea is correct or I´m still doing nosense (for example a great lost of the advantage of use bitboards)?

Thanks!

Code: Select all

Sub PawnMovements

	If WhoMoves=1 Then	'Whites moves.
		
		'Ahead 1:
		BBTo=(((Pawns And Whites) And (Not Rank7)) Shl 8) And (Not Whites) And (Not Blacks)
		If BBTo <>0 Then
			Do
				To=BSF(BBTo)
				From=To-8
				MovesList(Deep, Move, 1)=From
				MovesList(Deep, Move, 2)=To
				'MovesList(Deep, Move, 3)= Used for EnPassant
				'MovesList(Deep, Move, 4)= Used for Promotions
				'MovesList(Deep, Move, 5)= Used for MoveOrdering value
				BBTo=BitReset(BBTo,BSF(BBto))
				Move=Move+1			
			Loop Until BBTo=0
		EndIf
....
Pio
Posts: 334
Joined: Sat Feb 25, 2012 10:42 pm
Location: Stockholm

Re: Question about the way to use bitboards.

Post by Pio »

Luis Babboni wrote: Fri Apr 02, 2021 12:02 am Hi!
I bother you, anyone that could help me, again.

I redid the Pawn Movements for the Move Generator of the BitBoard version of Soberango.

I think this part of the code is understandable (I translated it from spanish to english)

It works, but could someone see if the idea is correct or I´m still doing nosense (for example a great lost of the advantage of use bitboards)?

Thanks!

Code: Select all

Sub PawnMovements

	If WhoMoves=1 Then	'Whites moves.
		
		'Ahead 1:
		BBTo=(((Pawns And Whites) And (Not Rank7)) Shl 8) And (Not Whites) And (Not Blacks)
		If BBTo <>0 Then
			Do
				To=BSF(BBTo)
				From=To-8
				MovesList(Deep, Move, 1)=From
				MovesList(Deep, Move, 2)=To
				'MovesList(Deep, Move, 3)= Used for EnPassant
				'MovesList(Deep, Move, 4)= Used for Promotions
				'MovesList(Deep, Move, 5)= Used for MoveOrdering value
				BBTo=BitReset(BBTo,BSF(BBto))
				Move=Move+1			
			Loop Until BBTo=0
		EndIf
....
1) I would put the colour as a parameter to the function, change whites to colour(1) and put black as colour(0). In that way you don’t need separate code for black and white.

2) I would replace the “if bbto <> 0” and “loop until bbto = 0” with a “while bbto <> 0”
User avatar
Luis Babboni
Posts: 464
Joined: Sat Feb 28, 2015 4:37 pm
Location: Argentina

Re: Question about the way to use bitboards.

Post by Luis Babboni »

Thanks Pio!
Pio wrote: Fri Apr 02, 2021 12:33 am ...
1) I would put the colour as a parameter to the function, change whites to colour(1) and put black as colour(0). In that way you don’t need separate code for black and white.
I thought in it.
The point is that I could add many variables to reduce the code lines. But this do not slow down the engine?
Pio wrote: Fri Apr 02, 2021 12:33 am ...
2) I would replace the “if bbto <> 0” and “loop until bbto = 0” with a “while bbto <> 0”
I´ll take a look to this!
Pio
Posts: 334
Joined: Sat Feb 25, 2012 10:42 pm
Location: Stockholm

Re: Question about the way to use bitboards.

Post by Pio »

Luis Babboni wrote: Fri Apr 02, 2021 1:10 am Thanks Pio!
Pio wrote: Fri Apr 02, 2021 12:33 am ...
1) I would put the colour as a parameter to the function, change whites to colour(1) and put black as colour(0). In that way you don’t need separate code for black and white.
I thought in it.
The point is that I could add many variables to reduce the code lines. But this do not slow down the engine?
Pio wrote: Fri Apr 02, 2021 12:33 am ...
2) I would replace the “if bbto <> 0” and “loop until bbto = 0” with a “while bbto <> 0”
I´ll take a look to this!
I do not think that will be the time critical part. If you want it faster you should not put the pawn moves in a separate routine since you could then put the whotomove condition before all the move generation instead of doing it multiple times. I really think you should simplify the code as much as possible and then do performance tuning, not the other way round. I actually think my way with parameter will be faster since it will remove a branch but someone like Gerd or HGM who knows much much more than me can probably answer that. Are you sure that the functions bsf and bitreset have been inlined by the clr if you are so concerned about speed? I could not see that you considered the double pawn push in the code.

Good luck!
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Question about the way to use bitboards.

Post by Sven »

I think the pawn move generation code indeed needs to be color-specific due to the shift left vs. shift right operations.
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)
Pio
Posts: 334
Joined: Sat Feb 25, 2012 10:42 pm
Location: Stockholm

Re: Question about the way to use bitboards.

Post by Pio »

Sven wrote: Fri Apr 02, 2021 12:57 pm I think the pawn move generation code indeed needs to be color-specific due to the shift left vs. shift right operations.
You are right. You would have to put in the operator in an array as well to make it work or do what I do and that is to rotate the entire board.