Mettle

byte-sum-narrow-accByte sum into an accumulator narrower than int64

Reported by --explain after a verdict.

mettle explain byte-sum-narrow-acc

Summing bytes has a very fast kernel (vpsadbw, 32 bytes per step), but it accumulates into int64: 32 lanes of a byte each can carry further than 32 bits, and the kernel will not silently give you a different answer from the scalar loop.

Example

var total: int64 = 0;
for i in 0..n {
    total = total + (int64)data[i];
}

Fix: declare the accumulator int64. The report simulates that change, re-runs the optimizer, and only prints the advice once the loop really does vectorize with it.

Related