| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include <esl_config.h> |
| 16 |
|
| 17 |
#include <stdlib.h> |
| 18 |
#include <stdint.h> |
| 19 |
#include <stdio.h> |
| 20 |
#if defined(_MSC_VER) |
| 21 |
#include <intrin.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
#include "easel.h" |
| 25 |
#include "esl_cpu.h" |
| 26 |
|
| 27 |
|
| 28 |
#if defined(eslENABLE_SSE) || defined(eslENABLE_SSE4) || defined(eslENABLE_AVX) || defined(eslENABLE_AVX512) |
| 29 |
static void cpu_run_id(uint32_t eax, uint32_t ecx, uint32_t *abcd); |
| 30 |
#endif |
| 31 |
#ifdef eslENABLE_SSE |
| 32 |
static int cpu_has_sse(void); |
| 33 |
#endif |
| 34 |
#ifdef eslENABLE_SSE4 |
| 35 |
static int cpu_has_sse4(void); |
| 36 |
#endif |
| 37 |
#ifdef eslENABLE_AVX |
| 38 |
static int cpu_check_xcr0_ymm(void); |
| 39 |
static int cpu_has_avx(void); |
| 40 |
#endif |
| 41 |
#ifdef eslENABLE_AVX512 |
| 42 |
static int cpu_check_xcr0_zmm(void); |
| 43 |
static int cpu_has_avx512(void); |
| 44 |
#endif |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
int |
| 66 |
esl_cpu_has_sse(void) |
| 67 |
{ |
| 68 |
#ifdef eslENABLE_SSE |
| 69 |
static int sse_support = -1; |
| 70 |
if (sse_support < 0) |
| 71 |
sse_support = cpu_has_sse(); |
| 72 |
return sse_support; |
| 73 |
#else |
| 74 |
return 0; |
| 75 |
#endif |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
int |
| 89 |
esl_cpu_has_sse4(void) |
| 90 |
{ |
| 91 |
#ifdef eslENABLE_SSE4 |
| 92 |
static int sse4_support = -1; |
| 93 |
if (sse4_support < 0) |
| 94 |
sse4_support = cpu_has_sse4(); |
| 95 |
return sse4_support; |
| 96 |
#else |
| 97 |
return 0; |
| 98 |
#endif |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
int |
| 113 |
esl_cpu_has_avx(void) |
| 114 |
{ |
| 115 |
#ifdef eslENABLE_AVX |
| 116 |
static int avx_support = -1; |
| 117 |
if (avx_support < 0) |
| 118 |
avx_support = cpu_has_avx(); |
| 119 |
return avx_support; |
| 120 |
#else |
| 121 |
return 0; |
| 122 |
#endif |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
int |
| 135 |
esl_cpu_has_avx512(void) |
| 136 |
{ |
| 137 |
#ifdef eslENABLE_AVX512 |
| 138 |
static int avx512_support = -1; |
| 139 |
if (avx512_support < 0) |
| 140 |
avx512_support = cpu_has_avx512(); |
| 141 |
return avx512_support; |
| 142 |
#else |
| 143 |
return 0; |
| 144 |
#endif |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
char * |
| 158 |
esl_cpu_Get(void) |
| 159 |
{ |
| 160 |
#ifdef eslENABLE_AVX512 |
| 161 |
if (esl_cpu_has_avx512()) return "AVX512"; |
| 162 |
#endif |
| 163 |
#ifdef eslENABLE_AVX |
| 164 |
if (esl_cpu_has_avx()) return "AVX"; |
| 165 |
#endif |
| 166 |
#ifdef eslENABLE_SSE4 |
| 167 |
if (esl_cpu_has_sse4()) return "SSE4"; |
| 168 |
#endif |
| 169 |
#ifdef eslENABLE_SSE |
| 170 |
if (esl_cpu_has_sse()) return "SSE"; |
| 171 |
#endif |
| 172 |
#ifdef eslENABLE_NEON |
| 173 |
return "NEON"; |
| 174 |
#endif |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
return "none"; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
#if defined(eslENABLE_SSE) || defined(eslENABLE_SSE4) || defined(eslENABLE_AVX) || defined(eslENABLE_AVX512) |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
static void |
| 202 |
cpu_run_id(uint32_t eax, uint32_t ecx, uint32_t *abcd) |
| 203 |
{ |
| 204 |
#if defined(_MSC_VER) |
| 205 |
__cpuidex(abcd, eax, ecx); |
| 206 |
#else |
| 207 |
uint32_t ebx = 0; |
| 208 |
uint32_t edx = 0; |
| 209 |
#if defined( __i386__ ) && defined ( __PIC__ ) |
| 210 |
__asm__ ( "movl %%ebx, %%edi \n\t cpuid \n\t xchgl %%ebx, %%edi" : "=D" (ebx), "+a" (eax), "+c" (ecx), "=d" (edx) ); |
| 211 |
#else |
| 212 |
__asm__ ( "cpuid" : "+b" (ebx), "+a" (eax), "+c" (ecx), "=d" (edx) ); |
| 213 |
#endif |
| 214 |
abcd[0] = eax; abcd[1] = ebx; abcd[2] = ecx; abcd[3] = edx; |
| 215 |
#endif |
| 216 |
} |
| 217 |
#endif |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
#ifdef eslENABLE_AVX |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
static int |
| 240 |
cpu_check_xcr0_ymm(void) |
| 241 |
{ |
| 242 |
uint32_t xcr0; |
| 243 |
uint32_t ymm_xmm = (1 << 2) | (1 << 1); |
| 244 |
#if defined(_MSC_VER) |
| 245 |
xcr0 = (uint32_t)_xgetbv(0); |
| 246 |
#else |
| 247 |
__asm__(".byte 0x0f, 0x01, 0xd0" : "=a" (xcr0) : "c" (0) : "%edx" ); |
| 248 |
#endif |
| 249 |
return ((xcr0 & ymm_xmm) == ymm_xmm); |
| 250 |
} |
| 251 |
#endif |
| 252 |
|
| 253 |
|
| 254 |
#ifdef eslENABLE_AVX512 |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
static int |
| 260 |
cpu_check_xcr0_zmm(void) |
| 261 |
{ |
| 262 |
uint32_t xcr0; |
| 263 |
uint32_t zmm_ymm_xmm = (7 << 5) | (1 << 2) | (1 << 1); |
| 264 |
#if defined(_MSC_VER) |
| 265 |
xcr0 = (uint32_t)_xgetbv(0); |
| 266 |
#else |
| 267 |
__asm__ (".byte 0x0f, 0x01, 0xd0" : "=a" (xcr0) : "c" (0) : "%edx" ); |
| 268 |
#endif |
| 269 |
return ((xcr0 & zmm_ymm_xmm) == zmm_ymm_xmm); |
| 270 |
} |
| 271 |
#endif |
| 272 |
|
| 273 |
|
| 274 |
#ifdef eslENABLE_SSE |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
static int |
| 281 |
cpu_has_sse(void) |
| 282 |
{ |
| 283 |
uint32_t abcd[4]; |
| 284 |
uint32_t sse2_mask = (1 << 25) | |
| 285 |
(1 << 26); |
| 286 |
|
| 287 |
cpu_run_id( 1, 0, abcd ); |
| 288 |
if ( (abcd[3] & sse2_mask) != sse2_mask) |
| 289 |
return 0; |
| 290 |
return 1; |
| 291 |
} |
| 292 |
#endif |
| 293 |
|
| 294 |
|
| 295 |
#ifdef eslENABLE_SSE4 |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
static int |
| 302 |
cpu_has_sse4(void) |
| 303 |
{ |
| 304 |
uint32_t abcd[4]; |
| 305 |
uint32_t sse2_mask = (1 << 25) | |
| 306 |
(1 << 26); |
| 307 |
uint32_t sse41_mask = (1 << 19); |
| 308 |
|
| 309 |
cpu_run_id( 1, 0, abcd ); |
| 310 |
if ( (abcd[3] & sse2_mask) != sse2_mask || |
| 311 |
(abcd[2] & sse41_mask) != sse41_mask) |
| 312 |
return 0; |
| 313 |
return 1; |
| 314 |
} |
| 315 |
#endif |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
#ifdef eslENABLE_AVX |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
static int |
| 326 |
cpu_has_avx(void) |
| 327 |
{ |
| 328 |
uint32_t abcd[4]; |
| 329 |
uint32_t fma_movbe_osxsave_mask = ((1 << 12) | (1 << 22) | (1 << 27)); |
| 330 |
uint32_t avx2_bmi12_mask = (1 << 5) | (1 << 3) | (1 << 8); |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
cpu_run_id( 1, 0, abcd ); |
| 336 |
if ( (abcd[2] & fma_movbe_osxsave_mask) != fma_movbe_osxsave_mask ) |
| 337 |
return 0; |
| 338 |
|
| 339 |
if ( ! cpu_check_xcr0_ymm() ) |
| 340 |
return 0; |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
cpu_run_id( 7, 0, abcd ); |
| 346 |
if ( (abcd[1] & avx2_bmi12_mask) != avx2_bmi12_mask ) |
| 347 |
return 0; |
| 348 |
|
| 349 |
|
| 350 |
cpu_run_id( 0x80000001, 0, abcd ); |
| 351 |
if ( (abcd[2] & (1 << 5)) == 0) |
| 352 |
return 0; |
| 353 |
|
| 354 |
return 1; |
| 355 |
} |
| 356 |
#endif |
| 357 |
|
| 358 |
|
| 359 |
#ifdef eslENABLE_AVX512 |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
static int |
| 368 |
cpu_has_avx512(void) |
| 369 |
{ |
| 370 |
uint32_t abcd[4]; |
| 371 |
uint32_t osxsave_mask = (1 << 27); |
| 372 |
uint32_t knl_mask = (1 << 16) | |
| 373 |
(1 << 17) | |
| 374 |
(1 << 30); |
| 375 |
|
| 376 |
cpu_run_id( 1, 0, abcd ); |
| 377 |
if ( (abcd[2] & osxsave_mask) != osxsave_mask ) |
| 378 |
return 0; |
| 379 |
|
| 380 |
if ( ! cpu_check_xcr0_zmm() ) |
| 381 |
return 0; |
| 382 |
|
| 383 |
cpu_run_id( 7, 0, abcd ); |
| 384 |
if ( (abcd[1] & knl_mask) != knl_mask ) |
| 385 |
return 0; |
| 386 |
|
| 387 |
return 1; |
| 388 |
} |
| 389 |
#endif |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
#ifdef eslCPU_TESTDRIVE |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
static void |
| 416 |
utest_consistency(void) |
| 417 |
{ |
| 418 |
|
| 419 |
|
| 420 |
#if defined (eslENABLE_AVX512) && defined (eslENABLE_AVX) |
| 421 |
if (esl_cpu_has_avx512() && ! esl_cpu_has_avx()) esl_fatal("utest_consistency() failed"); |
| 422 |
#endif |
| 423 |
#if defined (eslENABLE_AVX) && defined (eslENABLE_SSE4) |
| 424 |
if (esl_cpu_has_avx() && ! esl_cpu_has_sse4()) esl_fatal("utest_consistency() failed"); |
| 425 |
#endif |
| 426 |
#if defined (eslENABLE_SSE4) && defined (eslENABLE_SSE) |
| 427 |
if (esl_cpu_has_sse4() && ! esl_cpu_has_sse()) esl_fatal("utest_consistency() failed"); |
| 428 |
#endif |
| 429 |
} |
| 430 |
|
| 431 |
#endif |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
#ifdef eslCPU_TESTDRIVE |
| 438 |
|
| 439 |
int |
| 440 |
main(int argc, char **argv) |
| 441 |
{ |
| 442 |
fprintf(stderr, "## %s\n", argv[0]); |
| 443 |
|
| 444 |
utest_consistency(); |
| 445 |
|
| 446 |
fprintf(stderr, "# status = ok\n"); |
| 447 |
return eslOK; |
| 448 |
} |
| 449 |
#endif |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
#ifdef eslCPU_EXAMPLE |
| 456 |
|
| 457 |
#include <esl_config.h> |
| 458 |
|
| 459 |
#include "easel.h" |
| 460 |
#include "esl_cpu.h" |
| 461 |
|
| 462 |
int |
| 463 |
main(int argc, char **argv) |
| 464 |
{ |
| 465 |
printf("your cpu supports our SSE code : %s\n", esl_cpu_has_sse() ? "yes" : "no"); |
| 466 |
printf(" ...our SSE4 code : %s\n", esl_cpu_has_sse4() ? "yes" : "no"); |
| 467 |
printf(" ...our AVX code : %s\n", esl_cpu_has_avx() ? "yes" : "no"); |
| 468 |
printf(" ...our AVX512 code : %s\n", esl_cpu_has_avx512() ? "yes" : "no"); |
| 469 |
printf("Our dispatchers will choose : %s\n", esl_cpu_Get()); |
| 470 |
} |
| 471 |
#endif |