opencv

Форк
0
/
x86_features.c 
97 строк · 2.7 Кб
1
/* x86_features.c - x86 feature check
2
 *
3
 * Copyright (C) 2013 Intel Corporation. All rights reserved.
4
 * Author:
5
 *  Jim Kukunas
6
 *
7
 * For conditions of distribution and use, see copyright notice in zlib.h
8
 */
9

10
#include "../../zbuild.h"
11
#include "x86_features.h"
12

13
#ifdef _MSC_VER
14
#  include <intrin.h>
15
#else
16
// Newer versions of GCC and clang come with cpuid.h
17
#  include <cpuid.h>
18
#endif
19

20
#include <string.h>
21

22
static inline void cpuid(int info, unsigned* eax, unsigned* ebx, unsigned* ecx, unsigned* edx) {
23
#ifdef _MSC_VER
24
    unsigned int registers[4];
25
    __cpuid((int *)registers, info);
26

27
    *eax = registers[0];
28
    *ebx = registers[1];
29
    *ecx = registers[2];
30
    *edx = registers[3];
31
#else
32
    __cpuid(info, *eax, *ebx, *ecx, *edx);
33
#endif
34
}
35

36
static inline void cpuidex(int info, int subinfo, unsigned* eax, unsigned* ebx, unsigned* ecx, unsigned* edx) {
37
#ifdef _MSC_VER
38
    unsigned int registers[4];
39
    __cpuidex((int *)registers, info, subinfo);
40

41
    *eax = registers[0];
42
    *ebx = registers[1];
43
    *ecx = registers[2];
44
    *edx = registers[3];
45
#else
46
    __cpuid_count(info, subinfo, *eax, *ebx, *ecx, *edx);
47
#endif
48
}
49

50
static inline uint64_t xgetbv(unsigned int xcr) {
51
#ifdef _MSC_VER
52
    return _xgetbv(xcr);
53
#else
54
    uint32_t eax, edx;
55
    __asm__ ( ".byte 0x0f, 0x01, 0xd0" : "=a"(eax), "=d"(edx) : "c"(xcr));
56
    return (uint64_t)(edx) << 32 | eax;
57
#endif
58
}
59

60
void Z_INTERNAL x86_check_features(struct x86_cpu_features *features) {
61
    unsigned eax, ebx, ecx, edx;
62
    unsigned maxbasic;
63

64
    cpuid(0, &maxbasic, &ebx, &ecx, &edx);
65
    cpuid(1 /*CPU_PROCINFO_AND_FEATUREBITS*/, &eax, &ebx, &ecx, &edx);
66

67
    features->has_sse2 = edx & 0x4000000;
68
    features->has_ssse3 = ecx & 0x200;
69
    features->has_sse42 = ecx & 0x100000;
70
    features->has_pclmulqdq = ecx & 0x2;
71

72
    if (ecx & 0x08000000) {
73
        uint64_t xfeature = xgetbv(0);
74

75
        features->has_os_save_ymm = ((xfeature & 0x06) == 0x06);
76
        features->has_os_save_zmm = ((xfeature & 0xe6) == 0xe6);
77
    }
78

79
    if (maxbasic >= 7) {
80
        cpuidex(7, 0, &eax, &ebx, &ecx, &edx);
81

82
        // check BMI1 bit
83
        // Reference: https://software.intel.com/sites/default/files/article/405250/how-to-detect-new-instruction-support-in-the-4th-generation-intel-core-processor-family.pdf
84
        features->has_vpclmulqdq = ecx & 0x400;
85

86
        // check AVX2 bit if the OS supports saving YMM registers
87
        if (features->has_os_save_ymm) {
88
            features->has_avx2 = ebx & 0x20;
89
        }
90

91
        // check AVX512 bits if the OS supports saving ZMM registers
92
        if (features->has_os_save_zmm) {
93
            features->has_avx512 = ebx & 0x00010000;
94
            features->has_avx512vnni = ecx & 0x800;
95
        }
96
    }
97
}
98

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.