bits.h 2.8 KB

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