Improve 64 bit strcat functions with SSE2/SSSE3
[platform/upstream/glibc.git] / sysdeps / x86_64 / multiarch / init-arch.h
1 /* This file is part of the GNU C Library.
2    Copyright (C) 2008, 2009, 2010 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, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #define bit_Fast_Rep_String             (1 << 0)
20 #define bit_Fast_Copy_Backward          (1 << 1)
21 #define bit_Slow_BSF                    (1 << 2)
22 #define bit_Prefer_SSE_for_memop        (1 << 3)
23 #define bit_Fast_Unaligned_Load         (1 << 4)
24 #define bit_Prefer_PMINUB_for_stringop  (1 << 5)
25
26 #ifdef  __ASSEMBLER__
27
28 # include <ifunc-defines.h>
29
30 # define bit_SSE2       (1 << 26)
31 # define bit_SSSE3      (1 << 9)
32 # define bit_SSE4_1     (1 << 19)
33 # define bit_SSE4_2     (1 << 20)
34
35 # define index_SSE2     COMMON_CPUID_INDEX_1*CPUID_SIZE+CPUID_EDX_OFFSET
36 # define index_SSSE3    COMMON_CPUID_INDEX_1*CPUID_SIZE+CPUID_ECX_OFFSET
37 # define index_SSE4_1   COMMON_CPUID_INDEX_1*CPUID_SIZE+CPUID_ECX_OFFSET
38 # define index_SSE4_2   COMMON_CPUID_INDEX_1*CPUID_SIZE+CPUID_ECX_OFFSET
39
40 # define index_Fast_Rep_String          FEATURE_INDEX_1*FEATURE_SIZE
41 # define index_Fast_Copy_Backward       FEATURE_INDEX_1*FEATURE_SIZE
42 # define index_Slow_BSF                 FEATURE_INDEX_1*FEATURE_SIZE
43 # define index_Prefer_SSE_for_memop     FEATURE_INDEX_1*FEATURE_SIZE
44 # define index_Fast_Unaligned_Load      FEATURE_INDEX_1*FEATURE_SIZE
45 # define index_Prefer_PMINUB_for_stringop FEATURE_INDEX_1*FEATURE_SIZE
46
47 #else   /* __ASSEMBLER__ */
48
49 # include <sys/param.h>
50
51 enum
52   {
53     COMMON_CPUID_INDEX_1 = 0,
54     /* Keep the following line at the end.  */
55     COMMON_CPUID_INDEX_MAX
56   };
57
58 enum
59   {
60     FEATURE_INDEX_1 = 0,
61     /* Keep the following line at the end.  */
62     FEATURE_INDEX_MAX
63   };
64
65 extern struct cpu_features
66 {
67   enum cpu_features_kind
68     {
69       arch_kind_unknown = 0,
70       arch_kind_intel,
71       arch_kind_amd,
72       arch_kind_other
73     } kind;
74   int max_cpuid;
75   struct cpuid_registers
76   {
77     unsigned int eax;
78     unsigned int ebx;
79     unsigned int ecx;
80     unsigned int edx;
81   } cpuid[COMMON_CPUID_INDEX_MAX];
82   unsigned int family;
83   unsigned int model;
84   unsigned int feature[FEATURE_INDEX_MAX];
85 } __cpu_features attribute_hidden;
86
87
88 extern void __init_cpu_features (void) attribute_hidden;
89 #define INIT_ARCH()\
90   do                                                    \
91     if (__cpu_features.kind == arch_kind_unknown)       \
92       __init_cpu_features ();                           \
93   while (0)
94
95 /* Used from outside libc.so to get access to the CPU features structure.  */
96 extern const struct cpu_features *__get_cpu_features (void)
97      __attribute__ ((const));
98
99 # ifndef NOT_IN_libc
100 #  define __get_cpu_features()  (&__cpu_features)
101 # endif
102
103 # define HAS_CPU_FEATURE(idx, reg, bit) \
104   ((__get_cpu_features ()->cpuid[idx].reg & (1 << (bit))) != 0)
105
106 /* Following are the feature tests used throughout libc.  */
107
108 # define HAS_SSE2       HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, edx, 26)
109 # define HAS_POPCOUNT   HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, ecx, 23)
110 # define HAS_SSSE3      HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, ecx, 9)
111 # define HAS_SSE4_1     HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, ecx, 19)
112 # define HAS_SSE4_2     HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, ecx, 20)
113 # define HAS_FMA        HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, ecx, 12)
114
115 # define index_Fast_Rep_String          FEATURE_INDEX_1
116 # define index_Fast_Copy_Backward       FEATURE_INDEX_1
117 # define index_Slow_BSF                 FEATURE_INDEX_1
118 # define index_Prefer_SSE_for_memop     FEATURE_INDEX_1
119 # define index_Fast_Unaligned_Load      FEATURE_INDEX_1
120
121 #define HAS_ARCH_FEATURE(idx, bit) \
122   ((__get_cpu_features ()->feature[idx] & (bit)) != 0)
123
124 #define HAS_FAST_REP_STRING \
125   HAS_ARCH_FEATURE (index_Fast_Rep_String, bit_Fast_Rep_String)
126
127 #define HAS_FAST_COPY_BACKWARD \
128   HAS_ARCH_FEATURE (index_Fast_Copy_Backward, bit_Fast_Copy_Backward)
129
130 #define HAS_SLOW_BSF \
131   HAS_ARCH_FEATURE (index_Slow_BSF, bit_Slow_BSF)
132
133 #define HAS_PREFER_SSE_FOR_MEMOP \
134   HAS_ARCH_FEATURE (index_Prefer_SSE_for_memop, bit_Prefer_SSE_for_memop)
135
136 #define HAS_FAST_UNALIGNED_LOAD \
137   HAS_ARCH_FEATURE (index_Fast_Unaligned_Load, bit_Fast_Unaligned_Load)
138
139 #endif  /* __ASSEMBLER__ */