aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/cpu_id.h
blob: f6ec49a9fa32c595d5f673f7ce0bcb262cbd8153 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#pragma once

#include "types.h"
#include "compiler.h" 
#include <util/generic/singleton.h>

#define Y_CPU_ID_ENUMERATE(F) \
    F(SSE)                    \
    F(SSE2)                   \
    F(SSE3)                   \
    F(SSSE3)                  \
    F(SSE41)                  \
    F(SSE42)                  \
    F(F16C)                   \ 
    F(POPCNT)                 \
    F(BMI1)                   \
    F(BMI2)                   \
    F(PCLMUL)                 \
    F(AES)                    \
    F(AVX)                    \
    F(FMA)                    \
    F(AVX2)                   \
    F(AVX512F)                \
    F(AVX512DQ)               \
    F(AVX512IFMA)             \
    F(AVX512PF)               \
    F(AVX512ER)               \
    F(AVX512CD)               \
    F(AVX512BW)               \
    F(AVX512VL)               \
    F(AVX512VBMI)             \
    F(PREFETCHWT1)            \
    F(SHA)                    \
    F(ADX)                    \
    F(RDRAND)                 \
    F(RDSEED)                 \
    F(PCOMMIT)                \
    F(RDTSCP)                 \
    F(CLFLUSHOPT)             \
    F(CLWB)                   \
    F(XSAVE)                  \
    F(OSXSAVE)

#define Y_CPU_ID_ENUMERATE_OUTLINED_CACHED_DEFINE(F) \
    F(F16C)                                          \
    F(BMI1)                                          \
    F(BMI2)                                          \
    F(PCLMUL)                                        \
    F(AES)                                           \
    F(AVX)                                           \
    F(FMA)                                           \
    F(AVX2)                                          \
    F(AVX512F)                                       \
    F(AVX512DQ)                                      \
    F(AVX512IFMA)                                    \
    F(AVX512PF)                                      \
    F(AVX512ER)                                      \
    F(AVX512CD)                                      \
    F(AVX512BW)                                      \
    F(AVX512VL)                                      \
    F(AVX512VBMI)                                    \
    F(PREFETCHWT1)                                   \
    F(SHA)                                           \
    F(ADX)                                           \
    F(RDRAND)                                        \
    F(RDSEED)                                        \
    F(PCOMMIT)                                       \
    F(RDTSCP)                                        \
    F(CLFLUSHOPT)                                    \
    F(CLWB)                                          \
    F(XSAVE)                                         \
    F(OSXSAVE)

namespace NX86 {
    /**
     * returns false on non-x86 platforms
     */
    bool CpuId(ui32 op, ui32 res[4]) noexcept;
    bool CpuId(ui32 op, ui32 subOp, ui32 res[4]) noexcept;

#define Y_DEF_NAME(X) Y_CONST_FUNCTION bool Have##X() noexcept; 
    Y_CPU_ID_ENUMERATE(Y_DEF_NAME)
#undef Y_DEF_NAME

#define Y_DEF_NAME(X) Y_CONST_FUNCTION bool CachedHave##X() noexcept; 
    Y_CPU_ID_ENUMERATE_OUTLINED_CACHED_DEFINE(Y_DEF_NAME)
#undef Y_DEF_NAME

    struct TFlagsCache {
#define Y_DEF_NAME(X) const bool Have##X##_ = NX86::Have##X();
        Y_CPU_ID_ENUMERATE(Y_DEF_NAME)
#undef Y_DEF_NAME
    };

#define Y_LOOKUP_CPU_ID_IMPL(X) return SingletonWithPriority<TFlagsCache, 0>()->Have##X##_;

    inline bool CachedHaveSSE() noexcept {
#ifdef _sse_
        return true;
#else
        Y_LOOKUP_CPU_ID_IMPL(SSE)
#endif
    }

    inline bool CachedHaveSSE2() noexcept {
#ifdef _sse2_
        return true;
#else
        Y_LOOKUP_CPU_ID_IMPL(SSE2)
#endif
    }

    inline bool CachedHaveSSE3() noexcept {
#ifdef _sse3_
        return true;
#else
        Y_LOOKUP_CPU_ID_IMPL(SSE3)
#endif
    }

    inline bool CachedHaveSSSE3() noexcept {
#ifdef _ssse3_
        return true;
#else
        Y_LOOKUP_CPU_ID_IMPL(SSSE3)
#endif
    }

    inline bool CachedHaveSSE41() noexcept {
#ifdef _sse4_1_
        return true;
#else
        Y_LOOKUP_CPU_ID_IMPL(SSE41)
#endif
    }

    inline bool CachedHaveSSE42() noexcept {
#ifdef _sse4_2_
        return true;
#else
        Y_LOOKUP_CPU_ID_IMPL(SSE42)
#endif
    }

    inline bool CachedHavePOPCNT() noexcept {
#ifdef _popcnt_
        return true;
#else
        Y_LOOKUP_CPU_ID_IMPL(POPCNT)
#endif
    }

#undef Y_LOOKUP_CPU_ID_IMPL

}

const char* CpuBrand(ui32 store[12]) noexcept;