+#if HAVE_WIN32THREAD || PTW32_STATIC_LIB
+int x264_threading_init( void );
+#else
+#define x264_threading_init() 0
+#endif
+
+static ALWAYS_INLINE int x264_pthread_fetch_and_add( int *val, int add, x264_pthread_mutex_t *mutex )
+{
+#if HAVE_THREAD
+#if defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ > 0) && ARCH_X86
+ return __sync_fetch_and_add( val, add );
+#else
+ x264_pthread_mutex_lock( mutex );
+ int res = *val;
+ *val += add;
+ x264_pthread_mutex_unlock( mutex );
+ return res;
+#endif
+#else
+ int res = *val;
+ *val += add;
+ return res;
+#endif
+}
+
+#define WORD_SIZE sizeof(void*)
+
+#define asm __asm__
+
+#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 HAVE_X86_INLINE_ASM && HAVE_MMX
+static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
+{
+ asm("bswap %0":"+r"(x));
+ return x;
+}
+#elif defined(__GNUC__) && HAVE_ARMV6
+static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
+{
+ asm("rev %0, %0":"+r"(x));
+ return x;
+}
+#else
+static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
+{
+ return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
+}
+#endif
+#if HAVE_X86_INLINE_ASM && ARCH_X86_64
+static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
+{
+ asm("bswap %0":"+r"(x));
+ return x;
+}
+#else
+static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
+{
+ return endian_fix32(x>>32) + ((uint64_t)endian_fix32(x)<<32);
+}
+#endif
+static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
+{
+ return WORD_SIZE == 8 ? endian_fix64(x) : endian_fix32(x);
+}
+static ALWAYS_INLINE uint16_t endian_fix16( uint16_t x )
+{
+ return (x<<8)|(x>>8);
+}
+#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)
+#else
+static int ALWAYS_INLINE x264_clz( uint32_t x )
+{
+ static uint8_t lut[16] = {4,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
+ int y, z = (((x >> 16) - 1) >> 27) & 16;
+ x >>= z^16;
+ z += y = ((x - 0x100) >> 28) & 8;
+ x >>= y^8;
+ z += y = ((x - 0x10) >> 29) & 4;
+ x >>= y^4;
+ return z + lut[x];
+}
+
+static int ALWAYS_INLINE x264_ctz( uint32_t x )
+{
+ static uint8_t lut[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
+ int y, z = (((x & 0xffff) - 1) >> 27) & 16;
+ x >>= z;
+ z += y = (((x & 0xff) - 1) >> 28) & 8;
+ x >>= y;
+ z += y = (((x & 0xf) - 1) >> 29) & 4;
+ x >>= y;
+ return z + lut[x&0xf];
+}
+#endif
+
+#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 )
+{
+ asm volatile( "prefetcht0 %0"::"m"(*(uint8_t*)p) );
+}
+/* We require that prefetch not fault on invalid reads, so we only enable it on
+ * known architectures. */
+#elif defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 1) &&\
+ (ARCH_X86 || ARCH_X86_64 || ARCH_ARM || ARCH_PPC)
+#define x264_prefetch(x) __builtin_prefetch(x)
+#else
+#define x264_prefetch(x)
+#endif
+
+#if HAVE_POSIXTHREAD
+#if SYS_WINDOWS
+#define x264_lower_thread_priority(p)\
+{\
+ x264_pthread_t handle = pthread_self();\
+ struct sched_param sp;\
+ int policy = SCHED_OTHER;\
+ pthread_getschedparam( handle, &policy, &sp );\
+ sp.sched_priority -= p;\
+ pthread_setschedparam( handle, policy, &sp );\
+}
+#else
+#include <unistd.h>
+#define x264_lower_thread_priority(p) { UNUSED int nice_ret = nice(p); }
+#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
+
+static inline int x264_is_regular_file( FILE *filehandle )
+{
+ x264_struct_stat file_stat;
+ if( x264_fstat( fileno( filehandle ), &file_stat ) )
+ return 1;
+ return S_ISREG( file_stat.st_mode );
+}
+
+static inline int x264_is_regular_file_path( const char *filename )
+{
+ x264_struct_stat file_stat;
+ if( x264_stat( filename, &file_stat ) )
+ return !x264_is_pipe( filename );
+ return S_ISREG( file_stat.st_mode );
+}
+
+#endif /* X264_OSDEP_H */