1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef V8_MISC_INTRINSICS_H_
6 #define V8_MISC_INTRINSICS_H_
8 #include "include/v8.h"
9 #include "src/globals.h"
14 // Returns the index of the leading 1 bit, counting the least significant bit at
15 // index 0. (1 << IntegerLog2(x)) is a mask for the most significant bit of x.
16 // Result is undefined if input is zero.
17 int IntegerLog2(uint32_t value);
21 inline int IntegerLog2(uint32_t value) {
22 return 31 - __builtin_clz(value);
25 #elif defined(_MSC_VER)
27 #pragma intrinsic(_BitScanReverse)
29 inline int IntegerLog2(uint32_t value) {
30 unsigned long result; // NOLINT: MSVC intrinsic demands this type.
31 _BitScanReverse(&result, value);
37 // Default version using regular operations. Code taken from:
38 // http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog
39 inline int IntegerLog2(uint32_t value) {
42 shift = (value > 0xFFFF) << 4;
46 shift = (value > 0xFF) << 3;
50 shift = (value > 0xF) << 2;
54 shift = (value > 0x3) << 1;
58 result |= (value >> 1);
64 } } // namespace v8::internal
66 #endif // V8_MISC_INTRINSICS_H_