1 /* Test case to check if Multiversioning works. */ 2 /* { dg-do run { target i?86-*-* x86_64-*-* } } */ 3 /* { dg-require-ifunc "" } */ 4 /* { dg-options "-O2 -fPIC" } */ 5 6 #include <assert.h> 7 8 /* Default version. */ 9 int foo (); // Extra declaration that is merged with the second one. 10 int foo () __attribute__ ((target("default"))); 11 /* The other versions of foo. Mix up the ordering and 12 check if the dispatching does it in the order of priority. */ 13 /* Check combination of target attributes. */ 14 int foo () __attribute__ ((target("arch=corei7,popcnt"))); 15 /* The target operands in this declaration and the definition are re-ordered. 16 This should still work. */ 17 int foo () __attribute__ ((target("ssse3,avx2"))); 18 19 /* Check for all target attributes for which dispatchers are available. */ 20 /* Check arch= */ 21 int foo () __attribute__((target("arch=core2"))); 22 int foo () __attribute__((target("arch=corei7"))); 23 int foo () __attribute__((target("arch=atom"))); 24 /* Check ISAs */ 25 int foo () __attribute__((target("avx"))); 26 int foo () __attribute__ ((target("arch=core2,sse4.2"))); 27 /* Check more arch=. */ 28 int foo () __attribute__((target("arch=amdfam10"))); 29 int foo () __attribute__((target("arch=bdver1"))); 30 int foo () __attribute__((target("arch=bdver2"))); 31 32 int (*p)() = &foo; main()33int main () 34 { 35 int val = foo (); 36 assert (val == (*p)()); 37 38 /* Check in the exact same order in which the dispatching 39 is expected to happen. */ 40 if (__builtin_cpu_is ("bdver1")) 41 assert (val == 1); 42 else if (__builtin_cpu_is ("bdver2")) 43 assert (val == 2); 44 else if (__builtin_cpu_supports ("avx2") 45 && __builtin_cpu_supports ("ssse3")) 46 assert (val == 3); 47 else if (__builtin_cpu_supports ("avx")) 48 assert (val == 4); 49 else if (__builtin_cpu_is ("corei7") 50 && __builtin_cpu_supports ("popcnt")) 51 assert (val == 5); 52 else if (__builtin_cpu_is ("corei7")) 53 assert (val == 6); 54 else if (__builtin_cpu_is ("amdfam10h")) 55 assert (val == 7); 56 else if (__builtin_cpu_is ("core2") 57 && __builtin_cpu_supports ("sse4.2")) 58 assert (val == 8); 59 else if (__builtin_cpu_is ("core2")) 60 assert (val == 9); 61 else if (__builtin_cpu_is ("atom")) 62 assert (val == 10); 63 else 64 assert (val == 0); 65 66 return 0; 67 } 68 69 int __attribute__ ((target("default"))) foo()70foo () 71 { 72 return 0; 73 } 74 75 int __attribute__ ((target("arch=corei7,popcnt"))) foo()76foo () 77 { 78 return 5; 79 } 80 int __attribute__ ((target("avx2,ssse3"))) foo()81foo () 82 { 83 return 3; 84 } 85 86 int __attribute__ ((target("arch=core2"))) foo()87foo () 88 { 89 return 9; 90 } 91 92 int __attribute__ ((target("arch=corei7"))) foo()93foo () 94 { 95 return 6; 96 } 97 98 int __attribute__ ((target("arch=atom"))) foo()99foo () 100 { 101 return 10; 102 } 103 104 int __attribute__ ((target("avx"))) foo()105foo () 106 { 107 return 4; 108 } 109 110 int __attribute__ ((target("arch=core2,sse4.2"))) foo()111foo () 112 { 113 return 8; 114 } 115 116 int __attribute__ ((target("arch=amdfam10"))) foo()117foo () 118 { 119 return 7; 120 } 121 122 int __attribute__ ((target("arch=bdver1"))) foo()123foo () 124 { 125 return 1; 126 } 127 128 int __attribute__ ((target("arch=bdver2"))) foo()129foo () 130 { 131 return 2; 132 } 133