1 | /* This file is part of the GNU C Library. |
2 | Copyright (C) 2008-2017 Free Software Foundation, Inc. |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2.1 of the License, or (at your option) any later version. |
8 | |
9 | The GNU C Library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ |
17 | |
18 | #ifdef __ASSEMBLER__ |
19 | # include <cpu-features.h> |
20 | #else |
21 | # include <ldsodefs.h> |
22 | #endif |
23 | |
24 | /* These macros are used to implement ifunc selection in C. To implement |
25 | an ifunc function, foo, which returns the address of __foo_sse2 or |
26 | __foo_avx2: |
27 | |
28 | #define foo __redirect_foo |
29 | #define __foo __redirect___foo |
30 | #include <foo.h> |
31 | #undef foo |
32 | #undef __foo |
33 | #define SYMBOL_NAME foo |
34 | #include <init-arch.h> |
35 | |
36 | extern __typeof (REDIRECT_NAME) OPTIMIZE (sse2) attribute_hidden; |
37 | extern __typeof (REDIRECT_NAME) OPTIMIZE (avx2) attribute_hidden; |
38 | |
39 | static inline void * |
40 | foo_selector (void) |
41 | { |
42 | if (use AVX2) |
43 | return OPTIMIZE (avx2); |
44 | |
45 | return OPTIMIZE (sse2); |
46 | } |
47 | |
48 | libc_ifunc_redirected (__redirect_foo, foo, foo_selector ()); |
49 | |
50 | */ |
51 | |
52 | #define PASTER1(x,y) x##_##y |
53 | #define EVALUATOR1(x,y) PASTER1 (x,y) |
54 | #define PASTER2(x,y) __##x##_##y |
55 | #define EVALUATOR2(x,y) PASTER2 (x,y) |
56 | |
57 | /* Basically set '__redirect_<symbol>' to use as type definition, |
58 | '__<symbol>_<variant>' as the optimized implementation and |
59 | '<symbol>_ifunc_selector' as the IFUNC selector. */ |
60 | #define REDIRECT_NAME EVALUATOR1 (__redirect, SYMBOL_NAME) |
61 | #define OPTIMIZE(name) EVALUATOR2 (SYMBOL_NAME, name) |
62 | #define IFUNC_SELECTOR EVALUATOR1 (SYMBOL_NAME, ifunc_selector) |
63 | |
64 | #ifndef __x86_64__ |
65 | /* Due to the reordering and the other nifty extensions in i686, it is |
66 | not really good to use heavily i586 optimized code on an i686. It's |
67 | better to use i486 code if it isn't an i586. */ |
68 | # if MINIMUM_ISA == 686 |
69 | # define USE_I586 0 |
70 | # define USE_I686 1 |
71 | # else |
72 | # define USE_I586 (HAS_ARCH_FEATURE (I586) && !HAS_ARCH_FEATURE (I686)) |
73 | # define USE_I686 HAS_ARCH_FEATURE (I686) |
74 | # endif |
75 | #endif |
76 | |