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