gcc: Function Multiversioning
1
1 7.8 Function Multiversioning
1 ============================
1
1 With the GNU C++ front end, for x86 targets, you may specify multiple
1 versions of a function, where each function is specialized for a
1 specific target feature. At runtime, the appropriate version of the
1 function is automatically executed depending on the characteristics of
1 the execution platform. Here is an example.
1
1 __attribute__ ((target ("default")))
1 int foo ()
1 {
1 // The default version of foo.
1 return 0;
1 }
1
1 __attribute__ ((target ("sse4.2")))
1 int foo ()
1 {
1 // foo version for SSE4.2
1 return 1;
1 }
1
1 __attribute__ ((target ("arch=atom")))
1 int foo ()
1 {
1 // foo version for the Intel ATOM processor
1 return 2;
1 }
1
1 __attribute__ ((target ("arch=amdfam10")))
1 int foo ()
1 {
1 // foo version for the AMD Family 0x10 processors.
1 return 3;
1 }
1
1 int main ()
1 {
1 int (*p)() = &foo;
1 assert ((*p) () == foo ());
1 return 0;
1 }
1
1 In the above example, four versions of function foo are created. The
1 first version of foo with the target attribute "default" is the default
1 version. This version gets executed when no other target specific
1 version qualifies for execution on a particular platform. A new version
1 of foo is created by using the same function signature but with a
1 different target string. Function foo is called or a pointer to it is
1 taken just like a regular function. GCC takes care of doing the
1 dispatching to call the right version at runtime. Refer to the GCC wiki
1 on Function Multiversioning
1 (http://gcc.gnu.org/wiki/FunctionMultiVersioning) for more details.
1