clzll.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Tencent is pleased to support the open source community by making RapidJSON
  2. // available.
  3. //
  4. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All
  5. // rights reserved.
  6. //
  7. // Licensed under the MIT License (the "License"); you may not use this file
  8. // except in compliance with the License. You may obtain a copy of the License
  9. // at
  10. //
  11. // http://opensource.org/licenses/MIT
  12. //
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. // License for the specific language governing permissions and limitations under
  17. // the License.
  18. #ifndef RAPIDJSON_CLZLL_H_
  19. #define RAPIDJSON_CLZLL_H_
  20. #include "../rapidjson.h"
  21. #if defined(_MSC_VER)
  22. #include <intrin.h>
  23. #if defined(_WIN64)
  24. #pragma intrinsic(_BitScanReverse64)
  25. #else
  26. #pragma intrinsic(_BitScanReverse)
  27. #endif
  28. #endif
  29. RAPIDJSON_NAMESPACE_BEGIN
  30. namespace internal {
  31. #if (defined(__GNUC__) && __GNUC__ >= 4) || \
  32. RAPIDJSON_HAS_BUILTIN(__builtin_clzll)
  33. #define RAPIDJSON_CLZLL __builtin_clzll
  34. #else
  35. inline uint32_t clzll(uint64_t x) {
  36. // Passing 0 to __builtin_clzll is UB in GCC and results in an
  37. // infinite loop in the software implementation.
  38. RAPIDJSON_ASSERT(x != 0);
  39. #if defined(_MSC_VER)
  40. unsigned long r = 0;
  41. #if defined(_WIN64)
  42. _BitScanReverse64(&r, x);
  43. #else
  44. // Scan the high 32 bits.
  45. if (_BitScanReverse(&r, static_cast<uint32_t>(x >> 32))) return 63 - (r + 32);
  46. // Scan the low 32 bits.
  47. _BitScanReverse(&r, static_cast<uint32_t>(x & 0xFFFFFFFF));
  48. #endif // _WIN64
  49. return 63 - r;
  50. #else
  51. uint32_t r;
  52. while (!(x & (static_cast<uint64_t>(1) << 63))) {
  53. x <<= 1;
  54. ++r;
  55. }
  56. return r;
  57. #endif // _MSC_VER
  58. }
  59. #define RAPIDJSON_CLZLL RAPIDJSON_NAMESPACE::internal::clzll
  60. #endif // (defined(__GNUC__) && __GNUC__ >= 4) ||
  61. // RAPIDJSON_HAS_BUILTIN(__builtin_clzll)
  62. } // namespace internal
  63. RAPIDJSON_NAMESPACE_END
  64. #endif // RAPIDJSON_CLZLL_H_