| 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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 template<class T, int shift, int size> | 150 template<class T, int shift, int size> |
| 151 class BitField { | 151 class BitField { |
| 152 public: | 152 public: |
| 153 // Tells whether the provided value fits into the bit field. | 153 // Tells whether the provided value fits into the bit field. |
| 154 static bool is_valid(T value) { | 154 static bool is_valid(T value) { |
| 155 return (static_cast<uint32_t>(value) & ~((1U << (size)) - 1)) == 0; | 155 return (static_cast<uint32_t>(value) & ~((1U << (size)) - 1)) == 0; |
| 156 } | 156 } |
| 157 | 157 |
| 158 // Returns a uint32_t mask of bit field. | 158 // Returns a uint32_t mask of bit field. |
| 159 static uint32_t mask() { | 159 static uint32_t mask() { |
| 160 return (1U << (size + shift)) - (1U << shift); | 160 // To use all bits of a uint32 in a bitfield without compiler warnings we |
| 161 // have to compute 2^32 without using a shift count of 32. |
| 162 return ((1U << shift) << size) - (1U << shift); |
| 161 } | 163 } |
| 162 | 164 |
| 163 // Returns a uint32_t with the bit field value encoded. | 165 // Returns a uint32_t with the bit field value encoded. |
| 164 static uint32_t encode(T value) { | 166 static uint32_t encode(T value) { |
| 165 ASSERT(is_valid(value)); | 167 ASSERT(is_valid(value)); |
| 166 return static_cast<uint32_t>(value) << shift; | 168 return static_cast<uint32_t>(value) << shift; |
| 167 } | 169 } |
| 168 | 170 |
| 169 // Extracts the bit field from the value. | 171 // Extracts the bit field from the value. |
| 170 static T decode(uint32_t value) { | 172 static T decode(uint32_t value) { |
| 171 return static_cast<T>((value >> shift) & ((1U << (size)) - 1)); | 173 return static_cast<T>((value & mask()) >> shift); |
| 172 } | 174 } |
| 173 }; | 175 }; |
| 174 | 176 |
| 175 | 177 |
| 176 // ---------------------------------------------------------------------------- | 178 // ---------------------------------------------------------------------------- |
| 177 // Support for compressed, machine-independent encoding | |
| 178 // and decoding of integer values of arbitrary size. | |
| 179 | |
| 180 // Encoding and decoding from/to a buffer at position p; | |
| 181 // the result is the position after the encoded integer. | |
| 182 // Small signed integers in the range -64 <= x && x < 64 | |
| 183 // are encoded in 1 byte; larger values are encoded in 2 | |
| 184 // or more bytes. At most sizeof(int) + 1 bytes are used | |
| 185 // in the worst case. | |
| 186 byte* EncodeInt(byte* p, int x); | |
| 187 byte* DecodeInt(byte* p, int* x); | |
| 188 | |
| 189 | |
| 190 // Encoding and decoding from/to a buffer at position p - 1 | |
| 191 // moving backward; the result is the position of the last | |
| 192 // byte written. These routines are useful to read/write | |
| 193 // into a buffer starting at the end of the buffer. | |
| 194 byte* EncodeUnsignedIntBackward(byte* p, unsigned int x); | |
| 195 | |
| 196 // The decoding function is inlined since its performance is | |
| 197 // important to mark-sweep garbage collection. | |
| 198 inline byte* DecodeUnsignedIntBackward(byte* p, unsigned int* x) { | |
| 199 byte b = *--p; | |
| 200 if (b >= 128) { | |
| 201 *x = static_cast<unsigned int>(b) - 128; | |
| 202 return p; | |
| 203 } | |
| 204 unsigned int r = static_cast<unsigned int>(b); | |
| 205 unsigned int s = 7; | |
| 206 b = *--p; | |
| 207 while (b < 128) { | |
| 208 r |= static_cast<unsigned int>(b) << s; | |
| 209 s += 7; | |
| 210 b = *--p; | |
| 211 } | |
| 212 // b >= 128 | |
| 213 *x = r | ((static_cast<unsigned int>(b) - 128) << s); | |
| 214 return p; | |
| 215 } | |
| 216 | |
| 217 | |
| 218 // ---------------------------------------------------------------------------- | |
| 219 // Hash function. | 179 // Hash function. |
| 220 | 180 |
| 221 uint32_t ComputeIntegerHash(uint32_t key); | 181 uint32_t ComputeIntegerHash(uint32_t key); |
| 222 | 182 |
| 223 | 183 |
| 224 // ---------------------------------------------------------------------------- | 184 // ---------------------------------------------------------------------------- |
| 225 // I/O support. | 185 // I/O support. |
| 226 | 186 |
| 227 // Our version of printf(). Avoids compilation errors that we get | 187 // Our version of printf(). Avoids compilation errors that we get |
| 228 // with standard printf when attempting to print pointers, etc. | 188 // with standard printf when attempting to print pointers, etc. |
| (...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 } | 543 } |
| 584 } | 544 } |
| 585 | 545 |
| 586 | 546 |
| 587 // Calculate 10^exponent. | 547 // Calculate 10^exponent. |
| 588 int TenToThe(int exponent); | 548 int TenToThe(int exponent); |
| 589 | 549 |
| 590 } } // namespace v8::internal | 550 } } // namespace v8::internal |
| 591 | 551 |
| 592 #endif // V8_UTILS_H_ | 552 #endif // V8_UTILS_H_ |
| OLD | NEW |