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
//! SIMD-accelerated integer dot product operations
//!
//! Provides high-throughput i8 x i8 → i32 dot products for quantized inference.
//!
//! # Architecture Support
//!
//! | Architecture | Instruction Set | Elements/cycle | Key Intrinsic |
//! |--------------|------------------|----------------|------------------------|
//! | x86-64 | AVX-512BW | 64 | maddubs + madd |
//! | x86-64 | AVX2 | 32 | maddubs + madd |
//! | ARM64 | NEON | 16 | vmull_s8 + vpadalq_s16 |
use ;
/// Minimum elements to justify SIMD overhead for dot products
const DOT_SIMD_THRESHOLD: usize = 32;
/// SIMD iterations between spills of the i32 lane accumulator into an i64 total.
///
/// Every backend here accumulates products in i32 SIMD lanes, and every one of
/// them adds at most `2^16` to a single lane per iteration: a product of two i8
/// is bounded by `128 * 128 = 2^14`, and each lane receives at most four of them
/// per iteration. `2^14 * 2^16 = 2^30` stays inside i32, so spilling this often
/// is provably safe with a full bit of headroom.
///
/// Without a spill the lanes wrap after roughly a million elements and the dot
/// product returns a value with the wrong sign — silently, in release.
pub const DOT_SPILL_ITERS: usize = 16_384;
/// Narrow an exact i64 total to the i32 this op returns, clamping on overflow.
///
/// Saturating rather than wrapping, matching
/// [`crate::runtime::cpu::kernels::wide_acc`]: a wrapped total reports the
/// wrong sign and magnitude, while a clamped one is at least ordered correctly
/// and stays a total function.
pub
/// Dot product of signed i8 vectors, accumulated in i32.
///
/// Automatically dispatches to the best SIMD implementation available:
/// - x86-64/AVX-512BW: 64 elements per iteration via `_mm512_maddubs_epi16` + `_mm512_madd_epi16`
/// - x86-64/AVX2: 32 elements per iteration via `_mm256_maddubs_epi16` + `_mm256_madd_epi16`
/// - ARM64/NEON: 16 elements per iteration via `vmull_s8` + `vpadalq_s16`
/// - Scalar fallback for small arrays (<32 elements) or unsupported platforms
///
/// Computes sum(a[i] * b[i]) for i in 0..len.
///
/// # Safety
/// - `a` and `b` must be valid pointers to `len` elements
pub unsafe
/// Scaled dot product of signed i8 vectors, returning f32.
///
/// Computes scale * sum(a[i] * b[i]) for i in 0..len.
///
/// # Safety
/// - `a` and `b` must be valid pointers to `len` elements
// Public API for downstream crates (e.g., boostr quantized ops)
pub unsafe
/// Scalar fallback for i8 dot product.
///
/// Accumulates in i64 and clamps once at the end. An i32 accumulator wraps
/// after about 131k terms (`i32::MAX / 128^2`), which is well inside the sizes
/// a quantized matmul reaches along K. The i64 accumulator cannot overflow for
/// any reachable length: it would take `2^63 / 2^14` terms.
unsafe