OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 22 matching lines...) Expand all Loading... |
33 | 33 |
34 class CompilerIntrinsics { | 34 class CompilerIntrinsics { |
35 public: | 35 public: |
36 // Returns number of zero bits preceding least significant 1 bit. | 36 // Returns number of zero bits preceding least significant 1 bit. |
37 // Undefined for zero value. | 37 // Undefined for zero value. |
38 INLINE(static int CountTrailingZeros(uint32_t value)); | 38 INLINE(static int CountTrailingZeros(uint32_t value)); |
39 | 39 |
40 // Returns number of zero bits following most significant 1 bit. | 40 // Returns number of zero bits following most significant 1 bit. |
41 // Undefined for zero value. | 41 // Undefined for zero value. |
42 INLINE(static int CountLeadingZeros(uint32_t value)); | 42 INLINE(static int CountLeadingZeros(uint32_t value)); |
| 43 |
| 44 // Returns the number of bits set. |
| 45 INLINE(static int CountSetBits(uint32_t value)); |
43 }; | 46 }; |
44 | 47 |
45 #ifdef __GNUC__ | 48 #ifdef __GNUC__ |
46 int CompilerIntrinsics::CountTrailingZeros(uint32_t value) { | 49 int CompilerIntrinsics::CountTrailingZeros(uint32_t value) { |
47 return __builtin_ctz(value); | 50 return __builtin_ctz(value); |
48 } | 51 } |
49 | 52 |
50 int CompilerIntrinsics::CountLeadingZeros(uint32_t value) { | 53 int CompilerIntrinsics::CountLeadingZeros(uint32_t value) { |
51 return __builtin_clz(value); | 54 return __builtin_clz(value); |
52 } | 55 } |
53 | 56 |
| 57 int CompilerIntrinsics::CountSetBits(uint32_t value) { |
| 58 return __builtin_popcount(value); |
| 59 } |
| 60 |
54 #elif defined(_MSC_VER) | 61 #elif defined(_MSC_VER) |
55 | 62 |
56 #pragma intrinsic(_BitScanForward) | 63 #pragma intrinsic(_BitScanForward) |
57 #pragma intrinsic(_BitScanReverse) | 64 #pragma intrinsic(_BitScanReverse) |
58 | 65 |
59 int CompilerIntrinsics::CountTrailingZeros(uint32_t value) { | 66 int CompilerIntrinsics::CountTrailingZeros(uint32_t value) { |
60 unsigned long result; //NOLINT | 67 unsigned long result; //NOLINT |
61 _BitScanForward(&result, static_cast<long>(value)); //NOLINT | 68 _BitScanForward(&result, static_cast<long>(value)); //NOLINT |
62 return static_cast<int>(result); | 69 return static_cast<int>(result); |
63 } | 70 } |
64 | 71 |
65 int CompilerIntrinsics::CountLeadingZeros(uint32_t value) { | 72 int CompilerIntrinsics::CountLeadingZeros(uint32_t value) { |
66 unsigned long result; //NOLINT | 73 unsigned long result; //NOLINT |
67 _BitScanReverse(&result, static_cast<long>(value)); //NOLINT | 74 _BitScanReverse(&result, static_cast<long>(value)); //NOLINT |
68 return 31 - static_cast<int>(result); | 75 return 31 - static_cast<int>(result); |
69 } | 76 } |
70 | 77 |
| 78 int CompilerIntrinsics::CountSetBits(uint32_t value) { |
| 79 // __popcnt is only supported from VS2008. |
| 80 #define _MSC_VER_VS2008 1500 |
| 81 #if _MSC_VER >= _MSC_VER_VS2008 |
| 82 return __popcnt(value); |
| 83 #else |
| 84 // Manually count set bits. |
| 85 value = ((value >> 1) & 0x55555555) + (value & 0x55555555); |
| 86 value = ((value >> 2) & 0x33333333) + (value & 0x33333333); |
| 87 value = ((value >> 4) & 0x0f0f0f0f) + (value & 0x0f0f0f0f); |
| 88 value = ((value >> 8) & 0x00ff00ff) + (value & 0x00ff00ff); |
| 89 value = ((value >> 16) & 0x0000ffff) + (value & 0x0000ffff); |
| 90 return value; |
| 91 #endif |
| 92 #undef _MSC_VER_VS2008 |
| 93 } |
| 94 |
71 #else | 95 #else |
72 #error Unsupported compiler | 96 #error Unsupported compiler |
73 #endif | 97 #endif |
74 | 98 |
75 } } // namespace v8::internal | 99 } } // namespace v8::internal |
76 | 100 |
77 #endif // V8_COMPILER_INTRINSICS_H_ | 101 #endif // V8_COMPILER_INTRINSICS_H_ |
OLD | NEW |