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