]> git.sesse.net Git - x264/blob - common/osdep.h
XOP 8x8 zigzags
[x264] / common / osdep.h
1 /*****************************************************************************
2  * osdep.h: platform-specific code
3  *****************************************************************************
4  * Copyright (C) 2007-2012 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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.
22  *
23  * This program is also available under a commercial proprietary license.
24  * For more information, contact us at licensing@x264.com.
25  *****************************************************************************/
26
27 #ifndef X264_OSDEP_H
28 #define X264_OSDEP_H
29
30 #define _LARGEFILE_SOURCE 1
31 #define _FILE_OFFSET_BITS 64
32 #include <stdio.h>
33 #include <sys/stat.h>
34 #include <inttypes.h>
35
36 #include "config.h"
37
38 #if !HAVE_LOG2F
39 #define log2f(x) (logf(x)/0.693147180559945f)
40 #define log2(x) (log(x)/0.693147180559945)
41 #endif
42
43 #ifdef _WIN32
44 #include <io.h>    // _setmode()
45 #include <fcntl.h> // _O_BINARY
46 #endif
47
48 #ifdef __ICL
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)
55 #endif
56
57 #ifdef __INTEL_COMPILER
58 #include <mathimf.h>
59 #else
60 #include <math.h>
61 #endif
62
63 #if (defined(__GNUC__) || defined(__INTEL_COMPILER)) && (ARCH_X86 || ARCH_X86_64)
64 #define HAVE_X86_INLINE_ASM 1
65 #endif
66
67 #if !defined(isfinite) && (SYS_OPENBSD || SYS_SunOS)
68 #define isfinite finite
69 #endif
70 #ifdef _WIN32
71 #define rename(src,dst) (unlink(dst), rename(src,dst)) // POSIX says that rename() removes the destination, but win32 doesn't.
72 #ifndef strtok_r
73 #define strtok_r(str,delim,save) strtok(str,delim)
74 #endif
75 #endif
76
77 #ifdef __ICL
78 #define DECLARE_ALIGNED( var, n ) __declspec(align(n)) var
79 #else
80 #define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
81 #endif
82 #define ALIGNED_16( var ) DECLARE_ALIGNED( var, 16 )
83 #define ALIGNED_8( var )  DECLARE_ALIGNED( var, 8 )
84 #define ALIGNED_4( var )  DECLARE_ALIGNED( var, 4 )
85
86 // ARM compiliers don't reliably align stack variables
87 // - EABI requires only 8 byte stack alignment to be maintained
88 // - gcc can't align stack variables to more even if the stack were to be correctly aligned outside the function
89 // - armcc can't either, but is nice enough to actually tell you so
90 // - Apple gcc only maintains 4 byte alignment
91 // - llvm can align the stack, but only in svn and (unrelated) it exposes bugs in all released GNU binutils...
92
93 #define ALIGNED_ARRAY_EMU( mask, type, name, sub1, ... )\
94     uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + mask]; \
95     type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+mask) & ~mask)
96
97 #if ARCH_ARM && SYS_MACOSX
98 #define ALIGNED_ARRAY_8( ... ) ALIGNED_ARRAY_EMU( 7, __VA_ARGS__ )
99 #else
100 #define ALIGNED_ARRAY_8( type, name, sub1, ... )\
101     ALIGNED_8( type name sub1 __VA_ARGS__ )
102 #endif
103
104 #if ARCH_ARM
105 #define ALIGNED_ARRAY_16( ... ) ALIGNED_ARRAY_EMU( 15, __VA_ARGS__ )
106 #else
107 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
108     ALIGNED_16( type name sub1 __VA_ARGS__ )
109 #endif
110
111 #define ALIGNED_ARRAY_32( ... ) ALIGNED_ARRAY_EMU( 31, __VA_ARGS__ )
112 #define ALIGNED_ARRAY_64( ... ) ALIGNED_ARRAY_EMU( 63, __VA_ARGS__ )
113
114 #define UNINIT(x) x=x
115
116 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
117 #define UNUSED __attribute__((unused))
118 #define ALWAYS_INLINE __attribute__((always_inline)) inline
119 #define NOINLINE __attribute__((noinline))
120 #define MAY_ALIAS __attribute__((may_alias))
121 #define x264_constant_p(x) __builtin_constant_p(x)
122 #define x264_nonconstant_p(x) (!__builtin_constant_p(x))
123 #else
124 #ifdef __ICL
125 #define ALWAYS_INLINE __forceinline
126 #define NOINLINE __declspec(noinline)
127 #else
128 #define ALWAYS_INLINE inline
129 #define NOINLINE
130 #endif
131 #define UNUSED
132 #define MAY_ALIAS
133 #define x264_constant_p(x) 0
134 #define x264_nonconstant_p(x) 0
135 #endif
136
137 /* threads */
138 #if HAVE_BEOSTHREAD
139 #include <kernel/OS.h>
140 #define x264_pthread_t               thread_id
141 static inline int x264_pthread_create( x264_pthread_t *t, void *a, void *(*f)(void *), void *d )
142 {
143      *t = spawn_thread( f, "", 10, d );
144      if( *t < B_NO_ERROR )
145          return -1;
146      resume_thread( *t );
147      return 0;
148 }
149 #define x264_pthread_join(t,s)       { long tmp; \
150                                        wait_for_thread(t,(s)?(long*)(*(s)):&tmp); }
151
152 #elif HAVE_POSIXTHREAD
153 #include <pthread.h>
154 #define x264_pthread_t               pthread_t
155 #define x264_pthread_create          pthread_create
156 #define x264_pthread_join            pthread_join
157 #define x264_pthread_mutex_t         pthread_mutex_t
158 #define x264_pthread_mutex_init      pthread_mutex_init
159 #define x264_pthread_mutex_destroy   pthread_mutex_destroy
160 #define x264_pthread_mutex_lock      pthread_mutex_lock
161 #define x264_pthread_mutex_unlock    pthread_mutex_unlock
162 #define x264_pthread_cond_t          pthread_cond_t
163 #define x264_pthread_cond_init       pthread_cond_init
164 #define x264_pthread_cond_destroy    pthread_cond_destroy
165 #define x264_pthread_cond_broadcast  pthread_cond_broadcast
166 #define x264_pthread_cond_wait       pthread_cond_wait
167 #define x264_pthread_attr_t          pthread_attr_t
168 #define x264_pthread_attr_init       pthread_attr_init
169 #define x264_pthread_attr_destroy    pthread_attr_destroy
170 #define x264_pthread_num_processors_np pthread_num_processors_np
171 #define X264_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
172
173 #elif HAVE_WIN32THREAD
174 #include "win32thread.h"
175
176 #else
177 #define x264_pthread_t               int
178 #define x264_pthread_create(t,u,f,d) 0
179 #define x264_pthread_join(t,s)
180 #endif //HAVE_*THREAD
181
182 #if !HAVE_POSIXTHREAD && !HAVE_WIN32THREAD
183 #define x264_pthread_mutex_t         int
184 #define x264_pthread_mutex_init(m,f) 0
185 #define x264_pthread_mutex_destroy(m)
186 #define x264_pthread_mutex_lock(m)
187 #define x264_pthread_mutex_unlock(m)
188 #define x264_pthread_cond_t          int
189 #define x264_pthread_cond_init(c,f)  0
190 #define x264_pthread_cond_destroy(c)
191 #define x264_pthread_cond_broadcast(c)
192 #define x264_pthread_cond_wait(c,m)
193 #define x264_pthread_attr_t          int
194 #define x264_pthread_attr_init(a)    0
195 #define x264_pthread_attr_destroy(a)
196 #define X264_PTHREAD_MUTEX_INITIALIZER 0
197 #endif
198
199 #if HAVE_WIN32THREAD || PTW32_STATIC_LIB
200 int x264_threading_init( void );
201 #else
202 #define x264_threading_init() 0
203 #endif
204
205 #define WORD_SIZE sizeof(void*)
206
207 #define asm __asm__
208
209 #if WORDS_BIGENDIAN
210 #define endian_fix(x) (x)
211 #define endian_fix64(x) (x)
212 #define endian_fix32(x) (x)
213 #define endian_fix16(x) (x)
214 #else
215 #if HAVE_X86_INLINE_ASM && HAVE_MMX
216 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
217 {
218     asm("bswap %0":"+r"(x));
219     return x;
220 }
221 #elif defined(__GNUC__) && HAVE_ARMV6
222 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
223 {
224     asm("rev %0, %0":"+r"(x));
225     return x;
226 }
227 #else
228 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
229 {
230     return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
231 }
232 #endif
233 #if HAVE_X86_INLINE_ASM && ARCH_X86_64
234 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
235 {
236     asm("bswap %0":"+r"(x));
237     return x;
238 }
239 #else
240 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
241 {
242     return endian_fix32(x>>32) + ((uint64_t)endian_fix32(x)<<32);
243 }
244 #endif
245 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
246 {
247     return WORD_SIZE == 8 ? endian_fix64(x) : endian_fix32(x);
248 }
249 static ALWAYS_INLINE uint16_t endian_fix16( uint16_t x )
250 {
251     return (x<<8)|(x>>8);
252 }
253 #endif
254
255 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 3)
256 #define x264_clz(x) __builtin_clz(x)
257 #define x264_ctz(x) __builtin_ctz(x)
258 #else
259 static int ALWAYS_INLINE x264_clz( uint32_t x )
260 {
261     static uint8_t lut[16] = {4,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
262     int y, z = (((x >> 16) - 1) >> 27) & 16;
263     x >>= z^16;
264     z += y = ((x - 0x100) >> 28) & 8;
265     x >>= y^8;
266     z += y = ((x - 0x10) >> 29) & 4;
267     x >>= y^4;
268     return z + lut[x];
269 }
270
271 static int ALWAYS_INLINE x264_ctz( uint32_t x )
272 {
273     static uint8_t lut[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
274     int y, z = (((x & 0xffff) - 1) >> 27) & 16;
275     x >>= z;
276     z += y = (((x & 0xff) - 1) >> 28) & 8;
277     x >>= y;
278     z += y = (((x & 0xf) - 1) >> 29) & 4;
279     x >>= y;
280     return z + lut[x&0xf];
281 }
282 #endif
283
284 #if HAVE_X86_INLINE_ASM && HAVE_MMX
285 /* Don't use __builtin_prefetch; even as recent as 4.3.4, GCC seems incapable of
286  * using complex address modes properly unless we use inline asm. */
287 static ALWAYS_INLINE void x264_prefetch( void *p )
288 {
289     asm volatile( "prefetcht0 %0"::"m"(*(uint8_t*)p) );
290 }
291 /* We require that prefetch not fault on invalid reads, so we only enable it on
292  * known architectures. */
293 #elif defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 1) &&\
294       (ARCH_X86 || ARCH_X86_64 || ARCH_ARM || ARCH_PPC)
295 #define x264_prefetch(x) __builtin_prefetch(x)
296 #else
297 #define x264_prefetch(x)
298 #endif
299
300 #if HAVE_POSIXTHREAD
301 #if SYS_WINDOWS
302 #define x264_lower_thread_priority(p)\
303 {\
304     x264_pthread_t handle = pthread_self();\
305     struct sched_param sp;\
306     int policy = SCHED_OTHER;\
307     pthread_getschedparam( handle, &policy, &sp );\
308     sp.sched_priority -= p;\
309     pthread_setschedparam( handle, policy, &sp );\
310 }
311 #else
312 #include <unistd.h>
313 #define x264_lower_thread_priority(p) { UNUSED int nice_ret = nice(p); }
314 #endif /* SYS_WINDOWS */
315 #elif HAVE_WIN32THREAD
316 #define x264_lower_thread_priority(p) SetThreadPriority( GetCurrentThread(), X264_MAX( -2, -p ) )
317 #else
318 #define x264_lower_thread_priority(p)
319 #endif
320
321 static inline uint8_t x264_is_regular_file( FILE *filehandle )
322 {
323     struct stat file_stat;
324     if( fstat( fileno( filehandle ), &file_stat ) )
325         return -1;
326     return S_ISREG( file_stat.st_mode );
327 }
328
329 static inline uint8_t x264_is_regular_file_path( const char *filename )
330 {
331     struct stat file_stat;
332     if( stat( filename, &file_stat ) )
333         return -1;
334     return S_ISREG( file_stat.st_mode );
335 }
336
337 #endif /* X264_OSDEP_H */