]> git.sesse.net Git - ffmpeg/blob - libavutil/intmath.h
Merge commit 'b114d28a18050b5ebd22fc067332e5487243889c'
[ffmpeg] / libavutil / intmath.h
1 /*
2  * Copyright (c) 2010 Mans Rullgard <mans@mansr.com>
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 #ifndef AVUTIL_INTMATH_H
22 #define AVUTIL_INTMATH_H
23
24 #include <stdint.h>
25
26 #include "config.h"
27 #include "attributes.h"
28
29 #if ARCH_ARM
30 #   include "arm/intmath.h"
31 #endif
32 #if ARCH_X86
33 #   include "x86/intmath.h"
34 #endif
35
36 /**
37  * @addtogroup lavu_internal
38  * @{
39  */
40
41 #if HAVE_FAST_CLZ
42 #if defined( __INTEL_COMPILER )
43 #ifndef ff_log2
44 #   define ff_log2(x) (_bit_scan_reverse((x)|1))
45 #   ifndef ff_log2_16bit
46 #      define ff_log2_16bit av_log2
47 #   endif
48 #endif /* ff_log2 */
49 #elif AV_GCC_VERSION_AT_LEAST(3,4)
50 #ifndef ff_log2
51 #   define ff_log2(x) (31 - __builtin_clz((x)|1))
52 #   ifndef ff_log2_16bit
53 #      define ff_log2_16bit av_log2
54 #   endif
55 #endif /* ff_log2 */
56 #endif /* AV_GCC_VERSION_AT_LEAST(3,4) */
57 #endif
58
59 extern const uint8_t ff_log2_tab[256];
60
61 #ifndef ff_log2
62 #define ff_log2 ff_log2_c
63 #if !defined( _MSC_VER )
64 static av_always_inline av_const int ff_log2_c(unsigned int v)
65 {
66     int n = 0;
67     if (v & 0xffff0000) {
68         v >>= 16;
69         n += 16;
70     }
71     if (v & 0xff00) {
72         v >>= 8;
73         n += 8;
74     }
75     n += ff_log2_tab[v];
76
77     return n;
78 }
79 #else
80 static av_always_inline av_const int ff_log2_c(unsigned int v)
81 {
82     unsigned long n;
83     _BitScanReverse(&n, v|1);
84     return n;
85 }
86 #define ff_log2_16bit av_log2
87 #endif
88 #endif
89
90 #ifndef ff_log2_16bit
91 #define ff_log2_16bit ff_log2_16bit_c
92 static av_always_inline av_const int ff_log2_16bit_c(unsigned int v)
93 {
94     int n = 0;
95     if (v & 0xff00) {
96         v >>= 8;
97         n += 8;
98     }
99     n += ff_log2_tab[v];
100
101     return n;
102 }
103 #endif
104
105 #define av_log2       ff_log2
106 #define av_log2_16bit ff_log2_16bit
107
108 /**
109  * @}
110  */
111
112 /**
113  * @addtogroup lavu_math
114  * @{
115  */
116
117 #if HAVE_FAST_CLZ
118 #if defined( __INTEL_COMPILER )
119 #ifndef ff_ctz
120 #define ff_ctz(v) _bit_scan_forward(v)
121 #endif
122 #elif AV_GCC_VERSION_AT_LEAST(3,4)
123 #ifndef ff_ctz
124 #define ff_ctz(v) __builtin_ctz(v)
125 #endif
126 #endif
127 #endif
128
129 #ifndef ff_ctz
130 #define ff_ctz ff_ctz_c
131 #if !defined( _MSC_VER )
132 static av_always_inline av_const int ff_ctz_c(int v)
133 {
134     int c;
135
136     if (v & 0x1)
137         return 0;
138
139     c = 1;
140     if (!(v & 0xffff)) {
141         v >>= 16;
142         c += 16;
143     }
144     if (!(v & 0xff)) {
145         v >>= 8;
146         c += 8;
147     }
148     if (!(v & 0xf)) {
149         v >>= 4;
150         c += 4;
151     }
152     if (!(v & 0x3)) {
153         v >>= 2;
154         c += 2;
155     }
156     c -= v & 0x1;
157
158     return c;
159 }
160 #else
161 static av_always_inline av_const int ff_ctz_c( int v )
162 {
163     unsigned long c;
164     _BitScanForward(&c, v);
165     return c;
166 }
167 #endif
168 #endif
169
170 /**
171  * Trailing zero bit count.
172  *
173  * @param v  input value. If v is 0, the result is undefined.
174  * @return   the number of trailing 0-bits
175  */
176 int av_ctz(int v);
177
178 /**
179  * @}
180  */
181 #endif /* AVUTIL_INTMATH_H */