Line data Source code
1 : /*
2 : * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 : *
4 : * Use of this source code is governed by a BSD-style license
5 : * that can be found in the LICENSE file in the root of the source
6 : * tree. An additional intellectual property rights grant can be found
7 : * in the file PATENTS. All contributing project authors may
8 : * be found in the AUTHORS file in the root of the source tree.
9 : */
10 :
11 : #ifndef VPX_PORTS_BITOPS_H_
12 : #define VPX_PORTS_BITOPS_H_
13 :
14 : #include <assert.h>
15 :
16 : #include "vpx_ports/msvc.h"
17 :
18 : #ifdef _MSC_VER
19 : #if defined(_M_X64) || defined(_M_IX86)
20 : #include <intrin.h>
21 : #define USE_MSC_INTRINSICS
22 : #endif
23 : #endif
24 :
25 : #ifdef __cplusplus
26 : extern "C" {
27 : #endif
28 :
29 : // These versions of get_msb() are only valid when n != 0 because all
30 : // of the optimized versions are undefined when n == 0:
31 : // https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
32 :
33 : // use GNU builtins where available.
34 : #if defined(__GNUC__) && \
35 : ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
36 0 : static INLINE int get_msb(unsigned int n) {
37 0 : assert(n != 0);
38 0 : return 31 ^ __builtin_clz(n);
39 : }
40 : #elif defined(USE_MSC_INTRINSICS)
41 : #pragma intrinsic(_BitScanReverse)
42 :
43 : static INLINE int get_msb(unsigned int n) {
44 : unsigned long first_set_bit;
45 : assert(n != 0);
46 : _BitScanReverse(&first_set_bit, n);
47 : return first_set_bit;
48 : }
49 : #undef USE_MSC_INTRINSICS
50 : #else
51 : // Returns (int)floor(log2(n)). n must be > 0.
52 : static INLINE int get_msb(unsigned int n) {
53 : int log = 0;
54 : unsigned int value = n;
55 : int i;
56 :
57 : assert(n != 0);
58 :
59 : for (i = 4; i >= 0; --i) {
60 : const int shift = (1 << i);
61 : const unsigned int x = value >> shift;
62 : if (x != 0) {
63 : value = x;
64 : log += shift;
65 : }
66 : }
67 : return log;
68 : }
69 : #endif
70 :
71 : #ifdef __cplusplus
72 : } // extern "C"
73 : #endif
74 :
75 : #endif // VPX_PORTS_BITOPS_H_
|