]> git.sesse.net Git - ffmpeg/blob - libavutil/x86/intmath.h
avfilter/af_dynaudnorm: remove wasteful pow
[ffmpeg] / libavutil / x86 / intmath.h
1 /*
2  * Copyright (c) 2015 James Almer
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_X86_INTMATH_H
22 #define AVUTIL_X86_INTMATH_H
23
24 #include <stdint.h>
25 #if HAVE_FAST_CLZ
26 #if defined(_MSC_VER)
27 #include <intrin.h>
28 #elif defined(__INTEL_COMPILER)
29 #include <immintrin.h>
30 #endif
31 #endif
32 #include "config.h"
33
34 #if HAVE_FAST_CLZ
35 #if (defined(__INTEL_COMPILER) && (__INTEL_COMPILER>=1216)) || defined(_MSC_VER)
36 #   if defined(__INTEL_COMPILER)
37 #       define ff_log2(x) (_bit_scan_reverse((x)|1))
38 #   else
39 #       define ff_log2 ff_log2_x86
40 static av_always_inline av_const int ff_log2_x86(unsigned int v)
41 {
42     unsigned long n;
43     _BitScanReverse(&n, v|1);
44     return n;
45 }
46 #   endif
47 #   define ff_log2_16bit av_log2
48
49 #   define ff_ctz(v) _tzcnt_u32(v)
50
51 #   if ARCH_X86_64
52 #       define ff_ctzll(v) _tzcnt_u64(v)
53 #   else
54 #       define ff_ctzll ff_ctzll_x86
55 static av_always_inline av_const int ff_ctzll_x86(long long v)
56 {
57     return ((uint32_t)v == 0) ? _tzcnt_u32((uint32_t)(v >> 32)) + 32 : _tzcnt_u32((uint32_t)v);
58 }
59 #   endif
60
61 #endif /* __INTEL_COMPILER */
62
63 #endif /* HAVE_FAST_CLZ */
64
65 #if defined(__GNUC__)
66
67 /* Our generic version of av_popcount is faster than GCC's built-in on
68  * CPUs that don't support the popcnt instruction.
69  */
70 #if defined(__POPCNT__)
71     #define av_popcount   __builtin_popcount
72 #if ARCH_X86_64
73     #define av_popcount64 __builtin_popcountll
74 #endif
75
76 #endif /* __POPCNT__ */
77
78 #if defined(__BMI2__)
79
80 #if AV_GCC_VERSION_AT_LEAST(5,1)
81 #define av_mod_uintp2 __builtin_ia32_bzhi_si
82 #elif HAVE_INLINE_ASM
83 /* GCC releases before 5.1.0 have a broken bzhi builtin, so for those we
84  * implement it using inline assembly
85  */
86 #define av_mod_uintp2 av_mod_uintp2_bmi2
87 static av_always_inline av_const unsigned av_mod_uintp2_bmi2(unsigned a, unsigned p)
88 {
89     if (av_builtin_constant_p(p))
90         return a & ((1 << p) - 1);
91     else {
92         unsigned x;
93         __asm__ ("bzhi %2, %1, %0 \n\t" : "=r"(x) : "rm"(a), "r"(p));
94         return x;
95     }
96 }
97 #endif /* AV_GCC_VERSION_AT_LEAST */
98
99 #endif /* __BMI2__ */
100
101 #endif /* __GNUC__ */
102
103 #endif /* AVUTIL_X86_INTMATH_H */