]> git.sesse.net Git - x264/blobdiff - common/osdep.h
x86inc: fix some corner cases of SWAP
[x264] / common / osdep.h
index fbe8e96cc7fedd160b7d778b692683ad6c809bdd..1e17c6a176c45d68cc7911d2e51b5dd57f5a681b 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * osdep.h: platform-specific code
  *****************************************************************************
- * Copyright (C) 2007-2010 x264 project
+ * Copyright (C) 2007-2013 x264 project
  *
  * Authors: Loren Merritt <lorenm@u.washington.edu>
  *          Laurent Aimar <fenrir@via.ecp.fr>
 #define _FILE_OFFSET_BITS 64
 #include <stdio.h>
 #include <sys/stat.h>
+#include <inttypes.h>
 
 #include "config.h"
 
-#if HAVE_STDINT_H
-#include <stdint.h>
-#else
-#include <inttypes.h>
-#endif
-
 #if !HAVE_LOG2F
 #define log2f(x) (logf(x)/0.693147180559945f)
 #define log2(x) (log(x)/0.693147180559945)
 #include <fcntl.h> // _O_BINARY
 #endif
 
+#ifdef __ICL
+#define inline __inline
+#define strcasecmp _stricmp
+#define strncasecmp _strnicmp
+#define snprintf _snprintf
+#define strtok_r strtok_s
+#define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
+#endif
+
+#ifdef __INTEL_COMPILER
+#include <mathimf.h>
+#else
+#include <math.h>
+#endif
+
+#if (defined(__GNUC__) || defined(__INTEL_COMPILER)) && (ARCH_X86 || ARCH_X86_64)
+#define HAVE_X86_INLINE_ASM 1
+#endif
+
 #if !defined(isfinite) && (SYS_OPENBSD || SYS_SunOS)
 #define isfinite finite
 #endif
 #endif
 #endif
 
+#ifdef __ICL
+#define DECLARE_ALIGNED( var, n ) __declspec(align(n)) var
+#else
 #define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
+#endif
 #define ALIGNED_16( var ) DECLARE_ALIGNED( var, 16 )
 #define ALIGNED_8( var )  DECLARE_ALIGNED( var, 8 )
 #define ALIGNED_4( var )  DECLARE_ALIGNED( var, 4 )
 // - armcc can't either, but is nice enough to actually tell you so
 // - Apple gcc only maintains 4 byte alignment
 // - llvm can align the stack, but only in svn and (unrelated) it exposes bugs in all released GNU binutils...
+
+#define ALIGNED_ARRAY_EMU( mask, type, name, sub1, ... )\
+    uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + mask]; \
+    type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+mask) & ~mask)
+
 #if ARCH_ARM && SYS_MACOSX
-#define ALIGNED_ARRAY_8( type, name, sub1, ... )\
-    uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + 7]; \
-    type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+7) & ~7)
+#define ALIGNED_ARRAY_8( ... ) ALIGNED_ARRAY_EMU( 7, __VA_ARGS__ )
 #else
 #define ALIGNED_ARRAY_8( type, name, sub1, ... )\
     ALIGNED_8( type name sub1 __VA_ARGS__ )
 #endif
 
 #if ARCH_ARM
-#define ALIGNED_ARRAY_16( type, name, sub1, ... )\
-    uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + 15];\
-    type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+15) & ~15)
+#define ALIGNED_ARRAY_16( ... ) ALIGNED_ARRAY_EMU( 15, __VA_ARGS__ )
 #else
 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
     ALIGNED_16( type name sub1 __VA_ARGS__ )
 #endif
 
+#define EXPAND(x) x
+
+#define ALIGNED_ARRAY_32( ... ) EXPAND( ALIGNED_ARRAY_EMU( 31, __VA_ARGS__ ) )
+#define ALIGNED_ARRAY_64( ... ) EXPAND( ALIGNED_ARRAY_EMU( 63, __VA_ARGS__ ) )
+
+#define UNINIT(x) x=x
+
 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
 #define UNUSED __attribute__((unused))
 #define ALWAYS_INLINE __attribute__((always_inline)) inline
 #define x264_constant_p(x) __builtin_constant_p(x)
 #define x264_nonconstant_p(x) (!__builtin_constant_p(x))
 #else
-#define UNUSED
+#ifdef __ICL
+#define ALWAYS_INLINE __forceinline
+#define NOINLINE __declspec(noinline)
+#else
 #define ALWAYS_INLINE inline
 #define NOINLINE
+#endif
+#define UNUSED
 #define MAY_ALIAS
 #define x264_constant_p(x) 0
 #define x264_nonconstant_p(x) 0
 #endif
 
 /* threads */
-#if SYS_BEOS
+#if HAVE_BEOSTHREAD
 #include <kernel/OS.h>
 #define x264_pthread_t               thread_id
 static inline int x264_pthread_create( x264_pthread_t *t, void *a, void *(*f)(void *), void *d )
@@ -118,23 +149,10 @@ static inline int x264_pthread_create( x264_pthread_t *t, void *a, void *(*f)(vo
      return 0;
 }
 #define x264_pthread_join(t,s)       { long tmp; \
-                                       wait_for_thread(t,(s)?(long*)(*(s)):&tmp); }
-#ifndef usleep
-#define usleep(t)                    snooze(t)
-#endif
-#define HAVE_PTHREAD 1
+                                       wait_for_thread(t,(s)?(long*)(s):&tmp); }
 
-#elif HAVE_PTHREAD
+#elif HAVE_POSIXTHREAD
 #include <pthread.h>
-#define USE_REAL_PTHREAD 1
-
-#else
-#define x264_pthread_t               int
-#define x264_pthread_create(t,u,f,d) 0
-#define x264_pthread_join(t,s)
-#endif //SYS_*
-
-#if USE_REAL_PTHREAD
 #define x264_pthread_t               pthread_t
 #define x264_pthread_create          pthread_create
 #define x264_pthread_join            pthread_join
@@ -151,8 +169,19 @@ static inline int x264_pthread_create( x264_pthread_t *t, void *a, void *(*f)(vo
 #define x264_pthread_attr_t          pthread_attr_t
 #define x264_pthread_attr_init       pthread_attr_init
 #define x264_pthread_attr_destroy    pthread_attr_destroy
+#define x264_pthread_num_processors_np pthread_num_processors_np
 #define X264_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
+
+#elif HAVE_WIN32THREAD
+#include "win32thread.h"
+
 #else
+#define x264_pthread_t               int
+#define x264_pthread_create(t,u,f,d) 0
+#define x264_pthread_join(t,s)
+#endif //HAVE_*THREAD
+
+#if !HAVE_POSIXTHREAD && !HAVE_WIN32THREAD
 #define x264_pthread_mutex_t         int
 #define x264_pthread_mutex_init(m,f) 0
 #define x264_pthread_mutex_destroy(m)
@@ -169,23 +198,23 @@ static inline int x264_pthread_create( x264_pthread_t *t, void *a, void *(*f)(vo
 #define X264_PTHREAD_MUTEX_INITIALIZER 0
 #endif
 
+#if HAVE_WIN32THREAD || PTW32_STATIC_LIB
+int x264_threading_init( void );
+#else
+#define x264_threading_init() 0
+#endif
+
 #define WORD_SIZE sizeof(void*)
 
 #define asm __asm__
 
-#if !defined(_WIN64) && !defined(__LP64__)
-#if defined(__INTEL_COMPILER)
-#define BROKEN_STACK_ALIGNMENT 1 /* define it if stack is not mod16 */
-#endif
-#endif
-
 #if WORDS_BIGENDIAN
 #define endian_fix(x) (x)
 #define endian_fix64(x) (x)
 #define endian_fix32(x) (x)
 #define endian_fix16(x) (x)
 #else
-#if defined(__GNUC__) && HAVE_MMX
+#if HAVE_X86_INLINE_ASM && HAVE_MMX
 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
 {
     asm("bswap %0":"+r"(x));
@@ -203,7 +232,7 @@ static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
     return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
 }
 #endif
-#if defined(__GNUC__) && ARCH_X86_64
+#if HAVE_X86_INLINE_ASM && ARCH_X86_64
 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
 {
     asm("bswap %0":"+r"(x));
@@ -225,6 +254,13 @@ static ALWAYS_INLINE uint16_t endian_fix16( uint16_t x )
 }
 #endif
 
+/* For values with 4 bits or less. */
+static int ALWAYS_INLINE x264_ctz_4bit( uint32_t x )
+{
+    static uint8_t lut[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
+    return lut[x];
+}
+
 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 3)
 #define x264_clz(x) __builtin_clz(x)
 #define x264_ctz(x) __builtin_ctz(x)
@@ -254,7 +290,7 @@ static int ALWAYS_INLINE x264_ctz( uint32_t x )
 }
 #endif
 
-#if defined(__GNUC__) && HAVE_MMX
+#if HAVE_X86_INLINE_ASM && HAVE_MMX
 /* Don't use __builtin_prefetch; even as recent as 4.3.4, GCC seems incapable of
  * using complex address modes properly unless we use inline asm. */
 static ALWAYS_INLINE void x264_prefetch( void *p )
@@ -270,8 +306,8 @@ static ALWAYS_INLINE void x264_prefetch( void *p )
 #define x264_prefetch(x)
 #endif
 
-#if USE_REAL_PTHREAD
-#if SYS_MINGW
+#if HAVE_POSIXTHREAD
+#if SYS_WINDOWS
 #define x264_lower_thread_priority(p)\
 {\
     x264_pthread_t handle = pthread_self();\
@@ -284,7 +320,9 @@ static ALWAYS_INLINE void x264_prefetch( void *p )
 #else
 #include <unistd.h>
 #define x264_lower_thread_priority(p) { UNUSED int nice_ret = nice(p); }
-#endif /* USE_REAL_PTHREAD */
+#endif /* SYS_WINDOWS */
+#elif HAVE_WIN32THREAD
+#define x264_lower_thread_priority(p) SetThreadPriority( GetCurrentThread(), X264_MAX( -2, -p ) )
 #else
 #define x264_lower_thread_priority(p)
 #endif