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> |
11 | 11 |
12 #include "src/allocation.h" | 12 #include "src/allocation.h" |
| 13 #include "src/base/logging.h" |
13 #include "src/base/macros.h" | 14 #include "src/base/macros.h" |
14 #include "src/checks.h" | 15 #include "src/base/platform/platform.h" |
15 #include "src/globals.h" | 16 #include "src/globals.h" |
16 #include "src/list.h" | 17 #include "src/list.h" |
17 #include "src/platform.h" | |
18 #include "src/vector.h" | 18 #include "src/vector.h" |
19 | 19 |
20 namespace v8 { | 20 namespace v8 { |
21 namespace internal { | 21 namespace internal { |
22 | 22 |
23 // ---------------------------------------------------------------------------- | 23 // ---------------------------------------------------------------------------- |
24 // General helper functions | 24 // General helper functions |
25 | 25 |
26 // Returns true iff x is a power of 2. Cannot be used with the maximally | |
27 // negative value of the type T (the -1 overflows). | |
28 template <typename T> | |
29 inline bool IsPowerOf2(T x) { | |
30 return IS_POWER_OF_TWO(x); | |
31 } | |
32 | |
33 | |
34 // X must be a power of 2. Returns the number of trailing zeros. | 26 // X must be a power of 2. Returns the number of trailing zeros. |
35 inline int WhichPowerOf2(uint32_t x) { | 27 inline int WhichPowerOf2(uint32_t x) { |
36 ASSERT(IsPowerOf2(x)); | 28 ASSERT(IsPowerOf2(x)); |
37 int bits = 0; | 29 int bits = 0; |
38 #ifdef DEBUG | 30 #ifdef DEBUG |
39 int original_x = x; | 31 int original_x = x; |
40 #endif | 32 #endif |
41 if (x >= 0x10000) { | 33 if (x >= 0x10000) { |
42 bits += 16; | 34 bits += 16; |
43 x >>= 16; | 35 x >>= 16; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 | 75 |
84 | 76 |
85 // The C++ standard leaves the semantics of '>>' undefined for | 77 // The C++ standard leaves the semantics of '>>' undefined for |
86 // negative signed operands. Most implementations do the right thing, | 78 // negative signed operands. Most implementations do the right thing, |
87 // though. | 79 // though. |
88 inline int ArithmeticShiftRight(int x, int s) { | 80 inline int ArithmeticShiftRight(int x, int s) { |
89 return x >> s; | 81 return x >> s; |
90 } | 82 } |
91 | 83 |
92 | 84 |
93 // Compute the 0-relative offset of some absolute value x of type T. | |
94 // This allows conversion of Addresses and integral types into | |
95 // 0-relative int offsets. | |
96 template <typename T> | |
97 inline intptr_t OffsetFrom(T x) { | |
98 return x - static_cast<T>(0); | |
99 } | |
100 | |
101 | |
102 // Compute the absolute value of type T for some 0-relative offset x. | |
103 // This allows conversion of 0-relative int offsets into Addresses and | |
104 // integral types. | |
105 template <typename T> | |
106 inline T AddressFrom(intptr_t x) { | |
107 return static_cast<T>(static_cast<T>(0) + x); | |
108 } | |
109 | |
110 | |
111 // Return the largest multiple of m which is <= x. | |
112 template <typename T> | |
113 inline T RoundDown(T x, intptr_t m) { | |
114 ASSERT(IsPowerOf2(m)); | |
115 return AddressFrom<T>(OffsetFrom(x) & -m); | |
116 } | |
117 | |
118 | |
119 // Return the smallest multiple of m which is >= x. | |
120 template <typename T> | |
121 inline T RoundUp(T x, intptr_t m) { | |
122 return RoundDown<T>(static_cast<T>(x + m - 1), m); | |
123 } | |
124 | |
125 | |
126 // Increment a pointer until it has the specified alignment. | |
127 // This works like RoundUp, but it works correctly on pointer types where | |
128 // sizeof(*pointer) might not be 1. | |
129 template<class T> | |
130 T AlignUp(T pointer, size_t alignment) { | |
131 ASSERT(sizeof(pointer) == sizeof(uintptr_t)); | |
132 uintptr_t pointer_raw = reinterpret_cast<uintptr_t>(pointer); | |
133 return reinterpret_cast<T>(RoundUp(pointer_raw, alignment)); | |
134 } | |
135 | |
136 | |
137 template <typename T> | 85 template <typename T> |
138 int Compare(const T& a, const T& b) { | 86 int Compare(const T& a, const T& b) { |
139 if (a == b) | 87 if (a == b) |
140 return 0; | 88 return 0; |
141 else if (a < b) | 89 else if (a < b) |
142 return -1; | 90 return -1; |
143 else | 91 else |
144 return 1; | 92 return 1; |
145 } | 93 } |
146 | 94 |
147 | 95 |
148 template <typename T> | 96 template <typename T> |
149 int PointerValueCompare(const T* a, const T* b) { | 97 int PointerValueCompare(const T* a, const T* b) { |
150 return Compare<T>(*a, *b); | 98 return Compare<T>(*a, *b); |
151 } | 99 } |
152 | 100 |
153 | 101 |
154 // Compare function to compare the object pointer value of two | 102 // Compare function to compare the object pointer value of two |
155 // handlified objects. The handles are passed as pointers to the | 103 // handlified objects. The handles are passed as pointers to the |
156 // handles. | 104 // handles. |
157 template<typename T> class Handle; // Forward declaration. | 105 template<typename T> class Handle; // Forward declaration. |
158 template <typename T> | 106 template <typename T> |
159 int HandleObjectPointerCompare(const Handle<T>* a, const Handle<T>* b) { | 107 int HandleObjectPointerCompare(const Handle<T>* a, const Handle<T>* b) { |
160 return Compare<T*>(*(*a), *(*b)); | 108 return Compare<T*>(*(*a), *(*b)); |
161 } | 109 } |
162 | 110 |
163 | 111 |
164 // Returns the smallest power of two which is >= x. If you pass in a | |
165 // number that is already a power of two, it is returned as is. | |
166 // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., | |
167 // figure 3-3, page 48, where the function is called clp2. | |
168 inline uint32_t RoundUpToPowerOf2(uint32_t x) { | |
169 ASSERT(x <= 0x80000000u); | |
170 x = x - 1; | |
171 x = x | (x >> 1); | |
172 x = x | (x >> 2); | |
173 x = x | (x >> 4); | |
174 x = x | (x >> 8); | |
175 x = x | (x >> 16); | |
176 return x + 1; | |
177 } | |
178 | |
179 | |
180 inline uint32_t RoundDownToPowerOf2(uint32_t x) { | |
181 uint32_t rounded_up = RoundUpToPowerOf2(x); | |
182 if (rounded_up > x) return rounded_up >> 1; | |
183 return rounded_up; | |
184 } | |
185 | |
186 | |
187 template <typename T, typename U> | |
188 inline bool IsAligned(T value, U alignment) { | |
189 return (value & (alignment - 1)) == 0; | |
190 } | |
191 | |
192 | |
193 // Returns true if (addr + offset) is aligned. | 112 // Returns true if (addr + offset) is aligned. |
194 inline bool IsAddressAligned(Address addr, | 113 inline bool IsAddressAligned(Address addr, |
195 intptr_t alignment, | 114 intptr_t alignment, |
196 int offset = 0) { | 115 int offset = 0) { |
197 intptr_t offs = OffsetFrom(addr + offset); | 116 intptr_t offs = OffsetFrom(addr + offset); |
198 return IsAligned(offs, alignment); | 117 return IsAligned(offs, alignment); |
199 } | 118 } |
200 | 119 |
201 | 120 |
202 // Returns the maximum of the two parameters. | 121 // Returns the maximum of the two parameters. |
(...skipping 1405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1608 } | 1527 } |
1609 | 1528 |
1610 *index = result; | 1529 *index = result; |
1611 return true; | 1530 return true; |
1612 } | 1531 } |
1613 | 1532 |
1614 | 1533 |
1615 } } // namespace v8::internal | 1534 } } // namespace v8::internal |
1616 | 1535 |
1617 #endif // V8_UTILS_H_ | 1536 #endif // V8_UTILS_H_ |
OLD | NEW |