/src/spotify-json/include/spotify/json/detail/cpuid.hpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2015-2016 Spotify AB |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
5 | | * use this file except in compliance with the License. You may obtain a copy of |
6 | | * the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
12 | | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
13 | | * License for the specific language governing permissions and limitations under |
14 | | * the License. |
15 | | */ |
16 | | |
17 | | #pragma once |
18 | | |
19 | | #include <array> |
20 | | #include <cstdint> |
21 | | |
22 | | #if defined(_MSC_VER) |
23 | | #include <intrin.h> |
24 | | #endif |
25 | | |
26 | | #include <spotify/json/detail/macros.hpp> |
27 | | |
28 | | namespace spotify { |
29 | | namespace json { |
30 | | namespace detail { |
31 | | |
32 | | class cpuid { |
33 | | public: |
34 | 4.60k | cpuid() { |
35 | 4.60k | #if defined(json_arch_x86) |
36 | 4.60k | const uint32_t cpuid_function(1); |
37 | | #if defined(_MSC_VER) |
38 | | ::__cpuid(reinterpret_cast<int *>(_registers.data()), cpuid_function); |
39 | | #elif defined(__GNUC__) |
40 | | __asm__ __volatile__ ( |
41 | 4.60k | "cpuid ;\n" |
42 | 4.60k | : "=a" (_registers[cpu_register::eax]), |
43 | 4.60k | "=b" (_registers[cpu_register::ebx]), |
44 | 4.60k | "=c" (_registers[cpu_register::ecx]), |
45 | 4.60k | "=d" (_registers[cpu_register::edx]) |
46 | 4.60k | : "a" (cpuid_function) |
47 | 4.60k | :); |
48 | 4.60k | #endif // defined(_MSC_VER) |
49 | 4.60k | #endif // defined(json_arch_x86) |
50 | 4.60k | } |
51 | | |
52 | 4.60k | bool has_sse42() const { |
53 | 4.60k | return has_feature_bit(cpu_register::ecx, cpu_feature_bit::sse_42); |
54 | 4.60k | } |
55 | | |
56 | | private: |
57 | | struct cpu_register { |
58 | | enum type { |
59 | | eax = 0, |
60 | | ebx = 1, |
61 | | ecx = 2, |
62 | | edx = 3 |
63 | | }; |
64 | | }; |
65 | | |
66 | | struct cpu_feature_bit { |
67 | | enum type { |
68 | | sse_42 = 20, |
69 | | }; |
70 | | }; |
71 | | |
72 | | bool has_feature_bit( |
73 | | const cpu_register::type ®, |
74 | 4.60k | const cpu_feature_bit::type &bit) const { |
75 | 4.60k | return (_registers[reg] & (1 << bit)) != 0; |
76 | 4.60k | } |
77 | | |
78 | | std::array<uint32_t, 4> _registers; |
79 | | }; |
80 | | |
81 | | } // namespace detail |
82 | | } // namespace json |
83 | | } // namespace spotify |