summaryrefslogtreecommitdiff
path: root/devdocs/gcc~13/function-multiversioning.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/gcc~13/function-multiversioning.html
new repository
Diffstat (limited to 'devdocs/gcc~13/function-multiversioning.html')
-rw-r--r--devdocs/gcc~13/function-multiversioning.html40
1 files changed, 40 insertions, 0 deletions
diff --git a/devdocs/gcc~13/function-multiversioning.html b/devdocs/gcc~13/function-multiversioning.html
new file mode 100644
index 00000000..0e221265
--- /dev/null
+++ b/devdocs/gcc~13/function-multiversioning.html
@@ -0,0 +1,40 @@
+<div class="section-level-extent" id="Function-Multiversioning"> <div class="nav-panel"> <p> Next: <a href="type-traits" accesskey="n" rel="next">Type Traits</a>, Previous: <a href="c_002b_002b-attributes" accesskey="p" rel="prev">C++-Specific Variable, Function, and Type Attributes</a>, Up: <a href="c_002b_002b-extensions" accesskey="u" rel="up">Extensions to the C++ Language</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div> <h1 class="section" id="Function-Multiversioning-1"><span>7.8 Function Multiversioning<a class="copiable-link" href="#Function-Multiversioning-1"> ΒΆ</a></span></h1> <p>With the GNU C++ front end, for x86 targets, you may specify multiple versions of a function, where each function is specialized for a specific target feature. At runtime, the appropriate version of the function is automatically executed depending on the characteristics of the execution platform. Here is an example. </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">__attribute__ ((target ("default")))
+int foo ()
+{
+ // The default version of foo.
+ return 0;
+}
+
+__attribute__ ((target ("sse4.2")))
+int foo ()
+{
+ // foo version for SSE4.2
+ return 1;
+}
+
+__attribute__ ((target ("arch=atom")))
+int foo ()
+{
+ // foo version for the Intel ATOM processor
+ return 2;
+}
+
+__attribute__ ((target ("arch=amdfam10")))
+int foo ()
+{
+ // foo version for the AMD Family 0x10 processors.
+ return 3;
+}
+
+int main ()
+{
+ int (*p)() = &amp;foo;
+ assert ((*p) () == foo ());
+ return 0;
+}</pre>
+</div> <p>In the above example, four versions of function foo are created. The first version of foo with the target attribute "default" is the default version. This version gets executed when no other target specific version qualifies for execution on a particular platform. A new version of foo is created by using the same function signature but with a different target string. Function foo is called or a pointer to it is taken just like a regular function. GCC takes care of doing the dispatching to call the right version at runtime. Refer to the <a class="uref" href="https://gcc.gnu.org/wiki/FunctionMultiVersioning">GCC wiki on Function Multiversioning</a> for more details. </p> </div><div class="_attribution">
+ <p class="_attribution-p">
+ &copy; Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
+ <a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Function-Multiversioning.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Function-Multiversioning.html</a>
+ </p>
+</div>