]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavutil/internal.h
add FASTDIV macro for ARM. Reported speed-up on MPEG-4 decoding: 1.8%
[frescor/ffmpeg.git] / libavutil / internal.h
1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 /**
20  * @file internal.h
21  * common internal api header.
22  */
23
24 #ifndef INTERNAL_H
25 #define INTERNAL_H
26
27 #if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC)
28 #    define PIC
29 #endif
30
31 #    ifndef ENODATA
32 #        define ENODATA  61
33 #    endif
34
35 #include "bswap.h"
36
37 #include <stddef.h>
38 #ifndef offsetof
39 # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
40 #endif
41
42 #ifdef __MINGW32__
43 #    ifdef _DEBUG
44 #        define DEBUG
45 #    endif
46
47 #    define snprintf _snprintf
48 #    define vsnprintf _vsnprintf
49
50 #    ifdef CONFIG_WINCE
51 #        define perror(a)
52 #    endif
53
54 /* __MINGW32__ end */
55 #elif defined (CONFIG_OS2)
56 /* OS/2 EMX */
57
58 #include <float.h>
59
60 #endif /* !__MINGW32__ && CONFIG_OS2 */
61
62 #    ifdef USE_FASTMEMCPY
63 #        include "libvo/fastmemcpy.h"
64 #    endif
65
66 // Use rip-relative addressing if compiling PIC code on x86-64.
67 #    if defined(__MINGW32__) || defined(__CYGWIN__) || \
68         defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
69 #        if defined(ARCH_X86_64) && defined(PIC)
70 #            define MANGLE(a) "_" #a"(%%rip)"
71 #        else
72 #            define MANGLE(a) "_" #a
73 #        endif
74 #    else
75 #        if defined(ARCH_X86_64) && defined(PIC)
76 #            define MANGLE(a) #a"(%%rip)"
77 #        elif defined(CONFIG_DARWIN)
78 #            define MANGLE(a) "_" #a
79 #        else
80 #            define MANGLE(a) #a
81 #        endif
82 #    endif
83
84 /* debug stuff */
85
86 #    if !defined(DEBUG) && !defined(NDEBUG)
87 #        define NDEBUG
88 #    endif
89 #    include <assert.h>
90
91 /* dprintf macros */
92 #    ifdef DEBUG
93 #        define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
94 #    else
95 #        define dprintf(fmt,...)
96 #    endif
97
98 #    ifdef CONFIG_WINCE
99 #            define abort()
100 #    endif
101
102 #    define av_abort()      do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
103
104 extern const uint32_t inverse[256];
105
106 #if defined(ARCH_X86) || defined(ARCH_X86_64)
107 #    define FASTDIV(a,b) \
108     ({\
109         int ret,dmy;\
110         asm volatile(\
111             "mull %3"\
112             :"=d"(ret),"=a"(dmy)\
113             :"1"(a),"g"(inverse[b])\
114             );\
115         ret;\
116     })
117 #elif defined(ARCH_ARMV4L)
118 #    define FASTDIV(a,b) \
119     ({\
120         int ret,dmy;\
121         asm volatile(\
122             "umull %1, %0, %2, %3"\
123             :"=&r"(ret),"=&r"(dmy)\
124             :"r"(a),"r"(inverse[b])\
125             );\
126         ret;\
127     })
128 #elif defined(CONFIG_FASTDIV)
129 #    define FASTDIV(a,b)   ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
130 #else
131 #    define FASTDIV(a,b)   ((a)/(b))
132 #endif
133
134 /* math */
135 extern FF_IMPORT_ATTR const uint8_t ff_sqrt_tab[128];
136
137 static inline int ff_sqrt(int a)
138 {
139     int ret=0;
140     int s;
141     int ret_sq=0;
142
143     if(a<128) return ff_sqrt_tab[a];
144
145     for(s=15; s>=0; s--){
146         int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
147         if(b<=a){
148             ret_sq=b;
149             ret+= 1<<s;
150         }
151     }
152     return ret;
153 }
154
155 #if defined(ARCH_X86) || defined(ARCH_X86_64)
156 #define MASK_ABS(mask, level)\
157             asm volatile(\
158                 "cdq                    \n\t"\
159                 "xorl %1, %0            \n\t"\
160                 "subl %1, %0            \n\t"\
161                 : "+a" (level), "=&d" (mask)\
162             );
163 #else
164 #define MASK_ABS(mask, level)\
165             mask= level>>31;\
166             level= (level^mask)-mask;
167 #endif
168
169 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
170 #define COPY3_IF_LT(x,y,a,b,c,d)\
171 asm volatile (\
172     "cmpl %0, %3        \n\t"\
173     "cmovl %3, %0       \n\t"\
174     "cmovl %4, %1       \n\t"\
175     "cmovl %5, %2       \n\t"\
176     : "+r" (x), "+r" (a), "+r" (c)\
177     : "r" (y), "r" (b), "r" (d)\
178 );
179 #else
180 #define COPY3_IF_LT(x,y,a,b,c,d)\
181 if((y)<(x)){\
182      (x)=(y);\
183      (a)=(b);\
184      (c)=(d);\
185 }
186 #endif
187
188 /* avoid usage of various functions */
189 #define malloc please_use_av_malloc
190 #define free please_use_av_free
191 #define realloc please_use_av_realloc
192 #define time time_is_forbidden_due_to_security_issues
193 #define rand rand_is_forbidden_due_to_state_trashing
194 #define srand srand_is_forbidden_due_to_state_trashing
195 #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
196 #define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
197 #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
198 #define printf please_use_av_log
199 #define fprintf please_use_av_log
200 #endif
201
202 #define CHECKED_ALLOCZ(p, size)\
203 {\
204     p= av_mallocz(size);\
205     if(p==NULL && (size)!=0){\
206         perror("malloc");\
207         goto fail;\
208     }\
209 }
210
211 #ifndef HAVE_LRINTF
212 /* XXX: add ISOC specific test to avoid specific BSD testing. */
213 /* better than nothing implementation. */
214 /* btw, rintf() is existing on fbsd too -- alex */
215 static always_inline long int lrintf(float x)
216 {
217 #ifdef __MINGW32__
218 #  ifdef ARCH_X86
219     int32_t i;
220     asm volatile(
221         "fistpl %0\n\t"
222         : "=m" (i) : "t" (x) : "st"
223     );
224     return i;
225 #  else
226     /* XXX: incorrect, but make it compile */
227     return (int)(x + (x < 0 ? -0.5 : 0.5));
228 #  endif /* ARCH_X86 */
229 #else
230     return (int)(rint(x));
231 #endif /* __MINGW32__ */
232 }
233 #endif /* HAVE_LRINTF */
234
235 #endif /* INTERNAL_H */