OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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_BASE_MACROS_H_ | 5 #ifndef V8_BASE_MACROS_H_ |
6 #define V8_BASE_MACROS_H_ | 6 #define V8_BASE_MACROS_H_ |
7 | 7 |
8 #include "include/v8stdint.h" | 8 #include "include/v8stdint.h" |
9 #include "src/base/build_config.h" | 9 #include "src/base/build_config.h" |
10 #include "src/base/logging.h" | 10 #include "src/base/logging.h" |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 // integral types. | 188 // integral types. |
189 template <typename T> | 189 template <typename T> |
190 inline T AddressFrom(intptr_t x) { | 190 inline T AddressFrom(intptr_t x) { |
191 return static_cast<T>(static_cast<T>(0) + x); | 191 return static_cast<T>(static_cast<T>(0) + x); |
192 } | 192 } |
193 | 193 |
194 | 194 |
195 // Return the largest multiple of m which is <= x. | 195 // Return the largest multiple of m which is <= x. |
196 template <typename T> | 196 template <typename T> |
197 inline T RoundDown(T x, intptr_t m) { | 197 inline T RoundDown(T x, intptr_t m) { |
198 ASSERT(IsPowerOf2(m)); | 198 DCHECK(IsPowerOf2(m)); |
199 return AddressFrom<T>(OffsetFrom(x) & -m); | 199 return AddressFrom<T>(OffsetFrom(x) & -m); |
200 } | 200 } |
201 | 201 |
202 | 202 |
203 // Return the smallest multiple of m which is >= x. | 203 // Return the smallest multiple of m which is >= x. |
204 template <typename T> | 204 template <typename T> |
205 inline T RoundUp(T x, intptr_t m) { | 205 inline T RoundUp(T x, intptr_t m) { |
206 return RoundDown<T>(static_cast<T>(x + m - 1), m); | 206 return RoundDown<T>(static_cast<T>(x + m - 1), m); |
207 } | 207 } |
208 | 208 |
209 | 209 |
210 // Increment a pointer until it has the specified alignment. | 210 // Increment a pointer until it has the specified alignment. |
211 // This works like RoundUp, but it works correctly on pointer types where | 211 // This works like RoundUp, but it works correctly on pointer types where |
212 // sizeof(*pointer) might not be 1. | 212 // sizeof(*pointer) might not be 1. |
213 template<class T> | 213 template<class T> |
214 T AlignUp(T pointer, size_t alignment) { | 214 T AlignUp(T pointer, size_t alignment) { |
215 ASSERT(sizeof(pointer) == sizeof(uintptr_t)); | 215 DCHECK(sizeof(pointer) == sizeof(uintptr_t)); |
216 uintptr_t pointer_raw = reinterpret_cast<uintptr_t>(pointer); | 216 uintptr_t pointer_raw = reinterpret_cast<uintptr_t>(pointer); |
217 return reinterpret_cast<T>(RoundUp(pointer_raw, alignment)); | 217 return reinterpret_cast<T>(RoundUp(pointer_raw, alignment)); |
218 } | 218 } |
219 | 219 |
220 | 220 |
221 template <typename T, typename U> | 221 template <typename T, typename U> |
222 inline bool IsAligned(T value, U alignment) { | 222 inline bool IsAligned(T value, U alignment) { |
223 return (value & (alignment - 1)) == 0; | 223 return (value & (alignment - 1)) == 0; |
224 } | 224 } |
225 | 225 |
226 | 226 |
227 // Returns the smallest power of two which is >= x. If you pass in a | 227 // Returns the smallest power of two which is >= x. If you pass in a |
228 // number that is already a power of two, it is returned as is. | 228 // number that is already a power of two, it is returned as is. |
229 // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., | 229 // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
230 // figure 3-3, page 48, where the function is called clp2. | 230 // figure 3-3, page 48, where the function is called clp2. |
231 inline uint32_t RoundUpToPowerOf2(uint32_t x) { | 231 inline uint32_t RoundUpToPowerOf2(uint32_t x) { |
232 ASSERT(x <= 0x80000000u); | 232 DCHECK(x <= 0x80000000u); |
233 x = x - 1; | 233 x = x - 1; |
234 x = x | (x >> 1); | 234 x = x | (x >> 1); |
235 x = x | (x >> 2); | 235 x = x | (x >> 2); |
236 x = x | (x >> 4); | 236 x = x | (x >> 4); |
237 x = x | (x >> 8); | 237 x = x | (x >> 8); |
238 x = x | (x >> 16); | 238 x = x | (x >> 16); |
239 return x + 1; | 239 return x + 1; |
240 } | 240 } |
241 | 241 |
242 | 242 |
243 inline uint32_t RoundDownToPowerOf2(uint32_t x) { | 243 inline uint32_t RoundDownToPowerOf2(uint32_t x) { |
244 uint32_t rounded_up = RoundUpToPowerOf2(x); | 244 uint32_t rounded_up = RoundUpToPowerOf2(x); |
245 if (rounded_up > x) return rounded_up >> 1; | 245 if (rounded_up > x) return rounded_up >> 1; |
246 return rounded_up; | 246 return rounded_up; |
247 } | 247 } |
248 | 248 |
249 | 249 |
250 // Returns current value of top of the stack. Works correctly with ASAN. | 250 // Returns current value of top of the stack. Works correctly with ASAN. |
251 DISABLE_ASAN | 251 DISABLE_ASAN |
252 inline uintptr_t GetCurrentStackPosition() { | 252 inline uintptr_t GetCurrentStackPosition() { |
253 // Takes the address of the limit variable in order to find out where | 253 // Takes the address of the limit variable in order to find out where |
254 // the top of stack is right now. | 254 // the top of stack is right now. |
255 uintptr_t limit = reinterpret_cast<uintptr_t>(&limit); | 255 uintptr_t limit = reinterpret_cast<uintptr_t>(&limit); |
256 return limit; | 256 return limit; |
257 } | 257 } |
258 | 258 |
259 #endif // V8_BASE_MACROS_H_ | 259 #endif // V8_BASE_MACROS_H_ |
OLD | NEW |