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