/src/libreoffice/include/tools/cpuid.hxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
8 | | * |
9 | | */ |
10 | | |
11 | | #pragma once |
12 | | |
13 | | #include <sal/config.h> |
14 | | #include <tools/toolsdllapi.h> |
15 | | #include <o3tl/typed_flags_set.hxx> |
16 | | #include <rtl/ustring.hxx> |
17 | | |
18 | | /* |
19 | | |
20 | | Do NOT include this header in source files compiled with CPU-specific code. |
21 | | TODO: For the header to be safe that way, it should be free of any templates |
22 | | or inline functions, otherwise their possibly emitted copies compiled |
23 | | with the CPU-specific instructions might be chosen by the linker as the copy |
24 | | to keep. |
25 | | |
26 | | Also see the note at the top of simdsupport.hxx . |
27 | | |
28 | | */ |
29 | | |
30 | | namespace cpuid { |
31 | | |
32 | | enum class InstructionSetFlags |
33 | | { |
34 | | NONE = 0x00, |
35 | | HYPER = 0x01, |
36 | | SSE2 = 0x02, |
37 | | SSSE3 = 0x04, |
38 | | SSE41 = 0x08, |
39 | | SSE42 = 0x10, |
40 | | AVX = 0x20, |
41 | | AVX2 = 0x40, |
42 | | AVX512F = 0x80 |
43 | | }; |
44 | | |
45 | | } // end cpuid |
46 | | |
47 | | namespace o3tl { |
48 | | template<> struct typed_flags<cpuid::InstructionSetFlags> : is_typed_flags<cpuid::InstructionSetFlags, 0x0ff> {}; |
49 | | } |
50 | | |
51 | | namespace cpuid { |
52 | | |
53 | | /** Get supported instruction set flags determined at runtime by probing the CPU. |
54 | | */ |
55 | | TOOLS_DLLPUBLIC InstructionSetFlags getCpuInstructionSetFlags(); |
56 | | |
57 | | /** Check if a certain instruction set is supported by the CPU at runtime. |
58 | | */ |
59 | | TOOLS_DLLPUBLIC bool isCpuInstructionSetSupported(InstructionSetFlags eInstructions); |
60 | | |
61 | | /** Returns a string of supported instructions. |
62 | | */ |
63 | | TOOLS_DLLPUBLIC OUString instructionSetSupportedString(); |
64 | | |
65 | | /** Check if SSE2 is supported by the CPU |
66 | | */ |
67 | | inline bool hasSSE2() |
68 | 0 | { |
69 | 0 | return isCpuInstructionSetSupported(InstructionSetFlags::SSE2); |
70 | 0 | } |
71 | | |
72 | | /** Check if SSSE3 is supported by the CPU |
73 | | */ |
74 | | inline bool hasSSSE3() |
75 | 0 | { |
76 | 0 | return isCpuInstructionSetSupported(InstructionSetFlags::SSSE3); |
77 | 0 | } |
78 | | |
79 | | /** Check if AVX is supported by the CPU |
80 | | */ |
81 | | inline bool hasAVX() |
82 | 0 | { |
83 | 0 | return isCpuInstructionSetSupported(InstructionSetFlags::AVX); |
84 | 0 | } |
85 | | |
86 | | /** Check if AVX2 is supported by the CPU |
87 | | */ |
88 | | inline bool hasAVX2() |
89 | 0 | { |
90 | 0 | return isCpuInstructionSetSupported(InstructionSetFlags::AVX2); |
91 | 0 | } |
92 | | |
93 | | /** Check if AVX512F is supported by the CPU |
94 | | */ |
95 | | inline bool hasAVX512F() |
96 | 0 | { |
97 | 0 | return isCpuInstructionSetSupported(InstructionSetFlags::AVX512F); |
98 | 0 | } |
99 | | |
100 | | /** Check if Hyper Threading is supported |
101 | | */ |
102 | | inline bool hasHyperThreading() |
103 | 22 | { |
104 | 22 | return isCpuInstructionSetSupported(InstructionSetFlags::HYPER); |
105 | 22 | } |
106 | | |
107 | | } // end cpuid |
108 | | |
109 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |