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