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