Build two binaries from one addition contract
Add two float arrays into a separately allocated output. The length is 1,048,579, deliberately not divisible by four. Build one version permitting GCC vectorization and another disabling it, then verify every result for equal inputs. Read the SIMD guide first. The goal is to connect compiler diagnostics, actual instructions and tail outputs, rather than assume a speed ratio from lane width or infer packed arithmetic merely from a register name.
Input a[i] is i modulo 32 converted to float, and b[i] is two. These small values allow exact comparison with the required elementwise result. Every output is checked and contributes to a consumed checksum. Three separate allocations satisfy the non-overlap contract associated with restrict. This is not an experiment in passing overlapping ranges. Allocation failure aborts, and successful output coverage is established before interpreting performance. Correctness includes the last elements beyond the main packed loop.
Keep build settings and outputs separate
Save simd.c. On x86-64 GCC, build with gcc -O3 -std=c11 -march=x86-64 -fopt-info-vec-optimized simd.c -o simd-vector, then gcc -O3 -std=c11 -march=x86-64 -fno-tree-vectorize simd.c -o simd-scalar. Save the diagnostics printed on standard error. Run both binaries and retain their nine-row CSV outputs separately. The recorded compiler is GCC 13.3; other versions can report or generate different transformations.
The selected architecture flag defines the inspected ISA. Do not silently choose instructions unavailable on the machine receiving the binary. An arm64 reproduction needs appropriate compiler settings and instruction interpretation. Preserve each binary with its flags, diagnostics, disassembly and observations. Scalar here means disabling vectorization of the addition loop, not proving that no SIMD register occurs anywhere in the executable. Scalar floating-point instructions can use the same register file, and unrelated initialization or library code can have different transformations.
Trace the main loop and its remainder
Save disassembly for both binaries and open the called add function. Identify input loads, addition, output store and address increment. In the recorded vector build, addps performs packed addition and the main pointer advances sixteen bytes, corresponding to four float elements. Remainder handling includes a smaller group and scalar addss. Translate that progression back to source indices and explain exactly which outputs have already been produced.
In the disabled-vectorization build, locate elementwise addition and verify coverage of the same n. A diagnostic saying vectorized does not alone prove that the timed call used that function; follow main's call target. A length divisible by four may avoid the tail, so keep the original non-divisible case in validation. Interpret main loop, remainder, call and output together rather than relying on the presence of one mnemonic. Both language editions publish identical executable code so their observations refer to one implementation.
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static volatile uint64_t sink;
static uint64_t ns(void) { struct timespec t; assert(!clock_gettime(CLOCK_MONOTONIC,&t)); return (uint64_t)t.tv_sec*1000000000u+t.tv_nsec; }
__attribute__((noinline)) void add(float * restrict out,const float * restrict a,const float * restrict b,size_t n) {
for(size_t i=0;i<n;i++) out[i]=a[i]+b[i];
}
int main(void) {
size_t n=1048579;float *a=malloc(n*4),*b=malloc(n*4),*out=malloc(n*4);assert(a&&b&&out);
for(size_t i=0;i<n;i++){a[i]=(float)(i%32);b[i]=2;}
for(int r=0;r<9;r++){uint64_t start=ns();add(out,a,b,n);uint64_t elapsed=ns()-start;uint64_t sum=0;
for(size_t i=0;i<n;i++){assert(out[i]==a[i]+2);sum+=(uint64_t)out[i];}sink=sum;printf("%d,%llu,%llu\n",r,(unsigned long long)elapsed,(unsigned long long)sum);
}
free(a);free(b);free(out);return 0;
}
Time addition separately from validation
The timed interval covers one add call. Initialization, assertions over every output, checksum formation and printing remain outside it. Preserve observations zero through eight and calculate medians and quantiles for each build. The first call may include first writes to output pages, among other effects. Retain its first-call label rather than assigning every difference to an empty cache. Any summary excluding it should state the exclusion rule while keeping the original sample.
Equal length permits comparing total durations, but the program does not assert a fourfold speedup. Data movement, fixed call costs and scheduling can limit the benefit. The virtualized run establishes executable instructions, correct results and durations for the specified interval. It does not calibrate exclusive bare-metal peak throughput or instruction latency, and acquires no PMU events. A changed timing ranking on another system does not invalidate the separate objective of connecting generated instructions with complete correct output.
Preserve dependency and numerical contracts
Replacing independent output addition with a single sum introduces a reduction dependency and potentially a different rounding order. Adding fast-math then changes what transformations are permitted and requires explicit error and special-value requirements. Do not transfer the observed elementwise instructions into a guarantee about every reduction. Overlapping b with out also changes the restrict contract and cannot be added as an ordinary safe timing case without redesign.
Application decisions include copying or conversion needed to produce these contiguous arrays. A faster inner loop can lose overall when setup grows. Very small inputs change the relative importance of calls and remainder handling. Keep required sizes separate instead of averaging unrelated workloads into one ranking. Align assumptions, required output, inspected instructions and measured interval in the same record so another reader can assess how the conclusion follows from the actual implementation.
Keep API length in elements rather than bytes
The add parameter n counts elements. Passing allocated byte size instead would overrun the arrays, not create a valid larger workload. A portability extension can check sizeof(float) before relying on this example's four-byte assumption. Convert vector bytes to lane counts consistently for input, output and tail. This small contract check matters even when disassembly clearly contains packed arithmetic: correct instructions with an invalid range are not a successful SIMD implementation or meaningful performance experiment.
Solve the shorter-tail exercise
For n=7 and a four-element main group, indices zero through three form one full group and indices four through six remain. The compiler might use two-plus-one elements or three scalar operations; either must validate every required output safely. Length eight gives two full groups, but testing only it does not verify remainder correctness. Useful additional cases include empty, one element, shorter than a vector, an exact multiple and a multiple plus a remainder.
For the second exercise, change the update to a[i]=a[i-1]+1. It now requires a preceding iteration's result, so the original independent-addition vectorization argument no longer applies. A different algorithm needs renewed semantic and result checks. At completion organize both binaries, diagnostics, disassembly and CSV. Allocations are freed and no external service is involved. The final decision should return from instruction names to the input and output conditions those instructions must satisfy.