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/bits.h" | 5 #include "src/base/bits.h" |
| 6 |
| 7 #include <limits> |
| 8 |
6 #include "src/base/logging.h" | 9 #include "src/base/logging.h" |
7 | 10 |
8 namespace v8 { | 11 namespace v8 { |
9 namespace base { | 12 namespace base { |
10 namespace bits { | 13 namespace bits { |
11 | 14 |
12 uint32_t RoundUpToPowerOfTwo32(uint32_t value) { | 15 uint32_t RoundUpToPowerOfTwo32(uint32_t value) { |
13 DCHECK_LE(value, 0x80000000u); | 16 DCHECK_LE(value, 0x80000000u); |
14 value = value - 1; | 17 value = value - 1; |
15 value = value | (value >> 1); | 18 value = value | (value >> 1); |
16 value = value | (value >> 2); | 19 value = value | (value >> 2); |
17 value = value | (value >> 4); | 20 value = value | (value >> 4); |
18 value = value | (value >> 8); | 21 value = value | (value >> 8); |
19 value = value | (value >> 16); | 22 value = value | (value >> 16); |
20 return value + 1; | 23 return value + 1; |
21 } | 24 } |
22 | 25 |
23 | 26 |
24 int32_t SignedMulHigh32(int32_t lhs, int32_t rhs) { | 27 int32_t SignedMulHigh32(int32_t lhs, int32_t rhs) { |
25 int64_t const value = static_cast<int64_t>(lhs) * static_cast<int64_t>(rhs); | 28 int64_t const value = static_cast<int64_t>(lhs) * static_cast<int64_t>(rhs); |
26 return bit_cast<int32_t, uint32_t>(bit_cast<uint64_t>(value) >> 32u); | 29 return bit_cast<int32_t, uint32_t>(bit_cast<uint64_t>(value) >> 32u); |
27 } | 30 } |
28 | 31 |
29 | 32 |
30 int32_t SignedMulHighAndAdd32(int32_t lhs, int32_t rhs, int32_t acc) { | 33 int32_t SignedMulHighAndAdd32(int32_t lhs, int32_t rhs, int32_t acc) { |
31 return bit_cast<int32_t>(bit_cast<uint32_t>(acc) + | 34 return bit_cast<int32_t>(bit_cast<uint32_t>(acc) + |
32 bit_cast<uint32_t>(SignedMulHigh32(lhs, rhs))); | 35 bit_cast<uint32_t>(SignedMulHigh32(lhs, rhs))); |
33 } | 36 } |
34 | 37 |
| 38 |
| 39 int32_t SignedDiv32(int32_t lhs, int32_t rhs) { |
| 40 if (rhs == 0) return 0; |
| 41 if (rhs == -1) return -lhs; |
| 42 return lhs / rhs; |
| 43 } |
| 44 |
| 45 |
| 46 int32_t SignedMod32(int32_t lhs, int32_t rhs) { |
| 47 if (rhs == 0 || rhs == -1) return 0; |
| 48 return lhs % rhs; |
| 49 } |
| 50 |
35 } // namespace bits | 51 } // namespace bits |
36 } // namespace base | 52 } // namespace base |
37 } // namespace v8 | 53 } // namespace v8 |
OLD | NEW |