xref: /netbsd/share/man/man3/__builtin_prefetch.3 (revision 6550d01e)
1.\" $NetBSD: __builtin_prefetch.3,v 1.3 2010/12/22 22:02:54 wiz Exp $
2.\"
3.\" Copyright (c) 2010 Jukka Ruohonen <jruohonen@iki.fi>
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25.\" POSSIBILITY OF SUCH DAMAGE.
26.\"
27.Dd December 22, 2010
28.Dt __BUILTIN_PREFETCH 3
29.Os
30.Sh NAME
31.Nm __builtin_prefetch
32.Nd GNU extension to prefetch memory
33.Sh SYNOPSIS
34.Ft void
35.Fn __builtin_prefetch "const void *addr" "..."
36.Sh DESCRIPTION
37The
38.Fn __builtin_prefetch
39function prefetches memory from
40.Fa addr .
41The rationale is to minimize cache-miss latency by
42trying to move data into a cache before accessing the data.
43Possible use cases include frequently called sections of code
44in which it is known that the data in a given address is likely
45to be accessed soon.
46.Pp
47In addition to
48.Fa addr ,
49there are two optional
50.Xr stdarg 3
51arguments,
52.Fa rw
53and
54.Fa locality .
55The value of the latter should be a compile-time
56constant integer between 0 and 3.
57The higher the value, the higher the temporal locality in the data.
58When
59.Fa locality
60is 0, it is assumed that there is little or no temporal locality in the data;
61after access, it is not necessary to leave the data in the cache.
62The default value is 3.
63The value of
64.Fa rw
65is either 0 or 1, corresponding with read and write prefetch, respectively.
66The default value of
67.Fa rw
68is 0.
69Also
70.Fa rw
71must be a compile-time constant integer.
72.Pp
73The
74.Fn __builtin_prefetch
75function translates into prefetch instructions
76only if the architecture has support for these.
77If there is no support,
78.Fa addr
79is evaluated only if it includes side effects,
80although no warnings are issued by
81.Xr gcc 1 .
82.Sh EXAMPLES
83The following optimization appears in the heavily used
84.Fn cpu_in_cksum
85function that calculates checksums for the
86.Xr inet 4
87headers:
88.Bd -literal -offset indent
89while (mlen >= 32) {
90	__builtin_prefetch(data + 32);
91	partial += *(uint16_t *)data;
92	partial += *(uint16_t *)(data + 2);
93	partial += *(uint16_t *)(data + 4);
94
95	\&...
96
97	partial += *(uint16_t *)(data + 28);
98	partial += *(uint16_t *)(data + 30);
99
100	data += 32;
101	mlen -= 32;
102
103	\&...
104.Ed
105.Sh SEE ALSO
106.Xr gcc 1 ,
107.Xr attribute 3
108.Rs
109.%A Ulrich Drepper
110.%T What Every Programmer Should Know About Memory
111.%D November 21, 2007
112.%U http://www.akkadia.org/drepper/cpumemory.pdf
113.Re
114.Sh CAVEATS
115This is a non-standard, compiler-specific extension.
116