/*****************************************************************************
* osdep.h: platform-specific code
*****************************************************************************
- * Copyright (C) 2007-2011 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)
#else
#define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
#endif
+#define ALIGNED_32( var ) DECLARE_ALIGNED( var, 32 )
#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__ ) )
+
+/* For AVX2 */
+#if ARCH_X86 || ARCH_X86_64
+#define NATIVE_ALIGN 32
+#define ALIGNED_N ALIGNED_32
+#define ALIGNED_ARRAY_N ALIGNED_ARRAY_32
+#else
+#define NATIVE_ALIGN 16
+#define ALIGNED_N ALIGNED_16
+#define ALIGNED_ARRAY_N ALIGNED_ARRAY_16
+#endif
+
#define UNINIT(x) x=x
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
return 0;
}
#define x264_pthread_join(t,s) { long tmp; \
- wait_for_thread(t,(s)?(long*)(*(s)):&tmp); }
+ wait_for_thread(t,(s)?(long*)(s):&tmp); }
#elif HAVE_POSIXTHREAD
#include <pthread.h>
#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__
}
#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)