]> git.sesse.net Git - x264/blob - common/osdep.h
x86inc: Remove .rodata kludges
[x264] / common / osdep.h
1 /*****************************************************************************
2  * osdep.h: platform-specific code
3  *****************************************************************************
4  * Copyright (C) 2007-2013 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_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 )
86
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...
93
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)
97
98 #if ARCH_ARM && SYS_MACOSX
99 #define ALIGNED_ARRAY_8( ... ) ALIGNED_ARRAY_EMU( 7, __VA_ARGS__ )
100 #else
101 #define ALIGNED_ARRAY_8( type, name, sub1, ... )\
102     ALIGNED_8( type name sub1 __VA_ARGS__ )
103 #endif
104
105 #if ARCH_ARM
106 #define ALIGNED_ARRAY_16( ... ) ALIGNED_ARRAY_EMU( 15, __VA_ARGS__ )
107 #else
108 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
109     ALIGNED_16( type name sub1 __VA_ARGS__ )
110 #endif
111
112 #define EXPAND(x) x
113
114 #define ALIGNED_ARRAY_32( ... ) EXPAND( ALIGNED_ARRAY_EMU( 31, __VA_ARGS__ ) )
115 #define ALIGNED_ARRAY_64( ... ) EXPAND( ALIGNED_ARRAY_EMU( 63, __VA_ARGS__ ) )
116
117 /* For AVX2 */
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
122 #else
123 #define NATIVE_ALIGN 16
124 #define ALIGNED_N ALIGNED_16
125 #define ALIGNED_ARRAY_N ALIGNED_ARRAY_16
126 #endif
127
128 #define UNINIT(x) x=x
129
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))
137 #else
138 #ifdef __ICL
139 #define ALWAYS_INLINE __forceinline
140 #define NOINLINE __declspec(noinline)
141 #else
142 #define ALWAYS_INLINE inline
143 #define NOINLINE
144 #endif
145 #define UNUSED
146 #define MAY_ALIAS
147 #define x264_constant_p(x) 0
148 #define x264_nonconstant_p(x) 0
149 #endif
150
151 /* threads */
152 #if HAVE_BEOSTHREAD
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 )
156 {
157      *t = spawn_thread( f, "", 10, d );
158      if( *t < B_NO_ERROR )
159          return -1;
160      resume_thread( *t );
161      return 0;
162 }
163 #define x264_pthread_join(t,s)       { long tmp; \
164                                        wait_for_thread(t,(s)?(long*)(s):&tmp); }
165
166 #elif HAVE_POSIXTHREAD
167 #include <pthread.h>
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
186
187 #elif HAVE_WIN32THREAD
188 #include "win32thread.h"
189
190 #else
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
195
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
211 #endif
212
213 #if HAVE_WIN32THREAD || PTW32_STATIC_LIB
214 int x264_threading_init( void );
215 #else
216 #define x264_threading_init() 0
217 #endif
218
219 static ALWAYS_INLINE int x264_pthread_fetch_and_add( int *val, int add, x264_pthread_mutex_t *mutex )
220 {
221 #if HAVE_THREAD
222 #if defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ > 0) && ARCH_X86
223     return __sync_fetch_and_add( val, add );
224 #else
225     x264_pthread_mutex_lock( mutex );
226     int res = *val;
227     *val += add;
228     x264_pthread_mutex_unlock( mutex );
229     return res;
230 #endif
231 #else
232     int res = *val;
233     *val += add;
234     return res;
235 #endif
236 }
237
238 #define WORD_SIZE sizeof(void*)
239
240 #define asm __asm__
241
242 #if WORDS_BIGENDIAN
243 #define endian_fix(x) (x)
244 #define endian_fix64(x) (x)
245 #define endian_fix32(x) (x)
246 #define endian_fix16(x) (x)
247 #else
248 #if HAVE_X86_INLINE_ASM && HAVE_MMX
249 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
250 {
251     asm("bswap %0":"+r"(x));
252     return x;
253 }
254 #elif defined(__GNUC__) && HAVE_ARMV6
255 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
256 {
257     asm("rev %0, %0":"+r"(x));
258     return x;
259 }
260 #else
261 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
262 {
263     return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
264 }
265 #endif
266 #if HAVE_X86_INLINE_ASM && ARCH_X86_64
267 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
268 {
269     asm("bswap %0":"+r"(x));
270     return x;
271 }
272 #else
273 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
274 {
275     return endian_fix32(x>>32) + ((uint64_t)endian_fix32(x)<<32);
276 }
277 #endif
278 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
279 {
280     return WORD_SIZE == 8 ? endian_fix64(x) : endian_fix32(x);
281 }
282 static ALWAYS_INLINE uint16_t endian_fix16( uint16_t x )
283 {
284     return (x<<8)|(x>>8);
285 }
286 #endif
287
288 /* For values with 4 bits or less. */
289 static int ALWAYS_INLINE x264_ctz_4bit( uint32_t x )
290 {
291     static uint8_t lut[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
292     return lut[x];
293 }
294
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)
298 #else
299 static int ALWAYS_INLINE x264_clz( uint32_t x )
300 {
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;
303     x >>= z^16;
304     z += y = ((x - 0x100) >> 28) & 8;
305     x >>= y^8;
306     z += y = ((x - 0x10) >> 29) & 4;
307     x >>= y^4;
308     return z + lut[x];
309 }
310
311 static int ALWAYS_INLINE x264_ctz( uint32_t x )
312 {
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;
315     x >>= z;
316     z += y = (((x & 0xff) - 1) >> 28) & 8;
317     x >>= y;
318     z += y = (((x & 0xf) - 1) >> 29) & 4;
319     x >>= y;
320     return z + lut[x&0xf];
321 }
322 #endif
323
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 )
328 {
329     asm volatile( "prefetcht0 %0"::"m"(*(uint8_t*)p) );
330 }
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)
336 #else
337 #define x264_prefetch(x)
338 #endif
339
340 #if HAVE_POSIXTHREAD
341 #if SYS_WINDOWS
342 #define x264_lower_thread_priority(p)\
343 {\
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 );\
350 }
351 #else
352 #include <unistd.h>
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 ) )
357 #else
358 #define x264_lower_thread_priority(p)
359 #endif
360
361 static inline uint8_t x264_is_regular_file( FILE *filehandle )
362 {
363     struct stat file_stat;
364     if( fstat( fileno( filehandle ), &file_stat ) )
365         return -1;
366     return S_ISREG( file_stat.st_mode );
367 }
368
369 static inline uint8_t x264_is_regular_file_path( const char *filename )
370 {
371     struct stat file_stat;
372     if( stat( filename, &file_stat ) )
373         return -1;
374     return S_ISREG( file_stat.st_mode );
375 }
376
377 #endif /* X264_OSDEP_H */