1 /*****************************************************************************
2 * osdep.h: platform-specific code
3 *****************************************************************************
4 * Copyright (C) 2007-2013 x264 project
6 * Authors: Loren Merritt <lorenm@u.washington.edu>
7 * Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
23 * This program is also available under a commercial proprietary license.
24 * For more information, contact us at licensing@x264.com.
25 *****************************************************************************/
30 #define _LARGEFILE_SOURCE 1
31 #define _FILE_OFFSET_BITS 64
39 #define log2f(x) (logf(x)/0.693147180559945f)
40 #define log2(x) (log(x)/0.693147180559945)
44 #include <io.h> // _setmode()
45 #include <fcntl.h> // _O_BINARY
49 #define inline __inline
50 #define strcasecmp _stricmp
51 #define strncasecmp _strnicmp
52 #define snprintf _snprintf
53 #define strtok_r strtok_s
54 #define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
57 #ifdef __INTEL_COMPILER
63 #if (defined(__GNUC__) || defined(__INTEL_COMPILER)) && (ARCH_X86 || ARCH_X86_64)
64 #define HAVE_X86_INLINE_ASM 1
67 #if !defined(isfinite) && (SYS_OPENBSD || SYS_SunOS)
68 #define isfinite finite
71 #define rename(src,dst) (unlink(dst), rename(src,dst)) // POSIX says that rename() removes the destination, but win32 doesn't.
73 #define strtok_r(str,delim,save) strtok(str,delim)
78 #define DECLARE_ALIGNED( var, n ) __declspec(align(n)) var
80 #define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
82 #define ALIGNED_32( var ) DECLARE_ALIGNED( var, 32 )
83 #define ALIGNED_16( var ) DECLARE_ALIGNED( var, 16 )
84 #define ALIGNED_8( var ) DECLARE_ALIGNED( var, 8 )
85 #define ALIGNED_4( var ) DECLARE_ALIGNED( var, 4 )
87 // ARM compiliers don't reliably align stack variables
88 // - EABI requires only 8 byte stack alignment to be maintained
89 // - gcc can't align stack variables to more even if the stack were to be correctly aligned outside the function
90 // - armcc can't either, but is nice enough to actually tell you so
91 // - Apple gcc only maintains 4 byte alignment
92 // - llvm can align the stack, but only in svn and (unrelated) it exposes bugs in all released GNU binutils...
94 #define ALIGNED_ARRAY_EMU( mask, type, name, sub1, ... )\
95 uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + mask]; \
96 type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+mask) & ~mask)
98 #if ARCH_ARM && SYS_MACOSX
99 #define ALIGNED_ARRAY_8( ... ) ALIGNED_ARRAY_EMU( 7, __VA_ARGS__ )
101 #define ALIGNED_ARRAY_8( type, name, sub1, ... )\
102 ALIGNED_8( type name sub1 __VA_ARGS__ )
106 #define ALIGNED_ARRAY_16( ... ) ALIGNED_ARRAY_EMU( 15, __VA_ARGS__ )
108 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
109 ALIGNED_16( type name sub1 __VA_ARGS__ )
114 #define ALIGNED_ARRAY_32( ... ) EXPAND( ALIGNED_ARRAY_EMU( 31, __VA_ARGS__ ) )
115 #define ALIGNED_ARRAY_64( ... ) EXPAND( ALIGNED_ARRAY_EMU( 63, __VA_ARGS__ ) )
118 #if ARCH_X86 || ARCH_X86_64
119 #define NATIVE_ALIGN 32
120 #define ALIGNED_N ALIGNED_32
121 #define ALIGNED_ARRAY_N ALIGNED_ARRAY_32
123 #define NATIVE_ALIGN 16
124 #define ALIGNED_N ALIGNED_16
125 #define ALIGNED_ARRAY_N ALIGNED_ARRAY_16
128 #define UNINIT(x) x=x
130 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
131 #define UNUSED __attribute__((unused))
132 #define ALWAYS_INLINE __attribute__((always_inline)) inline
133 #define NOINLINE __attribute__((noinline))
134 #define MAY_ALIAS __attribute__((may_alias))
135 #define x264_constant_p(x) __builtin_constant_p(x)
136 #define x264_nonconstant_p(x) (!__builtin_constant_p(x))
139 #define ALWAYS_INLINE __forceinline
140 #define NOINLINE __declspec(noinline)
142 #define ALWAYS_INLINE inline
147 #define x264_constant_p(x) 0
148 #define x264_nonconstant_p(x) 0
153 #include <kernel/OS.h>
154 #define x264_pthread_t thread_id
155 static inline int x264_pthread_create( x264_pthread_t *t, void *a, void *(*f)(void *), void *d )
157 *t = spawn_thread( f, "", 10, d );
158 if( *t < B_NO_ERROR )
163 #define x264_pthread_join(t,s) { long tmp; \
164 wait_for_thread(t,(s)?(long*)(s):&tmp); }
166 #elif HAVE_POSIXTHREAD
168 #define x264_pthread_t pthread_t
169 #define x264_pthread_create pthread_create
170 #define x264_pthread_join pthread_join
171 #define x264_pthread_mutex_t pthread_mutex_t
172 #define x264_pthread_mutex_init pthread_mutex_init
173 #define x264_pthread_mutex_destroy pthread_mutex_destroy
174 #define x264_pthread_mutex_lock pthread_mutex_lock
175 #define x264_pthread_mutex_unlock pthread_mutex_unlock
176 #define x264_pthread_cond_t pthread_cond_t
177 #define x264_pthread_cond_init pthread_cond_init
178 #define x264_pthread_cond_destroy pthread_cond_destroy
179 #define x264_pthread_cond_broadcast pthread_cond_broadcast
180 #define x264_pthread_cond_wait pthread_cond_wait
181 #define x264_pthread_attr_t pthread_attr_t
182 #define x264_pthread_attr_init pthread_attr_init
183 #define x264_pthread_attr_destroy pthread_attr_destroy
184 #define x264_pthread_num_processors_np pthread_num_processors_np
185 #define X264_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
187 #elif HAVE_WIN32THREAD
188 #include "win32thread.h"
191 #define x264_pthread_t int
192 #define x264_pthread_create(t,u,f,d) 0
193 #define x264_pthread_join(t,s)
194 #endif //HAVE_*THREAD
196 #if !HAVE_POSIXTHREAD && !HAVE_WIN32THREAD
197 #define x264_pthread_mutex_t int
198 #define x264_pthread_mutex_init(m,f) 0
199 #define x264_pthread_mutex_destroy(m)
200 #define x264_pthread_mutex_lock(m)
201 #define x264_pthread_mutex_unlock(m)
202 #define x264_pthread_cond_t int
203 #define x264_pthread_cond_init(c,f) 0
204 #define x264_pthread_cond_destroy(c)
205 #define x264_pthread_cond_broadcast(c)
206 #define x264_pthread_cond_wait(c,m)
207 #define x264_pthread_attr_t int
208 #define x264_pthread_attr_init(a) 0
209 #define x264_pthread_attr_destroy(a)
210 #define X264_PTHREAD_MUTEX_INITIALIZER 0
213 #if HAVE_WIN32THREAD || PTW32_STATIC_LIB
214 int x264_threading_init( void );
216 #define x264_threading_init() 0
219 static ALWAYS_INLINE int x264_pthread_fetch_and_add( int *val, int add, x264_pthread_mutex_t *mutex )
222 #if defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ > 0) && ARCH_X86
223 return __sync_fetch_and_add( val, add );
225 x264_pthread_mutex_lock( mutex );
228 x264_pthread_mutex_unlock( mutex );
238 #define WORD_SIZE sizeof(void*)
243 #define endian_fix(x) (x)
244 #define endian_fix64(x) (x)
245 #define endian_fix32(x) (x)
246 #define endian_fix16(x) (x)
248 #if HAVE_X86_INLINE_ASM && HAVE_MMX
249 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
251 asm("bswap %0":"+r"(x));
254 #elif defined(__GNUC__) && HAVE_ARMV6
255 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
257 asm("rev %0, %0":"+r"(x));
261 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
263 return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
266 #if HAVE_X86_INLINE_ASM && ARCH_X86_64
267 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
269 asm("bswap %0":"+r"(x));
273 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
275 return endian_fix32(x>>32) + ((uint64_t)endian_fix32(x)<<32);
278 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
280 return WORD_SIZE == 8 ? endian_fix64(x) : endian_fix32(x);
282 static ALWAYS_INLINE uint16_t endian_fix16( uint16_t x )
284 return (x<<8)|(x>>8);
288 /* For values with 4 bits or less. */
289 static int ALWAYS_INLINE x264_ctz_4bit( uint32_t x )
291 static uint8_t lut[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
295 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 3)
296 #define x264_clz(x) __builtin_clz(x)
297 #define x264_ctz(x) __builtin_ctz(x)
299 static int ALWAYS_INLINE x264_clz( uint32_t x )
301 static uint8_t lut[16] = {4,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
302 int y, z = (((x >> 16) - 1) >> 27) & 16;
304 z += y = ((x - 0x100) >> 28) & 8;
306 z += y = ((x - 0x10) >> 29) & 4;
311 static int ALWAYS_INLINE x264_ctz( uint32_t x )
313 static uint8_t lut[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
314 int y, z = (((x & 0xffff) - 1) >> 27) & 16;
316 z += y = (((x & 0xff) - 1) >> 28) & 8;
318 z += y = (((x & 0xf) - 1) >> 29) & 4;
320 return z + lut[x&0xf];
324 #if HAVE_X86_INLINE_ASM && HAVE_MMX
325 /* Don't use __builtin_prefetch; even as recent as 4.3.4, GCC seems incapable of
326 * using complex address modes properly unless we use inline asm. */
327 static ALWAYS_INLINE void x264_prefetch( void *p )
329 asm volatile( "prefetcht0 %0"::"m"(*(uint8_t*)p) );
331 /* We require that prefetch not fault on invalid reads, so we only enable it on
332 * known architectures. */
333 #elif defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 1) &&\
334 (ARCH_X86 || ARCH_X86_64 || ARCH_ARM || ARCH_PPC)
335 #define x264_prefetch(x) __builtin_prefetch(x)
337 #define x264_prefetch(x)
342 #define x264_lower_thread_priority(p)\
344 x264_pthread_t handle = pthread_self();\
345 struct sched_param sp;\
346 int policy = SCHED_OTHER;\
347 pthread_getschedparam( handle, &policy, &sp );\
348 sp.sched_priority -= p;\
349 pthread_setschedparam( handle, policy, &sp );\
353 #define x264_lower_thread_priority(p) { UNUSED int nice_ret = nice(p); }
354 #endif /* SYS_WINDOWS */
355 #elif HAVE_WIN32THREAD
356 #define x264_lower_thread_priority(p) SetThreadPriority( GetCurrentThread(), X264_MAX( -2, -p ) )
358 #define x264_lower_thread_priority(p)
361 static inline uint8_t x264_is_regular_file( FILE *filehandle )
363 struct stat file_stat;
364 if( fstat( fileno( filehandle ), &file_stat ) )
366 return S_ISREG( file_stat.st_mode );
369 static inline uint8_t x264_is_regular_file_path( const char *filename )
371 struct stat file_stat;
372 if( stat( filename, &file_stat ) )
374 return S_ISREG( file_stat.st_mode );
377 #endif /* X264_OSDEP_H */