2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
11 #ifndef VPX_PORTS_BITOPS_H_
12 #define VPX_PORTS_BITOPS_H_
16 #include "vpx_ports/msvc.h"
19 #if defined(_M_X64) || defined(_M_IX86)
21 #define USE_MSC_INTRINSICS
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
33 // use GNU builtins where available.
34 #if defined(__GNUC__) && \
35 ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
36 static INLINE int get_msb(unsigned int n) {
38 return 31 ^ __builtin_clz(n);
40 #elif defined(USE_MSC_INTRINSICS)
41 #pragma intrinsic(_BitScanReverse)
43 static INLINE int get_msb(unsigned int n) {
44 unsigned long first_set_bit;
46 _BitScanReverse(&first_set_bit, n);
49 #undef USE_MSC_INTRINSICS
51 // Returns (int)floor(log2(n)). n must be > 0.
52 static INLINE int get_msb(unsigned int n) {
54 unsigned int value = n;
59 for (i = 4; i >= 0; --i) {
60 const int shift = (1 << i);
61 const unsigned int x = value >> shift;
75 #endif // VPX_PORTS_BITOPS_H_