]> git.sesse.net Git - x264/blob - common/osdep.h
Eliminate some compiler warnings on BSD
[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 int x264_snprintf( char *s, size_t n, const char *fmt, ... );
59 int x264_vsnprintf( char *s, size_t n, const char *fmt, va_list arg );
60 #define snprintf  x264_snprintf
61 #define vsnprintf x264_vsnprintf
62 #endif
63 #else
64 #include <strings.h>
65 #endif
66
67 #if !defined(va_copy) && defined(__INTEL_COMPILER)
68 #define va_copy(dst, src) ((dst) = (src))
69 #endif
70
71 #if !defined(isfinite) && (SYS_OPENBSD || SYS_SunOS)
72 #define isfinite finite
73 #endif
74
75 #ifdef _WIN32
76 #ifndef strtok_r
77 #define strtok_r(str,delim,save) strtok(str,delim)
78 #endif
79
80 #define utf8_to_utf16( utf8, utf16 )\
81     MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, utf8, -1, utf16, sizeof(utf16)/sizeof(wchar_t) )
82 FILE *x264_fopen( const char *filename, const char *mode );
83 int x264_rename( const char *oldname, const char *newname );
84 #define x264_struct_stat struct _stati64
85 #define x264_fstat _fstati64
86 int x264_stat( const char *path, x264_struct_stat *buf );
87 #else
88 #define x264_fopen       fopen
89 #define x264_rename      rename
90 #define x264_struct_stat struct stat
91 #define x264_fstat       fstat
92 #define x264_stat        stat
93 #endif
94
95 #if defined(_WIN32) && !HAVE_WINRT
96 int x264_vfprintf( FILE *stream, const char *format, va_list arg );
97 int x264_is_pipe( const char *path );
98 #else
99 #define x264_vfprintf vfprintf
100 #define x264_is_pipe(x) 0
101 #endif
102
103 #ifdef _MSC_VER
104 #define DECLARE_ALIGNED( var, n ) __declspec(align(n)) var
105 #else
106 #define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
107 #endif
108 #define ALIGNED_32( var ) DECLARE_ALIGNED( var, 32 )
109 #define ALIGNED_16( var ) DECLARE_ALIGNED( var, 16 )
110 #define ALIGNED_8( var )  DECLARE_ALIGNED( var, 8 )
111 #define ALIGNED_4( var )  DECLARE_ALIGNED( var, 4 )
112
113 // ARM compiliers don't reliably align stack variables
114 // - EABI requires only 8 byte stack alignment to be maintained
115 // - gcc can't align stack variables to more even if the stack were to be correctly aligned outside the function
116 // - armcc can't either, but is nice enough to actually tell you so
117 // - Apple gcc only maintains 4 byte alignment
118 // - llvm can align the stack, but only in svn and (unrelated) it exposes bugs in all released GNU binutils...
119
120 #define ALIGNED_ARRAY_EMU( mask, type, name, sub1, ... )\
121     uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + mask]; \
122     type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+mask) & ~mask)
123
124 #if ARCH_ARM && SYS_MACOSX
125 #define ALIGNED_ARRAY_8( ... ) ALIGNED_ARRAY_EMU( 7, __VA_ARGS__ )
126 #else
127 #define ALIGNED_ARRAY_8( type, name, sub1, ... )\
128     ALIGNED_8( type name sub1 __VA_ARGS__ )
129 #endif
130
131 #if ARCH_ARM
132 #define ALIGNED_ARRAY_16( ... ) ALIGNED_ARRAY_EMU( 15, __VA_ARGS__ )
133 #else
134 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
135     ALIGNED_16( type name sub1 __VA_ARGS__ )
136 #endif
137
138 #define EXPAND(x) x
139
140 #if STACK_ALIGNMENT >= 32
141 #define ALIGNED_ARRAY_32( type, name, sub1, ... )\
142     ALIGNED_32( type name sub1 __VA_ARGS__ )
143 #else
144 #define ALIGNED_ARRAY_32( ... ) EXPAND( ALIGNED_ARRAY_EMU( 31, __VA_ARGS__ ) )
145 #endif
146
147 #define ALIGNED_ARRAY_64( ... ) EXPAND( ALIGNED_ARRAY_EMU( 63, __VA_ARGS__ ) )
148
149 /* For AVX2 */
150 #if ARCH_X86 || ARCH_X86_64
151 #define NATIVE_ALIGN 32
152 #define ALIGNED_N ALIGNED_32
153 #define ALIGNED_ARRAY_N ALIGNED_ARRAY_32
154 #else
155 #define NATIVE_ALIGN 16
156 #define ALIGNED_N ALIGNED_16
157 #define ALIGNED_ARRAY_N ALIGNED_ARRAY_16
158 #endif
159
160 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
161 #define UNUSED __attribute__((unused))
162 #define ALWAYS_INLINE __attribute__((always_inline)) inline
163 #define NOINLINE __attribute__((noinline))
164 #define MAY_ALIAS __attribute__((may_alias))
165 #define x264_constant_p(x) __builtin_constant_p(x)
166 #define x264_nonconstant_p(x) (!__builtin_constant_p(x))
167 #else
168 #ifdef _MSC_VER
169 #define ALWAYS_INLINE __forceinline
170 #define NOINLINE __declspec(noinline)
171 #else
172 #define ALWAYS_INLINE inline
173 #define NOINLINE
174 #endif
175 #define UNUSED
176 #define MAY_ALIAS
177 #define x264_constant_p(x) 0
178 #define x264_nonconstant_p(x) 0
179 #endif
180
181 /* threads */
182 #if HAVE_BEOSTHREAD
183 #include <kernel/OS.h>
184 #define x264_pthread_t               thread_id
185 static inline int x264_pthread_create( x264_pthread_t *t, void *a, void *(*f)(void *), void *d )
186 {
187      *t = spawn_thread( f, "", 10, d );
188      if( *t < B_NO_ERROR )
189          return -1;
190      resume_thread( *t );
191      return 0;
192 }
193 #define x264_pthread_join(t,s)       { long tmp; \
194                                        wait_for_thread(t,(s)?(long*)(s):&tmp); }
195
196 #elif HAVE_POSIXTHREAD
197 #include <pthread.h>
198 #define x264_pthread_t               pthread_t
199 #define x264_pthread_create          pthread_create
200 #define x264_pthread_join            pthread_join
201 #define x264_pthread_mutex_t         pthread_mutex_t
202 #define x264_pthread_mutex_init      pthread_mutex_init
203 #define x264_pthread_mutex_destroy   pthread_mutex_destroy
204 #define x264_pthread_mutex_lock      pthread_mutex_lock
205 #define x264_pthread_mutex_unlock    pthread_mutex_unlock
206 #define x264_pthread_cond_t          pthread_cond_t
207 #define x264_pthread_cond_init       pthread_cond_init
208 #define x264_pthread_cond_destroy    pthread_cond_destroy
209 #define x264_pthread_cond_broadcast  pthread_cond_broadcast
210 #define x264_pthread_cond_wait       pthread_cond_wait
211 #define x264_pthread_attr_t          pthread_attr_t
212 #define x264_pthread_attr_init       pthread_attr_init
213 #define x264_pthread_attr_destroy    pthread_attr_destroy
214 #define x264_pthread_num_processors_np pthread_num_processors_np
215 #define X264_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
216
217 #elif HAVE_WIN32THREAD
218 #include "win32thread.h"
219
220 #else
221 #define x264_pthread_t               int
222 #define x264_pthread_create(t,u,f,d) 0
223 #define x264_pthread_join(t,s)
224 #endif //HAVE_*THREAD
225
226 #if !HAVE_POSIXTHREAD && !HAVE_WIN32THREAD
227 #define x264_pthread_mutex_t         int
228 #define x264_pthread_mutex_init(m,f) 0
229 #define x264_pthread_mutex_destroy(m)
230 #define x264_pthread_mutex_lock(m)
231 #define x264_pthread_mutex_unlock(m)
232 #define x264_pthread_cond_t          int
233 #define x264_pthread_cond_init(c,f)  0
234 #define x264_pthread_cond_destroy(c)
235 #define x264_pthread_cond_broadcast(c)
236 #define x264_pthread_cond_wait(c,m)
237 #define x264_pthread_attr_t          int
238 #define x264_pthread_attr_init(a)    0
239 #define x264_pthread_attr_destroy(a)
240 #define X264_PTHREAD_MUTEX_INITIALIZER 0
241 #endif
242
243 #if HAVE_WIN32THREAD || PTW32_STATIC_LIB
244 int x264_threading_init( void );
245 #else
246 #define x264_threading_init() 0
247 #endif
248
249 static ALWAYS_INLINE int x264_pthread_fetch_and_add( int *val, int add, x264_pthread_mutex_t *mutex )
250 {
251 #if HAVE_THREAD
252 #if defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ > 0) && ARCH_X86
253     return __sync_fetch_and_add( val, add );
254 #else
255     x264_pthread_mutex_lock( mutex );
256     int res = *val;
257     *val += add;
258     x264_pthread_mutex_unlock( mutex );
259     return res;
260 #endif
261 #else
262     int res = *val;
263     *val += add;
264     return res;
265 #endif
266 }
267
268 #define WORD_SIZE sizeof(void*)
269
270 #define asm __asm__
271
272 #if WORDS_BIGENDIAN
273 #define endian_fix(x) (x)
274 #define endian_fix64(x) (x)
275 #define endian_fix32(x) (x)
276 #define endian_fix16(x) (x)
277 #else
278 #if HAVE_X86_INLINE_ASM && HAVE_MMX
279 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
280 {
281     asm("bswap %0":"+r"(x));
282     return x;
283 }
284 #elif defined(__GNUC__) && HAVE_ARMV6
285 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
286 {
287     asm("rev %0, %0":"+r"(x));
288     return x;
289 }
290 #else
291 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
292 {
293     return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
294 }
295 #endif
296 #if HAVE_X86_INLINE_ASM && ARCH_X86_64
297 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
298 {
299     asm("bswap %0":"+r"(x));
300     return x;
301 }
302 #else
303 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
304 {
305     return endian_fix32(x>>32) + ((uint64_t)endian_fix32(x)<<32);
306 }
307 #endif
308 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
309 {
310     return WORD_SIZE == 8 ? endian_fix64(x) : endian_fix32(x);
311 }
312 static ALWAYS_INLINE uint16_t endian_fix16( uint16_t x )
313 {
314     return (x<<8)|(x>>8);
315 }
316 #endif
317
318 /* For values with 4 bits or less. */
319 static int ALWAYS_INLINE x264_ctz_4bit( uint32_t x )
320 {
321     static uint8_t lut[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
322     return lut[x];
323 }
324
325 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 3)
326 #define x264_clz(x) __builtin_clz(x)
327 #define x264_ctz(x) __builtin_ctz(x)
328 #else
329 static int ALWAYS_INLINE x264_clz( uint32_t x )
330 {
331     static uint8_t lut[16] = {4,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
332     int y, z = (((x >> 16) - 1) >> 27) & 16;
333     x >>= z^16;
334     z += y = ((x - 0x100) >> 28) & 8;
335     x >>= y^8;
336     z += y = ((x - 0x10) >> 29) & 4;
337     x >>= y^4;
338     return z + lut[x];
339 }
340
341 static int ALWAYS_INLINE x264_ctz( uint32_t x )
342 {
343     static uint8_t lut[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
344     int y, z = (((x & 0xffff) - 1) >> 27) & 16;
345     x >>= z;
346     z += y = (((x & 0xff) - 1) >> 28) & 8;
347     x >>= y;
348     z += y = (((x & 0xf) - 1) >> 29) & 4;
349     x >>= y;
350     return z + lut[x&0xf];
351 }
352 #endif
353
354 #if HAVE_X86_INLINE_ASM && HAVE_MMX
355 /* Don't use __builtin_prefetch; even as recent as 4.3.4, GCC seems incapable of
356  * using complex address modes properly unless we use inline asm. */
357 static ALWAYS_INLINE void x264_prefetch( void *p )
358 {
359     asm volatile( "prefetcht0 %0"::"m"(*(uint8_t*)p) );
360 }
361 /* We require that prefetch not fault on invalid reads, so we only enable it on
362  * known architectures. */
363 #elif defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 1) &&\
364       (ARCH_X86 || ARCH_X86_64 || ARCH_ARM || ARCH_PPC)
365 #define x264_prefetch(x) __builtin_prefetch(x)
366 #else
367 #define x264_prefetch(x)
368 #endif
369
370 #if HAVE_POSIXTHREAD
371 #if SYS_WINDOWS
372 #define x264_lower_thread_priority(p)\
373 {\
374     x264_pthread_t handle = pthread_self();\
375     struct sched_param sp;\
376     int policy = SCHED_OTHER;\
377     pthread_getschedparam( handle, &policy, &sp );\
378     sp.sched_priority -= p;\
379     pthread_setschedparam( handle, policy, &sp );\
380 }
381 #elif SYS_HAIKU
382 #include <OS.h>
383 #define x264_lower_thread_priority(p)\
384     { UNUSED status_t nice_ret = set_thread_priority( find_thread( NULL ), B_LOW_PRIORITY ); }
385 #else
386 #include <unistd.h>
387 #define x264_lower_thread_priority(p) { UNUSED int nice_ret = nice(p); }
388 #endif /* SYS_WINDOWS */
389 #elif HAVE_WIN32THREAD
390 #define x264_lower_thread_priority(p) SetThreadPriority( GetCurrentThread(), X264_MAX( -2, -p ) )
391 #else
392 #define x264_lower_thread_priority(p)
393 #endif
394
395 static inline int x264_is_regular_file( FILE *filehandle )
396 {
397     x264_struct_stat file_stat;
398     if( x264_fstat( fileno( filehandle ), &file_stat ) )
399         return 1;
400     return S_ISREG( file_stat.st_mode );
401 }
402
403 static inline int x264_is_regular_file_path( const char *filename )
404 {
405     x264_struct_stat file_stat;
406     if( x264_stat( filename, &file_stat ) )
407         return !x264_is_pipe( filename );
408     return S_ISREG( file_stat.st_mode );
409 }
410
411 #endif /* X264_OSDEP_H */