__BUILTIN_PREFETCH(3)
okay bestie, welcome to __BUILTIN_PREFETCH(3) from the Library Functions Manual β this is the group-chat translation, and every fact is staying put π
π NAME
__builtin_prefetch β GNU extension to prefetch memory β¨
π SYNOPSIS
void
__builtin_prefetch(const void *addr, ...);
π¦ DESCRIPTION
okay so __builtin_prefetch() is the function that prefetches memory from addr. The rationale? minimize cache-miss latency by trying to move data into a cache before accessing the data. Possible use cases include frequently called sections of code in which it is known that the data in a given address is likely to be accessed soon. It's giving βget ahead of the cache missβ energy, fr fr.
Besides addr, there are two optional stdarg(3) arguments, rw and locality.
localityβ the value of this latter arg should be a compile-time constant integer between0and3. The higher the value, the higher the temporal locality in the data. Whenlocalityis0, it is assumed that there is little or no temporal locality in the data; after access, it is not necessary to leave the data in the cache. The default value is3.rwβ the value is either0or1, corresponding with read and write prefetch, respectively. The default value ofrwis0. Alsorwmust be a compile-time constant integer.
The __builtin_prefetch() function translates into prefetch instructions only if the architecture has support for these. If there is no support, addr is evaluated only if it includes side effects, although no warnings are issued by gcc(1). Slay, but also noted.
β¨ EXAMPLES
The following optimization appears in the heavily used cpu_in_cksum() function that calculates checksums for the inet(4) headers:
while (mlen >= 32) {
__builtin_prefetch(data + 32);
partial += *(uint16_t *)data;
partial += *(uint16_t *)(data + 2);
partial += *(uint16_t *)(data + 4);
...
partial += *(uint16_t *)(data + 28);
partial += *(uint16_t *)(data + 30);
data += 32;
mlen -= 32;
...
π SEE ALSO
gcc(1)attribute(3)- Ulrich Drepper, What Every Programmer Should Know About Memory,
https://www.akkadia.org/drepper/cpumemory.pdf, November 21, 2007.
π¨ CAVEATS
bestie, listen: this is a non-standard, compiler-specific extension. That's the caveat. Periodt. π
okay bye bestie π«Ά source/version: Debian; date: December 22, 2010; manual page: __BUILTIN_PREFETCH(3). π
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 2010 Jukka Ruohonen <jruohonen@iki.fi>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms