|
43 | 43 | #define wmb() asm volatile("" ::: "memory") |
44 | 44 | #define wc_wmb() asm volatile("sfence" ::: "memory") |
45 | 45 |
|
46 | | -#define COPY_64B_NT(dst, src) \ |
47 | | - __asm__ __volatile__ ( \ |
48 | | - " movdqa (%1),%%xmm0\n" \ |
49 | | - " movdqa 16(%1),%%xmm1\n" \ |
50 | | - " movdqa 32(%1),%%xmm2\n" \ |
51 | | - " movdqa 48(%1),%%xmm3\n" \ |
52 | | - " movntdq %%xmm0, (%0)\n" \ |
53 | | - " movntdq %%xmm1, 16(%0)\n" \ |
54 | | - " movntdq %%xmm2, 32(%0)\n" \ |
55 | | - " movntdq %%xmm3, 48(%0)\n" \ |
56 | | - : : "r" (dst), "r" (src) : "memory"); \ |
57 | | - dst += 8; \ |
58 | | - src += 8 |
59 | | - |
60 | | - |
61 | 46 | /** |
62 | 47 | * Add to the atomic variable. |
63 | 48 | * @param i integer value to add. |
@@ -117,4 +102,135 @@ static inline void prefetch_range(void *addr, size_t len) |
117 | 102 | prefetch(cp); |
118 | 103 | } |
119 | 104 |
|
| 105 | +enum { |
| 106 | + CPU_FLAG_CMOV = (1 << 0), |
| 107 | + CPU_FLAG_MMX = (1 << 1), |
| 108 | + CPU_FLAG_MMX2 = (1 << 2), |
| 109 | + CPU_FLAG_SSE = (1 << 3), |
| 110 | + CPU_FLAG_SSE2 = (1 << 4), |
| 111 | + CPU_FLAG_SSE3 = (1 << 5), |
| 112 | + CPU_FLAG_SSSE3 = (1 << 6), |
| 113 | + CPU_FLAG_SSE41 = (1 << 7), |
| 114 | + CPU_FLAG_SSE42 = (1 << 8), |
| 115 | + CPU_FLAG_AVX = (1 << 9), |
| 116 | + CPU_FLAG_AVX2 = (1 << 10) |
| 117 | +}; |
| 118 | + |
| 119 | +#define X86_CPUID_GET_MODEL 0x00000001u |
| 120 | +#define X86_CPUID_GET_BASE_VALUE 0x00000000u |
| 121 | +#define X86_CPUID_GET_EXTD_VALUE 0x00000007u |
| 122 | +#define X86_CPUID_GET_MAX_VALUE 0x80000000u |
| 123 | + |
| 124 | +VMA_ATTRIBUTE_OPTIMIZE_NONE |
| 125 | + static inline void __x86_cpuid(uint32_t level, |
| 126 | + uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d) |
| 127 | +{ |
| 128 | + asm volatile ("cpuid\n\t" |
| 129 | + : "=a" (*a), "=b" (*b), "=c" (*c), "=d" (*d) |
| 130 | + : "0" (level)); |
| 131 | +} |
| 132 | + |
| 133 | +/* This allows the CPU detection to work with assemblers not supporting |
| 134 | + * the xgetbv mnemonic. |
| 135 | + */ |
| 136 | +#define __x86_xgetbv(_index, _eax, _edx) \ |
| 137 | + asm volatile (".byte 0x0f, 0x01, 0xd0" : "=a"(_eax), "=d"(_edx) : "c" (_index)) |
| 138 | + |
| 139 | +/** |
| 140 | + * Read CPU instruction set |
| 141 | + */ |
| 142 | +VMA_ATTRIBUTE_OPTIMIZE_NONE |
| 143 | + static inline int cpuid_flags() |
| 144 | +{ |
| 145 | + static int cpu_flag = -1; |
| 146 | + |
| 147 | + if (cpu_flag < 0) { |
| 148 | + uint32_t result = 0; |
| 149 | + uint32_t base_value; |
| 150 | + uint32_t _eax, _ebx, _ecx, _edx; |
| 151 | + |
| 152 | + __x86_cpuid(X86_CPUID_GET_BASE_VALUE, &_eax, &_ebx, &_ecx, &_edx); |
| 153 | + base_value = _eax; |
| 154 | + |
| 155 | + if (base_value >= 1) { |
| 156 | + __x86_cpuid(X86_CPUID_GET_MODEL, &_eax, &_ebx, &_ecx, &_edx); |
| 157 | + if (_edx & (1 << 15)) { |
| 158 | + result |= CPU_FLAG_CMOV; |
| 159 | + } |
| 160 | + if (_edx & (1 << 23)) { |
| 161 | + result |= CPU_FLAG_MMX; |
| 162 | + } |
| 163 | + if (_edx & (1 << 25)) { |
| 164 | + result |= CPU_FLAG_MMX2; |
| 165 | + } |
| 166 | + if (_edx & (1 << 25)) { |
| 167 | + result |= CPU_FLAG_SSE; |
| 168 | + } |
| 169 | + if (_edx & (1 << 26)) { |
| 170 | + result |= CPU_FLAG_SSE2; |
| 171 | + } |
| 172 | + if (_ecx & 1) { |
| 173 | + result |= CPU_FLAG_SSE3; |
| 174 | + } |
| 175 | + if (_ecx & (1 << 9)) { |
| 176 | + result |= CPU_FLAG_SSSE3; |
| 177 | + } |
| 178 | + if (_ecx & (1 << 19)) { |
| 179 | + result |= CPU_FLAG_SSE41; |
| 180 | + } |
| 181 | + if (_ecx & (1 << 20)) { |
| 182 | + result |= CPU_FLAG_SSE42; |
| 183 | + } |
| 184 | + if ((_ecx & 0x18000000) == 0x18000000) { |
| 185 | + __x86_xgetbv(0, _eax, _edx); |
| 186 | + if ((_eax & 0x6) == 0x6) { |
| 187 | + result |= CPU_FLAG_AVX; |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + if (base_value >= 7) { |
| 192 | + __x86_cpuid(X86_CPUID_GET_EXTD_VALUE, &_eax, &_ebx, &_ecx, &_edx); |
| 193 | + if ((result & CPU_FLAG_AVX) && (_ebx & (1 << 5))) { |
| 194 | + result |= CPU_FLAG_AVX2; |
| 195 | + } |
| 196 | + } |
| 197 | + cpu_flag = result; |
| 198 | + } |
| 199 | + |
| 200 | + return cpu_flag; |
| 201 | +} |
| 202 | + |
| 203 | +#define __vma_memory_copy64(_dst, _src) \ |
| 204 | +{ \ |
| 205 | + static int is_wc_simd = cpuid_flags() & \ |
| 206 | + (CPU_FLAG_SSE3 | CPU_FLAG_SSSE3 | \ |
| 207 | + CPU_FLAG_SSE41 | CPU_FLAG_SSE42 | \ |
| 208 | + CPU_FLAG_AVX | CPU_FLAG_AVX2); \ |
| 209 | + \ |
| 210 | + if (is_wc_simd) { \ |
| 211 | + __asm__ __volatile__ ( \ |
| 212 | + " movdqa (%1), %%xmm0\n" \ |
| 213 | + " movdqa 16(%1), %%xmm1\n" \ |
| 214 | + " movdqa 32(%1), %%xmm2\n" \ |
| 215 | + " movdqa 48(%1), %%xmm3\n" \ |
| 216 | + \ |
| 217 | + " movntdq %%xmm0, (%0)\n" \ |
| 218 | + " movntdq %%xmm1, 16(%0)\n" \ |
| 219 | + " movntdq %%xmm2, 32(%0)\n" \ |
| 220 | + " movntdq %%xmm3, 48(%0)\n" \ |
| 221 | + : : "r" (_dst), "r" (_src) : "memory"); \ |
| 222 | + _dst += 8; \ |
| 223 | + _src += 8; \ |
| 224 | + } else { \ |
| 225 | + *_dst++ = *_src++; \ |
| 226 | + *_dst++ = *_src++; \ |
| 227 | + *_dst++ = *_src++; \ |
| 228 | + *_dst++ = *_src++; \ |
| 229 | + *_dst++ = *_src++; \ |
| 230 | + *_dst++ = *_src++; \ |
| 231 | + *_dst++ = *_src++; \ |
| 232 | + *_dst++ = *_src++; \ |
| 233 | + } \ |
| 234 | +} |
| 235 | + |
120 | 236 | #endif |
0 commit comments