| 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/compiler-specific.h" | 10 #include "src/base/compiler-specific.h" |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 | 181 |
| 182 // The USE(x) template is used to silence C++ compiler warnings | 182 // The USE(x) template is used to silence C++ compiler warnings |
| 183 // issued for (yet) unused variables (typically parameters). | 183 // issued for (yet) unused variables (typically parameters). |
| 184 template <typename T> | 184 template <typename T> |
| 185 inline void USE(T) { } | 185 inline void USE(T) { } |
| 186 | 186 |
| 187 | 187 |
| 188 #define IS_POWER_OF_TWO(x) ((x) != 0 && (((x) & ((x) - 1)) == 0)) | 188 #define IS_POWER_OF_TWO(x) ((x) != 0 && (((x) & ((x) - 1)) == 0)) |
| 189 | 189 |
| 190 | 190 |
| 191 // Returns true iff x is a power of 2. Cannot be used with the maximally | |
| 192 // negative value of the type T (the -1 overflows). | |
| 193 template <typename T> | |
| 194 inline bool IsPowerOf2(T x) { | |
| 195 return IS_POWER_OF_TWO(x); | |
| 196 } | |
| 197 | |
| 198 | |
| 199 // Define our own macros for writing 64-bit constants. This is less fragile | 191 // Define our own macros for writing 64-bit constants. This is less fragile |
| 200 // than defining __STDC_CONSTANT_MACROS before including <stdint.h>, and it | 192 // than defining __STDC_CONSTANT_MACROS before including <stdint.h>, and it |
| 201 // works on compilers that don't have it (like MSVC). | 193 // works on compilers that don't have it (like MSVC). |
| 202 #if V8_CC_MSVC | 194 #if V8_CC_MSVC |
| 203 # define V8_UINT64_C(x) (x ## UI64) | 195 # define V8_UINT64_C(x) (x ## UI64) |
| 204 # define V8_INT64_C(x) (x ## I64) | 196 # define V8_INT64_C(x) (x ## I64) |
| 205 # if V8_HOST_ARCH_64_BIT | 197 # if V8_HOST_ARCH_64_BIT |
| 206 # define V8_INTPTR_C(x) (x ## I64) | 198 # define V8_INTPTR_C(x) (x ## I64) |
| 207 # define V8_PTR_PREFIX "ll" | 199 # define V8_PTR_PREFIX "ll" |
| 208 # else | 200 # else |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 // integral types. | 253 // integral types. |
| 262 template <typename T> | 254 template <typename T> |
| 263 inline T AddressFrom(intptr_t x) { | 255 inline T AddressFrom(intptr_t x) { |
| 264 return static_cast<T>(static_cast<T>(0) + x); | 256 return static_cast<T>(static_cast<T>(0) + x); |
| 265 } | 257 } |
| 266 | 258 |
| 267 | 259 |
| 268 // Return the largest multiple of m which is <= x. | 260 // Return the largest multiple of m which is <= x. |
| 269 template <typename T> | 261 template <typename T> |
| 270 inline T RoundDown(T x, intptr_t m) { | 262 inline T RoundDown(T x, intptr_t m) { |
| 271 DCHECK(IsPowerOf2(m)); | 263 DCHECK(IS_POWER_OF_TWO(m)); |
| 272 return AddressFrom<T>(OffsetFrom(x) & -m); | 264 return AddressFrom<T>(OffsetFrom(x) & -m); |
| 273 } | 265 } |
| 274 | 266 |
| 275 | 267 |
| 276 // Return the smallest multiple of m which is >= x. | 268 // Return the smallest multiple of m which is >= x. |
| 277 template <typename T> | 269 template <typename T> |
| 278 inline T RoundUp(T x, intptr_t m) { | 270 inline T RoundUp(T x, intptr_t m) { |
| 279 return RoundDown<T>(static_cast<T>(x + m - 1), m); | 271 return RoundDown<T>(static_cast<T>(x + m - 1), m); |
| 280 } | 272 } |
| 281 | 273 |
| 282 | 274 |
| 283 // Increment a pointer until it has the specified alignment. | |
| 284 // This works like RoundUp, but it works correctly on pointer types where | |
| 285 // sizeof(*pointer) might not be 1. | |
| 286 template<class T> | |
| 287 T AlignUp(T pointer, size_t alignment) { | |
| 288 DCHECK(sizeof(pointer) == sizeof(uintptr_t)); | |
| 289 uintptr_t pointer_raw = reinterpret_cast<uintptr_t>(pointer); | |
| 290 return reinterpret_cast<T>(RoundUp(pointer_raw, alignment)); | |
| 291 } | |
| 292 | |
| 293 | |
| 294 template <typename T, typename U> | 275 template <typename T, typename U> |
| 295 inline bool IsAligned(T value, U alignment) { | 276 inline bool IsAligned(T value, U alignment) { |
| 296 return (value & (alignment - 1)) == 0; | 277 return (value & (alignment - 1)) == 0; |
| 297 } | 278 } |
| 298 | 279 |
| 299 | 280 |
| 300 // Returns the smallest power of two which is >= x. If you pass in a | |
| 301 // number that is already a power of two, it is returned as is. | |
| 302 // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., | |
| 303 // figure 3-3, page 48, where the function is called clp2. | |
| 304 inline uint32_t RoundUpToPowerOf2(uint32_t x) { | |
| 305 DCHECK(x <= 0x80000000u); | |
| 306 x = x - 1; | |
| 307 x = x | (x >> 1); | |
| 308 x = x | (x >> 2); | |
| 309 x = x | (x >> 4); | |
| 310 x = x | (x >> 8); | |
| 311 x = x | (x >> 16); | |
| 312 return x + 1; | |
| 313 } | |
| 314 | |
| 315 | |
| 316 inline uint32_t RoundDownToPowerOf2(uint32_t x) { | |
| 317 uint32_t rounded_up = RoundUpToPowerOf2(x); | |
| 318 if (rounded_up > x) return rounded_up >> 1; | |
| 319 return rounded_up; | |
| 320 } | |
| 321 | |
| 322 | |
| 323 // Returns current value of top of the stack. Works correctly with ASAN. | 281 // Returns current value of top of the stack. Works correctly with ASAN. |
| 324 DISABLE_ASAN | 282 DISABLE_ASAN |
| 325 inline uintptr_t GetCurrentStackPosition() { | 283 inline uintptr_t GetCurrentStackPosition() { |
| 326 // Takes the address of the limit variable in order to find out where | 284 // Takes the address of the limit variable in order to find out where |
| 327 // the top of stack is right now. | 285 // the top of stack is right now. |
| 328 uintptr_t limit = reinterpret_cast<uintptr_t>(&limit); | 286 uintptr_t limit = reinterpret_cast<uintptr_t>(&limit); |
| 329 return limit; | 287 return limit; |
| 330 } | 288 } |
| 331 | 289 |
| 332 #endif // V8_BASE_MACROS_H_ | 290 #endif // V8_BASE_MACROS_H_ |
| OLD | NEW |