summaryrefslogtreecommitdiff
path: root/chromium/v8/src/bigint/vector-arithmetic.cc
blob: 9bbea3873ea79cedc883dd34ac81e2bd6261c2e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/bigint/vector-arithmetic.h"

#include "src/bigint/bigint-internal.h"
#include "src/bigint/digit-arithmetic.h"

namespace v8 {
namespace bigint {

digit_t AddAndReturnOverflow(RWDigits Z, Digits X) {
  X.Normalize();
  if (X.len() == 0) return 0;
  digit_t carry = 0;
  int i = 0;
  for (; i < X.len(); i++) {
    Z[i] = digit_add3(Z[i], X[i], carry, &carry);
  }
  for (; i < Z.len() && carry != 0; i++) {
    Z[i] = digit_add2(Z[i], carry, &carry);
  }
  return carry;
}

digit_t SubAndReturnBorrow(RWDigits Z, Digits X) {
  X.Normalize();
  if (X.len() == 0) return 0;
  digit_t borrow = 0;
  int i = 0;
  for (; i < X.len(); i++) {
    Z[i] = digit_sub2(Z[i], X[i], borrow, &borrow);
  }
  for (; i < Z.len() && borrow != 0; i++) {
    Z[i] = digit_sub(Z[i], borrow, &borrow);
  }
  return borrow;
}

void Add(RWDigits Z, Digits X, Digits Y) {
  if (X.len() < Y.len()) {
    return Add(Z, Y, X);
  }
  int i = 0;
  digit_t carry = 0;
  for (; i < Y.len(); i++) {
    Z[i] = digit_add3(X[i], Y[i], carry, &carry);
  }
  for (; i < X.len(); i++) {
    Z[i] = digit_add2(X[i], carry, &carry);
  }
  for (; i < Z.len(); i++) {
    Z[i] = carry;
    carry = 0;
  }
}

void Subtract(RWDigits Z, Digits X, Digits Y) {
  X.Normalize();
  Y.Normalize();
  DCHECK(X.len() >= Y.len());
  int i = 0;
  digit_t borrow = 0;
  for (; i < Y.len(); i++) {
    Z[i] = digit_sub2(X[i], Y[i], borrow, &borrow);
  }
  for (; i < X.len(); i++) {
    Z[i] = digit_sub(X[i], borrow, &borrow);
  }
  DCHECK(borrow == 0);  // NOLINT(readability/check)
  for (; i < Z.len(); i++) Z[i] = 0;
}

digit_t AddAndReturnCarry(RWDigits Z, Digits X, Digits Y) {
  DCHECK(Z.len() >= Y.len() && X.len() >= Y.len());
  digit_t carry = 0;
  for (int i = 0; i < Y.len(); i++) {
    Z[i] = digit_add3(X[i], Y[i], carry, &carry);
  }
  return carry;
}

digit_t SubtractAndReturnBorrow(RWDigits Z, Digits X, Digits Y) {
  DCHECK(Z.len() >= Y.len() && X.len() >= Y.len());
  digit_t borrow = 0;
  for (int i = 0; i < Y.len(); i++) {
    Z[i] = digit_sub2(X[i], Y[i], borrow, &borrow);
  }
  return borrow;
}

bool AddSigned(RWDigits Z, Digits X, bool x_negative, Digits Y,
               bool y_negative) {
  if (x_negative == y_negative) {
    Add(Z, X, Y);
    return x_negative;
  }
  if (GreaterThanOrEqual(X, Y)) {
    Subtract(Z, X, Y);
    return x_negative;
  }
  Subtract(Z, Y, X);
  return !x_negative;
}

bool SubtractSigned(RWDigits Z, Digits X, bool x_negative, Digits Y,
                    bool y_negative) {
  if (x_negative != y_negative) {
    Add(Z, X, Y);
    return x_negative;
  }
  if (GreaterThanOrEqual(X, Y)) {
    Subtract(Z, X, Y);
    return x_negative;
  }
  Subtract(Z, Y, X);
  return !x_negative;
}

void AddOne(RWDigits Z, Digits X) {
  digit_t carry = 1;
  int i = 0;
  for (; carry > 0 && i < X.len(); i++) Z[i] = digit_add2(X[i], carry, &carry);
  if (carry > 0) Z[i++] = carry;
  for (; i < X.len(); i++) Z[i] = X[i];
  for (; i < Z.len(); i++) Z[i] = 0;
}

void SubtractOne(RWDigits Z, Digits X) {
  digit_t borrow = 1;
  int i = 0;
  for (; borrow > 0; i++) Z[i] = digit_sub(X[i], borrow, &borrow);
  for (; i < X.len(); i++) Z[i] = X[i];
  for (; i < Z.len(); i++) Z[i] = 0;
}

}  // namespace bigint
}  // namespace v8