1 /*****************************************************************************
2 * osdep.h: platform-specific code
3 *****************************************************************************
4 * Copyright (C) 2007-2011 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
44 #define log2f(x) (logf(x)/0.693147180559945f)
45 #define log2(x) (log(x)/0.693147180559945)
49 #include <io.h> // _setmode()
50 #include <fcntl.h> // _O_BINARY
54 #define inline __inline
55 #define strcasecmp _stricmp
56 #define strncasecmp _strnicmp
57 #define snprintf _snprintf
58 #define strtok_r strtok_s
59 #define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
62 #ifdef __INTEL_COMPILER
68 #if (defined(__GNUC__) || defined(__INTEL_COMPILER)) && (ARCH_X86 || ARCH_X86_64)
69 #define HAVE_X86_INLINE_ASM 1
72 #if !defined(isfinite) && (SYS_OPENBSD || SYS_SunOS)
73 #define isfinite finite
76 #define rename(src,dst) (unlink(dst), rename(src,dst)) // POSIX says that rename() removes the destination, but win32 doesn't.
78 #define strtok_r(str,delim,save) strtok(str,delim)
83 #define DECLARE_ALIGNED( var, n ) __declspec(align(n)) var
85 #define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
87 #define ALIGNED_16( var ) DECLARE_ALIGNED( var, 16 )
88 #define ALIGNED_8( var ) DECLARE_ALIGNED( var, 8 )
89 #define ALIGNED_4( var ) DECLARE_ALIGNED( var, 4 )
91 // ARM compiliers don't reliably align stack variables
92 // - EABI requires only 8 byte stack alignment to be maintained
93 // - gcc can't align stack variables to more even if the stack were to be correctly aligned outside the function
94 // - armcc can't either, but is nice enough to actually tell you so
95 // - Apple gcc only maintains 4 byte alignment
96 // - llvm can align the stack, but only in svn and (unrelated) it exposes bugs in all released GNU binutils...
97 #if ARCH_ARM && SYS_MACOSX
98 #define ALIGNED_ARRAY_8( type, name, sub1, ... )\
99 uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + 7]; \
100 type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+7) & ~7)
102 #define ALIGNED_ARRAY_8( type, name, sub1, ... )\
103 ALIGNED_8( type name sub1 __VA_ARGS__ )
107 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
108 uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + 15];\
109 type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+15) & ~15)
111 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
112 ALIGNED_16( type name sub1 __VA_ARGS__ )
115 #define UNINIT(x) x=x
117 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
118 #define UNUSED __attribute__((unused))
119 #define ALWAYS_INLINE __attribute__((always_inline)) inline
120 #define NOINLINE __attribute__((noinline))
121 #define MAY_ALIAS __attribute__((may_alias))
122 #define x264_constant_p(x) __builtin_constant_p(x)
123 #define x264_nonconstant_p(x) (!__builtin_constant_p(x))
126 #define ALWAYS_INLINE __forceinline
127 #define NOINLINE __declspec(noinline)
129 #define ALWAYS_INLINE inline
134 #define x264_constant_p(x) 0
135 #define x264_nonconstant_p(x) 0
140 #include <kernel/OS.h>
141 #define x264_pthread_t thread_id
142 static inline int x264_pthread_create( x264_pthread_t *t, void *a, void *(*f)(void *), void *d )
144 *t = spawn_thread( f, "", 10, d );
145 if( *t < B_NO_ERROR )
150 #define x264_pthread_join(t,s) { long tmp; \
151 wait_for_thread(t,(s)?(long*)(*(s)):&tmp); }
153 #elif HAVE_POSIXTHREAD
155 #define x264_pthread_t pthread_t
156 #define x264_pthread_create pthread_create
157 #define x264_pthread_join pthread_join
158 #define x264_pthread_mutex_t pthread_mutex_t
159 #define x264_pthread_mutex_init pthread_mutex_init
160 #define x264_pthread_mutex_destroy pthread_mutex_destroy
161 #define x264_pthread_mutex_lock pthread_mutex_lock
162 #define x264_pthread_mutex_unlock pthread_mutex_unlock
163 #define x264_pthread_cond_t pthread_cond_t
164 #define x264_pthread_cond_init pthread_cond_init
165 #define x264_pthread_cond_destroy pthread_cond_destroy
166 #define x264_pthread_cond_broadcast pthread_cond_broadcast
167 #define x264_pthread_cond_wait pthread_cond_wait
168 #define x264_pthread_attr_t pthread_attr_t
169 #define x264_pthread_attr_init pthread_attr_init
170 #define x264_pthread_attr_destroy pthread_attr_destroy
171 #define x264_pthread_num_processors_np pthread_num_processors_np
172 #define X264_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
174 #elif HAVE_WIN32THREAD
175 #include "win32thread.h"
178 #define x264_pthread_t int
179 #define x264_pthread_create(t,u,f,d) 0
180 #define x264_pthread_join(t,s)
181 #endif //HAVE_*THREAD
183 #if !HAVE_POSIXTHREAD && !HAVE_WIN32THREAD
184 #define x264_pthread_mutex_t int
185 #define x264_pthread_mutex_init(m,f) 0
186 #define x264_pthread_mutex_destroy(m)
187 #define x264_pthread_mutex_lock(m)
188 #define x264_pthread_mutex_unlock(m)
189 #define x264_pthread_cond_t int
190 #define x264_pthread_cond_init(c,f) 0
191 #define x264_pthread_cond_destroy(c)
192 #define x264_pthread_cond_broadcast(c)
193 #define x264_pthread_cond_wait(c,m)
194 #define x264_pthread_attr_t int
195 #define x264_pthread_attr_init(a) 0
196 #define x264_pthread_attr_destroy(a)
197 #define X264_PTHREAD_MUTEX_INITIALIZER 0
200 #if HAVE_WIN32THREAD || PTW32_STATIC_LIB
201 int x264_threading_init( void );
203 #define x264_threading_init() 0
206 #define WORD_SIZE sizeof(void*)
211 #define endian_fix(x) (x)
212 #define endian_fix64(x) (x)
213 #define endian_fix32(x) (x)
214 #define endian_fix16(x) (x)
216 #if HAVE_X86_INLINE_ASM && HAVE_MMX
217 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
219 asm("bswap %0":"+r"(x));
222 #elif defined(__GNUC__) && HAVE_ARMV6
223 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
225 asm("rev %0, %0":"+r"(x));
229 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
231 return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
234 #if HAVE_X86_INLINE_ASM && ARCH_X86_64
235 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
237 asm("bswap %0":"+r"(x));
241 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
243 return endian_fix32(x>>32) + ((uint64_t)endian_fix32(x)<<32);
246 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
248 return WORD_SIZE == 8 ? endian_fix64(x) : endian_fix32(x);
250 static ALWAYS_INLINE uint16_t endian_fix16( uint16_t x )
252 return (x<<8)|(x>>8);
256 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 3)
257 #define x264_clz(x) __builtin_clz(x)
258 #define x264_ctz(x) __builtin_ctz(x)
260 static int ALWAYS_INLINE x264_clz( uint32_t x )
262 static uint8_t lut[16] = {4,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
263 int y, z = (((x >> 16) - 1) >> 27) & 16;
265 z += y = ((x - 0x100) >> 28) & 8;
267 z += y = ((x - 0x10) >> 29) & 4;
272 static int ALWAYS_INLINE x264_ctz( uint32_t x )
274 static uint8_t lut[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
275 int y, z = (((x & 0xffff) - 1) >> 27) & 16;
277 z += y = (((x & 0xff) - 1) >> 28) & 8;
279 z += y = (((x & 0xf) - 1) >> 29) & 4;
281 return z + lut[x&0xf];
285 #if HAVE_X86_INLINE_ASM && HAVE_MMX
286 /* Don't use __builtin_prefetch; even as recent as 4.3.4, GCC seems incapable of
287 * using complex address modes properly unless we use inline asm. */
288 static ALWAYS_INLINE void x264_prefetch( void *p )
290 asm volatile( "prefetcht0 %0"::"m"(*(uint8_t*)p) );
292 /* We require that prefetch not fault on invalid reads, so we only enable it on
293 * known architectures. */
294 #elif defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 1) &&\
295 (ARCH_X86 || ARCH_X86_64 || ARCH_ARM || ARCH_PPC)
296 #define x264_prefetch(x) __builtin_prefetch(x)
298 #define x264_prefetch(x)
303 #define x264_lower_thread_priority(p)\
305 x264_pthread_t handle = pthread_self();\
306 struct sched_param sp;\
307 int policy = SCHED_OTHER;\
308 pthread_getschedparam( handle, &policy, &sp );\
309 sp.sched_priority -= p;\
310 pthread_setschedparam( handle, policy, &sp );\
314 #define x264_lower_thread_priority(p) { UNUSED int nice_ret = nice(p); }
315 #endif /* SYS_WINDOWS */
316 #elif HAVE_WIN32THREAD
317 #define x264_lower_thread_priority(p) SetThreadPriority( GetCurrentThread(), X264_MAX( -2, -p ) )
319 #define x264_lower_thread_priority(p)
322 static inline uint8_t x264_is_regular_file( FILE *filehandle )
324 struct stat file_stat;
325 if( fstat( fileno( filehandle ), &file_stat ) )
327 return S_ISREG( file_stat.st_mode );
330 static inline uint8_t x264_is_regular_file_path( const char *filename )
332 struct stat file_stat;
333 if( stat( filename, &file_stat ) )
335 return S_ISREG( file_stat.st_mode );
338 #endif /* X264_OSDEP_H */