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