benchmarks-mv
Measurement harnesses and evidence bundles for AIEN on the NVIDIA DGX Spark GB10. Speed is reported against the machine's physical limit and my own previous run. Attachment of AienOS.
Brought by drakestapleton
benchmarks-mv / prime-drag-race / src / reference.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Independent reference: deterministic Miller-Rabin on every odd n <= limit. |
| 17 | //! Canonical bitmap: bit k of little-endian u64 words is set iff 2k+1 is prime, |
| 18 | //! for 0 <= k < odd_count = (limit+1)/2. Tail bits are zero. The prime 2 is implicit. |
| 19 | |
| 20 | pub fn mulmod(a: u64, b: u64, m: u64) -> u64 { |
| 21 | ((a as u128 * b as u128) % m as u128) as u64 |
| 22 | } |
| 23 | |
| 24 | pub fn powmod(mut b: u64, mut e: u64, m: u64) -> u64 { |
| 25 | let mut r = 1u64 % m; |
| 26 | b %= m; |
| 27 | while e > 0 { |
| 28 | if e & 1 == 1 { |
| 29 | r = mulmod(r, b, m); |
| 30 | } |
| 31 | b = mulmod(b, b, m); |
| 32 | e >>= 1; |
| 33 | } |
| 34 | r |
| 35 | } |
| 36 | |
| 37 | const BASES_SMALL: [u64; 4] = [2, 3, 5, 7]; |
| 38 | const BASES_TWELVE: [u64; 12] = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]; |
| 39 | |
| 40 | /// Deterministic Miller-Rabin. Bases 2,3,5,7 are exact below 3,215,031,751; |
| 41 | /// the first 12 primes are exact below 3.3e24 (covers all u64). |
| 42 | pub fn is_prime(n: u64) -> bool { |
| 43 | if n < 2 { |
| 44 | return false; |
| 45 | } |
| 46 | let bases: &[u64] = if n < 3_215_031_751 { &BASES_SMALL } else { &BASES_TWELVE }; |
| 47 | for &p in bases { |
| 48 | if n == p { |
| 49 | return true; |
| 50 | } |
| 51 | if n % p == 0 { |
| 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | let mut d = n - 1; |
| 56 | let mut s = 0u32; |
| 57 | while d % 2 == 0 { |
| 58 | d /= 2; |
| 59 | s += 1; |
| 60 | } |
| 61 | 'outer: for &a in bases { |
| 62 | let mut x = powmod(a, d, n); |
| 63 | if x == 1 || x == n - 1 { |
| 64 | continue; |
| 65 | } |
| 66 | for _ in 1..s { |
| 67 | x = mulmod(x, x, n); |
| 68 | if x == n - 1 { |
| 69 | continue 'outer; |
| 70 | } |
| 71 | } |
| 72 | return false; |
| 73 | } |
| 74 | true |
| 75 | } |
| 76 | |
| 77 | pub fn odd_count(limit: u64) -> u64 { |
| 78 | (limit + 1) / 2 |
| 79 | } |
| 80 | |
| 81 | pub fn word_count(limit: u64) -> usize { |
| 82 | odd_count(limit).div_ceil(64) as usize |
| 83 | } |
| 84 | |
| 85 | pub fn byte_len(limit: u64) -> usize { |
| 86 | word_count(limit) * 8 |
| 87 | } |
| 88 | |
| 89 | pub fn canonical_words(limit: u64) -> Vec<u64> { |
| 90 | let mut w = vec![0u64; word_count(limit)]; |
| 91 | for k in 0..odd_count(limit) { |
| 92 | if is_prime(2 * k + 1) { |
| 93 | w[(k / 64) as usize] |= 1u64 << (k % 64); |
| 94 | } |
| 95 | } |
| 96 | w |
| 97 | } |
| 98 | |
| 99 | pub fn canonical_bytes(limit: u64) -> Vec<u8> { |
| 100 | canonical_words(limit).iter().flat_map(|w| w.to_le_bytes()).collect() |
| 101 | } |
| 102 | |
| 103 | /// pi(limit) from a canonical bitmap: popcount + 1 for the implicit prime 2. |
| 104 | pub fn prime_count(bytes: &[u8], limit: u64) -> u64 { |
| 105 | let pc: u64 = bytes.iter().map(|b| b.count_ones() as u64).sum(); |
| 106 | pc + u64::from(limit >= 2) |
| 107 | } |
| 108 | |
| 109 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 110 | pub struct Diff { |
| 111 | /// The odd number 2k+1 for bit k (for tail garbage: the would-be number past the limit). |
| 112 | pub odd: u64, |
| 113 | pub tail: bool, |
| 114 | pub expected_prime: bool, |
| 115 | } |
| 116 | |
| 117 | impl std::fmt::Display for Diff { |
| 118 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 119 | if self.tail { |
| 120 | write!(f, "tail bit set past the limit (odd number {})", self.odd) |
| 121 | } else if self.expected_prime { |
| 122 | write!(f, "odd number {} is prime but the bit is clear", self.odd) |
| 123 | } else { |
| 124 | write!(f, "odd number {} is composite or 1 but the bit is set", self.odd) |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /// First differing bit between two equal-length bitmaps. |
| 130 | pub fn first_difference(expected: &[u8], got: &[u8], limit: u64) -> Option<Diff> { |
| 131 | debug_assert_eq!(expected.len(), got.len()); |
| 132 | for (i, (e, g)) in expected.iter().zip(got.iter()).enumerate() { |
| 133 | let x = e ^ g; |
| 134 | if x != 0 { |
| 135 | let k = i as u64 * 8 + x.trailing_zeros() as u64; |
| 136 | return Some(Diff { |
| 137 | odd: 2 * k + 1, |
| 138 | tail: k >= odd_count(limit), |
| 139 | expected_prime: (e >> (k % 8)) & 1 == 1, |
| 140 | }); |
| 141 | } |
| 142 | } |
| 143 | None |
| 144 | } |
| 145 | |
| 146 | #[cfg(test)] |
| 147 | mod tests { |
| 148 | use super::*; |
| 149 | |
| 150 | fn trial(n: u64) -> bool { |
| 151 | if n < 2 { |
| 152 | return false; |
| 153 | } |
| 154 | let mut d = 2; |
| 155 | while d * d <= n { |
| 156 | if n % d == 0 { |
| 157 | return false; |
| 158 | } |
| 159 | d += 1; |
| 160 | } |
| 161 | true |
| 162 | } |
| 163 | |
| 164 | fn trial_bytes(limit: u64) -> Vec<u8> { |
| 165 | let mut w = vec![0u64; word_count(limit)]; |
| 166 | for k in 0..odd_count(limit) { |
| 167 | if trial(2 * k + 1) { |
| 168 | w[(k / 64) as usize] |= 1u64 << (k % 64); |
| 169 | } |
| 170 | } |
| 171 | w.iter().flat_map(|x| x.to_le_bytes()).collect() |
| 172 | } |
| 173 | |
| 174 | #[test] |
| 175 | fn equals_trial_division_every_limit_to_2000() { |
| 176 | for limit in 0..=2000 { |
| 177 | assert_eq!(canonical_bytes(limit), trial_bytes(limit), "limit {limit}"); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | #[test] |
| 182 | fn equals_trial_division_at_1e5() { |
| 183 | assert_eq!(canonical_bytes(100_000), trial_bytes(100_000)); |
| 184 | } |
| 185 | |
| 186 | fn pi(limit: u64) -> u64 { |
| 187 | prime_count(&canonical_bytes(limit), limit) |
| 188 | } |
| 189 | |
| 190 | #[test] |
| 191 | fn pi_table() { |
| 192 | for (l, c) in [(10, 4), (100, 25), (1_000, 168), (10_000, 1229), (100_000, 9592), (1_000_000, 78_498)] { |
| 193 | assert_eq!(pi(l), c, "pi({l})"); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | #[test] |
| 198 | #[ignore = "slow in debug builds"] |
| 199 | fn pi_1e7() { |
| 200 | assert_eq!(pi(10_000_000), 664_579); |
| 201 | } |
| 202 | |
| 203 | #[test] |
| 204 | fn large_primes_use_twelve_bases() { |
| 205 | // 3,215,031,751 itself is the classic strong pseudoprime to bases 2,3,5,7. |
| 206 | assert!(!is_prime(3_215_031_751)); |
| 207 | assert!(is_prime(18_446_744_073_709_551_557)); // largest prime below 2^64 |
| 208 | } |
| 209 | |
| 210 | #[test] |
| 211 | fn empty_and_tiny_limits_have_no_words() { |
| 212 | assert_eq!(canonical_bytes(0).len(), 0); |
| 213 | assert_eq!(canonical_bytes(1).len(), 8); |
| 214 | } |
| 215 | } |