use lookup tables instead of actual exp/pow for AQ
[SFUResearch.git] / common / osdep.h
blob25bb1380a5c153412756d6f3fdfc11013317daa2
1 /*****************************************************************************
2 * osdep.h: h264 encoder
3 *****************************************************************************
4 * Copyright (C) 2007-2008 x264 project
6 * Authors: Loren Merritt <lorenm@u.washington.edu>
7 * Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 #ifndef X264_OSDEP_H
25 #define X264_OSDEP_H
27 #define _LARGEFILE_SOURCE 1
28 #define _FILE_OFFSET_BITS 64
29 #include <stdio.h>
31 #ifdef HAVE_STDINT_H
32 #include <stdint.h>
33 #else
34 #include <inttypes.h>
35 #endif
37 #ifdef _WIN32
38 #include <io.h> // _setmode()
39 #include <fcntl.h> // _O_BINARY
40 #endif
42 #ifdef _MSC_VER
43 #define inline __inline
44 #define strcasecmp stricmp
45 #define strncasecmp strnicmp
46 #define snprintf _snprintf
47 #define fseek _fseeki64
48 #define ftell _ftelli64
49 #define isfinite _finite
50 #define _CRT_SECURE_NO_DEPRECATE
51 #define X264_VERSION "" // no configure script for msvc
52 #endif
54 #if defined(SYS_OPENBSD) || defined(SYS_SunOS)
55 #define isfinite finite
56 #endif
57 #if defined(_MSC_VER) || defined(SYS_SunOS) || defined(SYS_MACOSX)
58 #define sqrtf sqrt
59 #endif
60 #ifdef _WIN32
61 #define rename(src,dst) (unlink(dst), rename(src,dst)) // POSIX says that rename() removes the destination, but win32 doesn't.
62 #ifndef strtok_r
63 #define strtok_r(str,delim,save) strtok(str,delim)
64 #endif
65 #endif
67 #ifdef _MSC_VER
68 #define DECLARE_ALIGNED( var, n ) __declspec(align(n)) var
69 #else
70 #define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
71 #endif
72 #define DECLARE_ALIGNED_16( var ) DECLARE_ALIGNED( var, 16 )
73 #define DECLARE_ALIGNED_8( var ) DECLARE_ALIGNED( var, 8 )
74 #define DECLARE_ALIGNED_4( var ) DECLARE_ALIGNED( var, 4 )
76 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
77 #define UNUSED __attribute__((unused))
78 #define ALWAYS_INLINE __attribute__((always_inline)) inline
79 #define NOINLINE __attribute__((noinline))
80 #else
81 #define UNUSED
82 #define ALWAYS_INLINE inline
83 #define NOINLINE
84 #endif
86 /* threads */
87 #if defined(SYS_BEOS)
88 #include <kernel/OS.h>
89 #define x264_pthread_t thread_id
90 #define x264_pthread_create(t,u,f,d) { *(t)=spawn_thread(f,"",10,d); \
91 resume_thread(*(t)); }
92 #define x264_pthread_join(t,s) { long tmp; \
93 wait_for_thread(t,(s)?(long*)(s):&tmp); }
94 #ifndef usleep
95 #define usleep(t) snooze(t)
96 #endif
97 #define HAVE_PTHREAD 1
99 #elif defined(HAVE_PTHREAD)
100 #include <pthread.h>
101 #define USE_REAL_PTHREAD
103 #else
104 #define x264_pthread_t int
105 #define x264_pthread_create(t,u,f,d)
106 #define x264_pthread_join(t,s)
107 #endif //SYS_*
109 #ifdef USE_REAL_PTHREAD
110 #define x264_pthread_t pthread_t
111 #define x264_pthread_create pthread_create
112 #define x264_pthread_join pthread_join
113 #define x264_pthread_mutex_t pthread_mutex_t
114 #define x264_pthread_mutex_init pthread_mutex_init
115 #define x264_pthread_mutex_destroy pthread_mutex_destroy
116 #define x264_pthread_mutex_lock pthread_mutex_lock
117 #define x264_pthread_mutex_unlock pthread_mutex_unlock
118 #define x264_pthread_cond_t pthread_cond_t
119 #define x264_pthread_cond_init pthread_cond_init
120 #define x264_pthread_cond_destroy pthread_cond_destroy
121 #define x264_pthread_cond_broadcast pthread_cond_broadcast
122 #define x264_pthread_cond_wait pthread_cond_wait
123 #else
124 #define x264_pthread_mutex_t int
125 #define x264_pthread_mutex_init(m,f)
126 #define x264_pthread_mutex_destroy(m)
127 #define x264_pthread_mutex_lock(m)
128 #define x264_pthread_mutex_unlock(m)
129 #define x264_pthread_cond_t int
130 #define x264_pthread_cond_init(c,f)
131 #define x264_pthread_cond_destroy(c)
132 #define x264_pthread_cond_broadcast(c)
133 #define x264_pthread_cond_wait(c,m)
134 #endif
136 #define WORD_SIZE sizeof(void*)
138 #if !defined(_WIN64) && !defined(__LP64__)
139 #if defined(_MSC_VER) || defined(__INTEL_COMPILER)
140 #define BROKEN_STACK_ALIGNMENT /* define it if stack is not mod16 */
141 #endif
142 #endif
144 #ifdef WORDS_BIGENDIAN
145 #define endian_fix(x) (x)
146 #define endian_fix32(x) (x)
147 #elif defined(__GNUC__) && defined(HAVE_MMX)
148 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
150 asm("bswap %0":"+r"(x));
151 return x;
153 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
155 asm("bswap %0":"+r"(x));
156 return x;
158 #else
159 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
161 return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
163 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
165 if( WORD_SIZE == 8 )
166 return endian_fix32(x>>32) + ((uint64_t)endian_fix32(x)<<32);
167 else
168 return endian_fix32(x);
170 #endif
172 #ifdef __GNUC__
173 #define x264_clz(x) __builtin_clz(x)
174 #else
175 static int ALWAYS_INLINE x264_clz( uint32_t x )
177 static uint8_t lut[16] = {4,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
178 int y, z = ((x - 0x10000) >> 27) & 16;
179 x >>= z^16;
180 z += y = ((x - 0x100) >> 28) & 8;
181 x >>= y^8;
182 z += y = ((x - 0x10) >> 29) & 4;
183 x >>= y^4;
184 return z + lut[x];
186 #endif
188 #endif /* X264_OSDEP_H */