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

Annotation of /trunk/xvidcore/src/portab.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2147 - (view) (download)

1 : edgomez 407 /*****************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * - Portable macros, types and inlined assembly -
5 :     *
6 : Isibaar 1883 * Copyright(C) 2002-2010 Michael Militzer <isibaar@xvid.org>
7 : edgomez 1382 * 2002-2003 Peter Ross <pross@xvid.org>
8 :     * 2002-2003 Edouard Gomez <ed.gomez@free.fr>
9 : edgomez 407 *
10 : edgomez 1382 * 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 : edgomez 407 * (at your option) any later version.
14 :     *
15 :     * This program is distributed in the hope that it will be useful,
16 : edgomez 1382 * but WITHOUT ANY WARRANTY ; without even the implied warranty of
17 : edgomez 407 * 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 : edgomez 1382 * along with this program ; if not, write to the Free Software
22 : edgomez 407 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 :     *
24 : Isibaar 1988 * $Id$
25 : edgomez 407 *
26 :     ****************************************************************************/
27 :    
28 : Isibaar 3 #ifndef _PORTAB_H_
29 :     #define _PORTAB_H_
30 :    
31 : edgomez 513 /*****************************************************************************
32 :     * Common things
33 :     ****************************************************************************/
34 : suxen_drol 252
35 : edgomez 826 /* Buffer size for msvc implementation because it outputs to DebugOutput */
36 : edgomez 1382 #if defined(_DEBUG)
37 :     extern unsigned int xvid_debug;
38 : suxen_drol 252 #define DPRINTF_BUF_SZ 1024
39 : edgomez 1382 #endif
40 : suxen_drol 252
41 : edgomez 513 /*****************************************************************************
42 : Isibaar 1883 * Types used in Xvid sources
43 : edgomez 513 ****************************************************************************/
44 : suxen_drol 252
45 : edgomez 513 /*----------------------------------------------------------------------------
46 : edgomez 1466 | For MSVC
47 : edgomez 513 *---------------------------------------------------------------------------*/
48 : Isibaar 3
49 : edgomez 851 #if defined(_MSC_VER) || defined (__WATCOMC__)
50 : edgomez 513 # define int8_t char
51 :     # define uint8_t unsigned char
52 :     # define int16_t short
53 :     # define uint16_t unsigned short
54 :     # define int32_t int
55 :     # define uint32_t unsigned int
56 :     # define int64_t __int64
57 :     # define uint64_t unsigned __int64
58 : suxen_drol 136
59 : edgomez 513 /*----------------------------------------------------------------------------
60 : edgomez 1466 | For all other compilers, use the standard header file
61 :     | (compiler should be ISO C99 compatible, perhaps ISO C89 is enough)
62 : edgomez 513 *---------------------------------------------------------------------------*/
63 : h 108
64 : edgomez 826 #else
65 : Isibaar 3
66 : edgomez 826 # include <inttypes.h>
67 : Isibaar 3
68 : suxen_drol 68 #endif
69 : Isibaar 42
70 : edgomez 513 /*****************************************************************************
71 : Isibaar 1883 * Some things that are OS dependant
72 :     ****************************************************************************/
73 :    
74 :     #ifdef WIN32
75 :    
76 :     # include <windows.h>
77 :     # define pthread_t HANDLE
78 :     # define pthread_create(t,u,f,d) *(t)=CreateThread(NULL,0,f,d,0,NULL)
79 :     # define pthread_join(t,s) { WaitForSingleObject(t,INFINITE); \
80 :     CloseHandle(t); }
81 :     # define sched_yield() Sleep(0);
82 :     static __inline int pthread_num_processors_np()
83 :     {
84 :     DWORD p_aff, s_aff, r = 0;
85 :     GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR) &p_aff, (PDWORD_PTR) &s_aff);
86 :     for(; p_aff != 0; p_aff>>=1) r += p_aff&1;
87 :     return r;
88 :     }
89 :    
90 :     #elif defined(__amigaos4__)
91 :    
92 :     # include <pthread.h>
93 : Isibaar 2147 # include <proto/exec.h>
94 : Isibaar 1883
95 : Isibaar 2147 static __inline void amiga_yield(void)
96 :     {
97 :     /* SetTaskPri() on the currently running task triggers a reschedule */
98 :     struct Task *me = IExec->FindTask(NULL);
99 :     IExec->SetTaskPri(me, me->tc_Node.ln_Pri);
100 :     }
101 :     # define sched_yield() amiga_yield()
102 :    
103 : Isibaar 1883 #elif defined(SYS_BEOS)
104 :    
105 :     # include <kernel/OS.h>
106 :     # define pthread_t thread_id
107 :     # define pthread_create(t,u,f,d) { *(t)=spawn_thread(f,"",10,d); \
108 :     resume_thread(*(t)); }
109 :     # define pthread_join(t,s) wait_for_thread(t,(long*)s)
110 :     # define sched_yield() snooze(0) /* is this correct? */
111 :    
112 :     #else
113 :     # include <pthread.h>
114 :     #endif
115 :    
116 :     /*****************************************************************************
117 : edgomez 1382 * Some things that are only architecture dependant
118 : edgomez 513 ****************************************************************************/
119 : Isibaar 3
120 : edgomez 824 #if defined(ARCH_IS_32BIT)
121 : chl 905 # define CACHE_LINE 64
122 : edgomez 513 # define ptr_t uint32_t
123 : chl 905 # define intptr_t int32_t
124 : suxen_drol 1705 # define _INTPTR_T_DEFINED
125 : edgomez 1423 # if defined(_MSC_VER) && _MSC_VER >= 1300 && !defined(__INTEL_COMPILER)
126 :     # include <stdarg.h>
127 :     # else
128 : edgomez 1382 # define uintptr_t uint32_t
129 :     # endif
130 : edgomez 824 #elif defined(ARCH_IS_64BIT)
131 : chl 869 # define CACHE_LINE 64
132 : edgomez 513 # define ptr_t uint64_t
133 : chl 905 # define intptr_t int64_t
134 : suxen_drol 1705 # define _INTPTR_T_DEFINED
135 : edgomez 1423 # if defined (_MSC_VER) && _MSC_VER >= 1300 && !defined(__INTEL_COMPILER)
136 :     # include <stdarg.h>
137 :     # else
138 : suxen_drol 956 # define uintptr_t uint64_t
139 : edgomez 1382 # endif
140 : chl 37 #else
141 : Isibaar 1883 # error You are trying to compile Xvid without defining address bus size.
142 : chl 37 #endif
143 : Isibaar 3
144 : edgomez 513 /*****************************************************************************
145 :     * Things that must be sorted by compiler and then by architecture
146 :     ****************************************************************************/
147 : Isibaar 3
148 : edgomez 513 /*****************************************************************************
149 :     * MSVC compiler specific macros, functions
150 :     ****************************************************************************/
151 : Isibaar 3
152 : edgomez 513 #if defined(_MSC_VER)
153 : canard 73
154 : edgomez 513 /*----------------------------------------------------------------------------
155 : edgomez 1466 | Common msvc stuff
156 : edgomez 513 *---------------------------------------------------------------------------*/
157 : Isibaar 3
158 : edgomez 854 # include <windows.h>
159 :     # include <stdio.h>
160 : edgomez 78
161 : edgomez 1466 /* Non ANSI mapping */
162 : edgomez 854 # define snprintf _snprintf
163 :     # define vsnprintf _vsnprintf
164 :    
165 : edgomez 1466 /*
166 :     * This function must be declared/defined all the time because MSVC does
167 :     * not support C99 variable arguments macros.
168 :     *
169 :     * Btw, if the MS compiler does its job well, it should remove the nop
170 :     * DPRINTF function when not compiling in _DEBUG mode
171 :     */
172 : edgomez 824 # ifdef _DEBUG
173 : edgomez 1466 static __inline void DPRINTF(int level, char *fmt, ...)
174 :     {
175 :     if (xvid_debug & level) {
176 :     va_list args;
177 :     char buf[DPRINTF_BUF_SZ];
178 :     va_start(args, fmt);
179 :     vsprintf(buf, fmt, args);
180 :     va_end(args);
181 : syskin 1774 OutputDebugStringA(buf);
182 : edgomez 1466 fprintf(stderr, "%s", buf);
183 :     }
184 :     }
185 : edgomez 824 # else
186 : edgomez 1466 static __inline void DPRINTF(int level, char *fmt, ...) {}
187 : edgomez 824 # endif
188 : Isibaar 3
189 : edgomez 513 # if _MSC_VER <= 1200
190 :     # define DECLARE_ALIGNED_MATRIX(name,sizex,sizey,type,alignment) \
191 : edgomez 1466 type name##_storage[(sizex)*(sizey)+(alignment)-1]; \
192 :     type * name = (type *) (((int32_t) name##_storage+(alignment - 1)) & ~((int32_t)(alignment)-1))
193 : edgomez 513 # else
194 :     # define DECLARE_ALIGNED_MATRIX(name,sizex,sizey,type,alignment) \
195 : edgomez 1466 __declspec(align(alignment)) type name[(sizex)*(sizey)]
196 : edgomez 513 # endif
197 : Isibaar 3
198 :    
199 : edgomez 513 /*----------------------------------------------------------------------------
200 : edgomez 1466 | msvc x86 specific macros/functions
201 : edgomez 513 *---------------------------------------------------------------------------*/
202 : Isibaar 1827 # if defined(ARCH_IS_IA32)
203 : h 518 # define BSWAP(a) __asm mov eax,a __asm bswap eax __asm mov a, eax
204 : canard 89
205 : edgomez 1466 static __inline int64_t read_counter(void)
206 :     {
207 :     int64_t ts;
208 :     uint32_t ts1, ts2;
209 :     __asm {
210 :     rdtsc
211 :     mov ts1, eax
212 :     mov ts2, edx
213 :     }
214 :     ts = ((uint64_t) ts2 << 32) | ((uint64_t) ts1);
215 :     return ts;
216 :     }
217 : edgomez 195
218 : Isibaar 1827 # elif defined(ARCH_IS_X86_64)
219 :    
220 :     # include <intrin.h>
221 :    
222 :     # define BSWAP(a) ((a) = _byteswap_ulong(a))
223 :    
224 :     static __inline int64_t read_counter(void) { return __rdtsc(); }
225 :    
226 : edgomez 513 /*----------------------------------------------------------------------------
227 : edgomez 1466 | msvc GENERIC (plain C only) - Probably alpha or some embedded device
228 : edgomez 513 *---------------------------------------------------------------------------*/
229 : edgomez 824 # elif defined(ARCH_IS_GENERIC)
230 :     # define BSWAP(a) \
231 : edgomez 1466 ((a) = (((a) & 0xff) << 24) | (((a) & 0xff00) << 8) | \
232 :     (((a) >> 8) & 0xff00) | (((a) >> 24) & 0xff))
233 : edgomez 824
234 : suxen_drol 860 # include <time.h>
235 : edgomez 1466 static __inline int64_t read_counter(void)
236 :     {
237 :     return (int64_t)clock();
238 :     }
239 : edgomez 824
240 :     /*----------------------------------------------------------------------------
241 : edgomez 1466 | msvc Not given architecture - This is probably an user who tries to build
242 : Isibaar 1883 | Xvid the wrong way.
243 : edgomez 824 *---------------------------------------------------------------------------*/
244 : edgomez 513 # else
245 : Isibaar 1883 # error You are trying to compile Xvid without defining the architecture type.
246 : edgomez 513 # endif
247 : edgomez 195
248 :    
249 : Isibaar 209
250 :    
251 : edgomez 513 /*****************************************************************************
252 :     * GNU CC compiler stuff
253 :     ****************************************************************************/
254 : Isibaar 209
255 : chl 674 #elif defined(__GNUC__) || defined(__ICC) /* Compiler test */
256 : Isibaar 209
257 : edgomez 513 /*----------------------------------------------------------------------------
258 : edgomez 1466 | Common gcc stuff
259 : edgomez 513 *---------------------------------------------------------------------------*/
260 : Isibaar 209
261 : edgomez 513 /*
262 :     * As gcc is (mostly) C99 compliant, we define DPRINTF only if it's realy needed
263 :     * and it's a macro calling fprintf directly
264 :     */
265 :     # ifdef _DEBUG
266 : Isibaar 209
267 : edgomez 1472 /* Needed for all debuf fprintf calls */
268 : edgomez 513 # include <stdio.h>
269 : edgomez 824 # include <stdarg.h>
270 : Isibaar 209
271 : edgomez 1472 static __inline void DPRINTF(int level, char *format, ...)
272 :     {
273 :     va_list args;
274 :     va_start(args, format);
275 :     if(xvid_debug & level) {
276 :     vfprintf(stderr, format, args);
277 :     }
278 :     va_end(args);
279 :     }
280 : ia64p 283
281 : edgomez 513 # else /* _DEBUG */
282 : edgomez 1472 static __inline void DPRINTF(int level, char *format, ...) {}
283 : edgomez 513 # endif /* _DEBUG */
284 : Isibaar 209
285 : ia64p 280
286 : edgomez 516 # define DECLARE_ALIGNED_MATRIX(name,sizex,sizey,type,alignment) \
287 : edgomez 1472 type name##_storage[(sizex)*(sizey)+(alignment)-1]; \
288 :     type * name = (type *) (((ptr_t) name##_storage+(alignment - 1)) & ~((ptr_t)(alignment)-1))
289 : edgomez 516
290 : edgomez 513 /*----------------------------------------------------------------------------
291 : edgomez 1472 | gcc IA32 specific macros/functions
292 : edgomez 513 *---------------------------------------------------------------------------*/
293 : edgomez 1586 # if defined(ARCH_IS_IA32) || defined(ARCH_IS_X86_64)
294 : edgomez 513 # define BSWAP(a) __asm__ ( "bswapl %0\n" : "=r" (a) : "0" (a) );
295 : Isibaar 209
296 : edgomez 1472 static __inline int64_t read_counter(void)
297 :     {
298 :     int64_t ts;
299 :     uint32_t ts1, ts2;
300 :     __asm__ __volatile__("rdtsc\n\t":"=a"(ts1), "=d"(ts2));
301 :     ts = ((uint64_t) ts2 << 32) | ((uint64_t) ts1);
302 :     return ts;
303 :     }
304 : ia64p 283
305 : edgomez 513 /*----------------------------------------------------------------------------
306 : edgomez 1472 | gcc PPC and PPC Altivec specific macros/functions
307 : edgomez 513 *---------------------------------------------------------------------------*/
308 : edgomez 824 # elif defined(ARCH_IS_PPC)
309 : edgomez 1412
310 :     # if defined(HAVE_ALTIVEC_PARENTHESES_DECL)
311 :     # define AVV(x...) (x)
312 :     # elif defined(HAVE_ALTIVEC_BRACES_DECL)
313 :     # define AVV(x...) {x}
314 :     # else
315 :     # error Trying to compile PPC target without a vector declaration type.
316 :     # endif
317 :    
318 : edgomez 513 # define BSWAP(a) __asm__ __volatile__ \
319 : edgomez 1472 ( "lwbrx %0,0,%1; eieio" : "=r" (a) : "r" (&(a)), "m" (a));
320 : ia64p 283
321 : edgomez 1472 static __inline unsigned long get_tbl(void)
322 :     {
323 :     unsigned long tbl;
324 :     asm volatile ("mftb %0":"=r" (tbl));
325 :     return tbl;
326 :     }
327 : ia64p 283
328 : edgomez 1472 static __inline unsigned long get_tbu(void)
329 :     {
330 :     unsigned long tbl;
331 :     asm volatile ("mftbu %0":"=r" (tbl));
332 :     return tbl;
333 :     }
334 : ia64p 283
335 : edgomez 1472 static __inline int64_t read_counter(void)
336 :     {
337 :     unsigned long tb, tu;
338 :     do {
339 :     tu = get_tbu();
340 :     tb = get_tbl();
341 :     }while (tb != get_tbl());
342 :     return (((int64_t) tu) << 32) | (int64_t) tb;
343 :     }
344 : ia64p 283
345 : edgomez 513 /*----------------------------------------------------------------------------
346 : edgomez 1472 | gcc IA64 specific macros/functions
347 : edgomez 513 *---------------------------------------------------------------------------*/
348 : edgomez 824 # elif defined(ARCH_IS_IA64)
349 : edgomez 513 # define BSWAP(a) __asm__ __volatile__ \
350 : Skal 1703 ("mux1 %0 = %1, @rev" ";;" \
351 :     "shr.u %0 = %0, 32" : "=r" (a) : "r" (a));
352 : ia64p 283
353 : edgomez 1472 static __inline int64_t read_counter(void)
354 :     {
355 :     unsigned long result;
356 :     __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory");
357 :     return result;
358 :     }
359 : ia64p 283
360 : edgomez 513 /*----------------------------------------------------------------------------
361 : edgomez 1472 | gcc GENERIC (plain C only) specific macros/functions
362 : edgomez 513 *---------------------------------------------------------------------------*/
363 : edgomez 824 # elif defined(ARCH_IS_GENERIC)
364 : edgomez 513 # define BSWAP(a) \
365 : edgomez 1472 ((a) = (((a) & 0xff) << 24) | (((a) & 0xff00) << 8) | \
366 :     (((a) >> 8) & 0xff00) | (((a) >> 24) & 0xff))
367 : ia64p 283
368 : suxen_drol 860 # include <time.h>
369 : edgomez 1472 static __inline int64_t read_counter(void)
370 :     {
371 :     return (int64_t)clock();
372 :     }
373 : ia64p 283
374 : edgomez 513 /*----------------------------------------------------------------------------
375 : edgomez 1472 | gcc Not given architecture - This is probably an user who tries to build
376 : Isibaar 1883 | Xvid the wrong way.
377 : edgomez 513 *---------------------------------------------------------------------------*/
378 :     # else
379 : Isibaar 1883 # error You are trying to compile Xvid without defining the architecture type.
380 : edgomez 824 # endif
381 : edgomez 677
382 : edgomez 854
383 :    
384 :    
385 : edgomez 513 /*****************************************************************************
386 : edgomez 854 * Open WATCOM C/C++ compiler
387 : edgomez 851 ****************************************************************************/
388 : edgomez 854
389 : edgomez 851 #elif defined(__WATCOMC__)
390 :    
391 :     # include <stdio.h>
392 :     # include <stdarg.h>
393 :    
394 :     # ifdef _DEBUG
395 : edgomez 1466 static __inline void DPRINTF(int level, char *fmt, ...)
396 :     {
397 :     if (xvid_debug & level) {
398 :     va_list args;
399 :     char buf[DPRINTF_BUF_SZ];
400 :     va_start(args, fmt);
401 :     vsprintf(buf, fmt, args);
402 :     va_end(args);
403 :     fprintf(stderr, "%s", buf);
404 :     }
405 :     }
406 : edgomez 851 # else /* _DEBUG */
407 : edgomez 1466 static __inline void DPRINTF(int level, char *format, ...) {}
408 : edgomez 851 # endif /* _DEBUG */
409 :    
410 : suxen_drol 860 # define DECLARE_ALIGNED_MATRIX(name,sizex,sizey,type,alignment) \
411 : edgomez 1466 type name##_storage[(sizex)*(sizey)+(alignment)-1]; \
412 :     type * name = (type *) (((int32_t) name##_storage+(alignment - 1)) & ~((int32_t)(alignment)-1))
413 : edgomez 851
414 :     /*----------------------------------------------------------------------------
415 : edgomez 1466 | watcom ia32 specific macros/functions
416 : edgomez 851 *---------------------------------------------------------------------------*/
417 : Isibaar 1807 # if defined(ARCH_IS_IA32) || defined(ARCH_IS_X86_64)
418 : edgomez 851
419 :     # define BSWAP(a) __asm mov eax,a __asm bswap eax __asm mov a, eax
420 : edgomez 854
421 : edgomez 1466 static __inline int64_t read_counter(void)
422 :     {
423 :     uint64_t ts;
424 :     uint32_t ts1, ts2;
425 :     __asm {
426 :     rdtsc
427 :     mov ts1, eax
428 :     mov ts2, edx
429 :     }
430 :     ts = ((uint64_t) ts2 << 32) | ((uint64_t) ts1);
431 :     return ts;
432 :     }
433 : edgomez 851
434 :     /*----------------------------------------------------------------------------
435 : edgomez 1466 | watcom GENERIC (plain C only) specific macros/functions.
436 : edgomez 851 *---------------------------------------------------------------------------*/
437 : suxen_drol 860 # elif defined(ARCH_IS_GENERIC)
438 : edgomez 851
439 : suxen_drol 860 # define BSWAP(x) \
440 : edgomez 1466 x = ((((x) & 0xff000000) >> 24) | \
441 :     (((x) & 0x00ff0000) >> 8) | \
442 :     (((x) & 0x0000ff00) << 8) | \
443 :     (((x) & 0x000000ff) << 24))
444 : edgomez 854
445 : edgomez 1466 static int64_t read_counter() { return 0; }
446 : edgomez 851
447 : edgomez 854 /*----------------------------------------------------------------------------
448 : edgomez 1466 | watcom Not given architecture - This is probably an user who tries to build
449 : Isibaar 1883 | Xvid the wrong way.
450 : edgomez 854 *---------------------------------------------------------------------------*/
451 :     # else
452 : Isibaar 1883 # error You are trying to compile Xvid without defining the architecture type.
453 : edgomez 854 # endif
454 : edgomez 851
455 : edgomez 854
456 : edgomez 851 /*****************************************************************************
457 : edgomez 513 * Unknown compiler
458 :     ****************************************************************************/
459 :     #else /* Compiler test */
460 : Isibaar 3
461 : edgomez 1466 /*
462 :     * Ok we know nothing about the compiler, so we fallback to ANSI C
463 :     * features, so every compiler should be happy and compile the code.
464 :     *
465 :     * This is (mostly) equivalent to ARCH_IS_GENERIC.
466 :     */
467 : Isibaar 3
468 : edgomez 826 # ifdef _DEBUG
469 :    
470 : edgomez 1466 /* Needed for all debuf fprintf calls */
471 : edgomez 826 # include <stdio.h>
472 :     # include <stdarg.h>
473 :    
474 : edgomez 1466 static __inline void DPRINTF(int level, char *format, ...)
475 :     {
476 :     va_list args;
477 :     va_start(args, format);
478 :     if(xvid_debug & level) {
479 :     vfprintf(stderr, format, args);
480 :     }
481 :     va_end(args);
482 :     }
483 : edgomez 826
484 :     # else /* _DEBUG */
485 : edgomez 1466 static __inline void DPRINTF(int level, char *format, ...) {}
486 : edgomez 826 # endif /* _DEBUG */
487 :    
488 :     # define BSWAP(a) \
489 : edgomez 1466 ((a) = (((a) & 0xff) << 24) | (((a) & 0xff00) << 8) | \
490 :     (((a) >> 8) & 0xff00) | (((a) >> 24) & 0xff))
491 : edgomez 826
492 : suxen_drol 860 # include <time.h>
493 : edgomez 1466 static __inline int64_t read_counter(void)
494 :     {
495 :     return (int64_t)clock();
496 :     }
497 : edgomez 826
498 : suxen_drol 860 # define DECLARE_ALIGNED_MATRIX(name,sizex,sizey,type,alignment) \
499 : edgomez 1466 type name[(sizex)*(sizey)]
500 : edgomez 826
501 : edgomez 513 #endif /* Compiler test */
502 : Isibaar 3
503 : Isibaar 209
504 : edgomez 854 #endif /* PORTAB_H */

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