1/* IFUNC resolver with CPU_FEATURE_ACTIVE.
2 Copyright (C) 2021-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <sys/platform/x86.h>
20
21enum isa
22{
23 none,
24 sse2,
25 sse4_2,
26 avx,
27 avx2,
28 avx512f
29};
30
31enum isa
32__attribute__ ((__optimize__ ("-fno-stack-protector")))
33get_isa (void)
34{
35 if (CPU_FEATURE_ACTIVE (AVX512F))
36 return avx512f;
37 if (CPU_FEATURE_ACTIVE (AVX2))
38 return avx2;
39 if (CPU_FEATURE_ACTIVE (AVX))
40 return avx;
41 if (CPU_FEATURE_ACTIVE (SSE4_2))
42 return sse4_2;
43 if (CPU_FEATURE_ACTIVE (SSE2))
44 return sse2;
45 return none;
46}
47
48static int
49isa_sse2 (void)
50{
51 return sse2;
52}
53
54static int
55isa_sse4_2 (void)
56{
57 return sse4_2;
58}
59
60static int
61isa_avx (void)
62{
63 return avx;
64}
65
66static int
67isa_avx2 (void)
68{
69 return avx2;
70}
71
72static int
73isa_avx512f (void)
74{
75 return avx512f;
76}
77
78static int
79isa_none (void)
80{
81 return none;
82}
83
84int foo (void) __attribute__ ((ifunc ("foo_ifunc")));
85
86void *
87__attribute__ ((__optimize__ ("-fno-stack-protector")))
88foo_ifunc (void)
89{
90 switch (get_isa ())
91 {
92 case avx512f:
93 return isa_avx512f;
94 case avx2:
95 return isa_avx2;
96 case avx:
97 return isa_avx;
98 case sse4_2:
99 return isa_sse4_2;
100 case sse2:
101 return isa_sse2;
102 default:
103 break;
104 }
105 return isa_none;
106}
107

source code of glibc/sysdeps/x86/tst-ifunc-isa.h