OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 case 4: bits++; // Fall through. | 78 case 4: bits++; // Fall through. |
79 case 2: bits++; // Fall through. | 79 case 2: bits++; // Fall through. |
80 case 1: break; | 80 case 1: break; |
81 } | 81 } |
82 ASSERT_EQ(1 << bits, original_x); | 82 ASSERT_EQ(1 << bits, original_x); |
83 return bits; | 83 return bits; |
84 return 0; | 84 return 0; |
85 } | 85 } |
86 | 86 |
87 | 87 |
88 // Magic numbers for integer division. | |
89 // These are kind of 2's complement reciprocal of the divisors. | |
90 // Details and proofs can be found in: | |
91 // - Hacker's Delight, Henry S. Warren, Jr. | |
92 // - The PowerPC Compiler Writer’s Guide | |
93 // and probably many others. | |
94 // See details in the implementation of the algorithm in | |
95 // lithium-codegen-arm.cc : LCodeGen::TryEmitSignedIntegerDivisionByConstant(). | |
96 struct DivMagicNumbers { | |
97 unsigned M; | |
98 unsigned s; | |
99 }; | |
100 | |
101 const DivMagicNumbers InvalidDivMagicNumber= {0, 0}; | |
102 const DivMagicNumbers DivMagicNumberFor3 = {0x55555556, 0}; | |
103 const DivMagicNumbers DivMagicNumberFor5 = {0x66666667, 1}; | |
104 const DivMagicNumbers DivMagicNumberFor6 = {0x2aaaaaab, 0}; | |
105 const DivMagicNumbers DivMagicNumberFor7 = {0x92492493, 2}; | |
106 const DivMagicNumbers DivMagicNumberFor9 = {0x38e38e39, 1}; | |
107 const DivMagicNumbers DivMagicNumberFor10 = {0x66666667, 2}; | |
108 const DivMagicNumbers DivMagicNumberFor11 = {0x2e8ba2e9, 1}; | |
109 const DivMagicNumbers DivMagicNumberFor12 = {0x2aaaaaab, 1}; | |
110 const DivMagicNumbers DivMagicNumberFor25 = {0x51eb851f, 3}; | |
111 const DivMagicNumbers DivMagicNumberFor125 = {0x10624dd3, 3}; | |
112 const DivMagicNumbers DivMagicNumberFor625 = {0x68db8bad, 8}; | |
alexeif
2012/04/17 16:59:28
How about adding 60, 100 and 1000? They seem to ha
Alexandre
2012/04/17 17:12:22
We can generate optimized code for numbers with an
alexeif
2012/04/17 18:30:26
Oh, I see, it has a shift anyway. Btw, why do you
Alexandre
2012/04/18 09:03:59
Fair question! I actually noticed a non-optimal pa
| |
113 | |
114 const DivMagicNumbers DivMagicNumberFor(int32_t divisor); | |
115 | |
116 | |
88 // The C++ standard leaves the semantics of '>>' undefined for | 117 // The C++ standard leaves the semantics of '>>' undefined for |
89 // negative signed operands. Most implementations do the right thing, | 118 // negative signed operands. Most implementations do the right thing, |
90 // though. | 119 // though. |
91 inline int ArithmeticShiftRight(int x, int s) { | 120 inline int ArithmeticShiftRight(int x, int s) { |
92 return x >> s; | 121 return x >> s; |
93 } | 122 } |
94 | 123 |
95 | 124 |
96 // Compute the 0-relative offset of some absolute value x of type T. | 125 // Compute the 0-relative offset of some absolute value x of type T. |
97 // This allows conversion of Addresses and integral types into | 126 // This allows conversion of Addresses and integral types into |
(...skipping 852 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
950 ASSERT(element < static_cast<int>(sizeof(T) * CHAR_BIT)); | 979 ASSERT(element < static_cast<int>(sizeof(T) * CHAR_BIT)); |
951 return 1 << element; | 980 return 1 << element; |
952 } | 981 } |
953 | 982 |
954 T bits_; | 983 T bits_; |
955 }; | 984 }; |
956 | 985 |
957 } } // namespace v8::internal | 986 } } // namespace v8::internal |
958 | 987 |
959 #endif // V8_UTILS_H_ | 988 #endif // V8_UTILS_H_ |
OLD | NEW |