aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/cpuid.h
blob: e0ccee515b16fad2a8c6c4ebda7fbce4d85a4ff8 (plain)
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
/*
* Runtime CPU detection
* (C) 2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#ifndef BOTAN_CPUID_H__
#define BOTAN_CPUID_H__

#include <botan/types.h>

namespace Botan {

/**
* A class handling runtime CPU feature detection
*/
class BOTAN_DLL CPUID
   {
   public:
      /**
      * Return a best guess of the cache line size
      */
      static u32bit cache_line_size();

      /**
      * Check if the processor supports RDTSC
      */
      static bool has_rdtsc()
         { return ((x86_processor_flags() >> CPUID_RDTSC_BIT) & 1); }

      /**
      * Check if the processor supports SSE2
      */
      static bool has_sse2()
         { return ((x86_processor_flags() >> CPUID_SSE2_BIT) & 1); }

      /**
      * Check if the processor supports SSSE3
      */
      static bool has_ssse3()
         { return ((x86_processor_flags() >> CPUID_SSSE3_BIT) & 1); }

      /**
      * Check if the processor supports SSE4.1
      */
      static bool has_sse41()
         { return ((x86_processor_flags() >> CPUID_SSE41_BIT) & 1); }

      /**
      * Check if the processor supports SSE4.2
      */
      static bool has_sse42()
         { return ((x86_processor_flags() >> CPUID_SSE42_BIT) & 1); }

      /**
      * Check if the processor supports extended AVX vector instructions
      */
      static bool has_avx()
         { return ((x86_processor_flags() >> CPUID_AVX_BIT) & 1); }

      /**
      * Check if the processor supports Intel's AES instructions
      */
      static bool has_aes_intel()
         { return ((x86_processor_flags() >> CPUID_INTEL_AES_BIT) & 1); }

      /**
      * Check if the processor supports AltiVec/VMX
      */
      static bool has_altivec();
   private:
      enum CPUID_bits {
         CPUID_RDTSC_BIT = 4,
         CPUID_SSE2_BIT = 26,
         CPUID_SSSE3_BIT = 41,
         CPUID_SSE41_BIT = 51,
         CPUID_SSE42_BIT = 52,
         CPUID_INTEL_AES_BIT = 57,
         CPUID_AVX_BIT = 60
      };

      static u64bit x86_processor_flags();
   };

}

#endif