Move constant multiplication out of the loop
[ffmpeg-lucabe.git] / libavutil / common.h
blob42fe9515d51ce0ce768f932ebeab75a49ea7b5ac
1 /*
2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
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.
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.
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
21 /**
22 * @file common.h
23 * common internal and external API header
26 #ifndef AVUTIL_COMMON_H
27 #define AVUTIL_COMMON_H
29 #include <inttypes.h>
31 #ifdef HAVE_AV_CONFIG_H
32 /* only include the following when compiling package */
33 # include "config.h"
35 # include <stdlib.h>
36 # include <stdio.h>
37 # include <string.h>
38 # include <ctype.h>
39 # include <limits.h>
40 # include <errno.h>
41 # include <math.h>
42 #endif /* HAVE_AV_CONFIG_H */
44 #ifndef av_always_inline
45 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
46 # define av_always_inline __attribute__((always_inline)) inline
47 #else
48 # define av_always_inline inline
49 #endif
50 #endif
52 #ifndef av_noinline
53 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
54 # define av_noinline __attribute__((noinline))
55 #else
56 # define av_noinline
57 #endif
58 #endif
60 #ifndef av_pure
61 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
62 # define av_pure __attribute__((pure))
63 #else
64 # define av_pure
65 #endif
66 #endif
68 #ifndef av_const
69 #if defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ > 5)
70 # define av_const __attribute__((const))
71 #else
72 # define av_const
73 #endif
74 #endif
76 #ifndef av_cold
77 #if defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ > 2)
78 # define av_cold __attribute__((cold))
79 #else
80 # define av_cold
81 #endif
82 #endif
84 #ifdef HAVE_AV_CONFIG_H
85 # include "internal.h"
86 #endif /* HAVE_AV_CONFIG_H */
88 #ifndef attribute_deprecated
89 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
90 # define attribute_deprecated __attribute__((deprecated))
91 #else
92 # define attribute_deprecated
93 #endif
94 #endif
96 #ifndef av_unused
97 #if defined(__GNUC__)
98 # define av_unused __attribute__((unused))
99 #else
100 # define av_unused
101 #endif
102 #endif
104 #include "mem.h"
106 //rounded divison & shift
107 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
108 /* assume b>0 */
109 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
110 #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
111 #define FFSIGN(a) ((a) > 0 ? 1 : -1)
113 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
114 #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
115 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
116 #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
118 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
120 /* misc math functions */
121 extern const uint8_t ff_log2_tab[256];
123 static inline av_const int av_log2(unsigned int v)
125 int n = 0;
126 if (v & 0xffff0000) {
127 v >>= 16;
128 n += 16;
130 if (v & 0xff00) {
131 v >>= 8;
132 n += 8;
134 n += ff_log2_tab[v];
136 return n;
139 static inline av_const int av_log2_16bit(unsigned int v)
141 int n = 0;
142 if (v & 0xff00) {
143 v >>= 8;
144 n += 8;
146 n += ff_log2_tab[v];
148 return n;
151 /* median of 3 */
152 static inline av_const int mid_pred(int a, int b, int c)
154 #ifdef HAVE_CMOV
155 int i=b;
156 asm volatile(
157 "cmp %2, %1 \n\t"
158 "cmovg %1, %0 \n\t"
159 "cmovg %2, %1 \n\t"
160 "cmp %3, %1 \n\t"
161 "cmovl %3, %1 \n\t"
162 "cmp %1, %0 \n\t"
163 "cmovg %1, %0 \n\t"
164 :"+&r"(i), "+&r"(a)
165 :"r"(b), "r"(c)
167 return i;
168 #elif 0
169 int t= (a-b)&((a-b)>>31);
170 a-=t;
171 b+=t;
172 b-= (b-c)&((b-c)>>31);
173 b+= (a-b)&((a-b)>>31);
175 return b;
176 #else
177 if(a>b){
178 if(c>b){
179 if(c>a) b=a;
180 else b=c;
182 }else{
183 if(b>c){
184 if(c>a) b=c;
185 else b=a;
188 return b;
189 #endif
193 * clip a signed integer value into the amin-amax range
194 * @param a value to clip
195 * @param amin minimum value of the clip range
196 * @param amax maximum value of the clip range
197 * @return clipped value
199 static inline av_const int av_clip(int a, int amin, int amax)
201 if (a < amin) return amin;
202 else if (a > amax) return amax;
203 else return a;
207 * clip a signed integer value into the 0-255 range
208 * @param a value to clip
209 * @return clipped value
211 static inline av_const uint8_t av_clip_uint8(int a)
213 if (a&(~255)) return (-a)>>31;
214 else return a;
218 * clip a signed integer value into the -32768,32767 range
219 * @param a value to clip
220 * @return clipped value
222 static inline av_const int16_t av_clip_int16(int a)
224 if ((a+32768) & ~65535) return (a>>31) ^ 32767;
225 else return a;
229 * clip a float value into the amin-amax range
230 * @param a value to clip
231 * @param amin minimum value of the clip range
232 * @param amax maximum value of the clip range
233 * @return clipped value
235 static inline av_const float av_clipf(float a, float amin, float amax)
237 if (a < amin) return amin;
238 else if (a > amax) return amax;
239 else return a;
242 /* math */
243 int64_t av_const ff_gcd(int64_t a, int64_t b);
246 * converts fourcc string to int
248 static inline av_pure int ff_get_fourcc(const char *s){
249 #ifdef HAVE_AV_CONFIG_H
250 assert( strlen(s)==4 );
251 #endif
253 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
256 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
257 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
260 * \def GET_UTF8(val, GET_BYTE, ERROR)
261 * converts a UTF-8 character (up to 4 bytes long) to its 32-bit UCS-4 encoded form
262 * \param val is the output and should be of type uint32_t. It holds the converted
263 * UCS-4 character and should be a left value.
264 * \param GET_BYTE gets UTF-8 encoded bytes from any proper source. It can be
265 * a function or a statement whose return value or evaluated value is of type
266 * uint8_t. It will be executed up to 4 times for values in the valid UTF-8 range,
267 * and up to 7 times in the general case.
268 * \param ERROR action that should be taken when an invalid UTF-8 byte is returned
269 * from GET_BYTE. It should be a statement that jumps out of the macro,
270 * like exit(), goto, return, break, or continue.
272 #define GET_UTF8(val, GET_BYTE, ERROR)\
273 val= GET_BYTE;\
275 int ones= 7 - av_log2(val ^ 255);\
276 if(ones==1)\
277 ERROR\
278 val&= 127>>ones;\
279 while(--ones > 0){\
280 int tmp= GET_BYTE - 128;\
281 if(tmp>>6)\
282 ERROR\
283 val= (val<<6) + tmp;\
288 * \def PUT_UTF8(val, tmp, PUT_BYTE)
289 * converts a 32-bit unicode character to its UTF-8 encoded form (up to 4 bytes long).
290 * \param val is an input only argument and should be of type uint32_t. It holds
291 * a ucs4 encoded unicode character that is to be converted to UTF-8. If
292 * val is given as a function it's executed only once.
293 * \param tmp is a temporary variable and should be of type uint8_t. It
294 * represents an intermediate value during conversion that is to be
295 * outputted by PUT_BYTE.
296 * \param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.
297 * It could be a function or a statement, and uses tmp as the input byte.
298 * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
299 * executed up to 4 times for values in the valid UTF-8 range and up to
300 * 7 times in the general case, depending on the length of the converted
301 * unicode character.
303 #define PUT_UTF8(val, tmp, PUT_BYTE)\
305 int bytes, shift;\
306 uint32_t in = val;\
307 if (in < 0x80) {\
308 tmp = in;\
309 PUT_BYTE\
310 } else {\
311 bytes = (av_log2(in) + 4) / 5;\
312 shift = (bytes - 1) * 6;\
313 tmp = (256 - (256 >> bytes)) | (in >> shift);\
314 PUT_BYTE\
315 while (shift >= 6) {\
316 shift -= 6;\
317 tmp = 0x80 | ((in >> shift) & 0x3f);\
318 PUT_BYTE\
323 #if defined(ARCH_X86) || defined(ARCH_POWERPC) || defined(ARCH_BFIN)
324 #define AV_READ_TIME read_time
325 #if defined(ARCH_X86_64)
326 static inline uint64_t read_time(void)
328 uint64_t a, d;
329 asm volatile("rdtsc\n\t"
330 : "=a" (a), "=d" (d));
331 return (d << 32) | (a & 0xffffffff);
333 #elif defined(ARCH_X86_32)
334 static inline long long read_time(void)
336 long long l;
337 asm volatile("rdtsc\n\t"
338 : "=A" (l));
339 return l;
341 #elif ARCH_BFIN
342 static inline uint64_t read_time(void)
344 union {
345 struct {
346 unsigned lo;
347 unsigned hi;
348 } p;
349 unsigned long long c;
350 } t;
351 asm volatile ("%0=cycles; %1=cycles2;" : "=d" (t.p.lo), "=d" (t.p.hi));
352 return t.c;
354 #else //FIXME check ppc64
355 static inline uint64_t read_time(void)
357 uint32_t tbu, tbl, temp;
359 /* from section 2.2.1 of the 32-bit PowerPC PEM */
360 asm volatile(
361 "1:\n"
362 "mftbu %2\n"
363 "mftb %0\n"
364 "mftbu %1\n"
365 "cmpw %2,%1\n"
366 "bne 1b\n"
367 : "=r"(tbl), "=r"(tbu), "=r"(temp)
369 : "cc");
371 return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
373 #endif
374 #elif defined(HAVE_GETHRTIME)
375 #define AV_READ_TIME gethrtime
376 #endif
378 #ifdef AV_READ_TIME
379 #define START_TIMER \
380 uint64_t tend;\
381 uint64_t tstart= AV_READ_TIME();\
383 #define STOP_TIMER(id) \
384 tend= AV_READ_TIME();\
386 static uint64_t tsum=0;\
387 static int tcount=0;\
388 static int tskip_count=0;\
389 if(tcount<2 || tend - tstart < FFMAX(8*tsum/tcount, 2000)){\
390 tsum+= tend - tstart;\
391 tcount++;\
392 }else\
393 tskip_count++;\
394 if(((tcount+tskip_count)&(tcount+tskip_count-1))==0){\
395 av_log(NULL, AV_LOG_ERROR, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n",\
396 tsum*10/tcount, id, tcount, tskip_count);\
399 #else
400 #define START_TIMER
401 #define STOP_TIMER(id) {}
402 #endif
405 * Returns NULL if CONFIG_SMALL is defined otherwise the argument
406 * without modifications, used to disable the definition of strings
407 * (for example AVCodec long_names).
409 #ifdef CONFIG_SMALL
410 # define NULL_IF_CONFIG_SMALL(x) NULL
411 #else
412 # define NULL_IF_CONFIG_SMALL(x) x
413 #endif
415 #endif /* AVUTIL_COMMON_H */