• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
6 
7 #include <limits>
8 
9 #include "src/base/logging.h"
10 #include "src/base/safe_math.h"
11 
12 namespace v8 {
13 namespace base {
14 namespace bits {
15 
RoundUpToPowerOfTwo32(uint32_t value)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 
27 
SignedMulHigh32(int32_t lhs,int32_t rhs)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 
SignedMulHighAndAdd32(int32_t lhs,int32_t rhs,int32_t acc)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 
SignedDiv32(int32_t lhs,int32_t rhs)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 
SignedMod32(int32_t lhs,int32_t rhs)47 int32_t SignedMod32(int32_t lhs, int32_t rhs) {
48   if (rhs == 0 || rhs == -1) return 0;
49   return lhs % rhs;
50 }
51 
52 
FromCheckedNumeric(const internal::CheckedNumeric<int64_t> value)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 
SignedSaturatedAdd64(int64_t lhs,int64_t rhs)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 
SignedSaturatedSub64(int64_t lhs,int64_t rhs)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 
SignedMulOverflow32(int32_t lhs,int32_t rhs,int32_t * val)81 bool SignedMulOverflow32(int32_t lhs, int32_t rhs, int32_t* val) {
82   internal::CheckedNumeric<int32_t> rv(lhs);
83   rv *= rhs;
84   int32_t limit = std::numeric_limits<int32_t>::max();
85   *val = rv.ValueOrDefault(limit);
86   return !rv.IsValid();
87 }
88 
SignedMulOverflow64(int64_t lhs,int64_t rhs,int64_t * val)89 bool SignedMulOverflow64(int64_t lhs, int64_t rhs, int64_t* val) {
90   internal::CheckedNumeric<int64_t> rv(lhs);
91   rv *= rhs;
92   int64_t limit = std::numeric_limits<int64_t>::max();
93   *val = rv.ValueOrDefault(limit);
94   return !rv.IsValid();
95 }
96 
97 }  // namespace bits
98 }  // namespace base
99 }  // namespace v8
100