]> git.sesse.net Git - vlc/blobdiff - src/misc/cpu.c
"#if HAVE_FOO" -> "#ifdef HAVE_FOO"
[vlc] / src / misc / cpu.c
index 3640865e9ccaa6d4c8619054c6cf0883fcdc0934..fc6e437ed6dda7c176e54dff259a9a2cc3f5ba0d 100644 (file)
@@ -40,9 +40,6 @@
 #include <signal.h>
 #else
 #include <errno.h>
-#include <windows.h>
-#include <winbase.h>
-#define PF_SSE3_INSTRUCTIONS_AVAILABLE 13
 #endif
 
 #include "libvlc.h"
@@ -88,9 +85,7 @@ static bool check_OS_capability( const char *psz_capability, pid_t pid )
 
 # else /* WIN32 */
 #  define check_capability(name, flag, code)   \
-     do {                                      \
-        i_capabilities |= (flag);              \
-     } while(0)
+        i_capabilities |= (flag);
 # endif
 #endif
 
@@ -172,12 +167,12 @@ uint32_t CPUCapabilities( void )
 
     /* test for the MMX flag */
     cpuid( 0x00000001 );
-
 # if !defined (__MMX__)
     if( ! (i_edx & 0x00800000) )
         goto out;
 # endif
     i_capabilities |= CPU_CAPABILITY_MMX;
+
 # if defined (__SSE__)
     i_capabilities |= CPU_CAPABILITY_MMXEXT | CPU_CAPABILITY_SSE;
 # else
@@ -186,13 +181,8 @@ uint32_t CPUCapabilities( void )
         i_capabilities |= CPU_CAPABILITY_MMXEXT;
 
 #   ifdef CAN_COMPILE_SSE
-#   ifdef WIN32
-        if( IsProcessorFeaturePresent( PF_XMMI_INSTRUCTIONS_AVAILABLE ) )
-            i_capabilities |= CPU_CAPABILITY_SSE;
-#   else
         check_capability( "SSE", CPU_CAPABILITY_SSE,
                           "xorps %%xmm0,%%xmm0\n" );
-#   endif
 #   endif
     }
 # endif
@@ -201,66 +191,40 @@ uint32_t CPUCapabilities( void )
     i_capabilities |= CPU_CAPABILITY_SSE2;
 # elif defined (CAN_COMPILE_SSE2)
     if( i_edx & 0x04000000 )
-    {
-#   ifdef WIN32
-        if( IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE ) )
-            i_capabilities |= CPU_CAPABILITY_SSE2;
-#   else
         check_capability( "SSE2", CPU_CAPABILITY_SSE2,
                           "movupd %%xmm0, %%xmm0\n" );
-#   endif
-    }
 # endif
 
 # if defined (__SSE3__)
     i_capabilities |= CPU_CAPABILITY_SSE3;
 # elif defined (CAN_COMPILE_SSE3)
     if( i_ecx & 0x00000001 )
-    {
-#   ifdef WIN32
-        if( IsProcessorFeaturePresent( PF_SSE3_INSTRUCTIONS_AVAILABLE ) )
-            i_capabilities |= CPU_CAPABILITY_SSE3;
-#   else
         check_capability( "SSE3", CPU_CAPABILITY_SSE3,
                           "movsldup %%xmm1, %%xmm0\n" );
-#   endif
-    }
 # endif
 
 # if defined (__SSSE3__)
     i_capabilities |= CPU_CAPABILITY_SSSE3;
 # elif defined (CAN_COMPILE_SSSE3)
-#   ifdef WIN32
-    /* FIXME: IsProcessorFeaturePresent can't check for SSSE3 */
-#   else
     if( i_ecx & 0x00000200 )
         check_capability( "SSSE3", CPU_CAPABILITY_SSSE3,
                           "pabsw %%xmm1, %%xmm0\n" );
-#   endif
 # endif
 
 # if defined (__SSE4_1__)
     i_capabilities |= CPU_CAPABILITY_SSE4_1;
 # elif defined (CAN_COMPILE_SSE4_1)
-#   ifdef WIN32
-    /* FIXME: IsProcessorFeaturePresent can't check for SSE4.1 */
-#   else
     if( i_ecx & 0x00080000 )
         check_capability( "SSE4.1", CPU_CAPABILITY_SSE4_1,
                           "pmaxsb %%xmm1, %%xmm0\n" );
-#   endif
 # endif
 
 # if defined (__SSE4_2__)
     i_capabilities |= CPU_CAPABILITY_SSE4_2;
 # elif defined (CAN_COMPILE_SSE4_2)
-#   ifdef WIN32
-    /* FIXME: IsProcessorFeaturePresent can't check for SSE4.2 */
-#   else
     if( i_ecx & 0x00100000 )
         check_capability( "SSE4.2", CPU_CAPABILITY_SSE4_2,
                           "pcmpgtq %%xmm1, %%xmm0\n" );
-#   endif
 # endif
 
     /* test for additional capabilities */
@@ -275,17 +239,9 @@ uint32_t CPUCapabilities( void )
 # if defined (__3dNOW__)
     i_capabilities |= CPU_CAPABILITY_3DNOW;
 # elif defined (CAN_COMPILE_3DNOW)
-
     if( i_edx & 0x80000000 )
-    {
-#   ifdef WIN32
-        if( IsProcessorFeaturePresent( PF_3DNOW_INSTRUCTIONS_AVAILABLE ) )
-            i_capabilities |= CPU_CAPABILITY_3DNOW;
-#   else
         check_capability( "3D Now!", CPU_CAPABILITY_3DNOW,
                           "pfadd %%mm0,%%mm0\n" "femms\n" );
-#   endif
-    }
 # endif
 
     if( b_amd && ( i_edx & 0x00400000 ) )
@@ -355,13 +311,51 @@ const struct
     { CPU_CAPABILITY_SSE,     "sse" },
 #endif
 #if defined (__ppc__) || defined (__ppc64__) || defined (__powerpc__)
-    { CPU_CAPABILITY_ALTIVEC, "altivec" },
+    { CPU_CAPABILITY_ALTIVEC, "altivec" },
 #endif
 #if defined (__arm__)
     { CPU_CAPABILITY_NEON,    "arm_neon" },
 #endif
 };
 
+/**
+ * Return the number of available logical CPU.
+ */
+unsigned vlc_GetCPUCount(void)
+{
+#if defined(WIN32) && !defined(UNDER_CE)
+    DWORD process_mask;
+    DWORD system_mask;
+    if (!GetProcessAffinityMask(GetCurrentProcess(), &process_mask, &system_mask))
+        return 1;
+
+    unsigned count = 0;
+    while (system_mask) {
+        count++;
+        system_mask >>= 1;
+    }
+    return count;
+#elif defined(HAVE_SCHED_GETAFFINITY)
+    cpu_set_t cpu;
+    CPU_ZERO(&cpu);
+    if (sched_getaffinity(0, sizeof(cpu), &cpu) < 0)
+        return 1;
+    unsigned count = 0;
+    for (unsigned i = 0; i < CPU_SETSIZE; i++)
+        count += CPU_ISSET(i, &cpu) != 0;
+    return count;
+#elif defined(__APPLE__)
+    int count;
+    size_t size = sizeof(count) ;
+    if (sysctlbyname("hw.ncpu", &count, &size, NULL, 0))
+        return 1; /* Failure */
+    return count;
+#else
+#   warning "vlc_GetCPUCount is not implemented for your platform"
+    return 1;
+#endif
+}
+
 /**
  * Check if a directory name contains usable plugins w.r.t. the hardware
  * capabilities. Loading a plugin when the hardware has insufficient