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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
use square;
use square::Square;
use std::fmt;
use std::ops::*;
use util::grid_to_string;

#[cfg(test)]
use rand;

/// BB represents a bitboard
#[derive(PartialEq, Copy, Clone)]
pub struct BB(pub u64);

impl BB {
    #[inline]
    pub fn new(sq: Square) -> BB {
        BB(1u64 << sq.to_usize())
    }

    /// Creates a random bitboard with a given average density
    #[inline]
    #[cfg(test)]
    pub fn random(density: f64) -> BB {
        let mut u = 0u64;

        for i in 0..64 {
            if rand::random::<f64>() < density {
                u |= 1u64 << i;
            }
        }

        BB(u)
    }

    #[inline]
    pub fn to_u64(&self) -> u64 {
        self.0
    }

    #[inline]
    pub fn to_usize(&self) -> usize {
        self.0 as usize
    }

    /// true if non empty
    #[inline]
    pub fn any(&self) -> bool {
        self.0 != 0u64
    }

    #[inline]
    pub fn none(&self) -> bool {
        self.0 == 0u64
    }

    /// swaps bytes
    #[inline]
    pub fn bswap(&self) -> BB {
        BB(self.0.swap_bytes())
    }

    #[inline]
    pub fn rot_left(&self, amount: u32) -> BB {
        BB(self.0.rotate_left(amount))
    }

    #[inline]
    pub fn rot_right(&self, amount: u32) -> BB {
        BB(self.0.rotate_right(amount))
    }

    #[inline]
    pub fn is_set(&self, sq: Square) -> bool {
        (self.0 >> sq.to_usize()) & 1 != 0
    }

    #[inline]
    pub fn row_empty(&self, row: usize) -> bool {
        let row_mask = 0xFFu64 << (row * 8);
        (self.0 & row_mask) == 0u64
    }

    #[inline]
    pub fn pop_count(&self) -> u32 {
        self.0.count_ones()
    }

    pub fn to_string(&self) -> String {
        let fun = |sq: Square| -> char {
            if self.is_set(sq) {
                '#'
            } else {
                '.'
            }
        };

        grid_to_string(fun)
    }

    #[inline]
    fn lsb(&self) -> BB {
        BB(self.0 & 0u64.wrapping_sub(self.0))
    }

    #[inline]
    pub fn bitscan(&self) -> Square {
        Square::new(self.0.trailing_zeros() as square::Internal)
    }

    #[inline]
    pub fn msb(&self) -> u32 {
        debug_assert!(self.0 != 0);
        63 ^ self.0.leading_zeros()
    }

    #[inline]
    pub fn leading_zeros(&self) -> u32 {
        debug_assert!(self.0 != 0);
        self.0.leading_zeros()
    }

    #[inline]
    pub fn bitscan_reverse(&self) -> u32 {
        self.0.leading_zeros() ^ 63
    }

    #[inline]
    pub fn iter(self) -> BBIterator {
        BBIterator(self)
    }

    #[inline]
    pub fn square_list(&self) -> Vec<Square> {
        self.iter().map(|(sq, _)| sq).collect::<Vec<Square>>()
    }

    #[inline]
    pub fn occluded_east_fill(&self, empty: BB) -> BB {
        let mut prop = empty.0 & NOT_FILE_A.0;
        let mut gen = self.0;

        gen |= prop & (gen << 1);
        prop &= prop << 1;
        gen |= prop & (gen << 2);
        prop &= prop << 2;
        gen |= prop & (gen << 4);

        BB(gen)
    }

    #[inline]
    pub fn east_attacks(&self, empty: BB) -> BB {
        let gen = self.occluded_east_fill(empty);

        (gen << 1) & NOT_FILE_A
    }

    #[inline]
    pub fn occluded_north_east_fill(&self, empty: BB) -> BB {
        let mut prop = empty.0 & NOT_FILE_A.0;
        let mut gen = self.0;

        gen |= prop & (gen << 9);
        prop &= prop << 9;
        gen |= prop & (gen << 18);
        prop &= prop << 18;
        gen |= prop & (gen << 36);

        BB(gen)
    }

    #[inline]
    pub fn north_east_attacks(&self, empty: BB) -> BB {
        let gen = self.occluded_north_east_fill(empty);

        (gen << 9) & NOT_FILE_A
    }

    #[inline]
    pub fn occluded_north_fill(&self, empty: BB) -> BB {
        let mut prop = empty.0;
        let mut gen = self.0;

        gen |= prop & (gen << 8);
        prop &= prop << 8;
        gen |= prop & (gen << 16);
        prop &= prop << 16;
        gen |= prop & (gen << 32);

        BB(gen)
    }

    #[inline]
    pub fn north_attacks(&self, empty: BB) -> BB {
        let gen = self.occluded_north_fill(empty);

        gen << 8
    }

    #[inline]
    pub fn occluded_south_east_fill(&self, empty: BB) -> BB {
        let mut prop = empty.0 & NOT_FILE_A.0;
        let mut gen = self.0;

        gen |= prop & (gen >> 7);
        prop &= prop >> 7;
        gen |= prop & (gen >> 14);
        prop &= prop >> 14;
        gen |= prop & (gen >> 28);

        BB(gen)
    }

    #[inline]
    pub fn south_east_attacks(&self, empty: BB) -> BB {
        let gen = self.occluded_south_east_fill(empty);

        (gen >> 7) & NOT_FILE_A
    }

    #[inline]
    pub fn occluded_west_fill(&self, empty: BB) -> BB {
        let mut prop = empty.0 & NOT_FILE_H.0;
        let mut gen = self.0;

        gen |= prop & (gen >> 1);
        prop &= prop >> 1;
        gen |= prop & (gen >> 2);
        prop &= prop >> 2;
        gen |= prop & (gen >> 4);

        BB(gen)
    }

    #[inline]
    pub fn west_attacks(&self, empty: BB) -> BB {
        let gen = self.occluded_west_fill(empty);

        (gen >> 1) & NOT_FILE_H
    }

    #[inline]
    pub fn occluded_south_west_fill(&self, empty: BB) -> BB {
        let mut prop = empty.0 & NOT_FILE_H.0;
        let mut gen = self.0;

        gen |= prop & (gen >> 9);
        prop &= prop >> 9;
        gen |= prop & (gen >> 18);
        prop &= prop >> 18;
        gen |= prop & (gen >> 36);

        BB(gen)
    }

    #[inline]
    pub fn south_west_attacks(&self, empty: BB) -> BB {
        let gen = self.occluded_south_west_fill(empty);

        (gen >> 9) & NOT_FILE_H
    }

    #[inline]
    pub fn occluded_north_west_fill(&self, empty: BB) -> BB {
        let mut prop = empty.0 & NOT_FILE_H.0;
        let mut gen = self.0;

        gen |= prop & (gen << 7);
        prop &= prop << 7;
        gen |= prop & (gen << 14);
        prop &= prop << 14;
        gen |= prop & (gen << 28);

        BB(gen)
    }

    #[inline]
    pub fn north_west_attacks(&self, empty: BB) -> BB {
        let gen = self.occluded_north_west_fill(empty);

        (gen << 7) & NOT_FILE_H
    }

    #[inline]
    pub fn occluded_south_fill(&self, empty: BB) -> BB {
        let mut prop = empty.0;
        let mut gen = self.0;

        gen |= prop & (gen >> 8);
        prop &= prop >> 8;
        gen |= prop & (gen >> 16);
        prop &= prop >> 16;
        gen |= prop & (gen >> 32);

        BB(gen)
    }

    #[inline]
    pub fn south_attacks(&self, empty: BB) -> BB {
        let gen = self.occluded_south_fill(empty);

        gen >> 8
    }

    #[inline]
    pub fn occluded_east_fill_with_occluders(&self, empty: BB) -> BB {
        let gen = self.occluded_east_fill(empty);

        BB(gen.0 | ((gen.0 << 1) & NOT_FILE_A.0))
    }

    #[inline]
    pub fn occluded_north_east_fill_with_occluders(&self, empty: BB) -> BB {
        let gen = self.occluded_north_east_fill(empty);

        BB(gen.0 | ((gen.0 << 9) & NOT_FILE_A.0))
    }

    #[inline]
    pub fn occluded_north_fill_with_occluders(&self, empty: BB) -> BB {
        let gen = self.occluded_north_fill(empty);

        BB(gen.0 | (gen.0 << 8))
    }

    #[inline]
    pub fn occluded_south_east_fill_with_occluders(&self, empty: BB) -> BB {
        let gen = self.occluded_south_east_fill(empty);

        BB(gen.0 | ((gen.0 >> 7) & NOT_FILE_A.0))
    }

    #[inline]
    pub fn occluded_west_fill_with_occluders(&self, empty: BB) -> BB {
        let gen = self.occluded_west_fill(empty);

        BB(gen.0 | ((gen.0 >> 1) & NOT_FILE_H.0))
    }

    #[inline]
    pub fn occluded_south_west_fill_with_occluders(&self, empty: BB) -> BB {
        let gen = self.occluded_south_west_fill(empty);

        BB(gen.0 | ((gen.0 >> 9) & NOT_FILE_H.0))
    }

    #[inline]
    pub fn occluded_north_west_fill_with_occluders(&self, empty: BB) -> BB {
        let gen = self.occluded_north_west_fill(empty);

        BB(gen.0 | ((gen.0 << 7) & NOT_FILE_H.0))
    }

    #[inline]
    pub fn occluded_south_fill_with_occluders(&self, empty: BB) -> BB {
        let gen = self.occluded_south_fill(empty);

        BB(gen.0 | (gen.0 >> 8))
    }
}

impl Shr<usize> for BB {
    type Output = BB;

    #[inline]
    fn shr(self, amount: usize) -> BB {
        BB(self.0 >> amount)
    }
}

impl Shl<usize> for BB {
    type Output = BB;

    #[inline]
    fn shl(self, amount: usize) -> BB {
        BB(self.0 << amount)
    }
}

impl Not for BB {
    type Output = BB;

    #[inline]
    fn not(self) -> BB {
        BB(!self.0)
    }
}

impl BitOr for BB {
    type Output = BB;

    #[inline]
    fn bitor(self, other: BB) -> BB {
        BB(self.0 | other.0)
    }
}

impl BitOrAssign for BB {
    #[inline]
    fn bitor_assign(&mut self, other: BB) {
        self.0 |= other.0
    }
}

impl BitXor for BB {
    type Output = BB;

    #[inline]
    fn bitxor(self, other: BB) -> BB {
        BB(self.0 ^ other.0)
    }
}

impl BitXorAssign for BB {
    #[inline]
    fn bitxor_assign(&mut self, other: BB) {
        self.0 ^= other.0
    }
}

impl BitAnd for BB {
    type Output = BB;

    #[inline]
    fn bitand(self, other: BB) -> BB {
        BB(self.0 & other.0)
    }
}

impl BitAndAssign for BB {
    #[inline]
    fn bitand_assign(&mut self, other: BB) {
        self.0 &= other.0
    }
}

impl Sub for BB {
    type Output = BB;

    #[inline]
    fn sub(self, other: BB) -> BB {
        BB(self.0.wrapping_sub(other.0))
    }
}

impl Add for BB {
    type Output = BB;

    #[inline]
    fn add(self, other: BB) -> BB {
        BB(self.0.wrapping_add(other.0))
    }
}

impl Mul for BB {
    type Output = BB;

    #[inline]
    fn mul(self, other: BB) -> BB {
        BB(self.0.wrapping_mul(other.0))
    }
}

impl Neg for BB {
    type Output = BB;

    #[inline]
    fn neg(self) -> BB {
        BB(self.0.wrapping_neg())
    }
}

#[allow(dead_code)]
pub const BB_A1: BB = BB(1u64 << 1);
#[allow(dead_code)]
pub const BB_B1: BB = BB(1u64 << 2);
#[allow(dead_code)]
pub const BB_C1: BB = BB(1u64 << 3);
#[allow(dead_code)]
pub const BB_D1: BB = BB(1u64 << 4);
#[allow(dead_code)]
pub const BB_C6: BB = BB(1u64 << 42);
#[allow(dead_code)]
pub const BB_D3: BB = BB(1u64 << 19);
#[allow(dead_code)]
pub const BB_H6: BB = BB(1u64 << 47);

pub const EMPTY: BB = BB(0);
pub const END_ROWS: BB = BB(ROW_1.0 | ROW_8.0);
pub const FILE_A: BB = BB(0x0101010101010101u64);
pub const FILE_B: BB = BB(FILE_A.0 << 1);
pub const FILE_G: BB = BB(FILE_A.0 << 6);
pub const FILE_H: BB = BB(FILE_A.0 << 7);
pub const NOT_FILE_A: BB = BB(!FILE_A.0);
pub const NOT_FILE_H: BB = BB(!FILE_H.0);
pub const ROW_1: BB = BB(0xFFu64);
pub const ROW_2: BB = BB(ROW_1.0 << (8));
pub const ROW_4: BB = BB(ROW_1.0 << (3 * 8));
pub const ROW_5: BB = BB(ROW_1.0 << (4 * 8));
pub const ROW_7: BB = BB(ROW_1.0 << (6 * 8));
pub const ROW_8: BB = BB(ROW_1.0 << (7 * 8));
pub const EDGES: BB = BB(FILE_A.0 | FILE_H.0 | ROW_1.0 | ROW_8.0);

/// `BBIterator` iterates over set bits in a bitboard, from low to high,
/// returning a Square and the bit-board with that bit set
pub struct BBIterator(BB);

impl Iterator for BBIterator {
    type Item = (Square, BB);

    #[inline]
    fn next(&mut self) -> Option<(Square, BB)> {
        if (self.0).0 == EMPTY.0 {
            return None;
        }

        let sq = self.0.bitscan();
        let lsb = self.0.lsb();
        self.0 ^= lsb;
        Some((sq, lsb))
    }
}

impl fmt::Display for BB {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_string())
    }
}

impl fmt::Debug for BB {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_string())
    }
}

#[cfg(test)]
pub mod test {
    use super::*;
    use square::*;
    use unindent;

    #[test]
    fn test_occluded_east_fill() {
        let expected = unindent::unindent(
            "
              ABCDEFGH
            8|........|8
            7|........|7
            6|...#####|6
            5|........|5
            4|........|4
            3|..###...|3
            2|........|2
            1|........|1
              ABCDEFGH
            ",
        );
        let source = BB::new(C3) | BB::new(D6);
        let empty = source | BB::new(F3).not();
        assert_eq!(source.occluded_east_fill(empty).to_string(), expected);
    }

    #[test]
    fn test_east_attacks() {
        let expected = unindent::unindent(
            "
              ABCDEFGH
            8|........|8
            7|........|7
            6|....####|6
            5|........|5
            4|........|4
            3|...###..|3
            2|........|2
            1|........|1
              ABCDEFGH
            ",
        );
        let source = BB::new(C3) | BB::new(D6);
        let empty = source | BB::new(F3).not();
        assert_eq!(source.east_attacks(empty).to_string(), expected);
    }

    #[test]
    fn consts_1() {
        let expected = unindent::unindent(
            "
              ABCDEFGH
            8|#.......|8
            7|#.......|7
            6|#.......|6
            5|#.......|5
            4|#.......|4
            3|#.......|3
            2|#.......|2
            1|#.......|1
              ABCDEFGH
            ",
        );
        assert_eq!(FILE_A.to_string(), expected);
    }

    #[test]
    fn consts_2() {
        let expected = unindent::unindent(
            "
              ABCDEFGH
            8|.......#|8
            7|.......#|7
            6|.......#|6
            5|.......#|5
            4|.......#|4
            3|.......#|3
            2|.......#|2
            1|.......#|1
              ABCDEFGH
            ",
        );
        assert_eq!(FILE_H.to_string(), expected);
    }

    #[test]
    fn consts_4() {
        let expected = unindent::unindent(
            "
              ABCDEFGH
            8|########|8
            7|........|7
            6|........|6
            5|........|5
            4|........|4
            3|........|3
            2|........|2
            1|########|1
              ABCDEFGH
            ",
        );
        assert_eq!(END_ROWS.to_string(), expected);
    }
}