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" |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 10 | #include "src/base/safe_math.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11 | |
| 12 | namespace v8 { |
| 13 | namespace base { |
| 14 | namespace bits { |
| 15 | |
| 16 | uint32_t RoundUpToPowerOfTwo32(uint32_t value) { |
| 17 | DCHECK_LE(value, 0x80000000u); |
| 18 | value = value - 1; |
| 19 | value = value | (value >> 1); |
| 20 | value = value | (value >> 2); |
| 21 | value = value | (value >> 4); |
| 22 | value = value | (value >> 8); |
| 23 | value = value | (value >> 16); |
| 24 | return value + 1; |
| 25 | } |
| 26 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 27 | |
| 28 | int32_t SignedMulHigh32(int32_t lhs, int32_t rhs) { |
| 29 | int64_t const value = static_cast<int64_t>(lhs) * static_cast<int64_t>(rhs); |
| 30 | return bit_cast<int32_t, uint32_t>(bit_cast<uint64_t>(value) >> 32u); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | int32_t SignedMulHighAndAdd32(int32_t lhs, int32_t rhs, int32_t acc) { |
| 35 | return bit_cast<int32_t>(bit_cast<uint32_t>(acc) + |
| 36 | bit_cast<uint32_t>(SignedMulHigh32(lhs, rhs))); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | int32_t SignedDiv32(int32_t lhs, int32_t rhs) { |
| 41 | if (rhs == 0) return 0; |
| 42 | if (rhs == -1) return -lhs; |
| 43 | return lhs / rhs; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | int32_t SignedMod32(int32_t lhs, int32_t rhs) { |
| 48 | if (rhs == 0 || rhs == -1) return 0; |
| 49 | return lhs % rhs; |
| 50 | } |
| 51 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 52 | |
| 53 | int64_t FromCheckedNumeric(const internal::CheckedNumeric<int64_t> value) { |
| 54 | if (value.IsValid()) |
| 55 | return value.ValueUnsafe(); |
| 56 | |
| 57 | // We could return max/min but we don't really expose what the maximum delta |
| 58 | // is. Instead, return max/(-max), which is something that clients can reason |
| 59 | // about. |
| 60 | // TODO(rvargas) crbug.com/332611: don't use internal values. |
| 61 | int64_t limit = std::numeric_limits<int64_t>::max(); |
| 62 | if (value.validity() == internal::RANGE_UNDERFLOW) |
| 63 | limit = -limit; |
| 64 | return value.ValueOrDefault(limit); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | int64_t SignedSaturatedAdd64(int64_t lhs, int64_t rhs) { |
| 69 | internal::CheckedNumeric<int64_t> rv(lhs); |
| 70 | rv += rhs; |
| 71 | return FromCheckedNumeric(rv); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | int64_t SignedSaturatedSub64(int64_t lhs, int64_t rhs) { |
| 76 | internal::CheckedNumeric<int64_t> rv(lhs); |
| 77 | rv -= rhs; |
| 78 | return FromCheckedNumeric(rv); |
| 79 | } |
| 80 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 81 | } // namespace bits |
| 82 | } // namespace base |
| 83 | } // namespace v8 |