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