1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Defines constants and functions for working with bitboards.
//!
//! `u64` bit-sets called *bitboards* can be used to represent a set
//! of squares on the chessboard. This module defines utility
//! functions and constants for working with `u64` bit-sets.
//!
//! **Note:** "LSB" means "least significant `1` bit".

use board::{Square, Bitboard};


/// Empty set of squares.
pub const BB_NONE: Bitboard = 0;

/// The set of all 64 squares on the board.
pub const BB_ALL: Bitboard = 0xffffffffffffffff;

/// The squares on rank 1.
pub const BB_RANK_1: Bitboard = 0b11111111;
/// The squares on rank 2.
pub const BB_RANK_2: Bitboard = BB_RANK_1 << 8;
/// The squares on rank 3.
pub const BB_RANK_3: Bitboard = BB_RANK_2 << 8;
/// The squares on rank 4.
pub const BB_RANK_4: Bitboard = BB_RANK_3 << 8;
/// The squares on rank 5.
pub const BB_RANK_5: Bitboard = BB_RANK_4 << 8;
/// The squares on rank 6.
pub const BB_RANK_6: Bitboard = BB_RANK_5 << 8;
/// The squares on rank 7.
pub const BB_RANK_7: Bitboard = BB_RANK_6 << 8;
/// The squares on rank 8.
pub const BB_RANK_8: Bitboard = BB_RANK_7 << 8;

/// The squares on file A.
pub const BB_FILE_A: Bitboard = 0x0101010101010101;
/// The squares on file B.
pub const BB_FILE_B: Bitboard = BB_FILE_A << 1;
/// The squares on file C.
pub const BB_FILE_C: Bitboard = BB_FILE_B << 1;
/// The squares on file D.
pub const BB_FILE_D: Bitboard = BB_FILE_C << 1;
/// The squares on file E.
pub const BB_FILE_E: Bitboard = BB_FILE_D << 1;
/// The squares on file F.
pub const BB_FILE_F: Bitboard = BB_FILE_E << 1;
/// The squares on file G.
pub const BB_FILE_G: Bitboard = BB_FILE_F << 1;
/// The squares on file H.
pub const BB_FILE_H: Bitboard = BB_FILE_G << 1;

/// The squares on the main diagonal (A1-H8).
pub const BB_MAIN_DIAG: Bitboard = 0x8040201008040201;

/// The squares on the main anti-diagonal (H1-A8).
pub const BB_MAIN_ANTI_DIAG: Bitboard = 0x0102040810204080;


/// Returns the LSB of a value.
///
/// The way to calculate this is: `lsb = x & -x;`.
///
/// If `x` is `0` this function returns `0`.
///
/// # Examples:
///
/// ```rust
/// # use alcibiades::bitsets::*;
/// assert_eq!(lsb(0b10100), 0b100);
/// assert_eq!(lsb(0), 0);
/// ```
/// ```text
/// 
///        x         &        -x         =      lsb(x)
/// . . . . . . . .     1 1 1 1 1 1 1 1     . . . . . . . .
/// . . 1 . 1 . . .     1 1 . 1 . 1 1 1     . . . . . . . .
/// . 1 . . . 1 . .     1 . 1 1 1 . 1 1     . . . . . . . .
/// . . . . . . . .     1 1 1 1 1 1 1 1     . . . . . . . .
/// . 1 . . . 1 . .  &  1 . 1 1 1 . 1 1  =  . . . . . . . .
/// . . 1 . 1 . . .     . . 1 1 . 1 1 1     . . 1 . . . . .
/// . . . . . . . .     . . . . . . . .     . . . . . . . .
/// . . . . . . . .     . . . . . . . .     . . . . . . . .
/// ```
#[inline]
pub fn lsb(x: Bitboard) -> Bitboard {
    x & x.wrapping_neg()
}


/// Resets the LSB of a value to zero.
///
/// The way to calculate this is: `x_with_reset_lsb = x & (x - 1);`.
///
/// If `*x` is `0` this function does nothing.
///
/// # Examples:
///
/// ```rust
/// # use alcibiades::bitsets::*;
/// let mut x = 0b100100;
/// reset_lsb(&mut x);
/// assert_eq!(x, 0b100000);
/// reset_lsb(&mut x);
/// assert_eq!(x, 0);
/// reset_lsb(&mut x);
/// assert_eq!(x, 0);
/// ```
/// ```text
/// 
///       x          &      (x - 1)      =  x_with_reset_lsb(x)
/// . . . . . . . .     . . . . . . . .     . . . . . . . .
/// . . 1 . 1 . . .     . . 1 . 1 . . .     . . 1 . 1 . . .
/// . 1 . . . 1 . .     . 1 . . . 1 . .     . 1 . . . 1 . .
/// . . . . . . . .     . . . . . . . .     . . . . . . . .
/// . 1 . . . 1 . .  &  . 1 . . . 1 . .  =  . 1 . . . 1 . .
/// . . 1 . 1 . . .     1 1 . . 1 . . .     . . . . 1 . . .
/// . . . . . . . .     1 1 1 1 1 1 1 1     . . . . . . . .
/// . . . . . . . .     1 1 1 1 1 1 1 1     . . . . . . . .
/// ```
#[inline]
pub fn reset_lsb(x: &mut Bitboard) {
    *x &= x.wrapping_sub(1);
}


/// Returns a mask with all bits above the LSB set to `1`.
///
/// The way to calculate this is: `above_lsb = x ^ -x;`.
///
/// If `x` is `0` this function returns `0`.
/// 
/// # Examples:
///
/// ```rust
/// # use alcibiades::bitsets::*;
/// assert_eq!(above_lsb(0b10100), !0b111);
/// assert_eq!(above_lsb(0), 0);
/// ```
/// ```text
/// 
///       x          ^        -x         =  above_lsb(x)
/// . . . . . . . .     1 1 1 1 1 1 1 1     1 1 1 1 1 1 1 1
/// . . 1 . 1 . . .     1 1 . 1 . 1 1 1     1 1 1 1 1 1 1 1
/// . 1 . . . 1 . .     1 . 1 1 1 . 1 1     1 1 1 1 1 1 1 1
/// . . . . . . . .     1 1 1 1 1 1 1 1     1 1 1 1 1 1 1 1
/// . 1 . . . 1 . .  ^  1 . 1 1 1 . 1 1  =  1 1 1 1 1 1 1 1
/// . . 1 . 1 . . .     . . 1 1 . 1 1 1     . . . 1 1 1 1 1
/// . . . . . . . .     . . . . . . . .     . . . . . . . .
/// . . . . . . . .     . . . . . . . .     . . . . . . . .
/// ```
#[inline]
pub fn above_lsb(x: Bitboard) -> Bitboard {
    x ^ x.wrapping_neg()
}


/// Returns a mask with all bits below and including the LSB set to
/// `1`.
///
/// The way to calculate this is: `below_lsb_including = x ^ (x - 1);`.
///
/// If `x` is `0` this function returns `0xffffffffffffffff`.
/// 
/// # Examples:
///
/// ```rust
/// # use alcibiades::bitsets::*;
/// assert_eq!(below_lsb_including(0b10100), 0b111);
/// assert_eq!(below_lsb_including(0), !0);
/// ```
/// ```text
///       x          ^      (x - 1)      =  below_lsb_including(x)
/// . . . . . . . .     . . . . . . . .     . . . . . . . .
/// . . 1 . 1 . . .     . . 1 . 1 . . .     . . . . . . . .
/// . 1 . . . 1 . .     . 1 . . . 1 . .     . . . . . . . .
/// . . . . . . . .     . . . . . . . .     . . . . . . . .
/// . 1 . . . 1 . .  ^  . 1 . . . 1 . .  =  . . . . . . . .
/// . . 1 . 1 . . .     1 1 . . 1 . . .     1 1 1 . . . . .
/// . . . . . . . .     1 1 1 1 1 1 1 1     1 1 1 1 1 1 1 1
/// . . . . . . . .     1 1 1 1 1 1 1 1     1 1 1 1 1 1 1 1
/// ```
#[inline]
pub fn below_lsb_including(x: Bitboard) -> Bitboard {
    x ^ x.wrapping_sub(1)
}


/// Returns a mask with all bits above and including the LSB set to
/// `1`.
///
/// The way to calculate this is: `above_lsb_including = x | -x;`.
///
/// If `x` is `0` this function returns `0`.
/// 
/// # Examples:
///
/// ```rust
/// # use alcibiades::bitsets::*;
/// assert_eq!(above_lsb_including(0b10100), !0b11);
/// assert_eq!(above_lsb_including(0), 0);
/// ```
/// ```text
///       x          |        -x         =  above_lsb_including(x)
/// . . . . . . . .     1 1 1 1 1 1 1 1     1 1 1 1 1 1 1 1
/// . . 1 . 1 . . .     1 1 . 1 . 1 1 1     1 1 1 1 1 1 1 1
/// . 1 . . . 1 . .     1 . 1 1 1 . 1 1     1 1 1 1 1 1 1 1
/// . . . . . . . .     1 1 1 1 1 1 1 1     1 1 1 1 1 1 1 1
/// . 1 . . . 1 . .  |  1 . 1 1 1 . 1 1  =  1 1 1 1 1 1 1 1
/// . . 1 . 1 . . .     . . 1 1 . 1 1 1     . . 1 1 1 1 1 1
/// . . . . . . . .     . . . . . . . .     . . . . . . . .
/// . . . . . . . .     . . . . . . . .     . . . . . . . .
/// ```
#[inline]
pub fn above_lsb_including(x: Bitboard) -> Bitboard {
    x | x.wrapping_neg()
}


/// Returns a mask with all bits below the LSB set to `1`.
///
/// The way to calculate this is: `below_lsb = !x & (x - 1);`.
///
/// If `x` is `0` this function returns `0xffffffffffffffff`.
/// 
/// # Examples:
///
/// ```rust
/// # use alcibiades::bitsets::*;
/// assert_eq!(below_lsb(0b10100), 0b11);
/// assert_eq!(below_lsb(0), !0);
/// ```
/// ```text
///      !x          &      (x - 1)      =  below_lsb(x)
/// 1 1 1 1 1 1 1 1     . . . . . . . .     . . . . . . . .
/// 1 1 . 1 . 1 1 1     . . 1 . 1 . . .     . . . . . . . .
/// 1 . 1 1 1 . 1 1     . 1 . . . 1 . .     . . . . . . . .
/// 1 1 1 1 1 1 1 1     . . . . . . . .     . . . . . . . .
/// 1 . 1 1 1 . 1 1  &  . 1 . . . 1 . .  =  . . . . . . . .
/// 1 1 . 1 . 1 1 1     1 1 . . 1 . . .     1 1 . . . . . .
/// 1 1 1 1 1 1 1 1     1 1 1 1 1 1 1 1     1 1 1 1 1 1 1 1
/// 1 1 1 1 1 1 1 1     1 1 1 1 1 1 1 1     1 1 1 1 1 1 1 1
/// ```
#[inline]
pub fn below_lsb(x: Bitboard) -> Bitboard {
    !x & x.wrapping_sub(1)
}


/// Shifts a value with a signed number.
///
/// Returns `x << s` if `s` is positive, `x >> s` if `s` is
/// negative, `x` if `s` is zero.
///
/// # Examples:
///
/// ```rust
/// # use alcibiades::bitsets::*;
/// assert_eq!(gen_shift(0b101, 1), 0b1010);
/// assert_eq!(gen_shift(0b101, -1), 0b10);
/// assert_eq!(gen_shift(0b101, 0), 0b101);
/// ```
#[inline]
pub fn gen_shift(x: Bitboard, s: isize) -> Bitboard {
    if s > 0 {
        x << s
    } else {
        x >> -s
    }
}


/// Returns the binary position of the LSB (bit-scan-forward).
///
/// If `x` is `0` this function returns `64`.
///
/// # Examples:
///
/// ```rust
/// # use alcibiades::bitsets::*;
/// assert_eq!(bsf(0b100100), 2);
/// assert_eq!(bsf(0), 64);
/// ```
#[inline]
pub fn bsf(x: Bitboard) -> Square {
    x.trailing_zeros() as Square
}


/// Returns the binary position of the LSB (bit-scan-forward), and
/// resets the LSB to zero.
///
/// If `*x` is `0` this function returns `64`.
/// 
/// # Examples:
///
/// ```rust
/// # use alcibiades::bitsets::*;
/// let mut x = 0b100100;
/// assert_eq!(bsf_reset(&mut x), 2);
/// assert_eq!(x, 0b100000);
/// assert_eq!(bsf_reset(&mut x), 5);
/// assert_eq!(x, 0);
/// assert_eq!(bsf_reset(&mut x), 64);
/// assert_eq!(x, 0);
/// ```
#[inline]
pub fn bsf_reset(x: &mut Bitboard) -> Square {
    let a = lsb(*x);
    *x ^= a;
    bsf(a)
}


/// Returns the number of `1`s in the binary representation of a
/// value.
///
/// # Examples:
///
/// ```rust
/// # use alcibiades::bitsets::*;
/// assert_eq!(pop_count(0b100101), 3);
/// assert_eq!(pop_count(0), 0);
/// ```
#[inline]
pub fn pop_count(b: Bitboard) -> usize {
    b.count_ones() as usize
}


/// Returns the set of squares on the same rank as the given square.
///
/// # Examples:
///
/// ```rust
/// # use alcibiades::bitsets::*;
/// # use alcibiades::squares::*;
/// assert_eq!(bb_rank(E4), BB_RANK_4);
/// ```
#[inline]
pub fn bb_rank(square: Square) -> Bitboard {
    BB_RANK_1 << ((square >> 3) << 3)
}


/// Returns the set of squares on the same file as the given square.
///
/// # Examples:
///
/// ```rust
/// # use alcibiades::bitsets::*;
/// # use alcibiades::squares::*;
/// assert_eq!(bb_file(E4), BB_FILE_E);
/// ```
#[inline]
pub fn bb_file(square: Square) -> Bitboard {
    BB_FILE_A << (square % 8)
}


/// Returns the set of squares on the same diagonal as the given square.
///
/// Diagonals go from white's queen-side to black's king-side (A1-H8
/// for example).
///
/// # Examples:
///
/// ```rust
/// # use alcibiades::bitsets::*;
/// # use alcibiades::squares::*;
/// assert_eq!(bb_diag(D4), BB_MAIN_DIAG);
/// ```
#[inline]
pub fn bb_diag(square: Square) -> Bitboard {
    let diag_index = ((square >> 3).wrapping_sub(square % 8)) & 15;
    if diag_index <= 7 {
        BB_MAIN_DIAG << (diag_index << 3)
    } else {
        BB_MAIN_DIAG >> ((16 - diag_index) << 3)
    }
}


/// Returns the set of squares on the same anti-diagonal as the given square.
///
/// Anti-diagonals go from white's king-side to black's queen-side
/// (H1-A8 for example).
///
/// # Examples:
///
/// ```rust
/// # use alcibiades::bitsets::*;
/// # use alcibiades::squares::*;
/// assert_eq!(bb_anti_diag(E4), BB_MAIN_ANTI_DIAG);
/// ```
#[inline]
pub fn bb_anti_diag(square: Square) -> Bitboard {
    let diag_index = ((square >> 3) + (square % 8)) ^ 7;
    if diag_index <= 7 {
        BB_MAIN_ANTI_DIAG >> (diag_index << 3)
    } else {
        BB_MAIN_ANTI_DIAG << ((16 - diag_index) << 3)
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn bitsets() {
        assert_eq!(lsb(0x100100), 0x100);
        assert_eq!(lsb(0x8000000000000000), 0x8000000000000000);
        assert_eq!(lsb(0xf800000000000000), 0x0800000000000000);
        assert_eq!(lsb(0), 0);
        let mut x = 0x100100u64;
        reset_lsb(&mut x);
        assert_eq!(x, 0x100000);
        reset_lsb(&mut x);
        assert_eq!(x, 0);
        reset_lsb(&mut x);
        assert_eq!(x, 0);
        assert_eq!(above_lsb(0), 0);
        assert_eq!(above_lsb(0b11101000), 0xfffffffffffffff0);
        assert_eq!(above_lsb(0x8000000000000000), 0);
        assert_eq!(below_lsb_including(0), 0xffffffffffffffff);
        assert_eq!(below_lsb_including(0b11101000), 0b1111);
        assert_eq!(below_lsb(0), 0xffffffffffffffff);
        assert_eq!(below_lsb(0b11101000), 0b111);
        assert_eq!(above_lsb_including(0), 0);
        assert_eq!(above_lsb_including(0b1010000), 0xfffffffffffffff0);
        assert_eq!(above_lsb_including(0x8000000000000000), 0x8000000000000000);
        assert_eq!(pop_count(0), 0);
        assert_eq!(pop_count(0b1001101), 4);
        assert_eq!(pop_count(0xffffffffffffffff), 64);
        assert_eq!(bsf(0b1001101), 0);
        assert_eq!(bsf(0b1001000), 3);
        assert_eq!(bsf(0xf000000000000000), 60);
        x = 0b1100100;
        assert_eq!(bsf_reset(&mut x), 2);
        assert_eq!(x, 0b1100000);
    }
}