annotate intmath.h @ 865:41db9ae634fe libavutil

Move ff_sqrt() to libavutil/intmath.h
author mru
date Mon, 08 Mar 2010 21:19:56 +0000
parents d87093e4b925
children 3790c30fc3ad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
807
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
1 /*
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
2 * Copyright (c) 2010 Mans Rullgard <mans@mansr.com>
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
3 *
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
4 * This file is part of FFmpeg.
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
5 *
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
6 * FFmpeg is free software; you can redistribute it and/or
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
7 * modify it under the terms of the GNU Lesser General Public
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
8 * License as published by the Free Software Foundation; either
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
9 * version 2.1 of the License, or (at your option) any later version.
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
10 *
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
11 * FFmpeg is distributed in the hope that it will be useful,
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
14 * Lesser General Public License for more details.
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
15 *
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
16 * You should have received a copy of the GNU Lesser General Public
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
17 * License along with FFmpeg; if not, write to the Free Software
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
19 */
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
20
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
21 #ifndef AVUTIL_INTMATH_H
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
22 #define AVUTIL_INTMATH_H
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
23
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
24 #include "config.h"
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
25 #include "common.h"
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
26
816
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
27 extern const uint32_t ff_inverse[257];
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
28
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
29 #if ARCH_ARM
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
30 # include "arm/intmath.h"
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
31 #elif ARCH_X86
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
32 # include "x86/intmath.h"
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
33 #endif
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
34
807
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
35 #if HAVE_FAST_CLZ && AV_GCC_VERSION_AT_LEAST(3,4)
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
36
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
37 #ifndef av_log2
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
38
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
39 #define av_log2(x) (31 - __builtin_clz((x)|1))
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
40
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
41 #ifndef av_log2_16bit
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
42 #define av_log2_16bit av_log2
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
43 #endif
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
44
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
45 #endif /* av_log2 */
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
46
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
47 #endif /* AV_GCC_VERSION_AT_LEAST(3,4) */
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
48
816
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
49 #ifndef FASTDIV
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
50
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
51 #if CONFIG_FASTDIV
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
52 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a) * ff_inverse[b]) >> 32))
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
53 #else
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
54 # define FASTDIV(a,b) ((a) / (b))
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
55 #endif
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
56
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
57 #endif /* FASTDIV */
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
58
865
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
59 extern const uint8_t ff_sqrt_tab[256];
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
60
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
61 static inline av_const unsigned int ff_sqrt(unsigned int a)
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
62 {
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
63 unsigned int b;
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
64
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
65 if (a < 255) return (ff_sqrt_tab[a + 1] - 1) >> 4;
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
66 else if (a < (1 << 12)) b = ff_sqrt_tab[a >> 4] >> 2;
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
67 #if !CONFIG_SMALL
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
68 else if (a < (1 << 14)) b = ff_sqrt_tab[a >> 6] >> 1;
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
69 else if (a < (1 << 16)) b = ff_sqrt_tab[a >> 8] ;
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
70 #endif
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
71 else {
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
72 int s = av_log2_16bit(a >> 16) >> 1;
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
73 unsigned int c = a >> (s + 2);
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
74 b = ff_sqrt_tab[c >> (s + 8)];
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
75 b = FASTDIV(c,b) + (b << s);
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
76 }
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
77
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
78 return b - (a < b * b);
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
79 }
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
80
807
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
81 #endif /* AVUTIL_INTMATH_H */