1 // Tencent is pleased to support the open source community by making RapidJSON available.
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
5 // Licensed under the MIT License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
8 // http://opensource.org/licenses/MIT
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
15 #ifndef RAPIDJSON_CLZLL_H_
16 #define RAPIDJSON_CLZLL_H_
18 #include "../rapidjson.h"
20 #if defined(_MSC_VER) && !defined(UNDER_CE)
23 #pragma intrinsic(_BitScanReverse64)
25 #pragma intrinsic(_BitScanReverse)
29 RAPIDJSON_NAMESPACE_BEGIN
32 inline uint32_t clzll(uint64_t x) {
33 // Passing 0 to __builtin_clzll is UB in GCC and results in an
34 // infinite loop in the software implementation.
35 RAPIDJSON_ASSERT(x != 0);
37 #if defined(_MSC_VER) && !defined(UNDER_CE)
40 _BitScanReverse64(&r, x);
42 // Scan the high 32 bits.
43 if (_BitScanReverse(&r, static_cast<uint32_t>(x >> 32)))
46 // Scan the low 32 bits.
47 _BitScanReverse(&r, static_cast<uint32_t>(x & 0xFFFFFFFF));
51 #elif (defined(__GNUC__) && __GNUC__ >= 4) || RAPIDJSON_HAS_BUILTIN(__builtin_clzll)
52 // __builtin_clzll wrapper
53 return static_cast<uint32_t>(__builtin_clzll(x));
57 while (!(x & (static_cast<uint64_t>(1) << 63))) {
66 #define RAPIDJSON_CLZLL RAPIDJSON_NAMESPACE::internal::clzll
68 } // namespace internal
69 RAPIDJSON_NAMESPACE_END
71 #endif // RAPIDJSON_CLZLL_H_