Experiments and implementations · Intermediate

Compare starting alignment with valid C accesses

Traverse equal valid typed ranges from two starting offsets, separating type alignment, line span and individual memory accesses.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Distinguish type alignment from line alignment
  2. Allocate enough valid storage for both views
  3. Trace offsets and complete ranges
  4. Retain small or absent timing differences
  5. Do not confuse this with false sharing
  6. A valid typed pointer may not meet a stronger instruction contract
  7. Solve the invalid-offset exercise
  8. Ask Logeia about this articleCheck your understanding

Distinguish type alignment from line alignment

Compare a uint32_t traversal beginning at a 64-byte-aligned allocation with one beginning sixteen bytes later. Both pointers satisfy the C type's alignment requirement and both traverse the same number of values. Read the cache-mapping guide first. The objective is a valid comparison of starting positions, not manufacturing a speed difference through undefined behavior or out-of-bounds access.

A pointer shifted by one byte may violate type alignment even if the processor can physically execute an unaligned load. This experiment advances four uint32_t elements instead. It checks address modulo _Alignof(uint32_t) and separately prints address modulo 64. A zero versus sixteen remainder at the line boundary is not the same question as a legal typed address. Keep these facts distinct before interpreting any timing. Hardware tolerance does not repair a violated language contract.

Allocate enough valid storage for both views

Save align.c, compile with gcc -O2 -std=c11 -fno-ipa-pure-const align.c -o align, and run ./align on a POSIX system. posix_memalign requests a 64-byte-aligned base and its return value is checked. Allocate sixteen extra elements beyond n=1,048,576, leaving the shifted n-element view fully inside storage. Free the original base, never the interior pointer.

Every element is initialized to one, so both views sum to n. They do not access identical addresses; changing that starting address is the stated experimental variable. Do not hide the different ranges behind equal values. Allocation and initialization remain outside timing, which covers one walk. CSV saves byte offset, address modulo 64, repetition and nanoseconds. Full pointer values are unnecessary for this comparison; the recorded remainders establish the intended relative placement while the allocation contract establishes valid access.

Trace offsets and complete ranges

The source offset is zero or four elements; multiplying by four prints byte offsets zero or sixteen. Each view reads n four-byte values. Under a 64-byte-line model, the aligned range spans 65,536 lines and the shifted range spans 65,537. This counts touched line ranges, not measured misses. Nor does an individual aligned four-byte scalar access cross a 64-byte boundary here. Distinguish a complete range spanning an extra line from one load crossing a line.

Alternate which position runs first and obtain nine observations of each. The views overlap in one allocation, so one traversal can warm data used by the other. This is not a comparison beginning from independently empty caches. Preserve result assertions and consumption, and inspect actual loads in disassembly. If compilation groups elements, record that implementation too. A source element type does not prove that every generated memory operation has exactly that element width.

c
#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)) static uint64_t walk(const uint32_t *a,size_t n) {uint64_t sum=0;for(size_t i=0;i<n;i++)sum+=a[i];return sum;}
int main(void) {
 size_t n=1048576,bytes=(n+16)*sizeof(uint32_t);void *base=NULL;assert(!posix_memalign(&base,64,bytes));uint32_t *a=base;
 for(size_t i=0;i<n+16;i++)a[i]=1;
 for(int r=0;r<9;r++)for(int k=0;k<2;k++){size_t offset=((r+k)%2)*4;uint32_t *p=a+offset;assert((uintptr_t)p%_Alignof(uint32_t)==0);
  uint64_t start=ns(),sum=walk(p,n),elapsed=ns()-start;assert(sum==n);sink=sum;printf("%zu,%zu,%d,%llu\n",offset*4,(size_t)((uintptr_t)p%64),r,(unsigned long long)elapsed);
 }
 free(base);return 0;
}

Retain small or absent timing differences

The structural expected result is address remainders zero and sixteen with every checksum assertion passing. Neither position is required to win. Load mechanisms, compiler decisions and retained data can make timing differences small or variable. Save all samples and summarize medians and quantiles, including differences smaller than the observed variation. An apparently absent effect proves neither that every misalignment is free nor that the program failed.

The recorded GCC 13.3 virtualized run verifies both valid ranges and their sums. PMU events are unmeasured, so starting-offset facts are not reported as cache-miss or split-load counts. A new type or vector width requires renewed checks of alignment, generated operations and allocation end. Explicit aligned-load instructions add their own requirements, which must hold for every call. Do not work backward from a hoped-for speed improvement and silently omit the conditions making an operation valid.

Do not confuse this with false sharing

One thread only reads these ranges. It does not create false sharing through writes by different cores to different variables in one line. The result cannot establish that line-aligned multithreaded updates always improve. A false-sharing experiment needs writers, threads, placement, shared lines and appropriate correctness checks. Follow the operations in the code rather than connecting experiments solely because both mention cache lines.

Type alignment, vector-instruction alignment, cache-line placement and page boundaries are separate conditions. A 64-byte-aligned base does not guarantee NUMA locality or every page's placement. Adopt required constraints and account for extra storage introduced by padding. With many arrays or infrequent traversal, increased allocation can matter more than a small loop timing difference. This is the boundary between a controlled starting-offset observation and a complete application layout decision.

A valid typed pointer may not meet a stronger instruction contract

The shifted pointer can satisfy uint32_t alignment while failing a stronger alignment requirement of a particular explicit vector instruction. Inspect generated operations and document any new requirement introduced by an intrinsic. Conversely, an unaligned-capable instruction does not authorize violating the source language's typed-access rules. Keep the language and ISA contracts side by side. The current experiment avoids claiming either property solely from the other, allowing the measured starting-position comparison to remain valid before making any instruction-specific extension.

Solve the invalid-offset exercise

Can the program instead cast base to char*, add one byte, cast back to uint32_t* and read? That is not a valid substitute: it can violate C alignment even if hardware accepts the access. A byte-oriented task needs a correctly specified mechanism such as suitable byte access or memcpy. Moving by four elements already changes line position while preserving the original valid typed-access objective.

For the second exercise, offset by sixteen elements. The byte displacement is 64 and the line remainder returns to zero. The n+16 allocation leaves exactly enough room for an n-element view, and the original base must still be freed. Equal line remainders do not guarantee identical elapsed durations at different addresses. At completion organize output and binaries. The program releases its allocations and leaves no background process or database.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 8

Prepare for AI systems · STEP 5

Choose a learning path