1 /*
2  * Copyright (c) 2010 Mans Rullgard <mans@mansr.com>
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; 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 
38 #if HAVE_FAST_CLZ && AV_GCC_VERSION_AT_LEAST(3,4)
39 
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 */
46 
47 #endif /* AV_GCC_VERSION_AT_LEAST(3,4) */
48 
49 extern const uint8_t ff_log2_tab[256];
50 
51 #ifndef ff_log2
52 #define ff_log2 ff_log2_c
53 static av_always_inline av_const int ff_log2_c(unsigned int v)
54 {
55     int n = 0;
56     if (v & 0xffff0000) {
57         v >>= 16;
58         n += 16;
59     }
60     if (v & 0xff00) {
61         v >>= 8;
62         n += 8;
63     }
64     n += ff_log2_tab[v];
65 
66     return n;
67 }
68 #endif
69 
70 #ifndef ff_log2_16bit
71 #define ff_log2_16bit ff_log2_16bit_c
72 static av_always_inline av_const int ff_log2_16bit_c(unsigned int v)
73 {
74     int n = 0;
75     if (v & 0xff00) {
76         v >>= 8;
77         n += 8;
78     }
79     n += ff_log2_tab[v];
80 
81     return n;
82 }
83 #endif
84 
85 #define av_log2       ff_log2
86 #define av_log2_16bit ff_log2_16bit
87 
88 /**
89  * @}
90  */
91 
92 /**
av_clip_c(int a,int amin,int amax)93  * @addtogroup lavu_math
94  * @{
95  */
96 
97 #if HAVE_FAST_CLZ && AV_GCC_VERSION_AT_LEAST(3,4)
98 #ifndef ff_ctz
99 #define ff_ctz(v) __builtin_ctz(v)
100 #endif
101 #endif
102 
103 #ifndef ff_ctz
104 #define ff_ctz ff_ctz_c
105 static av_always_inline av_const int ff_ctz_c(int v)
106 {
107     int c;
108 
109     if (v & 0x1)
110         return 0;
111 
112     c = 1;
113     if (!(v & 0xffff)) {
114         v >>= 16;
115         c += 16;
116     }
117     if (!(v & 0xff)) {
118         v >>= 8;
119         c += 8;
120     }
121     if (!(v & 0xf)) {
122         v >>= 4;
123         c += 4;
124     }
125     if (!(v & 0x3)) {
126         v >>= 2;
127         c += 2;
128     }
129     c -= v & 0x1;
130 
131     return c;
132 }
133 #endif
134 
135 /**
136  * Trailing zero bit count.
137  *
av_clip_int16_c(int a)138  * @param v  input value. If v is 0, the result is undefined.
139  * @return   the number of trailing 0-bits
140  */
141 int av_ctz(int v);
142 
143 /**
144  * @}
145  */
146 #endif /* AVUTIL_INTMATH_H */
147