2010-05-18 17:58:33 +02:00
|
|
|
/*
|
2010-09-09 14:16:39 +02:00
|
|
|
* Copyright (c) 2010 The WebM project authors. All Rights Reserved.
|
2010-05-18 17:58:33 +02:00
|
|
|
*
|
2010-06-18 18:39:21 +02:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
2010-06-04 22:19:40 +02:00
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
2010-06-18 18:39:21 +02:00
|
|
|
* in the file PATENTS. All contributing project authors may
|
2010-06-04 22:19:40 +02:00
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2010-05-18 17:58:33 +02:00
|
|
|
*/
|
2012-12-19 00:31:19 +01:00
|
|
|
|
2015-08-10 20:28:04 +02:00
|
|
|
#ifndef VPX_PORTS_BITOPS_H_
|
|
|
|
#define VPX_PORTS_BITOPS_H_
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2015-09-22 01:55:28 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2015-05-08 01:41:33 +02:00
|
|
|
#include "vpx_ports/msvc.h"
|
|
|
|
|
2014-02-05 03:28:45 +01:00
|
|
|
#ifdef _MSC_VER
|
2014-03-06 07:02:29 +01:00
|
|
|
# include <math.h> // the ceil() definition must precede intrin.h
|
2014-02-05 03:28:45 +01:00
|
|
|
# if _MSC_VER > 1310 && (defined(_M_X64) || defined(_M_IX86))
|
|
|
|
# include <intrin.h>
|
2015-05-08 01:41:33 +02:00
|
|
|
# define USE_MSC_INTRINSICS
|
2014-02-05 03:28:45 +01:00
|
|
|
# endif
|
2014-01-18 21:16:11 +01:00
|
|
|
#endif
|
|
|
|
|
2014-02-05 03:28:45 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
2012-12-13 23:51:27 +01:00
|
|
|
#endif
|
|
|
|
|
2015-09-22 01:55:28 +02:00
|
|
|
// These versions of get_msb() are only valid when n != 0 because all
|
|
|
|
// of the optimized versions are undefined when n == 0:
|
|
|
|
// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
|
|
|
|
|
2014-01-08 21:25:47 +01:00
|
|
|
// use GNU builtins where available.
|
|
|
|
#if defined(__GNUC__) && \
|
|
|
|
((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
|
|
|
|
static INLINE int get_msb(unsigned int n) {
|
2015-09-22 01:55:28 +02:00
|
|
|
assert(n != 0);
|
2014-01-08 21:25:47 +01:00
|
|
|
return 31 ^ __builtin_clz(n);
|
|
|
|
}
|
2015-05-08 01:41:33 +02:00
|
|
|
#elif defined(USE_MSC_INTRINSICS)
|
2014-01-08 21:25:47 +01:00
|
|
|
#pragma intrinsic(_BitScanReverse)
|
|
|
|
|
|
|
|
static INLINE int get_msb(unsigned int n) {
|
|
|
|
unsigned long first_set_bit;
|
2015-09-22 01:55:28 +02:00
|
|
|
assert(n != 0);
|
2014-01-08 21:25:47 +01:00
|
|
|
_BitScanReverse(&first_set_bit, n);
|
|
|
|
return first_set_bit;
|
|
|
|
}
|
2015-05-08 01:41:33 +02:00
|
|
|
#undef USE_MSC_INTRINSICS
|
2014-01-03 22:24:11 +01:00
|
|
|
#else
|
2014-01-08 21:25:47 +01:00
|
|
|
// Returns (int)floor(log2(n)). n must be > 0.
|
|
|
|
static INLINE int get_msb(unsigned int n) {
|
|
|
|
int log = 0;
|
|
|
|
unsigned int value = n;
|
|
|
|
int i;
|
|
|
|
|
2015-09-22 01:55:28 +02:00
|
|
|
assert(n != 0);
|
|
|
|
|
2014-01-08 21:25:47 +01:00
|
|
|
for (i = 4; i >= 0; --i) {
|
|
|
|
const int shift = (1 << i);
|
|
|
|
const unsigned int x = value >> shift;
|
|
|
|
if (x != 0) {
|
|
|
|
value = x;
|
|
|
|
log += shift;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return log;
|
2014-01-03 22:24:11 +01:00
|
|
|
}
|
2014-01-08 21:25:47 +01:00
|
|
|
#endif
|
2014-01-03 22:24:11 +01:00
|
|
|
|
2014-01-18 21:16:11 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
2015-08-10 20:28:04 +02:00
|
|
|
#endif // VPX_PORTS_BITOPS_H_
|