Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "src/base/bits.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 6 | |
| 7 | #include <limits> |
| 8 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9 | #include "src/base/logging.h" |
| 10 | |
| 11 | namespace v8 { |
| 12 | namespace base { |
| 13 | namespace bits { |
| 14 | |
| 15 | uint32_t RoundUpToPowerOfTwo32(uint32_t value) { |
| 16 | DCHECK_LE(value, 0x80000000u); |
| 17 | value = value - 1; |
| 18 | value = value | (value >> 1); |
| 19 | value = value | (value >> 2); |
| 20 | value = value | (value >> 4); |
| 21 | value = value | (value >> 8); |
| 22 | value = value | (value >> 16); |
| 23 | return value + 1; |
| 24 | } |
| 25 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 26 | |
| 27 | int32_t SignedMulHigh32(int32_t lhs, int32_t rhs) { |
| 28 | int64_t const value = static_cast<int64_t>(lhs) * static_cast<int64_t>(rhs); |
| 29 | return bit_cast<int32_t, uint32_t>(bit_cast<uint64_t>(value) >> 32u); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | int32_t SignedMulHighAndAdd32(int32_t lhs, int32_t rhs, int32_t acc) { |
| 34 | return bit_cast<int32_t>(bit_cast<uint32_t>(acc) + |
| 35 | bit_cast<uint32_t>(SignedMulHigh32(lhs, rhs))); |
| 36 | } |
| 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 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 51 | } // namespace bits |
| 52 | } // namespace base |
| 53 | } // namespace v8 |