[cvs] / xvidcore / src / portab.h Repository:
ViewVC logotype

Annotation of /xvidcore/src/portab.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.47 - (view) (download)

1 : edgomez 1.27 /*****************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * - Portable macros, types and inlined assembly -
5 :     *
6 : edgomez 1.33 * Copyright(C) 2002 Michael Militzer <isibaar@xvid.org>
7 : edgomez 1.34 * 2002 Peter Ross <pross@xvid.org>
8 : edgomez 1.40 * 2002 Edouard Gomez <ed.gomez@free.fr>
9 : edgomez 1.27 *
10 : edgomez 1.35 * This file is part of XviD, a free MPEG-4 video encoder/decoder
11 :     *
12 :     * XviD is free software; you can redistribute it and/or modify it
13 :     * under the terms of the GNU General Public License as published by
14 :     * the Free Software Foundation; either version 2 of the License, or
15 : edgomez 1.27 * (at your option) any later version.
16 :     *
17 :     * This program is distributed in the hope that it will be useful,
18 : edgomez 1.35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 : edgomez 1.27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 :     * GNU General Public License for more details.
21 :     *
22 :     * You should have received a copy of the GNU General Public License
23 : edgomez 1.35 * along with this program; if not, write to the Free Software
24 : edgomez 1.27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 :     *
26 : chl 1.47 * $Id: portab.h,v 1.46 2003/02/18 15:48:15 chl Exp $
27 : edgomez 1.27 *
28 :     ****************************************************************************/
29 :    
30 : Isibaar 1.1 #ifndef _PORTAB_H_
31 :     #define _PORTAB_H_
32 :    
33 : edgomez 1.28 /*****************************************************************************
34 :     * Common things
35 :     ****************************************************************************/
36 : suxen_drol 1.23
37 : edgomez 1.28 /* Debug level masks */
38 : edgomez 1.44 #define DPRINTF_ERROR 0x00000001
39 :     #define DPRINTF_STARTCODE 0x00000002
40 :     #define DPRINTF_HEADER 0x00000004
41 :     #define DPRINTF_TIMECODE 0x00000008
42 :     #define DPRINTF_MB 0x00000010
43 :     #define DPRINTF_COEFF 0x00000020
44 :     #define DPRINTF_MV 0x00000040
45 :     #define DPRINTF_RC 0x00000080
46 :     #define DPRINTF_DEBUG 0x80000000
47 : suxen_drol 1.23
48 : edgomez 1.28 /* debug level for this library */
49 : edgomez 1.44 #ifdef _DEBUG
50 :     #define DPRINTF_LEVEL 0x000000ff
51 :     #else
52 :     #define DPRINTF_LEVEL 0
53 :     #endif
54 : suxen_drol 1.23
55 : edgomez 1.42 /* Buffer size for msvc implementation because it outputs to DebugOutput */
56 : suxen_drol 1.23 #define DPRINTF_BUF_SZ 1024
57 :    
58 : edgomez 1.28 /*****************************************************************************
59 :     * Types used in XviD sources
60 :     ****************************************************************************/
61 : suxen_drol 1.23
62 : edgomez 1.28 /*----------------------------------------------------------------------------
63 : edgomez 1.41 | For MSVC
64 : edgomez 1.28 *---------------------------------------------------------------------------*/
65 :    
66 : edgomez 1.43 #if defined(_MSC_VER) || defined (__WATCOMC__)
67 : edgomez 1.28 # define int8_t char
68 :     # define uint8_t unsigned char
69 :     # define int16_t short
70 :     # define uint16_t unsigned short
71 :     # define int32_t int
72 :     # define uint32_t unsigned int
73 :     # define int64_t __int64
74 :     # define uint64_t unsigned __int64
75 :    
76 :     /*----------------------------------------------------------------------------
77 : edgomez 1.42 | For all other compilers, use the standard header file
78 :     | (compiler should be ISO C99 compatible, perhaps ISO C89 is enough)
79 : edgomez 1.28 *---------------------------------------------------------------------------*/
80 :    
81 : edgomez 1.42 #else
82 : edgomez 1.28
83 : edgomez 1.42 # include <inttypes.h>
84 : suxen_drol 1.16
85 : h 1.2 #endif
86 : Isibaar 1.1
87 : edgomez 1.28 /*****************************************************************************
88 : chl 1.46 * Some things that are only architecture dependant
89 : edgomez 1.28 ****************************************************************************/
90 : Isibaar 1.1
91 : edgomez 1.41 #if defined(ARCH_IS_32BIT)
92 : chl 1.47 # define CACHE_LINE 64
93 : edgomez 1.28 # define ptr_t uint32_t
94 : chl 1.47 # define intptr_t int32_t
95 :     # define uintptr_t uint32_t
96 : edgomez 1.41 #elif defined(ARCH_IS_64BIT)
97 : chl 1.46 # define CACHE_LINE 64
98 : edgomez 1.28 # define ptr_t uint64_t
99 : chl 1.47 # define intptr_t int64_t
100 :     # define uintptr_t uint64_t
101 : suxen_drol 1.7 #else
102 : edgomez 1.41 # error You are trying to compile XviD without defining address bus size.
103 : suxen_drol 1.7 #endif
104 : Isibaar 1.4
105 : edgomez 1.28 /*****************************************************************************
106 :     * Things that must be sorted by compiler and then by architecture
107 :     ****************************************************************************/
108 : Isibaar 1.1
109 : edgomez 1.28 /*****************************************************************************
110 :     * MSVC compiler specific macros, functions
111 :     ****************************************************************************/
112 : edgomez 1.18
113 : edgomez 1.28 #if defined(_MSC_VER)
114 : edgomez 1.18
115 : edgomez 1.28 /*----------------------------------------------------------------------------
116 :     | Common msvc stuff
117 :     *---------------------------------------------------------------------------*/
118 : Isibaar 1.1
119 : edgomez 1.44 # include <windows.h>
120 :     # include <stdio.h>
121 :    
122 :     /* Non ANSI mapping */
123 :     # define snprintf _snprintf
124 :     # define vsnprintf _vsnprintf
125 : suxen_drol 1.23
126 : edgomez 1.28 /*
127 :     * This function must be declared/defined all the time because MSVC does
128 : edgomez 1.41 * not support C99 variable arguments macros.
129 :     *
130 :     * Btw, if the MS compiler does its job well, it should remove the nop
131 :     * DPRINTF function when not compiling in _DEBUG mode
132 : edgomez 1.28 */
133 : edgomez 1.41 # ifdef _DEBUG
134 : edgomez 1.28 static __inline void DPRINTF(int level, char *fmt, ...)
135 :     {
136 :     if (DPRINTF_LEVEL & level) {
137 :     va_list args;
138 :     char buf[DPRINTF_BUF_SZ];
139 :     va_start(args, fmt);
140 :     vsprintf(buf, fmt, args);
141 :     OutputDebugString(buf);
142 :     fprintf(stderr, "%s\n", buf);
143 :     }
144 :     }
145 : edgomez 1.41 # else
146 : suxen_drol 1.45 static __inline void DPRINTF(int level, char *fmt, ...) {}
147 : edgomez 1.41 # endif
148 : edgomez 1.28
149 :     # if _MSC_VER <= 1200
150 :     # define DECLARE_ALIGNED_MATRIX(name,sizex,sizey,type,alignment) \
151 :     type name##_storage[(sizex)*(sizey)+(alignment)-1]; \
152 :     type * name = (type *) (((int32_t) name##_storage+(alignment - 1)) & ~((int32_t)(alignment)-1))
153 :     # else
154 :     # define DECLARE_ALIGNED_MATRIX(name,sizex,sizey,type,alignment) \
155 :     __declspec(align(alignment)) type name[(sizex)*(sizey)]
156 :     # endif
157 :    
158 :    
159 :     /*----------------------------------------------------------------------------
160 :     | msvc x86 specific macros/functions
161 :     *---------------------------------------------------------------------------*/
162 : edgomez 1.41 # if defined(ARCH_IS_IA32)
163 : h 1.31 # define BSWAP(a) __asm mov eax,a __asm bswap eax __asm mov a, eax
164 : edgomez 1.28
165 : suxen_drol 1.45 static __inline int64_t read_counter(void)
166 :     {
167 :     int64_t ts;
168 :     uint32_t ts1, ts2;
169 :     __asm {
170 :     rdtsc
171 :     mov ts1, eax
172 :     mov ts2, edx
173 : edgomez 1.28 }
174 : suxen_drol 1.45 ts = ((uint64_t) ts2 << 32) | ((uint64_t) ts1);
175 :     return ts;
176 :     }
177 : edgomez 1.28
178 :     /*----------------------------------------------------------------------------
179 : edgomez 1.41 | msvc GENERIC (plain C only) - Probably alpha or some embedded device
180 :     *---------------------------------------------------------------------------*/
181 :     # elif defined(ARCH_IS_GENERIC)
182 :     # define BSWAP(a) \
183 :     ((a) = (((a) & 0xff) << 24) | (((a) & 0xff00) << 8) | \
184 :     (((a) >> 8) & 0xff00) | (((a) >> 24) & 0xff))
185 :    
186 : suxen_drol 1.45 # include <time.h>
187 :     static __inline int64_t read_counter(void)
188 :     {
189 :     return (int64_t)clock();
190 :     }
191 : edgomez 1.41
192 :     /*----------------------------------------------------------------------------
193 :     | msvc Not given architecture - This is probably an user who tries to build
194 :     | XviD the wrong way.
195 : edgomez 1.28 *---------------------------------------------------------------------------*/
196 :     # else
197 : edgomez 1.41 # error You are trying to compile XviD without defining the architecture type.
198 : edgomez 1.28 # endif
199 : suxen_drol 1.22
200 : chl 1.3
201 : Isibaar 1.1
202 :    
203 : edgomez 1.28 /*****************************************************************************
204 :     * GNU CC compiler stuff
205 :     ****************************************************************************/
206 : canard 1.8
207 : chl 1.37 #elif defined(__GNUC__) || defined(__ICC) /* Compiler test */
208 : Isibaar 1.1
209 : edgomez 1.28 /*----------------------------------------------------------------------------
210 :     | Common gcc stuff
211 :     *---------------------------------------------------------------------------*/
212 :    
213 :     /*
214 :     * As gcc is (mostly) C99 compliant, we define DPRINTF only if it's realy needed
215 :     * and it's a macro calling fprintf directly
216 :     */
217 :     # ifdef _DEBUG
218 :    
219 :     /* Needed for all debuf fprintf calls */
220 :     # include <stdio.h>
221 : edgomez 1.41 # include <stdarg.h>
222 : edgomez 1.28
223 : edgomez 1.41 static __inline void DPRINTF(int level, char *format, ...)
224 :     {
225 :     va_list args;
226 :     va_start(args, format);
227 :     if(DPRINTF_LEVEL & level) {
228 :     vfprintf(stderr, format, args);
229 : edgomez 1.42 fprintf(stderr, "\n");
230 : suxen_drol 1.45 }
231 :     }
232 : edgomez 1.28
233 :     # else /* _DEBUG */
234 : edgomez 1.41 static __inline void DPRINTF(int level, char *format, ...) {}
235 : edgomez 1.28 # endif /* _DEBUG */
236 :    
237 :    
238 : edgomez 1.29 # define DECLARE_ALIGNED_MATRIX(name,sizex,sizey,type,alignment) \
239 :     type name##_storage[(sizex)*(sizey)+(alignment)-1]; \
240 :     type * name = (type *) (((ptr_t) name##_storage+(alignment - 1)) & ~((ptr_t)(alignment)-1))
241 : edgomez 1.28
242 :     /*----------------------------------------------------------------------------
243 : edgomez 1.41 | gcc IA32 specific macros/functions
244 : edgomez 1.28 *---------------------------------------------------------------------------*/
245 : edgomez 1.41 # if defined(ARCH_IS_IA32)
246 : edgomez 1.28 # define BSWAP(a) __asm__ ( "bswapl %0\n" : "=r" (a) : "0" (a) );
247 :    
248 : suxen_drol 1.45 static __inline int64_t read_counter(void)
249 :     {
250 :     int64_t ts;
251 :     uint32_t ts1, ts2;
252 :     __asm__ __volatile__("rdtsc\n\t":"=a"(ts1), "=d"(ts2));
253 :     ts = ((uint64_t) ts2 << 32) | ((uint64_t) ts1);
254 :     return ts;
255 :     }
256 : edgomez 1.28
257 :     /*----------------------------------------------------------------------------
258 :     | gcc PPC and PPC Altivec specific macros/functions
259 :     *---------------------------------------------------------------------------*/
260 : edgomez 1.41 # elif defined(ARCH_IS_PPC)
261 : edgomez 1.28 # define BSWAP(a) __asm__ __volatile__ \
262 :     ( "lwbrx %0,0,%1; eieio" : "=r" (a) : "r" (&(a)), "m" (a));
263 :    
264 : suxen_drol 1.45 static __inline unsigned long get_tbl(void)
265 :     {
266 :     unsigned long tbl;
267 :     asm volatile ("mftb %0":"=r" (tbl));
268 :     return tbl;
269 :     }
270 : edgomez 1.28
271 : suxen_drol 1.45 static __inline unsigned long get_tbu(void)
272 :     {
273 :     unsigned long tbl;
274 :     asm volatile ("mftbu %0":"=r" (tbl));
275 :     return tbl;
276 :     }
277 : edgomez 1.28
278 : suxen_drol 1.45 static __inline int64_t read_counter(void)
279 :     {
280 :     unsigned long tb, tu;
281 :     do {
282 :     tu = get_tbu();
283 :     tb = get_tbl();
284 :     }while (tb != get_tbl());
285 :     return (((int64_t) tu) << 32) | (int64_t) tb;
286 :     }
287 : edgomez 1.28
288 :     /*----------------------------------------------------------------------------
289 :     | gcc IA64 specific macros/functions
290 :     *---------------------------------------------------------------------------*/
291 : edgomez 1.41 # elif defined(ARCH_IS_IA64)
292 : edgomez 1.28 # define BSWAP(a) __asm__ __volatile__ \
293 :     ("mux1 %1 = %0, @rev" ";;" \
294 :     "shr.u %1 = %1, 32" : "=r" (a) : "r" (a));
295 :    
296 : suxen_drol 1.45 static __inline int64_t read_counter(void)
297 :     {
298 :     unsigned long result;
299 :     __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory");
300 :     return result;
301 :     }
302 : edgomez 1.28
303 :     /*----------------------------------------------------------------------------
304 : edgomez 1.41 | gcc GENERIC (plain C only) specific macros/functions
305 : edgomez 1.28 *---------------------------------------------------------------------------*/
306 : edgomez 1.41 # elif defined(ARCH_IS_GENERIC)
307 : edgomez 1.28 # define BSWAP(a) \
308 : edgomez 1.30 ((a) = (((a) & 0xff) << 24) | (((a) & 0xff00) << 8) | \
309 : edgomez 1.28 (((a) >> 8) & 0xff00) | (((a) >> 24) & 0xff))
310 :    
311 : suxen_drol 1.45 # include <time.h>
312 :     static __inline int64_t read_counter(void)
313 :     {
314 :     return (int64_t)clock();
315 :     }
316 : edgomez 1.28
317 :     /*----------------------------------------------------------------------------
318 : edgomez 1.41 | gcc Not given architecture - This is probably an user who tries to build
319 :     | XviD the wrong way.
320 : edgomez 1.28 *---------------------------------------------------------------------------*/
321 :     # else
322 : edgomez 1.41 # error You are trying to compile XviD without defining the architecture type.
323 :     # endif
324 : edgomez 1.43
325 : edgomez 1.44
326 :    
327 :    
328 : edgomez 1.43 /*****************************************************************************
329 : edgomez 1.44 * Open WATCOM C/C++ compiler
330 : edgomez 1.43 ****************************************************************************/
331 : edgomez 1.44
332 : edgomez 1.43 #elif defined(__WATCOMC__)
333 :    
334 :     # include <stdio.h>
335 :     # include <stdarg.h>
336 :    
337 :     # ifdef _DEBUG
338 : suxen_drol 1.45 static __inline void DPRINTF(int level, char *fmt, ...)
339 :     {
340 :     if (DPRINTF_LEVEL & level) {
341 :     va_list args;
342 :     char buf[DPRINTF_BUF_SZ];
343 :     va_start(args, fmt);
344 :     vsprintf(buf, fmt, args);
345 :     fprintf(stderr, "%s\n", buf);
346 :     }
347 :     }
348 : edgomez 1.43 # else /* _DEBUG */
349 : suxen_drol 1.45 static __inline void DPRINTF(int level, char *format, ...) {}
350 : edgomez 1.43 # endif /* _DEBUG */
351 :    
352 : suxen_drol 1.45 # define DECLARE_ALIGNED_MATRIX(name,sizex,sizey,type,alignment) \
353 : edgomez 1.43 type name##_storage[(sizex)*(sizey)+(alignment)-1]; \
354 :     type * name = (type *) (((int32_t) name##_storage+(alignment - 1)) & ~((int32_t)(alignment)-1))
355 :    
356 :     /*----------------------------------------------------------------------------
357 : edgomez 1.44 | watcom ia32 specific macros/functions
358 : edgomez 1.43 *---------------------------------------------------------------------------*/
359 :     # if defined(ARCH_IS_IA32)
360 :    
361 :     # define BSWAP(a) __asm mov eax,a __asm bswap eax __asm mov a, eax
362 : edgomez 1.44
363 : edgomez 1.43 static __inline int64_t read_counter(void)
364 :     {
365 :     uint64_t ts;
366 :     uint32_t ts1, ts2;
367 :     __asm {
368 :     rdtsc
369 :     mov ts1, eax
370 :     mov ts2, edx
371 :     }
372 :     ts = ((uint64_t) ts2 << 32) | ((uint64_t) ts1);
373 :     return ts;
374 :     }
375 :    
376 :     /*----------------------------------------------------------------------------
377 : edgomez 1.44 | watcom GENERIC (plain C only) specific macros/functions.
378 : edgomez 1.43 *---------------------------------------------------------------------------*/
379 : suxen_drol 1.45 # elif defined(ARCH_IS_GENERIC)
380 : edgomez 1.43
381 : suxen_drol 1.45 # define BSWAP(x) \
382 :     x = ((((x) & 0xff000000) >> 24) | \
383 :     (((x) & 0x00ff0000) >> 8) | \
384 :     (((x) & 0x0000ff00) << 8) | \
385 :     (((x) & 0x000000ff) << 24))
386 : edgomez 1.44
387 : edgomez 1.43 static int64_t read_counter() { return 0; }
388 :    
389 : edgomez 1.44 /*----------------------------------------------------------------------------
390 :     | watcom Not given architecture - This is probably an user who tries to build
391 :     | XviD the wrong way.
392 :     *---------------------------------------------------------------------------*/
393 :     # else
394 :     # error You are trying to compile XviD without defining the architecture type.
395 :     # endif
396 :    
397 : edgomez 1.9
398 : edgomez 1.28 /*****************************************************************************
399 :     * Unknown compiler
400 :     ****************************************************************************/
401 :     #else /* Compiler test */
402 : Isibaar 1.21
403 : edgomez 1.42 /*
404 : suxen_drol 1.45 * Ok we know nothing about the compiler, so we fallback to ANSI C
405 :     * features, so every compiler should be happy and compile the code.
406 :     *
407 :     * This is (mostly) equivalent to ARCH_IS_GENERIC.
408 :     */
409 : edgomez 1.42
410 :     # ifdef _DEBUG
411 :    
412 :     /* Needed for all debuf fprintf calls */
413 :     # include <stdio.h>
414 :     # include <stdarg.h>
415 :    
416 :     static __inline void DPRINTF(int level, char *format, ...)
417 :     {
418 :     va_list args;
419 :     va_start(args, format);
420 :     if(DPRINTF_LEVEL & level) {
421 :     vfprintf(stderr, format, args);
422 :     fprintf(stderr, "\n");
423 : suxen_drol 1.45 }
424 :     }
425 : edgomez 1.42
426 :     # else /* _DEBUG */
427 :     static __inline void DPRINTF(int level, char *format, ...) {}
428 :     # endif /* _DEBUG */
429 :    
430 :     # define BSWAP(a) \
431 :     ((a) = (((a) & 0xff) << 24) | (((a) & 0xff00) << 8) | \
432 :     (((a) >> 8) & 0xff00) | (((a) >> 24) & 0xff))
433 :    
434 : suxen_drol 1.45 # include <time.h>
435 :     static __inline int64_t read_counter(void)
436 :     {
437 :     return (int64_t)clock();
438 :     }
439 : edgomez 1.42
440 : suxen_drol 1.45 # define DECLARE_ALIGNED_MATRIX(name,sizex,sizey,type,alignment) \
441 : edgomez 1.42 type name[(sizex)*(sizey)]
442 : Isibaar 1.21
443 : edgomez 1.28 #endif /* Compiler test */
444 : Isibaar 1.21
445 : Isibaar 1.1
446 : edgomez 1.44 #endif /* PORTAB_H */

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4