annotate intmath.h @ 906:4ce17228fc5e libavutil

Workaround for missing llrintf()
author mru
date Fri, 23 Apr 2010 16:28:10 +0000
parents 3790c30fc3ad
children 2cbad2391250
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
870
3790c30fc3ad Fix build on configurations without fast av_log2()
mru
parents: 865
diff changeset
24 #include <stdint.h>
807
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
25 #include "config.h"
870
3790c30fc3ad Fix build on configurations without fast av_log2()
mru
parents: 865
diff changeset
26 #include "attributes.h"
807
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
27
816
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
28 extern const uint32_t ff_inverse[257];
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
29
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
30 #if ARCH_ARM
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
31 # include "arm/intmath.h"
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
32 #elif ARCH_X86
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
33 # include "x86/intmath.h"
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
34 #endif
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
35
807
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
36 #if HAVE_FAST_CLZ && AV_GCC_VERSION_AT_LEAST(3,4)
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
37
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
38 #ifndef av_log2
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
39
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
40 #define av_log2(x) (31 - __builtin_clz((x)|1))
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
41
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
42 #ifndef av_log2_16bit
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
43 #define av_log2_16bit av_log2
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
44 #endif
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
45
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
46 #endif /* av_log2 */
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
47
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
48 #endif /* AV_GCC_VERSION_AT_LEAST(3,4) */
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
49
816
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
50 #ifndef FASTDIV
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
51
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
52 #if CONFIG_FASTDIV
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
53 # 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
54 #else
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
55 # define FASTDIV(a,b) ((a) / (b))
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
56 #endif
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
57
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
58 #endif /* FASTDIV */
d87093e4b925 Move FASTDIV macro to intmath.h
mru
parents: 807
diff changeset
59
870
3790c30fc3ad Fix build on configurations without fast av_log2()
mru
parents: 865
diff changeset
60 /*
3790c30fc3ad Fix build on configurations without fast av_log2()
mru
parents: 865
diff changeset
61 * Get definition of av_log2_c from common.h. In the event we got
3790c30fc3ad Fix build on configurations without fast av_log2()
mru
parents: 865
diff changeset
62 * here through common.h including this file, including it again will
3790c30fc3ad Fix build on configurations without fast av_log2()
mru
parents: 865
diff changeset
63 * be a no-op due to multi-inclusion guards, so we must duplicate the
3790c30fc3ad Fix build on configurations without fast av_log2()
mru
parents: 865
diff changeset
64 * fallback defines here.
3790c30fc3ad Fix build on configurations without fast av_log2()
mru
parents: 865
diff changeset
65 */
3790c30fc3ad Fix build on configurations without fast av_log2()
mru
parents: 865
diff changeset
66
3790c30fc3ad Fix build on configurations without fast av_log2()
mru
parents: 865
diff changeset
67 #include "common.h"
3790c30fc3ad Fix build on configurations without fast av_log2()
mru
parents: 865
diff changeset
68
3790c30fc3ad Fix build on configurations without fast av_log2()
mru
parents: 865
diff changeset
69 #ifndef av_log2
3790c30fc3ad Fix build on configurations without fast av_log2()
mru
parents: 865
diff changeset
70 # define av_log2 av_log2_c
3790c30fc3ad Fix build on configurations without fast av_log2()
mru
parents: 865
diff changeset
71 #endif
3790c30fc3ad Fix build on configurations without fast av_log2()
mru
parents: 865
diff changeset
72 #ifndef av_log2_16bit
3790c30fc3ad Fix build on configurations without fast av_log2()
mru
parents: 865
diff changeset
73 # define av_log2_16bit av_log2_16bit_c
3790c30fc3ad Fix build on configurations without fast av_log2()
mru
parents: 865
diff changeset
74 #endif
3790c30fc3ad Fix build on configurations without fast av_log2()
mru
parents: 865
diff changeset
75
865
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
76 extern const uint8_t ff_sqrt_tab[256];
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 static inline av_const unsigned int ff_sqrt(unsigned int a)
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 unsigned int b;
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
81
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
82 if (a < 255) return (ff_sqrt_tab[a + 1] - 1) >> 4;
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
83 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
84 #if !CONFIG_SMALL
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
85 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
86 else if (a < (1 << 16)) b = ff_sqrt_tab[a >> 8] ;
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
87 #endif
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
88 else {
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
89 int s = av_log2_16bit(a >> 16) >> 1;
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
90 unsigned int c = a >> (s + 2);
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
91 b = ff_sqrt_tab[c >> (s + 8)];
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
92 b = FASTDIV(c,b) + (b << s);
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
93 }
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
94
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
95 return b - (a < b * b);
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
96 }
41db9ae634fe Move ff_sqrt() to libavutil/intmath.h
mru
parents: 816
diff changeset
97
807
36cc11e07d9b Optimise av_log2 with clz when available
mru
parents:
diff changeset
98 #endif /* AVUTIL_INTMATH_H */