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