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)); | |
46 }; | 43 }; |
47 | 44 |
48 #ifdef __GNUC__ | 45 #ifdef __GNUC__ |
49 int CompilerIntrinsics::CountTrailingZeros(uint32_t value) { | 46 int CompilerIntrinsics::CountTrailingZeros(uint32_t value) { |
50 return __builtin_ctz(value); | 47 return __builtin_ctz(value); |
51 } | 48 } |
52 | 49 |
53 int CompilerIntrinsics::CountLeadingZeros(uint32_t value) { | 50 int CompilerIntrinsics::CountLeadingZeros(uint32_t value) { |
54 return __builtin_clz(value); | 51 return __builtin_clz(value); |
55 } | 52 } |
56 | 53 |
57 int CompilerIntrinsics::CountSetBits(uint32_t value) { | |
58 return __builtin_popcount(value); | |
59 } | |
60 | |
61 #elif defined(_MSC_VER) | 54 #elif defined(_MSC_VER) |
62 | 55 |
63 #pragma intrinsic(_BitScanForward) | 56 #pragma intrinsic(_BitScanForward) |
64 #pragma intrinsic(_BitScanReverse) | 57 #pragma intrinsic(_BitScanReverse) |
65 | 58 |
66 int CompilerIntrinsics::CountTrailingZeros(uint32_t value) { | 59 int CompilerIntrinsics::CountTrailingZeros(uint32_t value) { |
67 unsigned long result; //NOLINT | 60 unsigned long result; //NOLINT |
68 _BitScanForward(&result, static_cast<long>(value)); //NOLINT | 61 _BitScanForward(&result, static_cast<long>(value)); //NOLINT |
69 return static_cast<int>(result); | 62 return static_cast<int>(result); |
70 } | 63 } |
71 | 64 |
72 int CompilerIntrinsics::CountLeadingZeros(uint32_t value) { | 65 int CompilerIntrinsics::CountLeadingZeros(uint32_t value) { |
73 unsigned long result; //NOLINT | 66 unsigned long result; //NOLINT |
74 _BitScanReverse(&result, static_cast<long>(value)); //NOLINT | 67 _BitScanReverse(&result, static_cast<long>(value)); //NOLINT |
75 return 31 - static_cast<int>(result); | 68 return 31 - static_cast<int>(result); |
76 } | 69 } |
77 | 70 |
78 int CompilerIntrinsics::CountSetBits(uint32_t value) { | |
79 return __popcnt(value); | |
80 } | |
81 | |
82 #else | 71 #else |
83 #error Unsupported compiler | 72 #error Unsupported compiler |
84 #endif | 73 #endif |
85 | 74 |
86 } } // namespace v8::internal | 75 } } // namespace v8::internal |
87 | 76 |
88 #endif // V8_COMPILER_INTRINSICS_H_ | 77 #endif // V8_COMPILER_INTRINSICS_H_ |
OLD | NEW |