Chromium Code Reviews| 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 29 matching lines...) Expand all Loading... | |
| 40 #define IS_POWER_OF_TWO(x) (((x) & ((x) - 1)) == 0) | 40 #define IS_POWER_OF_TWO(x) (((x) & ((x) - 1)) == 0) |
| 41 | 41 |
| 42 // Returns true iff x is a power of 2 (or zero). Cannot be used with the | 42 // Returns true iff x is a power of 2 (or zero). Cannot be used with the |
| 43 // maximally negative value of the type T (the -1 overflows). | 43 // maximally negative value of the type T (the -1 overflows). |
| 44 template <typename T> | 44 template <typename T> |
| 45 static inline bool IsPowerOf2(T x) { | 45 static inline bool IsPowerOf2(T x) { |
| 46 return IS_POWER_OF_TWO(x); | 46 return IS_POWER_OF_TWO(x); |
| 47 } | 47 } |
| 48 | 48 |
| 49 | 49 |
| 50 // X must be a power of 2. Returns the number of trailing zeros. | |
| 51 template <typename T> | |
| 52 static inline int WhichPowerOf2(T x) { | |
|
Søren Thygesen Gjesse
2010/06/14 07:56:47
Shouldn't there be a test case for this?
Erik Corry
2010/06/14 21:05:46
Assert added.
| |
| 53 ASSERT(IsPowerOf2(x)); | |
| 54 if (x < 0) return 31; | |
| 55 if (x == 0) return 32; | |
| 56 int bits = 0; | |
| 57 if (x >= 0x10000) { | |
| 58 bits += 16; | |
| 59 x >>= 16; | |
| 60 } | |
| 61 if (x >= 0x100) { | |
| 62 bits += 8; | |
| 63 x >>= 8; | |
| 64 } | |
| 65 if (x >= 0x10) { | |
| 66 bits += 4; | |
| 67 x >>= 4; | |
| 68 } | |
| 69 switch (x) { | |
| 70 case 1: return bits; | |
| 71 case 2: return bits + 1; | |
| 72 case 4: return bits + 2; | |
| 73 case 8: return bits + 3; | |
| 74 default: UNREACHABLE(); | |
| 75 } | |
| 76 return 0; | |
| 77 } | |
| 78 | |
| 79 | |
| 50 // The C++ standard leaves the semantics of '>>' undefined for | 80 // The C++ standard leaves the semantics of '>>' undefined for |
| 51 // negative signed operands. Most implementations do the right thing, | 81 // negative signed operands. Most implementations do the right thing, |
| 52 // though. | 82 // though. |
| 53 static inline int ArithmeticShiftRight(int x, int s) { | 83 static inline int ArithmeticShiftRight(int x, int s) { |
| 54 return x >> s; | 84 return x >> s; |
| 55 } | 85 } |
| 56 | 86 |
| 57 | 87 |
| 58 // Compute the 0-relative offset of some absolute value x of type T. | 88 // Compute the 0-relative offset of some absolute value x of type T. |
| 59 // This allows conversion of Addresses and integral types into | 89 // This allows conversion of Addresses and integral types into |
| (...skipping 641 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 701 | 731 |
| 702 Dest dest; | 732 Dest dest; |
| 703 memcpy(&dest, &source, sizeof(dest)); | 733 memcpy(&dest, &source, sizeof(dest)); |
| 704 return dest; | 734 return dest; |
| 705 } | 735 } |
| 706 | 736 |
| 707 } } // namespace v8::internal | 737 } } // namespace v8::internal |
| 708 | 738 |
| 709 | 739 |
| 710 #endif // V8_UTILS_H_ | 740 #endif // V8_UTILS_H_ |
| OLD | NEW |