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