|
| 1 | +//! Benchmarks for integer orient2d implementations. |
| 2 | +//! |
| 3 | +//! Measures per-call performance for: |
| 4 | +//! - Naive i32 (baseline, may overflow) |
| 5 | +//! - Hand-written i64 (exact, uses u128 intermediate) |
| 6 | +//! - apfp i32/i64 orient2d (exact, no overflow) |
| 7 | +//! - BigInt (exact but slow baseline) |
| 8 | +
|
| 9 | +use apfp::geometry::i32 as i32_types; |
| 10 | +use apfp::geometry::i64 as i64_types; |
| 11 | +use apfp::int_signum; |
| 12 | +use criterion::{BatchSize, Criterion, criterion_group, criterion_main}; |
| 13 | +use num_bigint::{BigInt, Sign}; |
| 14 | +use std::cmp::Ordering; |
| 15 | +use std::hint::black_box; |
| 16 | + |
| 17 | +const LCG_A: u64 = 6364136223846793005; |
| 18 | +const LCG_C: u64 = 1; |
| 19 | + |
| 20 | +/// Simple i32 orient2d - may overflow for large inputs. |
| 21 | +fn orient2d_naive_i32(ax: i32, ay: i32, bx: i32, by: i32, cx: i32, cy: i32) -> i32 { |
| 22 | + let adx = ax as i64 - cx as i64; |
| 23 | + let ady = ay as i64 - cy as i64; |
| 24 | + let bdx = bx as i64 - cx as i64; |
| 25 | + let bdy = by as i64 - cy as i64; |
| 26 | + |
| 27 | + let det = adx * bdy - ady * bdx; |
| 28 | + if det > 0 { |
| 29 | + 1 |
| 30 | + } else if det < 0 { |
| 31 | + -1 |
| 32 | + } else { |
| 33 | + 0 |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/// Hand-written exact orient2d for i64 using u128 intermediate. |
| 38 | +/// Works by computing absolute differences with sign tracking, then comparing |
| 39 | +/// products in u128 space. Never overflows for any i64 inputs. |
| 40 | +fn orient2d_handwritten_i64(ax: i64, ay: i64, bx: i64, by: i64, cx: i64, cy: i64) -> Ordering { |
| 41 | + // Return the absolute difference along with its sign. |
| 42 | + // diff(0, 10) => (10, true) // positive: b > a |
| 43 | + // diff(10, 0) => (10, false) // negative: a > b |
| 44 | + #[inline(always)] |
| 45 | + fn diff(a: i64, b: i64) -> (u128, bool) { |
| 46 | + if b > a { |
| 47 | + ((b.wrapping_sub(a)) as u64 as u128, true) |
| 48 | + } else { |
| 49 | + ((a.wrapping_sub(b)) as u64 as u128, false) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // Compute (b - a) relative to c: ux = bx - cx, uy = by - cy |
| 54 | + // and (c - a) relative to c: vx = cx - cx = 0... wait, that's not right. |
| 55 | + // Actually for orient2d we want: |
| 56 | + // det = (ax - cx) * (by - cy) - (ay - cy) * (bx - cx) |
| 57 | + // Let's rename: ux = ax - cx, vy = by - cy, uy = ay - cy, vx = bx - cx |
| 58 | + let (ux, ux_neg) = diff(cx, ax); // ax - cx: positive if ax > cx |
| 59 | + let (vy, vy_neg) = diff(cy, by); // by - cy: positive if by > cy |
| 60 | + let ux_vy_neg = ux_neg != vy_neg && ux != 0 && vy != 0; |
| 61 | + |
| 62 | + let (uy, uy_neg) = diff(cy, ay); // ay - cy: positive if ay > cy |
| 63 | + let (vx, vx_neg) = diff(cx, bx); // bx - cx: positive if bx > cx |
| 64 | + let uy_vx_neg = uy_neg != vx_neg && uy != 0 && vx != 0; |
| 65 | + |
| 66 | + // det = ux * vy - uy * vx |
| 67 | + // We need to compare ux * vy against uy * vx, accounting for signs |
| 68 | + match (ux_vy_neg, uy_vx_neg) { |
| 69 | + // ux*vy negative, uy*vx non-negative => det < 0 |
| 70 | + (true, false) => Ordering::Less, |
| 71 | + // ux*vy non-negative, uy*vx negative => det > 0 |
| 72 | + (false, true) => Ordering::Greater, |
| 73 | + // Both negative: det = -|ux*vy| - (-|uy*vx|) = |uy*vx| - |ux*vy| |
| 74 | + (true, true) => (uy * vx).cmp(&(ux * vy)), |
| 75 | + // Both non-negative: det = |ux*vy| - |uy*vx| |
| 76 | + (false, false) => (ux * vy).cmp(&(uy * vx)), |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// Exact orient2d using BigInt (slow). |
| 81 | +fn orient2d_bigint_i32(ax: i32, ay: i32, bx: i32, by: i32, cx: i32, cy: i32) -> i32 { |
| 82 | + let ax = BigInt::from(ax); |
| 83 | + let ay = BigInt::from(ay); |
| 84 | + let bx = BigInt::from(bx); |
| 85 | + let by = BigInt::from(by); |
| 86 | + let cx = BigInt::from(cx); |
| 87 | + let cy = BigInt::from(cy); |
| 88 | + |
| 89 | + let adx = &ax - &cx; |
| 90 | + let ady = &ay - &cy; |
| 91 | + let bdx = &bx - &cx; |
| 92 | + let bdy = &by - &cy; |
| 93 | + |
| 94 | + let det = &adx * &bdy - &ady * &bdx; |
| 95 | + match det.sign() { |
| 96 | + Sign::Plus => 1, |
| 97 | + Sign::Minus => -1, |
| 98 | + Sign::NoSign => 0, |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/// Exact orient2d using BigInt for i64 inputs. |
| 103 | +fn orient2d_bigint_i64(ax: i64, ay: i64, bx: i64, by: i64, cx: i64, cy: i64) -> i32 { |
| 104 | + let ax = BigInt::from(ax); |
| 105 | + let ay = BigInt::from(ay); |
| 106 | + let bx = BigInt::from(bx); |
| 107 | + let by = BigInt::from(by); |
| 108 | + let cx = BigInt::from(cx); |
| 109 | + let cy = BigInt::from(cy); |
| 110 | + |
| 111 | + let adx = &ax - &cx; |
| 112 | + let ady = &ay - &cy; |
| 113 | + let bdx = &bx - &cx; |
| 114 | + let bdy = &by - &cy; |
| 115 | + |
| 116 | + let det = &adx * &bdy - &ady * &bdx; |
| 117 | + match det.sign() { |
| 118 | + Sign::Plus => 1, |
| 119 | + Sign::Minus => -1, |
| 120 | + Sign::NoSign => 0, |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +fn bench_orient2d_int(c: &mut Criterion) { |
| 125 | + let mut group = c.benchmark_group("orient2d_int"); |
| 126 | + let batch_size = BatchSize::SmallInput; |
| 127 | + |
| 128 | + // Setup function for random i32 coordinates |
| 129 | + let make_i32_setup = || { |
| 130 | + let mut state = 0x1234_5678_9abc_def0u64; |
| 131 | + move || { |
| 132 | + let ax = lcg_i32(&mut state); |
| 133 | + let ay = lcg_i32(&mut state); |
| 134 | + let bx = lcg_i32(&mut state); |
| 135 | + let by = lcg_i32(&mut state); |
| 136 | + let cx = lcg_i32(&mut state); |
| 137 | + let cy = lcg_i32(&mut state); |
| 138 | + (ax, ay, bx, by, cx, cy) |
| 139 | + } |
| 140 | + }; |
| 141 | + |
| 142 | + // Setup function for random i64 coordinates (limited to avoid BigInt overhead) |
| 143 | + let make_i64_setup = || { |
| 144 | + let mut state = 0x1234_5678_9abc_def0u64; |
| 145 | + move || { |
| 146 | + let ax = lcg_i64(&mut state, 1_000_000_000); |
| 147 | + let ay = lcg_i64(&mut state, 1_000_000_000); |
| 148 | + let bx = lcg_i64(&mut state, 1_000_000_000); |
| 149 | + let by = lcg_i64(&mut state, 1_000_000_000); |
| 150 | + let cx = lcg_i64(&mut state, 1_000_000_000); |
| 151 | + let cy = lcg_i64(&mut state, 1_000_000_000); |
| 152 | + (ax, ay, bx, by, cx, cy) |
| 153 | + } |
| 154 | + }; |
| 155 | + |
| 156 | + // Naive i32 (uses i64 internally, may overflow for extreme inputs) |
| 157 | + group.bench_function("naive_i32", |bencher| { |
| 158 | + bencher.iter_batched( |
| 159 | + make_i32_setup(), |
| 160 | + |(ax, ay, bx, by, cx, cy)| black_box(orient2d_naive_i32(ax, ay, bx, by, cx, cy)), |
| 161 | + batch_size, |
| 162 | + ) |
| 163 | + }); |
| 164 | + |
| 165 | + // apfp i32 (uses int_signum! macro) |
| 166 | + group.bench_function("apfp_i32", |bencher| { |
| 167 | + bencher.iter_batched( |
| 168 | + || { |
| 169 | + let mut state = 0x1234_5678_9abc_def0u64; |
| 170 | + let ax = lcg_i32(&mut state); |
| 171 | + let ay = lcg_i32(&mut state); |
| 172 | + let bx = lcg_i32(&mut state); |
| 173 | + let by = lcg_i32(&mut state); |
| 174 | + let cx = lcg_i32(&mut state); |
| 175 | + let cy = lcg_i32(&mut state); |
| 176 | + (ax, ay, bx, by, cx, cy) |
| 177 | + }, |
| 178 | + |(ax, ay, bx, by, cx, cy)| { |
| 179 | + black_box(int_signum!((ax - cx) * (by - cy) - (ay - cy) * (bx - cx))) |
| 180 | + }, |
| 181 | + batch_size, |
| 182 | + ) |
| 183 | + }); |
| 184 | + |
| 185 | + // apfp i32 with Coord (struct-based API) |
| 186 | + group.bench_function("apfp_i32_coord", |bencher| { |
| 187 | + bencher.iter_batched( |
| 188 | + || { |
| 189 | + let mut state = 0x1234_5678_9abc_def0u64; |
| 190 | + let a = i32_types::Coord::new(lcg_i32(&mut state), lcg_i32(&mut state)); |
| 191 | + let b = i32_types::Coord::new(lcg_i32(&mut state), lcg_i32(&mut state)); |
| 192 | + let c = i32_types::Coord::new(lcg_i32(&mut state), lcg_i32(&mut state)); |
| 193 | + (a, b, c) |
| 194 | + }, |
| 195 | + |(a, b, c)| black_box(i32_types::orient2d(&a, &b, &c)), |
| 196 | + batch_size, |
| 197 | + ) |
| 198 | + }); |
| 199 | + |
| 200 | + // BigInt i32 (exact but slow) |
| 201 | + group.bench_function("bigint_i32", |bencher| { |
| 202 | + bencher.iter_batched( |
| 203 | + make_i32_setup(), |
| 204 | + |(ax, ay, bx, by, cx, cy)| black_box(orient2d_bigint_i32(ax, ay, bx, by, cx, cy)), |
| 205 | + batch_size, |
| 206 | + ) |
| 207 | + }); |
| 208 | + |
| 209 | + // Hand-written i64 (exact, uses u128 intermediate) |
| 210 | + group.bench_function("handwritten_i64", |bencher| { |
| 211 | + bencher.iter_batched( |
| 212 | + make_i64_setup(), |
| 213 | + |(ax, ay, bx, by, cx, cy)| black_box(orient2d_handwritten_i64(ax, ay, bx, by, cx, cy)), |
| 214 | + batch_size, |
| 215 | + ) |
| 216 | + }); |
| 217 | + |
| 218 | + // apfp i64 (uses int_signum! macro with BigInt<4> internally) |
| 219 | + group.bench_function("apfp_i64", |bencher| { |
| 220 | + bencher.iter_batched( |
| 221 | + || { |
| 222 | + let mut state = 0x1234_5678_9abc_def0u64; |
| 223 | + let ax = lcg_i64(&mut state, 1_000_000_000); |
| 224 | + let ay = lcg_i64(&mut state, 1_000_000_000); |
| 225 | + let bx = lcg_i64(&mut state, 1_000_000_000); |
| 226 | + let by = lcg_i64(&mut state, 1_000_000_000); |
| 227 | + let cx = lcg_i64(&mut state, 1_000_000_000); |
| 228 | + let cy = lcg_i64(&mut state, 1_000_000_000); |
| 229 | + (ax, ay, bx, by, cx, cy) |
| 230 | + }, |
| 231 | + |(ax, ay, bx, by, cx, cy)| { |
| 232 | + black_box(int_signum!((ax - cx) * (by - cy) - (ay - cy) * (bx - cx))) |
| 233 | + }, |
| 234 | + batch_size, |
| 235 | + ) |
| 236 | + }); |
| 237 | + |
| 238 | + // apfp i64 with Coord |
| 239 | + group.bench_function("apfp_i64_coord", |bencher| { |
| 240 | + bencher.iter_batched( |
| 241 | + || { |
| 242 | + let mut state = 0x1234_5678_9abc_def0u64; |
| 243 | + let a = i64_types::Coord::new( |
| 244 | + lcg_i64(&mut state, 1_000_000_000), |
| 245 | + lcg_i64(&mut state, 1_000_000_000), |
| 246 | + ); |
| 247 | + let b = i64_types::Coord::new( |
| 248 | + lcg_i64(&mut state, 1_000_000_000), |
| 249 | + lcg_i64(&mut state, 1_000_000_000), |
| 250 | + ); |
| 251 | + let c = i64_types::Coord::new( |
| 252 | + lcg_i64(&mut state, 1_000_000_000), |
| 253 | + lcg_i64(&mut state, 1_000_000_000), |
| 254 | + ); |
| 255 | + (a, b, c) |
| 256 | + }, |
| 257 | + |(a, b, c)| black_box(i64_types::orient2d(&a, &b, &c)), |
| 258 | + batch_size, |
| 259 | + ) |
| 260 | + }); |
| 261 | + |
| 262 | + // BigInt i64 |
| 263 | + group.bench_function("bigint_i64", |bencher| { |
| 264 | + bencher.iter_batched( |
| 265 | + make_i64_setup(), |
| 266 | + |(ax, ay, bx, by, cx, cy)| black_box(orient2d_bigint_i64(ax, ay, bx, by, cx, cy)), |
| 267 | + batch_size, |
| 268 | + ) |
| 269 | + }); |
| 270 | + |
| 271 | + group.finish(); |
| 272 | +} |
| 273 | + |
| 274 | +criterion_group!(benches, bench_orient2d_int); |
| 275 | +criterion_main!(benches); |
| 276 | + |
| 277 | +fn lcg_i32(state: &mut u64) -> i32 { |
| 278 | + *state = state.wrapping_mul(LCG_A).wrapping_add(LCG_C); |
| 279 | + ((*state >> 33) as i32).wrapping_sub(i32::MAX / 2) |
| 280 | +} |
| 281 | + |
| 282 | +fn lcg_i64(state: &mut u64, limit: i64) -> i64 { |
| 283 | + *state = state.wrapping_mul(LCG_A).wrapping_add(LCG_C); |
| 284 | + let val = ((*state >> 1) as i64).wrapping_rem(limit * 2); |
| 285 | + val - limit |
| 286 | +} |
0 commit comments