Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/torch/torch7.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSoumith Chintala <soumith@fb.com>2016-12-31 02:10:57 +0300
committerSoumith Chintala <soumith@fb.com>2016-12-31 02:10:57 +0300
commit87213287e51e866c5ee6c2dba21b78adbbc0b51b (patch)
treec9f65483d89f911cd373b82f9388ec87d08ea285
parentb19b8c741ea6b84c0ddb340bc7e9c96360105a22 (diff)
use __get_cpuid when available
-rw-r--r--lib/TH/CMakeLists.txt15
-rw-r--r--lib/TH/generic/simd/simd.h23
2 files changed, 29 insertions, 9 deletions
diff --git a/lib/TH/CMakeLists.txt b/lib/TH/CMakeLists.txt
index 3912bf2..25fb5d3 100644
--- a/lib/TH/CMakeLists.txt
+++ b/lib/TH/CMakeLists.txt
@@ -78,6 +78,21 @@ IF (CORTEXA9_FOUND)
SET(CMAKE_C_FLAGS "-mcpu=cortex-a9 ${CMAKE_C_FLAGS}")
ENDIF (CORTEXA9_FOUND)
+INCLUDE(CheckIncludeFile)
+CHECK_INCLUDE_FILE(cpuid.h HAVE_CPUID_H)
+# Check for a cpuid intrinsic
+IF(HAVE_CPUID_H)
+ CHECK_C_SOURCE_COMPILES("#include <cpuid.h>
+ int main()
+ {
+ unsigned int eax, ebx, ecx, edx;
+ return __get_cpuid(0, &eax, &ebx, &ecx, &edx);
+ }" HAVE_GCC_GET_CPUID)
+ENDIF()
+IF(HAVE_GCC_GET_CPUID)
+ SET(CMAKE_C_FLAGS "-DHAVE_GCC_GET_CPUID")
+ENDIF(HAVE_GCC_GET_CPUID)
+
FIND_PACKAGE(SSE)
IF(C_SSE2_FOUND)
SET(CMAKE_C_FLAGS "${C_SSE2_FLAGS} -DUSE_SSE2 ${CMAKE_C_FLAGS}")
diff --git a/lib/TH/generic/simd/simd.h b/lib/TH/generic/simd/simd.h
index caf671e..ee53e2c 100644
--- a/lib/TH/generic/simd/simd.h
+++ b/lib/TH/generic/simd/simd.h
@@ -2,8 +2,10 @@
#define TH_SIMD_INC
#include <stdint.h>
-#ifdef _MSC_VER
+#if defined(_MSC_VER)
#include <intrin.h>
+#elif defined(HAVE_GCC_GET_CPUID)
+#include <cpuid.h>
#endif
// Can be found on Intel ISA Reference for CPUID
@@ -90,7 +92,17 @@ static inline uint32_t detectHostSIMDExtensions()
#else // x86
static inline void cpuid(uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
{
-#ifndef _MSC_VER
+#if defined(_MSC_VER)
+ uint32_t cpuInfo[4];
+ __cpuid(cpuInfo, *eax);
+ *eax = cpuInfo[0];
+ *ebx = cpuInfo[1];
+ *ecx = cpuInfo[2];
+ *edx = cpuInfo[3];
+#elif defined(HAVE_GCC_GET_CPUID)
+ uint32_t level = *eax;
+ __get_cpuid (level, eax, ebx, ecx, edx);
+#else
uint32_t a = *eax, b, c, d;
asm volatile ( "cpuid\n\t"
: "+a"(a), "=b"(b), "=c"(c), "=d"(d) );
@@ -98,13 +110,6 @@ static inline void cpuid(uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *
*ebx = b;
*ecx = c;
*edx = d;
-#else
- uint32_t cpuInfo[4];
- __cpuid(cpuInfo, *eax);
- *eax = cpuInfo[0];
- *ebx = cpuInfo[1];
- *ecx = cpuInfo[2];
- *edx = cpuInfo[3];
#endif
}