//! returns: 0 /// Keep two full-width values and a boolean live across alternating joins. fn run(limit: u32, start: u64) -> u64 { let mut first = start; let mut second = start; let mut selected = true; for index in 0..limit { let amount = (index as u64) + 1; if selected { set first += amount; } else { set second += 2 * amount; } set selected = not selected; } return first ^ second; } /// Exercise a function with no local cache entries. fn empty() { } /// Preserve cached values across continue and break edges. fn exits(limit: u32, start: u64) -> u64 { let mut result = start; for index in 0..limit { if index == 7 { break; } if index % 2 == 0 { continue; } set result += index as u64; } return result; } /// Compare cache joins with the closed-form sums of odd and even integers. @default fn main() -> u32 { for start in [0 as u64, 0x100000001, 0x8000000000000000] { for limit in 0..32 { empty(); let odds = ((limit + 1) / 2) as u64; let evens = (limit / 2) as u64; let first = start + odds * odds; let second = start + 2 * evens * (evens + 1); assert run(limit, start) == first ^ second; assert run(limit, start) == first ^ second; let bound: u32 = 7 if limit > 7 else limit; let count = (bound / 2) as u64; assert exits(limit, start) == start + count * count; } } return 0; }