]> git.sesse.net Git - x264/blob - common/osdep.h
configure: Define feature test macros for --std=gnu99
[x264] / common / osdep.h
1 /*****************************************************************************
2  * osdep.h: platform-specific code
3  *****************************************************************************
4  * Copyright (C) 2007-2016 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Laurent Aimar <fenrir@via.ecp.fr>
8  *          Henrik Gramner <henrik@gramner.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
23  *
24  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27
28 #ifndef X264_OSDEP_H
29 #define X264_OSDEP_H
30
31 #define _LARGEFILE_SOURCE 1
32 #define _FILE_OFFSET_BITS 64
33 #include <stdio.h>
34 #include <sys/stat.h>
35 #include <inttypes.h>
36 #include <stdarg.h>
37
38 #include "config.h"
39
40 #ifdef __INTEL_COMPILER
41 #include <mathimf.h>
42 #else
43 #include <math.h>
44 #endif
45
46 #if !HAVE_LOG2F
47 #define log2f(x) (logf(x)/0.693147180559945f)
48 #define log2(x) (log(x)/0.693147180559945)
49 #endif
50
51 #ifdef _MSC_VER
52 #define inline __inline
53 #define strcasecmp _stricmp
54 #define strncasecmp _strnicmp
55 #define strtok_r strtok_s
56 #define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
57 #if _MSC_VER < 1900
58 #define snprintf _snprintf
59 #endif
60 #endif
61
62 #if !defined(va_copy) && defined(__INTEL_COMPILER)
63 #define va_copy(dst, src) ((dst) = (src))
64 #endif
65
66 #if !defined(isfinite) && (SYS_OPENBSD || SYS_SunOS)
67 #define isfinite finite
68 #endif
69
70 #ifdef _WIN32
71 #ifndef strtok_r
72 #define strtok_r(str,delim,save) strtok(str,delim)
73 #endif
74
75 #define utf8_to_utf16( utf8, utf16 )\
76     MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, utf8, -1, utf16, sizeof(utf16)/sizeof(wchar_t) )
77 FILE *x264_fopen( const char *filename, const char *mode );
78 int x264_rename( const char *oldname, const char *newname );
79 #define x264_struct_stat struct _stati64
80 #define x264_fstat _fstati64
81 int x264_stat( const char *path, x264_struct_stat *buf );
82 #else
83 #define x264_fopen       fopen
84 #define x264_rename      rename
85 #define x264_struct_stat struct stat
86 #define x264_fstat       fstat
87 #define x264_stat        stat
88 #endif
89
90 #if defined(_WIN32) && !HAVE_WINRT
91 int x264_vfprintf( FILE *stream, const char *format, va_list arg );
92 int x264_is_pipe( const char *path );
93 #else
94 #define x264_vfprintf vfprintf
95 #define x264_is_pipe(x) 0
96 #endif
97
98 #ifdef _MSC_VER
99 #define DECLARE_ALIGNED( var, n ) __declspec(align(n)) var
100 #else
101 #define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
102 #endif
103 #define ALIGNED_32( var ) DECLARE_ALIGNED( var, 32 )
104 #define ALIGNED_16( var ) DECLARE_ALIGNED( var, 16 )
105 #define ALIGNED_8( var )  DECLARE_ALIGNED( var, 8 )
106 #define ALIGNED_4( var )  DECLARE_ALIGNED( var, 4 )
107
108 // ARM compiliers don't reliably align stack variables
109 // - EABI requires only 8 byte stack alignment to be maintained
110 // - gcc can't align stack variables to more even if the stack were to be correctly aligned outside the function
111 // - armcc can't either, but is nice enough to actually tell you so
112 // - Apple gcc only maintains 4 byte alignment
113 // - llvm can align the stack, but only in svn and (unrelated) it exposes bugs in all released GNU binutils...
114
115 #define ALIGNED_ARRAY_EMU( mask, type, name, sub1, ... )\
116     uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + mask]; \
117     type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+mask) & ~mask)
118
119 #if ARCH_ARM && SYS_MACOSX
120 #define ALIGNED_ARRAY_8( ... ) ALIGNED_ARRAY_EMU( 7, __VA_ARGS__ )
121 #else
122 #define ALIGNED_ARRAY_8( type, name, sub1, ... )\
123     ALIGNED_8( type name sub1 __VA_ARGS__ )
124 #endif
125
126 #if ARCH_ARM
127 #define ALIGNED_ARRAY_16( ... ) ALIGNED_ARRAY_EMU( 15, __VA_ARGS__ )
128 #else
129 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
130     ALIGNED_16( type name sub1 __VA_ARGS__ )
131 #endif
132
133 #define EXPAND(x) x
134
135 #if STACK_ALIGNMENT >= 32
136 #define ALIGNED_ARRAY_32( type, name, sub1, ... )\
137     ALIGNED_32( type name sub1 __VA_ARGS__ )
138 #else
139 #define ALIGNED_ARRAY_32( ... ) EXPAND( ALIGNED_ARRAY_EMU( 31, __VA_ARGS__ ) )
140 #endif
141
142 #define ALIGNED_ARRAY_64( ... ) EXPAND( ALIGNED_ARRAY_EMU( 63, __VA_ARGS__ ) )
143
144 /* For AVX2 */
145 #if ARCH_X86 || ARCH_X86_64
146 #define NATIVE_ALIGN 32
147 #define ALIGNED_N ALIGNED_32
148 #define ALIGNED_ARRAY_N ALIGNED_ARRAY_32
149 #else
150 #define NATIVE_ALIGN 16
151 #define ALIGNED_N ALIGNED_16
152 #define ALIGNED_ARRAY_N ALIGNED_ARRAY_16
153 #endif
154
155 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
156 #define UNUSED __attribute__((unused))
157 #define ALWAYS_INLINE __attribute__((always_inline)) inline
158 #define NOINLINE __attribute__((noinline))
159 #define MAY_ALIAS __attribute__((may_alias))
160 #define x264_constant_p(x) __builtin_constant_p(x)
161 #define x264_nonconstant_p(x) (!__builtin_constant_p(x))
162 #else
163 #ifdef _MSC_VER
164 #define ALWAYS_INLINE __forceinline
165 #define NOINLINE __declspec(noinline)
166 #else
167 #define ALWAYS_INLINE inline
168 #define NOINLINE
169 #endif
170 #define UNUSED
171 #define MAY_ALIAS
172 #define x264_constant_p(x) 0
173 #define x264_nonconstant_p(x) 0
174 #endif
175
176 /* threads */
177 #if HAVE_BEOSTHREAD
178 #include <kernel/OS.h>
179 #define x264_pthread_t               thread_id
180 static inline int x264_pthread_create( x264_pthread_t *t, void *a, void *(*f)(void *), void *d )
181 {
182      *t = spawn_thread( f, "", 10, d );
183      if( *t < B_NO_ERROR )
184          return -1;
185      resume_thread( *t );
186      return 0;
187 }
188 #define x264_pthread_join(t,s)       { long tmp; \
189                                        wait_for_thread(t,(s)?(long*)(s):&tmp); }
190
191 #elif HAVE_POSIXTHREAD
192 #include <pthread.h>
193 #define x264_pthread_t               pthread_t
194 #define x264_pthread_create          pthread_create
195 #define x264_pthread_join            pthread_join
196 #define x264_pthread_mutex_t         pthread_mutex_t
197 #define x264_pthread_mutex_init      pthread_mutex_init
198 #define x264_pthread_mutex_destroy   pthread_mutex_destroy
199 #define x264_pthread_mutex_lock      pthread_mutex_lock
200 #define x264_pthread_mutex_unlock    pthread_mutex_unlock
201 #define x264_pthread_cond_t          pthread_cond_t
202 #define x264_pthread_cond_init       pthread_cond_init
203 #define x264_pthread_cond_destroy    pthread_cond_destroy
204 #define x264_pthread_cond_broadcast  pthread_cond_broadcast
205 #define x264_pthread_cond_wait       pthread_cond_wait
206 #define x264_pthread_attr_t          pthread_attr_t
207 #define x264_pthread_attr_init       pthread_attr_init
208 #define x264_pthread_attr_destroy    pthread_attr_destroy
209 #define x264_pthread_num_processors_np pthread_num_processors_np
210 #define X264_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
211
212 #elif HAVE_WIN32THREAD
213 #include "win32thread.h"
214
215 #else
216 #define x264_pthread_t               int
217 #define x264_pthread_create(t,u,f,d) 0
218 #define x264_pthread_join(t,s)
219 #endif //HAVE_*THREAD
220
221 #if !HAVE_POSIXTHREAD && !HAVE_WIN32THREAD
222 #define x264_pthread_mutex_t         int
223 #define x264_pthread_mutex_init(m,f) 0
224 #define x264_pthread_mutex_destroy(m)
225 #define x264_pthread_mutex_lock(m)
226 #define x264_pthread_mutex_unlock(m)
227 #define x264_pthread_cond_t          int
228 #define x264_pthread_cond_init(c,f)  0
229 #define x264_pthread_cond_destroy(c)
230 #define x264_pthread_cond_broadcast(c)
231 #define x264_pthread_cond_wait(c,m)
232 #define x264_pthread_attr_t          int
233 #define x264_pthread_attr_init(a)    0
234 #define x264_pthread_attr_destroy(a)
235 #define X264_PTHREAD_MUTEX_INITIALIZER 0
236 #endif
237
238 #if HAVE_WIN32THREAD || PTW32_STATIC_LIB
239 int x264_threading_init( void );
240 #else
241 #define x264_threading_init() 0
242 #endif
243
244 static ALWAYS_INLINE int x264_pthread_fetch_and_add( int *val, int add, x264_pthread_mutex_t *mutex )
245 {
246 #if HAVE_THREAD
247 #if defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ > 0) && ARCH_X86
248     return __sync_fetch_and_add( val, add );
249 #else
250     x264_pthread_mutex_lock( mutex );
251     int res = *val;
252     *val += add;
253     x264_pthread_mutex_unlock( mutex );
254     return res;
255 #endif
256 #else
257     int res = *val;
258     *val += add;
259     return res;
260 #endif
261 }
262
263 #define WORD_SIZE sizeof(void*)
264
265 #define asm __asm__
266
267 #if WORDS_BIGENDIAN
268 #define endian_fix(x) (x)
269 #define endian_fix64(x) (x)
270 #define endian_fix32(x) (x)
271 #define endian_fix16(x) (x)
272 #else
273 #if HAVE_X86_INLINE_ASM && HAVE_MMX
274 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
275 {
276     asm("bswap %0":"+r"(x));
277     return x;
278 }
279 #elif defined(__GNUC__) && HAVE_ARMV6
280 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
281 {
282     asm("rev %0, %0":"+r"(x));
283     return x;
284 }
285 #else
286 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
287 {
288     return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
289 }
290 #endif
291 #if HAVE_X86_INLINE_ASM && ARCH_X86_64
292 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
293 {
294     asm("bswap %0":"+r"(x));
295     return x;
296 }
297 #else
298 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
299 {
300     return endian_fix32(x>>32) + ((uint64_t)endian_fix32(x)<<32);
301 }
302 #endif
303 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
304 {
305     return WORD_SIZE == 8 ? endian_fix64(x) : endian_fix32(x);
306 }
307 static ALWAYS_INLINE uint16_t endian_fix16( uint16_t x )
308 {
309     return (x<<8)|(x>>8);
310 }
311 #endif
312
313 /* For values with 4 bits or less. */
314 static int ALWAYS_INLINE x264_ctz_4bit( uint32_t x )
315 {
316     static uint8_t lut[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
317     return lut[x];
318 }
319
320 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 3)
321 #define x264_clz(x) __builtin_clz(x)
322 #define x264_ctz(x) __builtin_ctz(x)
323 #else
324 static int ALWAYS_INLINE x264_clz( uint32_t x )
325 {
326     static uint8_t lut[16] = {4,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
327     int y, z = (((x >> 16) - 1) >> 27) & 16;
328     x >>= z^16;
329     z += y = ((x - 0x100) >> 28) & 8;
330     x >>= y^8;
331     z += y = ((x - 0x10) >> 29) & 4;
332     x >>= y^4;
333     return z + lut[x];
334 }
335
336 static int ALWAYS_INLINE x264_ctz( uint32_t x )
337 {
338     static uint8_t lut[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
339     int y, z = (((x & 0xffff) - 1) >> 27) & 16;
340     x >>= z;
341     z += y = (((x & 0xff) - 1) >> 28) & 8;
342     x >>= y;
343     z += y = (((x & 0xf) - 1) >> 29) & 4;
344     x >>= y;
345     return z + lut[x&0xf];
346 }
347 #endif
348
349 #if HAVE_X86_INLINE_ASM && HAVE_MMX
350 /* Don't use __builtin_prefetch; even as recent as 4.3.4, GCC seems incapable of
351  * using complex address modes properly unless we use inline asm. */
352 static ALWAYS_INLINE void x264_prefetch( void *p )
353 {
354     asm volatile( "prefetcht0 %0"::"m"(*(uint8_t*)p) );
355 }
356 /* We require that prefetch not fault on invalid reads, so we only enable it on
357  * known architectures. */
358 #elif defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 1) &&\
359       (ARCH_X86 || ARCH_X86_64 || ARCH_ARM || ARCH_PPC)
360 #define x264_prefetch(x) __builtin_prefetch(x)
361 #else
362 #define x264_prefetch(x)
363 #endif
364
365 #if HAVE_POSIXTHREAD
366 #if SYS_WINDOWS
367 #define x264_lower_thread_priority(p)\
368 {\
369     x264_pthread_t handle = pthread_self();\
370     struct sched_param sp;\
371     int policy = SCHED_OTHER;\
372     pthread_getschedparam( handle, &policy, &sp );\
373     sp.sched_priority -= p;\
374     pthread_setschedparam( handle, policy, &sp );\
375 }
376 #elif SYS_HAIKU
377 #include <OS.h>
378 #define x264_lower_thread_priority(p)\
379     { UNUSED status_t nice_ret = set_thread_priority( find_thread( NULL ), B_LOW_PRIORITY ); }
380 #else
381 #include <unistd.h>
382 #define x264_lower_thread_priority(p) { UNUSED int nice_ret = nice(p); }
383 #endif /* SYS_WINDOWS */
384 #elif HAVE_WIN32THREAD
385 #define x264_lower_thread_priority(p) SetThreadPriority( GetCurrentThread(), X264_MAX( -2, -p ) )
386 #else
387 #define x264_lower_thread_priority(p)
388 #endif
389
390 static inline int x264_is_regular_file( FILE *filehandle )
391 {
392     x264_struct_stat file_stat;
393     if( x264_fstat( fileno( filehandle ), &file_stat ) )
394         return 1;
395     return S_ISREG( file_stat.st_mode );
396 }
397
398 static inline int x264_is_regular_file_path( const char *filename )
399 {
400     x264_struct_stat file_stat;
401     if( x264_stat( filename, &file_stat ) )
402         return !x264_is_pipe( filename );
403     return S_ISREG( file_stat.st_mode );
404 }
405
406 #endif /* X264_OSDEP_H */