Bullet Collision Detection & Physics Library: btCpuFeatureUtility.h Source File

Bullet Collision Detection & Physics Library

btCpuFeatureUtility.h
Go to the documentation of this file.
1
2 #ifndef BT_CPU_UTILITY_H
3 #define BT_CPU_UTILITY_H
4
5 #include "LinearMath/btScalar.h"
6
7 #include <string.h> //memset
8 #ifdef USE_SIMD
9 #include <emmintrin.h>
10 #ifdef BT_ALLOW_SSE4
11 #include <intrin.h>
12 #endif //BT_ALLOW_SSE4
13 #endif //USE_SIMD
14
15 #if defined BT_USE_NEON
16 #define ARM_NEON_GCC_COMPATIBILITY 1
17 #include <arm_neon.h>
18 #include <sys/types.h>
19 #include <sys/sysctl.h> //for sysctlbyname
20 #endif //BT_USE_NEON
21
24 class btCpuFeatureUtility
25 {
26 public:
27 enum btCpuFeature
28 {
29 CPU_FEATURE_FMA3 = 1,
30 CPU_FEATURE_SSE4_1 = 2,
31 CPU_FEATURE_NEON_HPFP = 4
32 };
33
34 static int getCpuFeatures()
35 {
36 static int capabilities = 0;
37 static bool testedCapabilities = false;
38 if (0 != testedCapabilities)
39 {
40 return capabilities;
41 }
42
43 #ifdef BT_USE_NEON
44 {
45 uint32_t hasFeature = 0;
46 size_t featureSize = sizeof(hasFeature);
47 int err = sysctlbyname("hw.optional.neon_hpfp", &hasFeature, &featureSize, NULL, 0);
48 if (0 == err && hasFeature)
49 capabilities |= CPU_FEATURE_NEON_HPFP;
50 }
51 #endif //BT_USE_NEON
52
53 #ifdef BT_ALLOW_SSE4
54 {
55 int cpuInfo[4];
56 memset(cpuInfo, 0, sizeof(cpuInfo));
57 unsigned long long sseExt = 0;
58 __cpuid(cpuInfo, 1);
59
60 bool osUsesXSAVE_XRSTORE = cpuInfo[2] & (1 << 27) || false;
61 bool cpuAVXSuport = cpuInfo[2] & (1 << 28) || false;
62
63 if (osUsesXSAVE_XRSTORE && cpuAVXSuport)
64 {
65 sseExt = _xgetbv(0);
66 }
67 const int OSXSAVEFlag = (1UL << 27);
68 const int AVXFlag = ((1UL << 28) | OSXSAVEFlag);
69 const int FMAFlag = ((1UL << 12) | AVXFlag | OSXSAVEFlag);
70 if ((cpuInfo[2] & FMAFlag) == FMAFlag && (sseExt & 6) == 6)
71 {
72 capabilities |= btCpuFeatureUtility::CPU_FEATURE_FMA3;
73 }
74
75 const int SSE41Flag = (1 << 19);
76 if (cpuInfo[2] & SSE41Flag)
77 {
78 capabilities |= btCpuFeatureUtility::CPU_FEATURE_SSE4_1;
79 }
80 }
81 #endif //BT_ALLOW_SSE4
82
83 testedCapabilities = true;
84 return capabilities;
85 }
86 };
87
88 #endif //BT_CPU_UTILITY_H
uint32_t
unsigned int uint32_t
Definition: btConvexHullComputer.cpp:32
btScalar.h
btCpuFeatureUtility
Rudimentary btCpuFeatureUtility for CPU features: only report the features that Bullet actually uses ...
Definition: btCpuFeatureUtility.h:25
btCpuFeatureUtility::btCpuFeature
btCpuFeature
Definition: btCpuFeatureUtility.h:28
btCpuFeatureUtility::CPU_FEATURE_NEON_HPFP
@ CPU_FEATURE_NEON_HPFP
Definition: btCpuFeatureUtility.h:31
btCpuFeatureUtility::CPU_FEATURE_FMA3
@ CPU_FEATURE_FMA3
Definition: btCpuFeatureUtility.h:29
btCpuFeatureUtility::CPU_FEATURE_SSE4_1
@ CPU_FEATURE_SSE4_1
Definition: btCpuFeatureUtility.h:30
btCpuFeatureUtility::getCpuFeatures
static int getCpuFeatures()
Definition: btCpuFeatureUtility.h:34