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 #include "src/base/division-by-constant.h" | 5 #include "src/base/division-by-constant.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "src/base/logging.h" | 9 #include "src/base/logging.h" |
10 #include "src/base/macros.h" | 10 #include "src/base/macros.h" |
11 | 11 |
12 namespace v8 { | 12 namespace v8 { |
13 namespace base { | 13 namespace base { |
14 | 14 |
15 template <class T> | 15 template <class T> |
| 16 bool MagicNumbersForDivision<T>::operator==( |
| 17 const MagicNumbersForDivision& rhs) const { |
| 18 return multiplier == rhs.multiplier && shift == rhs.shift && add == rhs.add; |
| 19 } |
| 20 |
| 21 |
| 22 template <class T> |
16 MagicNumbersForDivision<T> SignedDivisionByConstant(T d) { | 23 MagicNumbersForDivision<T> SignedDivisionByConstant(T d) { |
17 STATIC_ASSERT(static_cast<T>(0) < static_cast<T>(-1)); | 24 STATIC_ASSERT(static_cast<T>(0) < static_cast<T>(-1)); |
18 DCHECK(d != static_cast<T>(-1) && d != 0 && d != 1); | 25 DCHECK(d != static_cast<T>(-1) && d != 0 && d != 1); |
19 const unsigned bits = static_cast<unsigned>(sizeof(T)) * 8; | 26 const unsigned bits = static_cast<unsigned>(sizeof(T)) * 8; |
20 const T min = (static_cast<T>(1) << (bits - 1)); | 27 const T min = (static_cast<T>(1) << (bits - 1)); |
21 const bool neg = (min & d) != 0; | 28 const bool neg = (min & d) != 0; |
22 const T ad = neg ? (0 - d) : d; | 29 const T ad = neg ? (0 - d) : d; |
23 const T t = min + (d >> (bits - 1)); | 30 const T t = min + (d >> (bits - 1)); |
24 const T anc = t - 1 - t % ad; // Absolute value of nc | 31 const T anc = t - 1 - t % ad; // Absolute value of nc |
25 unsigned p = bits - 1; // Init. p. | 32 unsigned p = bits - 1; // Init. p. |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 } | 93 } |
87 delta = d - 1 - r2; | 94 delta = d - 1 - r2; |
88 } while (p < bits * 2 && (q1 < delta || (q1 == delta && r1 == 0))); | 95 } while (p < bits * 2 && (q1 < delta || (q1 == delta && r1 == 0))); |
89 return MagicNumbersForDivision<T>(q2 + 1, p - bits, a); | 96 return MagicNumbersForDivision<T>(q2 + 1, p - bits, a); |
90 } | 97 } |
91 | 98 |
92 | 99 |
93 // ----------------------------------------------------------------------------- | 100 // ----------------------------------------------------------------------------- |
94 // Instantiations. | 101 // Instantiations. |
95 | 102 |
| 103 template struct MagicNumbersForDivision<uint32_t>; |
| 104 template struct MagicNumbersForDivision<uint64_t>; |
| 105 |
96 template MagicNumbersForDivision<uint32_t> SignedDivisionByConstant(uint32_t d); | 106 template MagicNumbersForDivision<uint32_t> SignedDivisionByConstant(uint32_t d); |
97 template MagicNumbersForDivision<uint64_t> SignedDivisionByConstant(uint64_t d); | 107 template MagicNumbersForDivision<uint64_t> SignedDivisionByConstant(uint64_t d); |
98 | 108 |
99 template MagicNumbersForDivision<uint32_t> UnsignedDivisionByConstant( | 109 template MagicNumbersForDivision<uint32_t> UnsignedDivisionByConstant( |
100 uint32_t d, unsigned leading_zeros); | 110 uint32_t d, unsigned leading_zeros); |
101 template MagicNumbersForDivision<uint64_t> UnsignedDivisionByConstant( | 111 template MagicNumbersForDivision<uint64_t> UnsignedDivisionByConstant( |
102 uint64_t d, unsigned leading_zeros); | 112 uint64_t d, unsigned leading_zeros); |
103 | 113 |
104 } // namespace base | 114 } // namespace base |
105 } // namespace v8 | 115 } // namespace v8 |
OLD | NEW |