2 * Copyright (c) 2015 James Almer
4 * This file is part of FFmpeg.
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.
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.
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
21 #ifndef AVUTIL_X86_INTMATH_H
22 #define AVUTIL_X86_INTMATH_H
29 #elif defined(__INTEL_COMPILER)
30 #include <immintrin.h>
36 #if (defined(__INTEL_COMPILER) && (__INTEL_COMPILER>=1216)) || defined(_MSC_VER)
37 # if defined(__INTEL_COMPILER)
38 # define ff_log2(x) (_bit_scan_reverse((x)|1))
40 # define ff_log2 ff_log2_x86
41 static av_always_inline av_const
int ff_log2_x86(unsigned int v
)
44 _BitScanReverse(&n
, v
|1);
48 # define ff_log2_16bit av_log2
50 #if defined(__INTEL_COMPILER) || (defined(_MSC_VER) && (_MSC_VER >= 1700) && \
51 (defined(__BMI__) || !defined(__clang__)))
52 # define ff_ctz(v) _tzcnt_u32(v)
55 # define ff_ctzll(v) _tzcnt_u64(v)
57 # define ff_ctzll ff_ctzll_x86
58 static av_always_inline av_const
int ff_ctzll_x86(long long v
)
60 return ((uint32_t)v
== 0) ? _tzcnt_u32((uint32_t)(v
>> 32)) + 32 : _tzcnt_u32((uint32_t)v
);
65 #endif /* __INTEL_COMPILER */
67 #endif /* HAVE_FAST_CLZ */
69 #if defined(__GNUC__) || defined(__clang__)
71 /* Our generic version of av_popcount is faster than GCC's built-in on
72 * CPUs that don't support the popcnt instruction.
74 #if defined(__POPCNT__)
75 #define av_popcount __builtin_popcount
77 #define av_popcount64 __builtin_popcountll
80 #endif /* __POPCNT__ */
84 #if AV_GCC_VERSION_AT_LEAST(5,1) || AV_HAS_BUILTIN(__builtin_ia32_bzhi_si)
85 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
86 #define av_zero_extend av_zero_extend_bmi2
87 static av_always_inline av_const
unsigned av_zero_extend_bmi2(unsigned a
, unsigned p
)
90 return __builtin_ia32_bzhi_si(a
, p
);
93 #define av_zero_extend __builtin_ia32_bzhi_si
96 /* GCC releases before 5.1.0 have a broken bzhi builtin, so for those we
97 * implement it using inline assembly
99 #define av_zero_extend av_zero_extend_bmi2
100 static av_always_inline av_const
unsigned av_zero_extend_bmi2(unsigned a
, unsigned p
)
102 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
105 if (av_builtin_constant_p(p
))
106 return a
& ((1U << p
) - 1);
109 __asm__ ("bzhi %2, %1, %0 \n\t" : "=r"(x
) : "rm"(a
), "r"(p
));
113 #endif /* AV_GCC_VERSION_AT_LEAST */
115 #endif /* __BMI2__ */
117 #endif /* __GNUC__ */
119 #endif /* AVUTIL_X86_INTMATH_H */