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 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "src/base/logging.h" | 9 #include "src/base/logging.h" |
| 10 #include "src/base/safe_math.h" |
10 | 11 |
11 namespace v8 { | 12 namespace v8 { |
12 namespace base { | 13 namespace base { |
13 namespace bits { | 14 namespace bits { |
14 | 15 |
15 uint32_t RoundUpToPowerOfTwo32(uint32_t value) { | 16 uint32_t RoundUpToPowerOfTwo32(uint32_t value) { |
16 DCHECK_LE(value, 0x80000000u); | 17 DCHECK_LE(value, 0x80000000u); |
17 value = value - 1; | 18 value = value - 1; |
18 value = value | (value >> 1); | 19 value = value | (value >> 1); |
19 value = value | (value >> 2); | 20 value = value | (value >> 2); |
(...skipping 21 matching lines...) Expand all Loading... |
41 if (rhs == -1) return -lhs; | 42 if (rhs == -1) return -lhs; |
42 return lhs / rhs; | 43 return lhs / rhs; |
43 } | 44 } |
44 | 45 |
45 | 46 |
46 int32_t SignedMod32(int32_t lhs, int32_t rhs) { | 47 int32_t SignedMod32(int32_t lhs, int32_t rhs) { |
47 if (rhs == 0 || rhs == -1) return 0; | 48 if (rhs == 0 || rhs == -1) return 0; |
48 return lhs % rhs; | 49 return lhs % rhs; |
49 } | 50 } |
50 | 51 |
| 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 |
51 } // namespace bits | 81 } // namespace bits |
52 } // namespace base | 82 } // namespace base |
53 } // namespace v8 | 83 } // namespace v8 |
OLD | NEW |