[board] Add a "count leading zeros" implementation for ARM machines
Call the `clz` instruction on the bitboard via `asm!`.
This commit is contained in:
		
							parent
							
								
									4eb734d3eb
								
							
						
					
					
						commit
						ed9a919db6
					
				
					 1 changed files with 24 additions and 0 deletions
				
			
		|  | @ -1,6 +1,7 @@ | |||
| // Eryn Wells <eryn@erynwells.me>
 | ||||
| 
 | ||||
| use crate::square::Square; | ||||
| use std::arch::asm; | ||||
| 
 | ||||
| #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] | ||||
| pub struct BitBoard(pub u64); | ||||
|  | @ -17,6 +18,20 @@ impl BitBoard { | |||
|     fn place_piece_at(&mut self, sq: &Square) { | ||||
|         self.0 |= 1 << sq.index | ||||
|     } | ||||
| 
 | ||||
|     #[cfg(target_arch = "arm")] | ||||
|     fn _arm_count_leading_zeros(&self) -> u8 { | ||||
|         let mut number_of_leading_zeros: u8 = 0; | ||||
|         unsafe { | ||||
|             asm!( | ||||
|                 "clz {count}, {bitfield}", | ||||
|                 count = out(reg) number_of_leading_zeros, | ||||
|                 bitfield = in(reg) self.0, | ||||
|             ); | ||||
|         } | ||||
| 
 | ||||
|         number_of_leading_zeros | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| #[cfg(test)] | ||||
|  | @ -47,4 +62,13 @@ mod tests { | |||
|         bb.place_piece_at(&Square::from_index(34).expect("Invalid square index")); | ||||
|         assert!(bb.has_piece_at(&Square::from_index(34).expect("Invalid square index"))); | ||||
|     } | ||||
| 
 | ||||
|     #[cfg(target_arch = "arm")] | ||||
|     #[test] | ||||
|     fn arm_count_leading_zeros() { | ||||
|         assert_eq!(BitBoard(0)._arm_count_leading_zeros(), 0); | ||||
|         assert_eq!(BitBoard(0b10)._arm_count_leading_zeros(), 62); | ||||
|         assert_eq!(BitBoard(0b1010)._arm_count_leading_zeros(), 60); | ||||
|         assert_eq!(BitBoard(0x80000000)._arm_count_leading_zeros(), 0); | ||||
|     } | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue