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