diff --git a/examples/eval_speed.rs b/examples/eval_speed.rs new file mode 100644 index 00000000..f59a1395 --- /dev/null +++ b/examples/eval_speed.rs @@ -0,0 +1,17 @@ +use rechner::card::Card; +use rechner::hand::fast7::{mask, score}; +fn main() { + let deck: Vec = Card::parse_list("2c 3c 4c 5c 6c 7c 8c 9c Tc Jc Qc Kc Ac 2d 3d 4d 5d 6d 7d 8d 9d Td Jd Qd Kd Ad 2h 3h 4h 5h 6h 7h 8h 9h Th Jh Qh Kh Ah 2s 3s 4s 5s 6s 7s 8s 9s Ts Js Qs Ks As").unwrap(); + let mut x: u64 = 12345; + let mut hands = Vec::with_capacity(1_000_000); + while hands.len() < 1_000_000 { + let mut m = 0u64; let mut k = 0; + while k < 7 { x ^= x << 13; x ^= x >> 7; x ^= x << 17; let b = mask(&[deck[(x % 52) as usize]]); if m & b == 0 { m |= b; k += 1; } } + hands.push(m); + } + let t = std::time::Instant::now(); + let mut acc = 0u64; + for _ in 0..20 { for &m in &hands { acc = acc.wrapping_add(score(m) as u64); } } + let s = t.elapsed().as_secs_f64(); + println!("20M evals in {:.2}s = {:.1} M/s ({acc})", s, 20.0 / s); +} diff --git a/src/hand/fast7.rs b/src/hand/fast7.rs new file mode 100644 index 00000000..49f5c549 --- /dev/null +++ b/src/hand/fast7.rs @@ -0,0 +1,158 @@ +//! Fast 5-to-7-card Hold'em evaluator on a 64-bit card mask (plan E2a). +//! +//! `showdown::best` is general (variants, Omaha, decks) and enumerates 5-card subsets: about 0.8 M +//! hands per second. A blueprint solve (plan E2) needs tens of millions. This evaluator works on +//! rank bitmasks per suit, no subsets and no tables, and returns a `u32` whose ORDER equals the +//! order of `best` under standard rules (ace low in A-2-3-4-5, full house beats flush). Only the +//! order is promised, not the value; the spec checks it against `best` on random hands. +//! +//! Layout: bit `16 * suit + rank` (rank 0 = deuce ... 12 = ace), so each suit is one 13-bit lane. + +use crate::card::Card; + +/// The mask bit of one card. +pub fn bit(c: Card) -> u64 { + 1u64 << (16 * c.suit as u32 + c.rank as u32) +} + +/// The mask of a set of cards. +pub fn mask(cards: &[Card]) -> u64 { + cards.iter().fold(0, |m, c| m | bit(*c)) +} + +const LANE: u64 = 0x1FFF; + +/// The highest straight in a 13-bit rank set: index of its top rank (3 = five-high wheel), or `None`. +fn straight_top(ranks: u32) -> Option { + // Shift up by one and put the ace at position 0 as well, so the wheel is a run of five. + let b = (ranks << 1) | ((ranks >> 12) & 1); + let run = b & (b >> 1) & (b >> 2) & (b >> 3) & (b >> 4); + (run != 0).then(|| 31 - run.leading_zeros() + 4 - 1) +} + +/// Keep the `n` highest set bits. +fn top(mut bits: u32, n: u32) -> u32 { + while bits.count_ones() > n { + bits &= bits - 1; + } + bits +} + +fn high(bits: u32) -> u32 { + 31 - bits.leading_zeros() +} + +const fn cat(c: u32) -> u32 { + c << 24 +} + +/// Score of the best five of the 5-7 cards in `m`; higher is better, equal is a split. +pub fn score(m: u64) -> u32 { + let s = [(m & LANE) as u32, ((m >> 16) & LANE) as u32, ((m >> 32) & LANE) as u32, ((m >> 48) & LANE) as u32]; + for lane in s { + if lane.count_ones() >= 5 { + return match straight_top(lane) { + Some(t) => cat(8) | t, + None => cat(5) | top(lane, 5), + }; + } + } + let ranks = s[0] | s[1] | s[2] | s[3]; + let four = s[0] & s[1] & s[2] & s[3]; + if four != 0 { + let q = high(four); + return cat(7) | (q << 13) | top(ranks & !(1 << q), 1); + } + let three = (s[0] & s[1] & s[2]) | (s[0] & s[1] & s[3]) | (s[0] & s[2] & s[3]) | (s[1] & s[2] & s[3]); + let two = (s[0] & s[1]) | (s[0] & s[2]) | (s[0] & s[3]) | (s[1] & s[2]) | (s[1] & s[3]) | (s[2] & s[3]); + if three != 0 { + let t = high(three); + let pair = two & !(1 << t); + if pair != 0 { + return cat(6) | (t << 13) | (1 << high(pair)); + } + } + if let Some(t) = straight_top(ranks) { + return cat(4) | t; + } + if three != 0 { + let t = high(three); + return cat(3) | (t << 13) | top(ranks & !(1 << t), 2); + } + if two.count_ones() >= 2 { + let p = top(two, 2); + return cat(2) | (p_code(p) << 13) | top(ranks & !p, 1); + } + if two != 0 { + let p = high(two); + return cat(1) | (p << 13) | top(ranks & !(1 << p), 3); + } + top(ranks, 5) +} + +/// Two-pair code: the two pair ranks as one comparable number (high pair dominates). +fn p_code(p: u32) -> u32 { + let hi = high(p); + let lo = high(p & !(1 << hi)); + hi * 13 + lo +} + +/// Score of hole + board cards. +pub fn score_cards(hole: &[Card], board: &[Card]) -> u32 { + score(mask(hole) | mask(board)) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::hand::showdown::best; + use crate::variant::HoleUsage; + + fn deck() -> Vec { + Card::parse_list("2c 3c 4c 5c 6c 7c 8c 9c Tc Jc Qc Kc Ac 2d 3d 4d 5d 6d 7d 8d 9d Td Jd Qd Kd Ad 2h 3h 4h 5h 6h 7h 8h 9h Th Jh Qh Kh Ah 2s 3s 4s 5s 6s 7s 8s 9s Ts Js Qs Ks As").unwrap() + } + + fn deal(x: &mut u64, d: &[Card], n: usize) -> Vec { + let mut out = Vec::with_capacity(n); + while out.len() < n { + *x ^= *x << 13; + *x ^= *x >> 7; + *x ^= *x << 17; + let c = d[(*x % 52) as usize]; + if !out.contains(&c) { + out.push(c); + } + } + out + } + + /// Same order as `showdown::best` on random 7-card pairs sharing a board (the solver's use), and + /// on random independent 5-, 6- and 7-card hands. + #[test] + fn the_order_matches_the_general_evaluator() { + let d = deck(); + let mut x = 0x5EED_FA57u64; + for round in 0..300_000 { + let n = 5 + round % 3; + let cards = deal(&mut x, &d, n + 2); + let board = &cards[2..n]; + let (a, b) = (&cards[..2], &cards[n..n + 2]); + let a_board: Vec = board.to_vec(); + let sa = best(HoleUsage::BestFiveOfAll, a, &a_board).unwrap(); + let sb = best(HoleUsage::BestFiveOfAll, b, &a_board).unwrap(); + let (fa, fb) = (score_cards(a, board), score_cards(b, board)); + assert_eq!(sa.cmp(&sb), fa.cmp(&fb), "{a:?} vs {b:?} on {board:?}: {sa:?} {sb:?} / {fa} {fb}"); + } + } + + #[test] + fn known_hands() { + let s = |t: &str| score(mask(&Card::parse_list(t).unwrap())); + assert!(s("Ah 2c 3d 4s 5h 9c Kd") < s("2h 3c 4d 5s 6h 9c Kd"), "wheel below six-high straight"); + assert!(s("Ah Kh Qh Jh Th 2c 3d") > s("9h 9c 9d 9s Th 2c 3d"), "royal over quads"); + assert!(s("Kh Kc Kd 2s 2h 3c 4d") > s("Ah Qh 9h 5h 2h 3c 4d"), "full house over flush"); + assert!(s("Ah Ac Kd Ks 2c 2d 7h") > s("Ah Ac Qd Qs Jc Jd 7h"), "aces up kings over aces up queens"); + assert!(s("Ah Ac Kd Ks 7c 2d 3h") > s("Ah Ac Kd Ks 6c 5d 3h"), "two-pair kicker"); + assert_eq!(s("Ah Ac Kd Ks 7c 2d 3h"), s("Ad As Kc Kh 7d 2c 3s"), "split"); + } +} diff --git a/src/hand/mod.rs b/src/hand/mod.rs index fb8a3a88..7700e909 100644 --- a/src/hand/mod.rs +++ b/src/hand/mod.rs @@ -1,6 +1,7 @@ //! Hand evaluation: rank categories, comparable strengths, best-hand selection per variant. pub mod category; +pub mod fast7; pub mod five; pub mod ranking_order; pub mod showdown;