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 VP9_COMMON_VP9_SYSTEMDEPENDENT_H_
12 #define VP9_COMMON_VP9_SYSTEMDEPENDENT_H_
13 
14 #ifdef _MSC_VER
15 # include <math.h>  // the ceil() definition must precede intrin.h
16 # if _MSC_VER > 1310 && (defined(_M_X64) || defined(_M_IX86))
17 #  include <intrin.h>
18 #  define USE_MSC_INTRIN
19 # endif
20 # define snprintf _snprintf
21 #endif
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #include "./vpx_config.h"
28 #if ARCH_X86_32 || ARCH_X86_64
29 void vpx_reset_mmx_state(void);
30 #define vp9_clear_system_state() vpx_reset_mmx_state()
31 #else
32 #define vp9_clear_system_state()
33 #endif
34 
35 #if defined(_MSC_VER) && _MSC_VER < 1800
36 // round is not defined in MSVC before VS2013.
round(double x)37 static INLINE int round(double x) {
38   if (x < 0)
39     return (int)ceil(x - 0.5);
40   else
41     return (int)floor(x + 0.5);
42 }
43 #endif
44 
45 // use GNU builtins where available.
46 #if defined(__GNUC__) && \
47     ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
get_msb(unsigned int n)48 static INLINE int get_msb(unsigned int n) {
49   return 31 ^ __builtin_clz(n);
50 }
51 #elif defined(USE_MSC_INTRIN)
52 #pragma intrinsic(_BitScanReverse)
53 
get_msb(unsigned int n)54 static INLINE int get_msb(unsigned int n) {
55   unsigned long first_set_bit;
56   _BitScanReverse(&first_set_bit, n);
57   return first_set_bit;
58 }
59 #undef USE_MSC_INTRIN
60 #else
61 // Returns (int)floor(log2(n)). n must be > 0.
get_msb(unsigned int n)62 static INLINE int get_msb(unsigned int n) {
63   int log = 0;
64   unsigned int value = n;
65   int i;
66 
67   for (i = 4; i >= 0; --i) {
68     const int shift = (1 << i);
69     const unsigned int x = value >> shift;
70     if (x != 0) {
71       value = x;
72       log += shift;
73     }
74   }
75   return log;
76 }
77 #endif
78 
79 #ifdef __cplusplus
80 }  // extern "C"
81 #endif
82 
83 #endif  // VP9_COMMON_VP9_SYSTEMDEPENDENT_H_
84