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 #ifndef V8_BASE_DIVISION_BY_CONSTANT_H_ | 5 #ifndef V8_BASE_DIVISION_BY_CONSTANT_H_ |
6 #define V8_BASE_DIVISION_BY_CONSTANT_H_ | 6 #define V8_BASE_DIVISION_BY_CONSTANT_H_ |
7 | 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include "src/base/base-export.h" |
| 11 |
8 namespace v8 { | 12 namespace v8 { |
9 namespace base { | 13 namespace base { |
10 | 14 |
11 // ---------------------------------------------------------------------------- | 15 // ---------------------------------------------------------------------------- |
12 | 16 |
13 // The magic numbers for division via multiplication, see Warren's "Hacker's | 17 // The magic numbers for division via multiplication, see Warren's "Hacker's |
14 // Delight", chapter 10. The template parameter must be one of the unsigned | 18 // Delight", chapter 10. The template parameter must be one of the unsigned |
15 // integral types. | 19 // integral types. |
16 template <class T> | 20 template <class T> |
17 struct MagicNumbersForDivision { | 21 struct V8_BASE_EXPORT MagicNumbersForDivision { |
18 MagicNumbersForDivision(T m, unsigned s, bool a) | 22 MagicNumbersForDivision(T m, unsigned s, bool a) |
19 : multiplier(m), shift(s), add(a) {} | 23 : multiplier(m), shift(s), add(a) {} |
20 bool operator==(const MagicNumbersForDivision& rhs) const; | 24 bool operator==(const MagicNumbersForDivision& rhs) const { |
| 25 return multiplier == rhs.multiplier && shift == rhs.shift && add == rhs.add; |
| 26 } |
21 | 27 |
22 T multiplier; | 28 T multiplier; |
23 unsigned shift; | 29 unsigned shift; |
24 bool add; | 30 bool add; |
25 }; | 31 }; |
26 | 32 |
27 | 33 |
28 // Calculate the multiplier and shift for signed division via multiplication. | 34 // Calculate the multiplier and shift for signed division via multiplication. |
29 // The divisor must not be -1, 0 or 1 when interpreted as a signed value. | 35 // The divisor must not be -1, 0 or 1 when interpreted as a signed value. |
30 template <class T> | 36 template <class T> |
31 MagicNumbersForDivision<T> SignedDivisionByConstant(T d); | 37 V8_BASE_EXPORT MagicNumbersForDivision<T> SignedDivisionByConstant(T d); |
32 | |
33 | 38 |
34 // Calculate the multiplier and shift for unsigned division via multiplication, | 39 // Calculate the multiplier and shift for unsigned division via multiplication, |
35 // see Warren's "Hacker's Delight", chapter 10. The divisor must not be 0 and | 40 // see Warren's "Hacker's Delight", chapter 10. The divisor must not be 0 and |
36 // leading_zeros can be used to speed up the calculation if the given number of | 41 // leading_zeros can be used to speed up the calculation if the given number of |
37 // upper bits of the dividend value are known to be zero. | 42 // upper bits of the dividend value are known to be zero. |
38 template <class T> | 43 template <class T> |
39 MagicNumbersForDivision<T> UnsignedDivisionByConstant( | 44 V8_BASE_EXPORT MagicNumbersForDivision<T> UnsignedDivisionByConstant( |
40 T d, unsigned leading_zeros = 0); | 45 T d, unsigned leading_zeros = 0); |
41 | 46 |
| 47 template struct V8_BASE_EXPORT MagicNumbersForDivision<uint32_t>; |
| 48 template struct V8_BASE_EXPORT MagicNumbersForDivision<uint64_t>; |
| 49 |
| 50 extern template V8_BASE_EXPORT MagicNumbersForDivision<uint32_t> |
| 51 SignedDivisionByConstant(uint32_t d); |
| 52 extern template V8_BASE_EXPORT MagicNumbersForDivision<uint64_t> |
| 53 SignedDivisionByConstant(uint64_t d); |
| 54 |
| 55 extern template V8_BASE_EXPORT MagicNumbersForDivision<uint32_t> |
| 56 UnsignedDivisionByConstant(uint32_t d, unsigned leading_zeros); |
| 57 extern template V8_BASE_EXPORT MagicNumbersForDivision<uint64_t> |
| 58 UnsignedDivisionByConstant(uint64_t d, unsigned leading_zeros); |
| 59 |
42 } // namespace base | 60 } // namespace base |
43 } // namespace v8 | 61 } // namespace v8 |
44 | 62 |
45 #endif // V8_BASE_DIVISION_BY_CONSTANT_H_ | 63 #endif // V8_BASE_DIVISION_BY_CONSTANT_H_ |
OLD | NEW |