1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_BITS_H_
16 #define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_BITS_H_
17 
18 #ifdef __cplusplus
19 #include <cstdint>
20 
21 extern "C" {
22 #endif
23 
CountLeadingZeros32Slow(uint64_t n)24 static inline int CountLeadingZeros32Slow(uint64_t n) {
25   int zeroes = 28;
26   if (n >> 16) zeroes -= 16, n >>= 16;
27   if (n >> 8) zeroes -= 8, n >>= 8;
28   if (n >> 4) zeroes -= 4, n >>= 4;
29   return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[n] + zeroes;
30 }
31 
CountLeadingZeros32(uint32_t n)32 static inline int CountLeadingZeros32(uint32_t n) {
33 #if defined(_MSC_VER)
34   unsigned long result = 0;  // NOLINT(runtime/int)
35   if (_BitScanReverse(&result, n)) {
36     return 31 - result;
37   }
38   return 32;
39 #elif defined(__GNUC__)
40 
41   // Handle 0 as a special case because __builtin_clz(0) is undefined.
42   if (n == 0) {
43     return 32;
44   }
45   return __builtin_clz(n);
46 #else
47   return CountLeadingZeros32Slow(n);
48 #endif
49 }
50 
MostSignificantBit32(uint32_t n)51 static inline int MostSignificantBit32(uint32_t n) {
52   return 32 - CountLeadingZeros32(n);
53 }
54 
CountLeadingZeros64Slow(uint64_t n)55 static inline int CountLeadingZeros64Slow(uint64_t n) {
56   int zeroes = 60;
57   if (n >> 32) zeroes -= 32, n >>= 32;
58   if (n >> 16) zeroes -= 16, n >>= 16;
59   if (n >> 8) zeroes -= 8, n >>= 8;
60   if (n >> 4) zeroes -= 4, n >>= 4;
61   return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[n] + zeroes;
62 }
63 
CountLeadingZeros64(uint64_t n)64 static inline int CountLeadingZeros64(uint64_t n) {
65 #if defined(_MSC_VER) && defined(_M_X64)
66   // MSVC does not have __builtin_clzll. Use _BitScanReverse64.
67   unsigned long result = 0;  // NOLINT(runtime/int)
68   if (_BitScanReverse64(&result, n)) {
69     return 63 - result;
70   }
71   return 64;
72 #elif defined(_MSC_VER)
73   // MSVC does not have __builtin_clzll. Compose two calls to _BitScanReverse
74   unsigned long result = 0;  // NOLINT(runtime/int)
75   if ((n >> 32) && _BitScanReverse(&result, n >> 32)) {
76     return 31 - result;
77   }
78   if (_BitScanReverse(&result, n)) {
79     return 63 - result;
80   }
81   return 64;
82 #elif defined(__GNUC__)
83 
84   // Handle 0 as a special case because __builtin_clzll(0) is undefined.
85   if (n == 0) {
86     return 64;
87   }
88   return __builtin_clzll(n);
89 #else
90   return CountLeadingZeros64Slow(n);
91 #endif
92 }
93 
MostSignificantBit64(uint64_t n)94 static inline int MostSignificantBit64(uint64_t n) {
95   return 64 - CountLeadingZeros64(n);
96 }
97 
98 #ifdef __cplusplus
99 }  // extern "C"
100 #endif
101 
102 #endif  // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_BITS_H_
103