OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_UTILS_H_ | 5 #ifndef V8_UTILS_H_ |
6 #define V8_UTILS_H_ | 6 #define V8_UTILS_H_ |
7 | 7 |
8 #include <limits.h> | 8 #include <limits.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 nibble += 8; | 130 nibble += 8; |
131 x >>= 8; | 131 x >>= 8; |
132 } | 132 } |
133 if (x & 0xf0) { | 133 if (x & 0xf0) { |
134 nibble += 4; | 134 nibble += 4; |
135 x >>= 4; | 135 x >>= 4; |
136 } | 136 } |
137 return nibble + msb4[x]; | 137 return nibble + msb4[x]; |
138 } | 138 } |
139 | 139 |
140 | 140 template <typename T> |
141 // The C++ standard leaves the semantics of '>>' undefined for | 141 static T ArithmeticShiftRight(T x, int shift) { |
142 // negative signed operands. Most implementations do the right thing, | 142 DCHECK_LE(0, shift); |
143 // though. | 143 if (x < 0) { |
144 inline int ArithmeticShiftRight(int x, int s) { | 144 // Right shift of signed values is implementation defined. Simulate a |
145 return x >> s; | 145 // true arithmetic right shift by adding leading sign bits. |
| 146 using UnsignedT = typename std::make_unsigned<T>::type; |
| 147 UnsignedT mask = ~(static_cast<UnsignedT>(~0) >> shift); |
| 148 return (static_cast<UnsignedT>(x) >> shift) | mask; |
| 149 } else { |
| 150 return x >> shift; |
| 151 } |
146 } | 152 } |
147 | 153 |
148 | |
149 template <typename T> | 154 template <typename T> |
150 int Compare(const T& a, const T& b) { | 155 int Compare(const T& a, const T& b) { |
151 if (a == b) | 156 if (a == b) |
152 return 0; | 157 return 0; |
153 else if (a < b) | 158 else if (a < b) |
154 return -1; | 159 return -1; |
155 else | 160 else |
156 return 1; | 161 return 1; |
157 } | 162 } |
158 | 163 |
(...skipping 1540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1699 private: | 1704 private: |
1700 T value_; | 1705 T value_; |
1701 ThreadedListZoneEntry<T>* next_; | 1706 ThreadedListZoneEntry<T>* next_; |
1702 DISALLOW_COPY_AND_ASSIGN(ThreadedListZoneEntry); | 1707 DISALLOW_COPY_AND_ASSIGN(ThreadedListZoneEntry); |
1703 }; | 1708 }; |
1704 | 1709 |
1705 } // namespace internal | 1710 } // namespace internal |
1706 } // namespace v8 | 1711 } // namespace v8 |
1707 | 1712 |
1708 #endif // V8_UTILS_H_ | 1713 #endif // V8_UTILS_H_ |
OLD | NEW |