1 // Copyright 2006-2008 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_COMPILER_INTRINSICS_H_
6 #define V8_COMPILER_INTRINSICS_H_
11 class CompilerIntrinsics {
13 // Returns number of zero bits preceding least significant 1 bit.
14 // Undefined for zero value.
15 INLINE(static int CountTrailingZeros(uint32_t value));
17 // Returns number of zero bits following most significant 1 bit.
18 // Undefined for zero value.
19 INLINE(static int CountLeadingZeros(uint32_t value));
21 // Returns the number of bits set.
22 INLINE(static int CountSetBits(uint32_t value));
26 int CompilerIntrinsics::CountTrailingZeros(uint32_t value) {
27 return __builtin_ctz(value);
30 int CompilerIntrinsics::CountLeadingZeros(uint32_t value) {
31 return __builtin_clz(value);
34 int CompilerIntrinsics::CountSetBits(uint32_t value) {
35 return __builtin_popcount(value);
38 #elif defined(_MSC_VER)
40 #pragma intrinsic(_BitScanForward)
41 #pragma intrinsic(_BitScanReverse)
43 int CompilerIntrinsics::CountTrailingZeros(uint32_t value) {
44 unsigned long result; //NOLINT
45 _BitScanForward(&result, static_cast<long>(value)); //NOLINT
46 return static_cast<int>(result);
49 int CompilerIntrinsics::CountLeadingZeros(uint32_t value) {
50 unsigned long result; //NOLINT
51 _BitScanReverse(&result, static_cast<long>(value)); //NOLINT
52 return 31 - static_cast<int>(result);
55 int CompilerIntrinsics::CountSetBits(uint32_t value) {
56 // Manually count set bits.
57 value = ((value >> 1) & 0x55555555) + (value & 0x55555555);
58 value = ((value >> 2) & 0x33333333) + (value & 0x33333333);
59 value = ((value >> 4) & 0x0f0f0f0f) + (value & 0x0f0f0f0f);
60 value = ((value >> 8) & 0x00ff00ff) + (value & 0x00ff00ff);
61 value = ((value >> 16) & 0x0000ffff) + (value & 0x0000ffff);
66 #error Unsupported compiler
69 } } // namespace v8::internal
71 #endif // V8_COMPILER_INTRINSICS_H_