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