• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 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/bigint/div-helpers.h"
6 
7 #include "src/bigint/bigint-internal.h"
8 
9 namespace v8 {
10 namespace bigint {
11 
12 namespace {
13 
Copy(RWDigits Z,Digits X)14 void Copy(RWDigits Z, Digits X) {
15   if (Z == X) return;
16   int i = 0;
17   for (; i < X.len(); i++) Z[i] = X[i];
18   for (; i < Z.len(); i++) Z[i] = 0;
19 }
20 
21 }  // namespace
22 
23 // Z := X << shift
24 // Z and X may alias for an in-place shift.
LeftShift(RWDigits Z,Digits X,int shift)25 void LeftShift(RWDigits Z, Digits X, int shift) {
26   DCHECK(shift >= 0);
27   DCHECK(shift < kDigitBits);
28   DCHECK(Z.len() >= X.len());
29   if (shift == 0) return Copy(Z, X);
30   digit_t carry = 0;
31   int i = 0;
32   for (; i < X.len(); i++) {
33     digit_t d = X[i];
34     Z[i] = (d << shift) | carry;
35     carry = d >> (kDigitBits - shift);
36   }
37   if (i < Z.len()) {
38     Z[i++] = carry;
39   } else {
40     DCHECK(carry == 0);
41   }
42   for (; i < Z.len(); i++) Z[i] = 0;
43 }
44 
45 // Z := X >> shift
46 // Z and X may alias for an in-place shift.
RightShift(RWDigits Z,Digits X,int shift)47 void RightShift(RWDigits Z, Digits X, int shift) {
48   DCHECK(shift >= 0);
49   DCHECK(shift < kDigitBits);
50   X.Normalize();
51   DCHECK(Z.len() >= X.len());
52   if (shift == 0) return Copy(Z, X);
53   int i = 0;
54   if (X.len() > 0) {
55     digit_t carry = X[0] >> shift;
56     int last = X.len() - 1;
57     for (; i < last; i++) {
58       digit_t d = X[i + 1];
59       Z[i] = (d << (kDigitBits - shift)) | carry;
60       carry = d >> shift;
61     }
62     Z[i++] = carry;
63   }
64   for (; i < Z.len(); i++) Z[i] = 0;
65 }
66 
67 }  // namespace bigint
68 }  // namespace v8
69