1 /*****************************************************************************
2 * osdep.h: platform-specific code
3 *****************************************************************************
4 * Copyright (C) 2007-2010 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
53 #if !defined(isfinite) && (SYS_OPENBSD || SYS_SunOS)
54 #define isfinite finite
57 #define rename(src,dst) (unlink(dst), rename(src,dst)) // POSIX says that rename() removes the destination, but win32 doesn't.
59 #define strtok_r(str,delim,save) strtok(str,delim)
63 #define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
64 #define ALIGNED_16( var ) DECLARE_ALIGNED( var, 16 )
65 #define ALIGNED_8( var ) DECLARE_ALIGNED( var, 8 )
66 #define ALIGNED_4( var ) DECLARE_ALIGNED( var, 4 )
68 // ARM compiliers don't reliably align stack variables
69 // - EABI requires only 8 byte stack alignment to be maintained
70 // - gcc can't align stack variables to more even if the stack were to be correctly aligned outside the function
71 // - armcc can't either, but is nice enough to actually tell you so
72 // - Apple gcc only maintains 4 byte alignment
73 // - llvm can align the stack, but only in svn and (unrelated) it exposes bugs in all released GNU binutils...
74 #if ARCH_ARM && SYS_MACOSX
75 #define ALIGNED_ARRAY_8( type, name, sub1, ... )\
76 uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + 7]; \
77 type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+7) & ~7)
79 #define ALIGNED_ARRAY_8( type, name, sub1, ... )\
80 ALIGNED_8( type name sub1 __VA_ARGS__ )
84 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
85 uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + 15];\
86 type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+15) & ~15)
88 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
89 ALIGNED_16( type name sub1 __VA_ARGS__ )
92 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
93 #define UNUSED __attribute__((unused))
94 #define ALWAYS_INLINE __attribute__((always_inline)) inline
95 #define NOINLINE __attribute__((noinline))
96 #define MAY_ALIAS __attribute__((may_alias))
97 #define x264_constant_p(x) __builtin_constant_p(x)
98 #define x264_nonconstant_p(x) (!__builtin_constant_p(x))
101 #define ALWAYS_INLINE inline
104 #define x264_constant_p(x) 0
105 #define x264_nonconstant_p(x) 0
110 #include <kernel/OS.h>
111 #define x264_pthread_t thread_id
112 static inline int x264_pthread_create( x264_pthread_t *t, void *a, void *(*f)(void *), void *d )
114 *t = spawn_thread( f, "", 10, d );
115 if( *t < B_NO_ERROR )
120 #define x264_pthread_join(t,s) { long tmp; \
121 wait_for_thread(t,(s)?(long*)(*(s)):&tmp); }
123 #define usleep(t) snooze(t)
125 #define HAVE_PTHREAD 1
129 #define USE_REAL_PTHREAD 1
132 #define x264_pthread_t int
133 #define x264_pthread_create(t,u,f,d) 0
134 #define x264_pthread_join(t,s)
138 #define x264_pthread_t pthread_t
139 #define x264_pthread_create pthread_create
140 #define x264_pthread_join pthread_join
141 #define x264_pthread_mutex_t pthread_mutex_t
142 #define x264_pthread_mutex_init pthread_mutex_init
143 #define x264_pthread_mutex_destroy pthread_mutex_destroy
144 #define x264_pthread_mutex_lock pthread_mutex_lock
145 #define x264_pthread_mutex_unlock pthread_mutex_unlock
146 #define x264_pthread_cond_t pthread_cond_t
147 #define x264_pthread_cond_init pthread_cond_init
148 #define x264_pthread_cond_destroy pthread_cond_destroy
149 #define x264_pthread_cond_broadcast pthread_cond_broadcast
150 #define x264_pthread_cond_wait pthread_cond_wait
151 #define x264_pthread_attr_t pthread_attr_t
152 #define x264_pthread_attr_init pthread_attr_init
153 #define x264_pthread_attr_destroy pthread_attr_destroy
154 #define X264_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
156 #define x264_pthread_mutex_t int
157 #define x264_pthread_mutex_init(m,f) 0
158 #define x264_pthread_mutex_destroy(m)
159 #define x264_pthread_mutex_lock(m)
160 #define x264_pthread_mutex_unlock(m)
161 #define x264_pthread_cond_t int
162 #define x264_pthread_cond_init(c,f) 0
163 #define x264_pthread_cond_destroy(c)
164 #define x264_pthread_cond_broadcast(c)
165 #define x264_pthread_cond_wait(c,m)
166 #define x264_pthread_attr_t int
167 #define x264_pthread_attr_init(a) 0
168 #define x264_pthread_attr_destroy(a)
169 #define X264_PTHREAD_MUTEX_INITIALIZER 0
172 #define WORD_SIZE sizeof(void*)
176 #if !defined(_WIN64) && !defined(__LP64__)
177 #if defined(__INTEL_COMPILER)
178 #define BROKEN_STACK_ALIGNMENT 1 /* define it if stack is not mod16 */
183 #define endian_fix(x) (x)
184 #define endian_fix64(x) (x)
185 #define endian_fix32(x) (x)
186 #define endian_fix16(x) (x)
188 #if defined(__GNUC__) && HAVE_MMX
189 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
191 asm("bswap %0":"+r"(x));
194 #elif defined(__GNUC__) && HAVE_ARMV6
195 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
197 asm("rev %0, %0":"+r"(x));
201 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
203 return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
206 #if defined(__GNUC__) && ARCH_X86_64
207 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
209 asm("bswap %0":"+r"(x));
213 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
215 return endian_fix32(x>>32) + ((uint64_t)endian_fix32(x)<<32);
218 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
220 return WORD_SIZE == 8 ? endian_fix64(x) : endian_fix32(x);
222 static ALWAYS_INLINE uint16_t endian_fix16( uint16_t x )
224 return (x<<8)|(x>>8);
228 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 3)
229 #define x264_clz(x) __builtin_clz(x)
230 #define x264_ctz(x) __builtin_ctz(x)
232 static int ALWAYS_INLINE x264_clz( uint32_t x )
234 static uint8_t lut[16] = {4,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
235 int y, z = (((x >> 16) - 1) >> 27) & 16;
237 z += y = ((x - 0x100) >> 28) & 8;
239 z += y = ((x - 0x10) >> 29) & 4;
244 static int ALWAYS_INLINE x264_ctz( uint32_t x )
246 static uint8_t lut[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
247 int y, z = (((x & 0xffff) - 1) >> 27) & 16;
249 z += y = (((x & 0xff) - 1) >> 28) & 8;
251 z += y = (((x & 0xf) - 1) >> 29) & 4;
253 return z + lut[x&0xf];
257 #if defined(__GNUC__) && HAVE_MMX
258 /* Don't use __builtin_prefetch; even as recent as 4.3.4, GCC seems incapable of
259 * using complex address modes properly unless we use inline asm. */
260 static ALWAYS_INLINE void x264_prefetch( void *p )
262 asm volatile( "prefetcht0 %0"::"m"(*(uint8_t*)p) );
264 /* We require that prefetch not fault on invalid reads, so we only enable it on
265 * known architectures. */
266 #elif defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 1) &&\
267 (ARCH_X86 || ARCH_X86_64 || ARCH_ARM || ARCH_PPC)
268 #define x264_prefetch(x) __builtin_prefetch(x)
270 #define x264_prefetch(x)
275 #define x264_lower_thread_priority(p)\
277 x264_pthread_t handle = pthread_self();\
278 struct sched_param sp;\
279 int policy = SCHED_OTHER;\
280 pthread_getschedparam( handle, &policy, &sp );\
281 sp.sched_priority -= p;\
282 pthread_setschedparam( handle, policy, &sp );\
286 #define x264_lower_thread_priority(p) { UNUSED int nice_ret = nice(p); }
287 #endif /* USE_REAL_PTHREAD */
289 #define x264_lower_thread_priority(p)
292 static inline uint8_t x264_is_regular_file( FILE *filehandle )
294 struct stat file_stat;
295 if( fstat( fileno( filehandle ), &file_stat ) )
297 return S_ISREG( file_stat.st_mode );
300 static inline uint8_t x264_is_regular_file_path( const char *filename )
302 struct stat file_stat;
303 if( stat( filename, &file_stat ) )
305 return S_ISREG( file_stat.st_mode );
308 #endif /* X264_OSDEP_H */