blob: 9b949cc66f8e380a658fbb33dac8bc1e520622ec [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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 Bernierd0a1eb72015-03-24 16:35:39 -04006
7#include <limits>
8
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/base/logging.h"
Ben Murdochc5610432016-08-08 18:44:38 +010010#include "src/base/safe_math.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011
12namespace v8 {
13namespace base {
14namespace bits {
15
16uint32_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 Bernierd0a1eb72015-03-24 16:35:39 -040027
28int32_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
34int32_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
40int32_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
47int32_t SignedMod32(int32_t lhs, int32_t rhs) {
48 if (rhs == 0 || rhs == -1) return 0;
49 return lhs % rhs;
50}
51
Ben Murdochc5610432016-08-08 18:44:38 +010052
53int64_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
68int64_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
75int64_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 Murdochb8a8cc12014-11-26 15:28:44 +000081} // namespace bits
82} // namespace base
83} // namespace v8