Coverage Report

Created: 2025-08-25 07:49

/src/keystone/llvm/lib/Support/APInt.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- APInt.cpp - Implement APInt class ---------------------------------===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This file implements a class to represent arbitrary precision integer
11
// constant values and provide a variety of arithmetic operations on them.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "llvm/ADT/APInt.h"
16
#include "llvm/ADT/FoldingSet.h"
17
#include "llvm/ADT/Hashing.h"
18
#include "llvm/ADT/SmallString.h"
19
#include "llvm/ADT/StringRef.h"
20
#include "llvm/Support/Debug.h"
21
#include "llvm/Support/ErrorHandling.h"
22
#include "llvm/Support/MathExtras.h"
23
#include "llvm/Support/raw_ostream.h"
24
#include <cmath>
25
#include <cstdlib>
26
#include <cstring>
27
#include <limits>
28
using namespace llvm_ks;
29
30
#define DEBUG_TYPE "apint"
31
32
/// A utility function for allocating memory, checking for allocation failures,
33
/// and ensuring the contents are zeroed.
34
5.69M
inline static uint64_t* getClearedMemory(unsigned numWords) {
35
5.69M
  uint64_t * result = new uint64_t[numWords];
36
5.69M
  assert(result && "APInt memory allocation fails!");
37
5.69M
  memset(result, 0, numWords * sizeof(uint64_t));
38
5.69M
  return result;
39
5.69M
}
40
41
/// A utility function for allocating memory and checking for allocation
42
/// failure.  The content is not zeroed.
43
10.0M
inline static uint64_t* getMemory(unsigned numWords) {
44
10.0M
  uint64_t * result = new uint64_t[numWords];
45
10.0M
  assert(result && "APInt memory allocation fails!");
46
10.0M
  return result;
47
10.0M
}
48
49
/// A utility function that converts a character to a digit.
50
0
inline static unsigned getDigit(char cdigit, uint8_t radix) {
51
0
  unsigned r;
52
53
0
  if (radix == 16 || radix == 36) {
54
0
    r = cdigit - '0';
55
0
    if (r <= 9)
56
0
      return r;
57
58
0
    r = cdigit - 'A';
59
0
    if (r <= radix - 11U)
60
0
      return r + 10;
61
62
0
    r = cdigit - 'a';
63
0
    if (r <= radix - 11U)
64
0
      return r + 10;
65
    
66
0
    radix = 10;
67
0
  }
68
69
0
  r = cdigit - '0';
70
0
  if (r < radix)
71
0
    return r;
72
73
0
  return -1U;
74
0
}
75
76
77
5.69M
void APInt::initSlowCase(unsigned numBits, uint64_t val, bool isSigned) {
78
5.69M
  pVal = getClearedMemory(getNumWords());
79
5.69M
  pVal[0] = val;
80
5.69M
  if (isSigned && int64_t(val) < 0)
81
0
    for (unsigned i = 1; i < getNumWords(); ++i)
82
0
      pVal[i] = -1ULL;
83
5.69M
}
84
85
9.81M
void APInt::initSlowCase(const APInt& that) {
86
9.81M
  pVal = getMemory(getNumWords());
87
9.81M
  memcpy(pVal, that.pVal, getNumWords() * APINT_WORD_SIZE);
88
9.81M
}
89
90
0
void APInt::initFromArray(ArrayRef<uint64_t> bigVal) {
91
0
  assert(BitWidth && "Bitwidth too small");
92
0
  assert(bigVal.data() && "Null pointer detected!");
93
0
  if (isSingleWord())
94
0
    VAL = bigVal[0];
95
0
  else {
96
    // Get memory, cleared to 0
97
0
    pVal = getClearedMemory(getNumWords());
98
    // Calculate the number of words to copy
99
0
    unsigned words = std::min<unsigned>(bigVal.size(), getNumWords());
100
    // Copy the words from bigVal to pVal
101
0
    memcpy(pVal, bigVal.data(), words * APINT_WORD_SIZE);
102
0
  }
103
  // Make sure unused high bits are cleared
104
0
  clearUnusedBits();
105
0
}
106
107
APInt::APInt(unsigned numBits, ArrayRef<uint64_t> bigVal)
108
0
  : BitWidth(numBits), VAL(0) {
109
0
  initFromArray(bigVal);
110
0
}
111
112
APInt::APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[])
113
0
  : BitWidth(numBits), VAL(0) {
114
0
  initFromArray(makeArrayRef(bigVal, numWords));
115
0
}
116
117
APInt::APInt(unsigned numbits, StringRef Str, uint8_t radix)
118
0
  : BitWidth(numbits), VAL(0) {
119
0
  assert(BitWidth && "Bitwidth too small");
120
0
  fromString(numbits, Str, radix);
121
0
}
122
123
216k
APInt& APInt::AssignSlowCase(const APInt& RHS) {
124
  // Don't do anything for X = X
125
216k
  if (this == &RHS)
126
0
    return *this;
127
128
216k
  if (BitWidth == RHS.getBitWidth()) {
129
    // assume same bit-width single-word case is already handled
130
3.88k
    assert(!isSingleWord());
131
3.88k
    memcpy(pVal, RHS.pVal, getNumWords() * APINT_WORD_SIZE);
132
3.88k
    return *this;
133
3.88k
  }
134
135
212k
  if (isSingleWord()) {
136
    // assume case where both are single words is already handled
137
205k
    assert(!RHS.isSingleWord());
138
205k
    VAL = 0;
139
205k
    pVal = getMemory(RHS.getNumWords());
140
205k
    memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE);
141
205k
  } else if (getNumWords() == RHS.getNumWords())
142
140
    memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE);
143
6.18k
  else if (RHS.isSingleWord()) {
144
5.77k
    delete [] pVal;
145
5.77k
    VAL = RHS.VAL;
146
5.77k
  } else {
147
409
    delete [] pVal;
148
409
    pVal = getMemory(RHS.getNumWords());
149
409
    memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE);
150
409
  }
151
212k
  BitWidth = RHS.BitWidth;
152
212k
  return clearUnusedBits();
153
212k
}
154
155
4.77M
APInt& APInt::operator=(uint64_t RHS) {
156
4.77M
  if (isSingleWord())
157
815
    VAL = RHS;
158
4.77M
  else {
159
4.77M
    pVal[0] = RHS;
160
4.77M
    memset(pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE);
161
4.77M
  }
162
4.77M
  return clearUnusedBits();
163
4.77M
}
164
165
/// This method 'profiles' an APInt for use with FoldingSet.
166
0
void APInt::Profile(FoldingSetNodeID& ID) const {
167
#if 0
168
  ID.AddInteger(BitWidth);
169
170
  if (isSingleWord()) {
171
    ID.AddInteger(VAL);
172
    return;
173
  }
174
175
  unsigned NumWords = getNumWords();
176
  for (unsigned i = 0; i < NumWords; ++i)
177
    ID.AddInteger(pVal[i]);
178
#endif
179
0
}
180
181
/// This function adds a single "digit" integer, y, to the multiple
182
/// "digit" integer array,  x[]. x[] is modified to reflect the addition and
183
/// 1 is returned if there is a carry out, otherwise 0 is returned.
184
/// @returns the carry of the addition.
185
0
static bool add_1(uint64_t dest[], uint64_t x[], unsigned len, uint64_t y) {
186
0
  for (unsigned i = 0; i < len; ++i) {
187
0
    dest[i] = y + x[i];
188
0
    if (dest[i] < y)
189
0
      y = 1; // Carry one to next digit.
190
0
    else {
191
0
      y = 0; // No need to carry so exit early
192
0
      break;
193
0
    }
194
0
  }
195
0
  return y;
196
0
}
197
198
/// @brief Prefix increment operator. Increments the APInt by one.
199
0
APInt& APInt::operator++() {
200
0
  if (isSingleWord())
201
0
    ++VAL;
202
0
  else
203
0
    add_1(pVal, pVal, getNumWords(), 1);
204
0
  return clearUnusedBits();
205
0
}
206
207
/// This function subtracts a single "digit" (64-bit word), y, from
208
/// the multi-digit integer array, x[], propagating the borrowed 1 value until
209
/// no further borrowing is neeeded or it runs out of "digits" in x.  The result
210
/// is 1 if "borrowing" exhausted the digits in x, or 0 if x was not exhausted.
211
/// In other words, if y > x then this function returns 1, otherwise 0.
212
/// @returns the borrow out of the subtraction
213
0
static bool sub_1(uint64_t x[], unsigned len, uint64_t y) {
214
0
  for (unsigned i = 0; i < len; ++i) {
215
0
    uint64_t X = x[i];
216
0
    x[i] -= y;
217
0
    if (y > X)
218
0
      y = 1;  // We have to "borrow 1" from next "digit"
219
0
    else {
220
0
      y = 0;  // No need to borrow
221
0
      break;  // Remaining digits are unchanged so exit early
222
0
    }
223
0
  }
224
0
  return bool(y);
225
0
}
226
227
/// @brief Prefix decrement operator. Decrements the APInt by one.
228
0
APInt& APInt::operator--() {
229
0
  if (isSingleWord())
230
0
    --VAL;
231
0
  else
232
0
    sub_1(pVal, getNumWords(), 1);
233
0
  return clearUnusedBits();
234
0
}
235
236
/// This function adds the integer array x to the integer array Y and
237
/// places the result in dest.
238
/// @returns the carry out from the addition
239
/// @brief General addition of 64-bit integer arrays
240
static bool add(uint64_t *dest, const uint64_t *x, const uint64_t *y,
241
14.2k
                unsigned len) {
242
14.2k
  bool carry = false;
243
795k
  for (unsigned i = 0; i< len; ++i) {
244
780k
    uint64_t limit = std::min(x[i],y[i]); // must come first in case dest == x
245
780k
    dest[i] = x[i] + y[i] + carry;
246
780k
    carry = dest[i] < limit || (carry && dest[i] == limit);
247
780k
  }
248
14.2k
  return carry;
249
14.2k
}
250
251
/// Adds the RHS APint to this APInt.
252
/// @returns this, after addition of RHS.
253
/// @brief Addition assignment operator.
254
14.9k
APInt& APInt::operator+=(const APInt& RHS) {
255
14.9k
  assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
256
14.9k
  if (isSingleWord())
257
716
    VAL += RHS.VAL;
258
14.2k
  else {
259
14.2k
    add(pVal, pVal, RHS.pVal, getNumWords());
260
14.2k
  }
261
14.9k
  return clearUnusedBits();
262
14.9k
}
263
264
/// Subtracts the integer array y from the integer array x
265
/// @returns returns the borrow out.
266
/// @brief Generalized subtraction of 64-bit integer arrays.
267
static bool sub(uint64_t *dest, const uint64_t *x, const uint64_t *y,
268
0
                unsigned len) {
269
0
  bool borrow = false;
270
0
  for (unsigned i = 0; i < len; ++i) {
271
0
    uint64_t x_tmp = borrow ? x[i] - 1 : x[i];
272
0
    borrow = y[i] > x_tmp || (borrow && x[i] == 0);
273
0
    dest[i] = x_tmp - y[i];
274
0
  }
275
0
  return borrow;
276
0
}
277
278
/// Subtracts the RHS APInt from this APInt
279
/// @returns this, after subtraction
280
/// @brief Subtraction assignment operator.
281
0
APInt& APInt::operator-=(const APInt& RHS) {
282
0
  assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
283
0
  if (isSingleWord())
284
0
    VAL -= RHS.VAL;
285
0
  else
286
0
    sub(pVal, pVal, RHS.pVal, getNumWords());
287
0
  return clearUnusedBits();
288
0
}
289
290
/// Multiplies an integer array, x, by a uint64_t integer and places the result
291
/// into dest.
292
/// @returns the carry out of the multiplication.
293
/// @brief Multiply a multi-digit APInt by a single digit (64-bit) integer.
294
14.1k
static uint64_t mul_1(uint64_t dest[], uint64_t x[], unsigned len, uint64_t y) {
295
  // Split y into high 32-bit part (hy)  and low 32-bit part (ly)
296
14.1k
  uint64_t ly = y & 0xffffffffULL, hy = y >> 32;
297
14.1k
  uint64_t carry = 0;
298
299
  // For each digit of x.
300
341k
  for (unsigned i = 0; i < len; ++i) {
301
    // Split x into high and low words
302
327k
    uint64_t lx = x[i] & 0xffffffffULL;
303
327k
    uint64_t hx = x[i] >> 32;
304
    // hasCarry - A flag to indicate if there is a carry to the next digit.
305
    // hasCarry == 0, no carry
306
    // hasCarry == 1, has carry
307
    // hasCarry == 2, no carry and the calculation result == 0.
308
327k
    uint8_t hasCarry = 0;
309
327k
    dest[i] = carry + lx * ly;
310
    // Determine if the add above introduces carry.
311
327k
    hasCarry = (dest[i] < carry) ? 1 : 0;
312
327k
    carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0);
313
    // The upper limit of carry can be (2^32 - 1)(2^32 - 1) +
314
    // (2^32 - 1) + 2^32 = 2^64.
315
327k
    hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
316
317
327k
    carry += (lx * hy) & 0xffffffffULL;
318
327k
    dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL);
319
327k
    carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) +
320
327k
            (carry >> 32) + ((lx * hy) >> 32) + hx * hy;
321
327k
  }
322
14.1k
  return carry;
323
14.1k
}
324
325
/// Multiplies integer array x by integer array y and stores the result into
326
/// the integer array dest. Note that dest's size must be >= xlen + ylen.
327
/// @brief Generalized multiplicate of integer arrays.
328
static void mul(uint64_t dest[], uint64_t x[], unsigned xlen, uint64_t y[],
329
14.1k
                unsigned ylen) {
330
14.1k
  dest[xlen] = mul_1(dest, x, xlen, y[0]);
331
14.1k
  for (unsigned i = 1; i < ylen; ++i) {
332
0
    uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32;
333
0
    uint64_t carry = 0, lx = 0, hx = 0;
334
0
    for (unsigned j = 0; j < xlen; ++j) {
335
0
      lx = x[j] & 0xffffffffULL;
336
0
      hx = x[j] >> 32;
337
      // hasCarry - A flag to indicate if has carry.
338
      // hasCarry == 0, no carry
339
      // hasCarry == 1, has carry
340
      // hasCarry == 2, no carry and the calculation result == 0.
341
0
      uint8_t hasCarry = 0;
342
0
      uint64_t resul = carry + lx * ly;
343
0
      hasCarry = (resul < carry) ? 1 : 0;
344
0
      carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32);
345
0
      hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
346
347
0
      carry += (lx * hy) & 0xffffffffULL;
348
0
      resul = (carry << 32) | (resul & 0xffffffffULL);
349
0
      dest[i+j] += resul;
350
0
      carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+
351
0
              (carry >> 32) + (dest[i+j] < resul ? 1 : 0) +
352
0
              ((lx * hy) >> 32) + hx * hy;
353
0
    }
354
0
    dest[i+xlen] = carry;
355
0
  }
356
14.1k
}
357
358
14.9k
APInt& APInt::operator*=(const APInt& RHS) {
359
14.9k
  assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
360
14.9k
  if (isSingleWord()) {
361
716
    VAL *= RHS.VAL;
362
716
    clearUnusedBits();
363
716
    return *this;
364
716
  }
365
366
  // Get some bit facts about LHS and check for zero
367
14.2k
  unsigned lhsBits = getActiveBits();
368
14.2k
  unsigned lhsWords = !lhsBits ? 0 : whichWord(lhsBits - 1) + 1;
369
14.2k
  if (!lhsWords)
370
    // 0 * X ===> 0
371
109
    return *this;
372
373
  // Get some bit facts about RHS and check for zero
374
14.1k
  unsigned rhsBits = RHS.getActiveBits();
375
14.1k
  unsigned rhsWords = !rhsBits ? 0 : whichWord(rhsBits - 1) + 1;
376
14.1k
  if (!rhsWords) {
377
    // X * 0 ===> 0
378
0
    clearAllBits();
379
0
    return *this;
380
0
  }
381
382
  // Allocate space for the result
383
14.1k
  unsigned destWords = rhsWords + lhsWords;
384
14.1k
  uint64_t *dest = getMemory(destWords);
385
386
  // Perform the long multiply
387
14.1k
  mul(dest, pVal, lhsWords, RHS.pVal, rhsWords);
388
389
  // Copy result back into *this
390
14.1k
  clearAllBits();
391
14.1k
  unsigned wordsToCopy = destWords >= getNumWords() ? getNumWords() : destWords;
392
14.1k
  memcpy(pVal, dest, wordsToCopy * APINT_WORD_SIZE);
393
14.1k
  clearUnusedBits();
394
395
  // delete dest array and return
396
14.1k
  delete[] dest;
397
14.1k
  return *this;
398
14.1k
}
399
400
0
APInt& APInt::operator&=(const APInt& RHS) {
401
0
  assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
402
0
  if (isSingleWord()) {
403
0
    VAL &= RHS.VAL;
404
0
    return *this;
405
0
  }
406
0
  unsigned numWords = getNumWords();
407
0
  for (unsigned i = 0; i < numWords; ++i)
408
0
    pVal[i] &= RHS.pVal[i];
409
0
  return *this;
410
0
}
411
412
0
APInt& APInt::operator|=(const APInt& RHS) {
413
0
  assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
414
0
  if (isSingleWord()) {
415
0
    VAL |= RHS.VAL;
416
0
    return *this;
417
0
  }
418
0
  unsigned numWords = getNumWords();
419
0
  for (unsigned i = 0; i < numWords; ++i)
420
0
    pVal[i] |= RHS.pVal[i];
421
0
  return *this;
422
0
}
423
424
0
APInt& APInt::operator^=(const APInt& RHS) {
425
0
  assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
426
0
  if (isSingleWord()) {
427
0
    VAL ^= RHS.VAL;
428
0
    this->clearUnusedBits();
429
0
    return *this;
430
0
  }
431
0
  unsigned numWords = getNumWords();
432
0
  for (unsigned i = 0; i < numWords; ++i)
433
0
    pVal[i] ^= RHS.pVal[i];
434
0
  return clearUnusedBits();
435
0
}
436
437
0
APInt APInt::AndSlowCase(const APInt& RHS) const {
438
0
  unsigned numWords = getNumWords();
439
0
  uint64_t* val = getMemory(numWords);
440
0
  for (unsigned i = 0; i < numWords; ++i)
441
0
    val[i] = pVal[i] & RHS.pVal[i];
442
0
  return APInt(val, getBitWidth());
443
0
}
444
445
0
APInt APInt::OrSlowCase(const APInt& RHS) const {
446
0
  unsigned numWords = getNumWords();
447
0
  uint64_t *val = getMemory(numWords);
448
0
  for (unsigned i = 0; i < numWords; ++i)
449
0
    val[i] = pVal[i] | RHS.pVal[i];
450
0
  return APInt(val, getBitWidth());
451
0
}
452
453
0
APInt APInt::XorSlowCase(const APInt& RHS) const {
454
0
  unsigned numWords = getNumWords();
455
0
  uint64_t *val = getMemory(numWords);
456
0
  for (unsigned i = 0; i < numWords; ++i)
457
0
    val[i] = pVal[i] ^ RHS.pVal[i];
458
459
0
  APInt Result(val, getBitWidth());
460
  // 0^0==1 so clear the high bits in case they got set.
461
0
  Result.clearUnusedBits();
462
0
  return Result;
463
0
}
464
465
0
APInt APInt::operator*(const APInt& RHS) const {
466
0
  assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
467
0
  if (isSingleWord())
468
0
    return APInt(BitWidth, VAL * RHS.VAL);
469
0
  APInt Result(*this);
470
0
  Result *= RHS;
471
0
  return Result;
472
0
}
473
474
0
APInt APInt::operator+(const APInt& RHS) const {
475
0
  assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
476
0
  if (isSingleWord())
477
0
    return APInt(BitWidth, VAL + RHS.VAL);
478
0
  APInt Result(BitWidth, 0);
479
0
  add(Result.pVal, this->pVal, RHS.pVal, getNumWords());
480
0
  Result.clearUnusedBits();
481
0
  return Result;
482
0
}
483
484
0
APInt APInt::operator-(const APInt& RHS) const {
485
0
  assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
486
0
  if (isSingleWord())
487
0
    return APInt(BitWidth, VAL - RHS.VAL);
488
0
  APInt Result(BitWidth, 0);
489
0
  sub(Result.pVal, this->pVal, RHS.pVal, getNumWords());
490
0
  Result.clearUnusedBits();
491
0
  return Result;
492
0
}
493
494
0
bool APInt::EqualSlowCase(const APInt& RHS) const {
495
  // Get some facts about the number of bits used in the two operands.
496
0
  unsigned n1 = getActiveBits();
497
0
  unsigned n2 = RHS.getActiveBits();
498
499
  // If the number of bits isn't the same, they aren't equal
500
0
  if (n1 != n2)
501
0
    return false;
502
503
  // If the number of bits fits in a word, we only need to compare the low word.
504
0
  if (n1 <= APINT_BITS_PER_WORD)
505
0
    return pVal[0] == RHS.pVal[0];
506
507
  // Otherwise, compare everything
508
0
  for (int i = whichWord(n1 - 1); i >= 0; --i)
509
0
    if (pVal[i] != RHS.pVal[i])
510
0
      return false;
511
0
  return true;
512
0
}
513
514
0
bool APInt::EqualSlowCase(uint64_t Val) const {
515
0
  unsigned n = getActiveBits();
516
0
  if (n <= APINT_BITS_PER_WORD)
517
0
    return pVal[0] == Val;
518
0
  else
519
0
    return false;
520
0
}
521
522
0
bool APInt::ult(const APInt& RHS) const {
523
0
  assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
524
0
  if (isSingleWord())
525
0
    return VAL < RHS.VAL;
526
527
  // Get active bit length of both operands
528
0
  unsigned n1 = getActiveBits();
529
0
  unsigned n2 = RHS.getActiveBits();
530
531
  // If magnitude of LHS is less than RHS, return true.
532
0
  if (n1 < n2)
533
0
    return true;
534
535
  // If magnitude of RHS is greather than LHS, return false.
536
0
  if (n2 < n1)
537
0
    return false;
538
539
  // If they bot fit in a word, just compare the low order word
540
0
  if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD)
541
0
    return pVal[0] < RHS.pVal[0];
542
543
  // Otherwise, compare all words
544
0
  unsigned topWord = whichWord(std::max(n1,n2)-1);
545
0
  for (int i = topWord; i >= 0; --i) {
546
0
    if (pVal[i] > RHS.pVal[i])
547
0
      return false;
548
0
    if (pVal[i] < RHS.pVal[i])
549
0
      return true;
550
0
  }
551
0
  return false;
552
0
}
553
554
0
bool APInt::slt(const APInt& RHS) const {
555
0
  assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
556
0
  if (isSingleWord()) {
557
0
    int64_t lhsSext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth);
558
0
    int64_t rhsSext = (int64_t(RHS.VAL) << (64-BitWidth)) >> (64-BitWidth);
559
0
    return lhsSext < rhsSext;
560
0
  }
561
562
0
  APInt lhs(*this);
563
0
  APInt rhs(RHS);
564
0
  bool lhsNeg = isNegative();
565
0
  bool rhsNeg = rhs.isNegative();
566
0
  if (lhsNeg) {
567
    // Sign bit is set so perform two's complement to make it positive
568
0
    lhs.flipAllBits();
569
0
    ++lhs;
570
0
  }
571
0
  if (rhsNeg) {
572
    // Sign bit is set so perform two's complement to make it positive
573
0
    rhs.flipAllBits();
574
0
    ++rhs;
575
0
  }
576
577
  // Now we have unsigned values to compare so do the comparison if necessary
578
  // based on the negativeness of the values.
579
0
  if (lhsNeg)
580
0
    if (rhsNeg)
581
0
      return lhs.ugt(rhs);
582
0
    else
583
0
      return true;
584
0
  else if (rhsNeg)
585
0
    return false;
586
0
  else
587
0
    return lhs.ult(rhs);
588
0
}
589
590
0
void APInt::setBit(unsigned bitPosition) {
591
0
  if (isSingleWord())
592
0
    VAL |= maskBit(bitPosition);
593
0
  else
594
0
    pVal[whichWord(bitPosition)] |= maskBit(bitPosition);
595
0
}
596
597
/// Set the given bit to 0 whose position is given as "bitPosition".
598
/// @brief Set a given bit to 0.
599
0
void APInt::clearBit(unsigned bitPosition) {
600
0
  if (isSingleWord())
601
0
    VAL &= ~maskBit(bitPosition);
602
0
  else
603
0
    pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition);
604
0
}
605
606
/// @brief Toggle every bit to its opposite value.
607
608
/// Toggle a given bit to its opposite value whose position is given
609
/// as "bitPosition".
610
/// @brief Toggles a given bit to its opposite value.
611
0
void APInt::flipBit(unsigned bitPosition) {
612
0
  assert(bitPosition < BitWidth && "Out of the bit-width range!");
613
0
  if ((*this)[bitPosition]) clearBit(bitPosition);
614
0
  else setBit(bitPosition);
615
0
}
616
617
0
unsigned APInt::getBitsNeeded(StringRef str, uint8_t radix) {
618
0
  assert(!str.empty() && "Invalid string length");
619
0
  assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 || 
620
0
          radix == 36) &&
621
0
         "Radix should be 2, 8, 10, 16, or 36!");
622
623
0
  size_t slen = str.size();
624
625
  // Each computation below needs to know if it's negative.
626
0
  StringRef::iterator p = str.begin();
627
0
  unsigned isNegative = *p == '-';
628
0
  if (*p == '-' || *p == '+') {
629
0
    p++;
630
0
    slen--;
631
0
    assert(slen && "String is only a sign, needs a value.");
632
0
  }
633
634
  // For radixes of power-of-two values, the bits required is accurately and
635
  // easily computed
636
0
  if (radix == 2)
637
0
    return slen + isNegative;
638
0
  if (radix == 8)
639
0
    return slen * 3 + isNegative;
640
0
  if (radix == 16)
641
0
    return slen * 4 + isNegative;
642
643
  // FIXME: base 36
644
  
645
  // This is grossly inefficient but accurate. We could probably do something
646
  // with a computation of roughly slen*64/20 and then adjust by the value of
647
  // the first few digits. But, I'm not sure how accurate that could be.
648
649
  // Compute a sufficient number of bits that is always large enough but might
650
  // be too large. This avoids the assertion in the constructor. This
651
  // calculation doesn't work appropriately for the numbers 0-9, so just use 4
652
  // bits in that case.
653
0
  unsigned sufficient 
654
0
    = radix == 10? (slen == 1 ? 4 : slen * 64/18)
655
0
                 : (slen == 1 ? 7 : slen * 16/3);
656
657
  // Convert to the actual binary value.
658
0
  APInt tmp(sufficient, StringRef(p, slen), radix);
659
660
  // Compute how many bits are required. If the log is infinite, assume we need
661
  // just bit.
662
0
  unsigned log = tmp.logBase2();
663
0
  if (log == (unsigned)-1) {
664
0
    return isNegative + 1;
665
0
  } else {
666
0
    return isNegative + log + 1;
667
0
  }
668
0
}
669
670
0
hash_code llvm_ks::hash_value(const APInt &Arg) {
671
0
  if (Arg.isSingleWord())
672
0
    return hash_combine(Arg.VAL);
673
674
0
  return hash_combine_range(Arg.pVal, Arg.pVal + Arg.getNumWords());
675
0
}
676
677
0
bool APInt::isSplat(unsigned SplatSizeInBits) const {
678
0
  assert(getBitWidth() % SplatSizeInBits == 0 &&
679
0
         "SplatSizeInBits must divide width!");
680
  // We can check that all parts of an integer are equal by making use of a
681
  // little trick: rotate and check if it's still the same value.
682
0
  return *this == rotl(SplatSizeInBits);
683
0
}
684
685
/// This function returns the high "numBits" bits of this APInt.
686
4.48k
APInt APInt::getHiBits(unsigned numBits) const {
687
4.48k
  return APIntOps::lshr(*this, BitWidth - numBits);
688
4.48k
}
689
690
/// This function returns the low "numBits" bits of this APInt.
691
4.48k
APInt APInt::getLoBits(unsigned numBits) const {
692
4.48k
  return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits),
693
4.48k
                        BitWidth - numBits);
694
4.48k
}
695
696
7.53M
unsigned APInt::countLeadingZerosSlowCase() const {
697
  // Treat the most significand word differently because it might have
698
  // meaningless bits set beyond the precision.
699
7.53M
  unsigned BitsInMSW = BitWidth % APINT_BITS_PER_WORD;
700
7.53M
  integerPart MSWMask;
701
7.53M
  if (BitsInMSW) MSWMask = (integerPart(1) << BitsInMSW) - 1;
702
7.46M
  else {
703
7.46M
    MSWMask = ~integerPart(0);
704
7.46M
    BitsInMSW = APINT_BITS_PER_WORD;
705
7.46M
  }
706
707
7.53M
  unsigned i = getNumWords();
708
7.53M
  integerPart MSW = pVal[i-1] & MSWMask;
709
7.53M
  if (MSW)
710
122k
    return llvm_ks::countLeadingZeros(MSW) - (APINT_BITS_PER_WORD - BitsInMSW);
711
712
7.41M
  unsigned Count = BitsInMSW;
713
8.60M
  for (--i; i > 0u; --i) {
714
8.60M
    if (pVal[i-1] == 0)
715
1.19M
      Count += APINT_BITS_PER_WORD;
716
7.40M
    else {
717
7.40M
      Count += llvm_ks::countLeadingZeros(pVal[i-1]);
718
7.40M
      break;
719
7.40M
    }
720
8.60M
  }
721
7.41M
  return Count;
722
7.53M
}
723
724
104
unsigned APInt::countLeadingOnes() const {
725
104
  if (isSingleWord())
726
104
    return llvm_ks::countLeadingOnes(VAL << (APINT_BITS_PER_WORD - BitWidth));
727
728
0
  unsigned highWordBits = BitWidth % APINT_BITS_PER_WORD;
729
0
  unsigned shift;
730
0
  if (!highWordBits) {
731
0
    highWordBits = APINT_BITS_PER_WORD;
732
0
    shift = 0;
733
0
  } else {
734
0
    shift = APINT_BITS_PER_WORD - highWordBits;
735
0
  }
736
0
  int i = getNumWords() - 1;
737
0
  unsigned Count = llvm_ks::countLeadingOnes(pVal[i] << shift);
738
0
  if (Count == highWordBits) {
739
0
    for (i--; i >= 0; --i) {
740
0
      if (pVal[i] == -1ULL)
741
0
        Count += APINT_BITS_PER_WORD;
742
0
      else {
743
0
        Count += llvm_ks::countLeadingOnes(pVal[i]);
744
0
        break;
745
0
      }
746
0
    }
747
0
  }
748
0
  return Count;
749
104
}
750
751
0
unsigned APInt::countTrailingZeros() const {
752
0
  if (isSingleWord())
753
0
    return std::min(unsigned(llvm_ks::countTrailingZeros(VAL)), BitWidth);
754
0
  unsigned Count = 0;
755
0
  unsigned i = 0;
756
0
  for (; i < getNumWords() && pVal[i] == 0; ++i)
757
0
    Count += APINT_BITS_PER_WORD;
758
0
  if (i < getNumWords())
759
0
    Count += llvm_ks::countTrailingZeros(pVal[i]);
760
0
  return std::min(Count, BitWidth);
761
0
}
762
763
0
unsigned APInt::countTrailingOnesSlowCase() const {
764
0
  unsigned Count = 0;
765
0
  unsigned i = 0;
766
0
  for (; i < getNumWords() && pVal[i] == -1ULL; ++i)
767
0
    Count += APINT_BITS_PER_WORD;
768
0
  if (i < getNumWords())
769
0
    Count += llvm_ks::countTrailingOnes(pVal[i]);
770
0
  return std::min(Count, BitWidth);
771
0
}
772
773
0
unsigned APInt::countPopulationSlowCase() const {
774
0
  unsigned Count = 0;
775
0
  for (unsigned i = 0; i < getNumWords(); ++i)
776
0
    Count += llvm_ks::countPopulation(pVal[i]);
777
0
  return Count;
778
0
}
779
780
/// Perform a logical right-shift from Src to Dst, which must be equal or
781
/// non-overlapping, of Words words, by Shift, which must be less than 64.
782
static void lshrNear(uint64_t *Dst, uint64_t *Src, unsigned Words,
783
0
                     unsigned Shift) {
784
0
  uint64_t Carry = 0;
785
0
  for (int I = Words - 1; I >= 0; --I) {
786
0
    uint64_t Tmp = Src[I];
787
0
    Dst[I] = (Tmp >> Shift) | Carry;
788
0
    Carry = Tmp << (64 - Shift);
789
0
  }
790
0
}
791
792
0
APInt APInt::byteSwap() const {
793
0
  assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!");
794
0
  if (BitWidth == 16)
795
0
    return APInt(BitWidth, ByteSwap_16(uint16_t(VAL)));
796
0
  if (BitWidth == 32)
797
0
    return APInt(BitWidth, ByteSwap_32(unsigned(VAL)));
798
0
  if (BitWidth == 48) {
799
0
    unsigned Tmp1 = unsigned(VAL >> 16);
800
0
    Tmp1 = ByteSwap_32(Tmp1);
801
0
    uint16_t Tmp2 = uint16_t(VAL);
802
0
    Tmp2 = ByteSwap_16(Tmp2);
803
0
    return APInt(BitWidth, (uint64_t(Tmp2) << 32) | Tmp1);
804
0
  }
805
0
  if (BitWidth == 64)
806
0
    return APInt(BitWidth, ByteSwap_64(VAL));
807
808
0
  APInt Result(getNumWords() * APINT_BITS_PER_WORD, 0);
809
0
  for (unsigned I = 0, N = getNumWords(); I != N; ++I)
810
0
    Result.pVal[I] = ByteSwap_64(pVal[N - I - 1]);
811
0
  if (Result.BitWidth != BitWidth) {
812
0
    lshrNear(Result.pVal, Result.pVal, getNumWords(),
813
0
             Result.BitWidth - BitWidth);
814
0
    Result.BitWidth = BitWidth;
815
0
  }
816
0
  return Result;
817
0
}
818
819
APInt llvm_ks::APIntOps::GreatestCommonDivisor(const APInt& API1,
820
0
                                            const APInt& API2) {
821
0
  APInt A = API1, B = API2;
822
0
  while (!!B) {
823
0
    APInt T = B;
824
0
    B = APIntOps::urem(A, B);
825
0
    A = T;
826
0
  }
827
0
  return A;
828
0
}
829
830
0
APInt llvm_ks::APIntOps::RoundDoubleToAPInt(double Double, unsigned width) {
831
0
  union {
832
0
    double D;
833
0
    uint64_t I;
834
0
  } T;
835
0
  T.D = Double;
836
837
  // Get the sign bit from the highest order bit
838
0
  bool isNeg = T.I >> 63;
839
840
  // Get the 11-bit exponent and adjust for the 1023 bit bias
841
0
  int64_t exp = ((T.I >> 52) & 0x7ff) - 1023;
842
843
  // If the exponent is negative, the value is < 0 so just return 0.
844
0
  if (exp < 0)
845
0
    return APInt(width, 0u);
846
847
  // Extract the mantissa by clearing the top 12 bits (sign + exponent).
848
0
  uint64_t mantissa = (T.I & (~0ULL >> 12)) | 1ULL << 52;
849
850
  // If the exponent doesn't shift all bits out of the mantissa
851
0
  if (exp < 52)
852
0
    return isNeg ? -APInt(width, mantissa >> (52 - exp)) :
853
0
                    APInt(width, mantissa >> (52 - exp));
854
855
  // If the client didn't provide enough bits for us to shift the mantissa into
856
  // then the result is undefined, just return 0
857
0
  if (width <= exp - 52)
858
0
    return APInt(width, 0);
859
860
  // Otherwise, we have to shift the mantissa bits up to the right location
861
0
  APInt Tmp(width, mantissa);
862
0
  Tmp = Tmp.shl((unsigned)exp - 52);
863
0
  return isNeg ? -Tmp : Tmp;
864
0
}
865
866
/// This function converts this APInt to a double.
867
/// The layout for double is as following (IEEE Standard 754):
868
///  --------------------------------------
869
/// |  Sign    Exponent    Fraction    Bias |
870
/// |-------------------------------------- |
871
/// |  1[63]   11[62-52]   52[51-00]   1023 |
872
///  --------------------------------------
873
0
double APInt::roundToDouble(bool isSigned) const {
874
875
  // Handle the simple case where the value is contained in one uint64_t.
876
  // It is wrong to optimize getWord(0) to VAL; there might be more than one word.
877
0
  if (isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD) {
878
0
    if (isSigned) {
879
0
      int64_t sext = (int64_t(getWord(0)) << (64-BitWidth)) >> (64-BitWidth);
880
0
      return double(sext);
881
0
    } else
882
0
      return double(getWord(0));
883
0
  }
884
885
  // Determine if the value is negative.
886
0
  bool isNeg = isSigned ? (*this)[BitWidth-1] : false;
887
888
  // Construct the absolute value if we're negative.
889
0
  APInt Tmp(isNeg ? -(*this) : (*this));
890
891
  // Figure out how many bits we're using.
892
0
  unsigned n = Tmp.getActiveBits();
893
894
  // The exponent (without bias normalization) is just the number of bits
895
  // we are using. Note that the sign bit is gone since we constructed the
896
  // absolute value.
897
0
  uint64_t exp = n;
898
899
  // Return infinity for exponent overflow
900
0
  if (exp > 1023) {
901
0
    if (!isSigned || !isNeg)
902
0
      return std::numeric_limits<double>::infinity();
903
0
    else
904
0
      return -std::numeric_limits<double>::infinity();
905
0
  }
906
0
  exp += 1023; // Increment for 1023 bias
907
908
  // Number of bits in mantissa is 52. To obtain the mantissa value, we must
909
  // extract the high 52 bits from the correct words in pVal.
910
0
  uint64_t mantissa;
911
0
  unsigned hiWord = whichWord(n-1);
912
0
  if (hiWord == 0) {
913
0
    mantissa = Tmp.pVal[0];
914
0
    if (n > 52)
915
0
      mantissa >>= n - 52; // shift down, we want the top 52 bits.
916
0
  } else {
917
0
    assert(hiWord > 0 && "huh?");
918
0
    uint64_t hibits = Tmp.pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD);
919
0
    uint64_t lobits = Tmp.pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD);
920
0
    mantissa = hibits | lobits;
921
0
  }
922
923
  // The leading bit of mantissa is implicit, so get rid of it.
924
0
  uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0;
925
0
  union {
926
0
    double D;
927
0
    uint64_t I;
928
0
  } T;
929
0
  T.I = sign | (exp << 52) | mantissa;
930
0
  return T.D;
931
0
}
932
933
// Truncate to new width.
934
0
APInt APInt::trunc(unsigned width) const {
935
0
  assert(width < BitWidth && "Invalid APInt Truncate request");
936
0
  assert(width && "Can't truncate to 0 bits");
937
938
0
  if (width <= APINT_BITS_PER_WORD)
939
0
    return APInt(width, getRawData()[0]);
940
941
0
  APInt Result(getMemory(getNumWords(width)), width);
942
943
  // Copy full words.
944
0
  unsigned i;
945
0
  for (i = 0; i != width / APINT_BITS_PER_WORD; i++)
946
0
    Result.pVal[i] = pVal[i];
947
948
  // Truncate and copy any partial word.
949
0
  unsigned bits = (0 - width) % APINT_BITS_PER_WORD;
950
0
  if (bits != 0)
951
0
    Result.pVal[i] = pVal[i] << bits >> bits;
952
953
0
  return Result;
954
0
}
955
956
// Sign extend to a new width.
957
0
APInt APInt::sext(unsigned width) const {
958
0
  assert(width > BitWidth && "Invalid APInt SignExtend request");
959
960
0
  if (width <= APINT_BITS_PER_WORD) {
961
0
    uint64_t val = VAL << (APINT_BITS_PER_WORD - BitWidth);
962
0
    val = (int64_t)val >> (width - BitWidth);
963
0
    return APInt(width, val >> (APINT_BITS_PER_WORD - width));
964
0
  }
965
966
0
  APInt Result(getMemory(getNumWords(width)), width);
967
968
  // Copy full words.
969
0
  unsigned i;
970
0
  uint64_t word = 0;
971
0
  for (i = 0; i != BitWidth / APINT_BITS_PER_WORD; i++) {
972
0
    word = getRawData()[i];
973
0
    Result.pVal[i] = word;
974
0
  }
975
976
  // Read and sign-extend any partial word.
977
0
  unsigned bits = (0 - BitWidth) % APINT_BITS_PER_WORD;
978
0
  if (bits != 0)
979
0
    word = (int64_t)getRawData()[i] << bits >> bits;
980
0
  else
981
0
    word = (int64_t)word >> (APINT_BITS_PER_WORD - 1);
982
983
  // Write remaining full words.
984
0
  for (; i != width / APINT_BITS_PER_WORD; i++) {
985
0
    Result.pVal[i] = word;
986
0
    word = (int64_t)word >> (APINT_BITS_PER_WORD - 1);
987
0
  }
988
989
  // Write any partial word.
990
0
  bits = (0 - width) % APINT_BITS_PER_WORD;
991
0
  if (bits != 0)
992
0
    Result.pVal[i] = word << bits >> bits;
993
994
0
  return Result;
995
0
}
996
997
//  Zero extend to a new width.
998
46.2k
APInt APInt::zext(unsigned width) const {
999
46.2k
  assert(width > BitWidth && "Invalid APInt ZeroExtend request");
1000
1001
46.2k
  if (width <= APINT_BITS_PER_WORD)
1002
99
    return APInt(width, VAL);
1003
1004
46.1k
  APInt Result(getMemory(getNumWords(width)), width);
1005
1006
  // Copy words.
1007
46.1k
  unsigned i;
1008
138k
  for (i = 0; i != getNumWords(); i++)
1009
92.1k
    Result.pVal[i] = getRawData()[i];
1010
1011
  // Zero remaining words.
1012
46.1k
  memset(&Result.pVal[i], 0, (Result.getNumWords() - i) * APINT_WORD_SIZE);
1013
1014
46.1k
  return Result;
1015
46.2k
}
1016
1017
0
APInt APInt::zextOrTrunc(unsigned width) const {
1018
0
  if (BitWidth < width)
1019
0
    return zext(width);
1020
0
  if (BitWidth > width)
1021
0
    return trunc(width);
1022
0
  return *this;
1023
0
}
1024
1025
0
APInt APInt::sextOrTrunc(unsigned width) const {
1026
0
  if (BitWidth < width)
1027
0
    return sext(width);
1028
0
  if (BitWidth > width)
1029
0
    return trunc(width);
1030
0
  return *this;
1031
0
}
1032
1033
0
APInt APInt::zextOrSelf(unsigned width) const {
1034
0
  if (BitWidth < width)
1035
0
    return zext(width);
1036
0
  return *this;
1037
0
}
1038
1039
0
APInt APInt::sextOrSelf(unsigned width) const {
1040
0
  if (BitWidth < width)
1041
0
    return sext(width);
1042
0
  return *this;
1043
0
}
1044
1045
/// Arithmetic right-shift this APInt by shiftAmt.
1046
/// @brief Arithmetic right-shift function.
1047
0
APInt APInt::ashr(const APInt &shiftAmt) const {
1048
0
  return ashr((unsigned)shiftAmt.getLimitedValue(BitWidth));
1049
0
}
1050
1051
/// Arithmetic right-shift this APInt by shiftAmt.
1052
/// @brief Arithmetic right-shift function.
1053
0
APInt APInt::ashr(unsigned shiftAmt) const {
1054
0
  assert(shiftAmt <= BitWidth && "Invalid shift amount");
1055
  // Handle a degenerate case
1056
0
  if (shiftAmt == 0)
1057
0
    return *this;
1058
1059
  // Handle single word shifts with built-in ashr
1060
0
  if (isSingleWord()) {
1061
0
    if (shiftAmt == BitWidth)
1062
0
      return APInt(BitWidth, 0); // undefined
1063
0
    else {
1064
0
      unsigned SignBit = APINT_BITS_PER_WORD - BitWidth;
1065
0
      return APInt(BitWidth,
1066
0
        (((int64_t(VAL) << SignBit) >> SignBit) >> shiftAmt));
1067
0
    }
1068
0
  }
1069
1070
  // If all the bits were shifted out, the result is, technically, undefined.
1071
  // We return -1 if it was negative, 0 otherwise. We check this early to avoid
1072
  // issues in the algorithm below.
1073
0
  if (shiftAmt == BitWidth) {
1074
0
    if (isNegative())
1075
0
      return APInt(BitWidth, -1ULL, true);
1076
0
    else
1077
0
      return APInt(BitWidth, 0);
1078
0
  }
1079
1080
  // Create some space for the result.
1081
0
  uint64_t * val = new uint64_t[getNumWords()];
1082
1083
  // Compute some values needed by the following shift algorithms
1084
0
  unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD; // bits to shift per word
1085
0
  unsigned offset = shiftAmt / APINT_BITS_PER_WORD; // word offset for shift
1086
0
  unsigned breakWord = getNumWords() - 1 - offset; // last word affected
1087
0
  unsigned bitsInWord = whichBit(BitWidth); // how many bits in last word?
1088
0
  if (bitsInWord == 0)
1089
0
    bitsInWord = APINT_BITS_PER_WORD;
1090
1091
  // If we are shifting whole words, just move whole words
1092
0
  if (wordShift == 0) {
1093
    // Move the words containing significant bits
1094
0
    for (unsigned i = 0; i <= breakWord; ++i)
1095
0
      val[i] = pVal[i+offset]; // move whole word
1096
1097
    // Adjust the top significant word for sign bit fill, if negative
1098
0
    if (isNegative())
1099
0
      if (bitsInWord < APINT_BITS_PER_WORD)
1100
0
        val[breakWord] |= ~0ULL << bitsInWord; // set high bits
1101
0
  } else {
1102
    // Shift the low order words
1103
0
    for (unsigned i = 0; i < breakWord; ++i) {
1104
      // This combines the shifted corresponding word with the low bits from
1105
      // the next word (shifted into this word's high bits).
1106
0
      val[i] = (pVal[i+offset] >> wordShift) |
1107
0
               (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift));
1108
0
    }
1109
1110
    // Shift the break word. In this case there are no bits from the next word
1111
    // to include in this word.
1112
0
    val[breakWord] = pVal[breakWord+offset] >> wordShift;
1113
1114
    // Deal with sign extension in the break word, and possibly the word before
1115
    // it.
1116
0
    if (isNegative()) {
1117
0
      if (wordShift > bitsInWord) {
1118
0
        if (breakWord > 0)
1119
0
          val[breakWord-1] |=
1120
0
            ~0ULL << (APINT_BITS_PER_WORD - (wordShift - bitsInWord));
1121
0
        val[breakWord] |= ~0ULL;
1122
0
      } else
1123
0
        val[breakWord] |= (~0ULL << (bitsInWord - wordShift));
1124
0
    }
1125
0
  }
1126
1127
  // Remaining words are 0 or -1, just assign them.
1128
0
  uint64_t fillValue = (isNegative() ? -1ULL : 0);
1129
0
  for (unsigned i = breakWord+1; i < getNumWords(); ++i)
1130
0
    val[i] = fillValue;
1131
0
  APInt Result(val, BitWidth);
1132
0
  Result.clearUnusedBits();
1133
0
  return Result;
1134
0
}
1135
1136
/// Logical right-shift this APInt by shiftAmt.
1137
/// @brief Logical right-shift function.
1138
0
APInt APInt::lshr(const APInt &shiftAmt) const {
1139
0
  return lshr((unsigned)shiftAmt.getLimitedValue(BitWidth));
1140
0
}
1141
1142
/// Logical right-shift this APInt by shiftAmt.
1143
/// @brief Logical right-shift function.
1144
11.2k
APInt APInt::lshr(unsigned shiftAmt) const {
1145
11.2k
  if (isSingleWord()) {
1146
2.30k
    if (shiftAmt >= BitWidth)
1147
0
      return APInt(BitWidth, 0);
1148
2.30k
    else
1149
2.30k
      return APInt(BitWidth, this->VAL >> shiftAmt);
1150
2.30k
  }
1151
1152
  // If all the bits were shifted out, the result is 0. This avoids issues
1153
  // with shifting by the size of the integer type, which produces undefined
1154
  // results. We define these "undefined results" to always be 0.
1155
8.97k
  if (shiftAmt >= BitWidth)
1156
0
    return APInt(BitWidth, 0);
1157
1158
  // If none of the bits are shifted out, the result is *this. This avoids
1159
  // issues with shifting by the size of the integer type, which produces
1160
  // undefined results in the code below. This is also an optimization.
1161
8.97k
  if (shiftAmt == 0)
1162
0
    return *this;
1163
1164
  // Create some space for the result.
1165
8.97k
  uint64_t * val = new uint64_t[getNumWords()];
1166
1167
  // If we are shifting less than a word, compute the shift with a simple carry
1168
8.97k
  if (shiftAmt < APINT_BITS_PER_WORD) {
1169
0
    lshrNear(val, pVal, getNumWords(), shiftAmt);
1170
0
    APInt Result(val, BitWidth);
1171
0
    Result.clearUnusedBits();
1172
0
    return Result;
1173
0
  }
1174
1175
  // Compute some values needed by the remaining shift algorithms
1176
8.97k
  unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD;
1177
8.97k
  unsigned offset = shiftAmt / APINT_BITS_PER_WORD;
1178
1179
  // If we are shifting whole words, just move whole words
1180
8.97k
  if (wordShift == 0) {
1181
17.9k
    for (unsigned i = 0; i < getNumWords() - offset; ++i)
1182
8.97k
      val[i] = pVal[i+offset];
1183
17.8k
    for (unsigned i = getNumWords()-offset; i < getNumWords(); i++)
1184
8.94k
      val[i] = 0;
1185
8.94k
    APInt Result(val, BitWidth);
1186
8.94k
    Result.clearUnusedBits();
1187
8.94k
    return Result;
1188
8.94k
  }
1189
1190
  // Shift the low order words
1191
36
  unsigned breakWord = getNumWords() - offset -1;
1192
72
  for (unsigned i = 0; i < breakWord; ++i)
1193
36
    val[i] = (pVal[i+offset] >> wordShift) |
1194
36
             (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift));
1195
  // Shift the break word.
1196
36
  val[breakWord] = pVal[breakWord+offset] >> wordShift;
1197
1198
  // Remaining words are 0
1199
72
  for (unsigned i = breakWord+1; i < getNumWords(); ++i)
1200
36
    val[i] = 0;
1201
36
  APInt Result(val, BitWidth);
1202
36
  Result.clearUnusedBits();
1203
36
  return Result;
1204
8.97k
}
1205
1206
/// Left-shift this APInt by shiftAmt.
1207
/// @brief Left-shift function.
1208
0
APInt APInt::shl(const APInt &shiftAmt) const {
1209
  // It's undefined behavior in C to shift by BitWidth or greater.
1210
0
  return shl((unsigned)shiftAmt.getLimitedValue(BitWidth));
1211
0
}
1212
1213
14.5M
APInt APInt::shlSlowCase(unsigned shiftAmt) const {
1214
  // If all the bits were shifted out, the result is 0. This avoids issues
1215
  // with shifting by the size of the integer type, which produces undefined
1216
  // results. We define these "undefined results" to always be 0.
1217
14.5M
  if (shiftAmt == BitWidth)
1218
0
    return APInt(BitWidth, 0);
1219
1220
  // If none of the bits are shifted out, the result is *this. This avoids a
1221
  // lshr by the words size in the loop below which can produce incorrect
1222
  // results. It also avoids the expensive computation below for a common case.
1223
14.5M
  if (shiftAmt == 0)
1224
0
    return *this;
1225
1226
  // Create some space for the result.
1227
14.5M
  uint64_t * val = new uint64_t[getNumWords()];
1228
1229
  // If we are shifting less than a word, do it the easy way
1230
14.5M
  if (shiftAmt < APINT_BITS_PER_WORD) {
1231
14.5M
    uint64_t carry = 0;
1232
205M
    for (unsigned i = 0; i < getNumWords(); i++) {
1233
190M
      val[i] = pVal[i] << shiftAmt | carry;
1234
190M
      carry = pVal[i] >> (APINT_BITS_PER_WORD - shiftAmt);
1235
190M
    }
1236
14.5M
    APInt Result(val, BitWidth);
1237
14.5M
    Result.clearUnusedBits();
1238
14.5M
    return Result;
1239
14.5M
  }
1240
1241
  // Compute some values needed by the remaining shift algorithms
1242
4.48k
  unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD;
1243
4.48k
  unsigned offset = shiftAmt / APINT_BITS_PER_WORD;
1244
1245
  // If we are shifting whole words, just move whole words
1246
4.48k
  if (wordShift == 0) {
1247
8.90k
    for (unsigned i = 0; i < offset; i++)
1248
4.45k
      val[i] = 0;
1249
8.90k
    for (unsigned i = offset; i < getNumWords(); i++)
1250
4.45k
      val[i] = pVal[i-offset];
1251
4.45k
    APInt Result(val, BitWidth);
1252
4.45k
    Result.clearUnusedBits();
1253
4.45k
    return Result;
1254
4.45k
  }
1255
1256
  // Copy whole words from this to Result.
1257
36
  unsigned i = getNumWords() - 1;
1258
72
  for (; i > offset; --i)
1259
36
    val[i] = pVal[i-offset] << wordShift |
1260
36
             pVal[i-offset-1] >> (APINT_BITS_PER_WORD - wordShift);
1261
36
  val[offset] = pVal[0] << wordShift;
1262
72
  for (i = 0; i < offset; ++i)
1263
36
    val[i] = 0;
1264
36
  APInt Result(val, BitWidth);
1265
36
  Result.clearUnusedBits();
1266
36
  return Result;
1267
4.48k
}
1268
1269
0
APInt APInt::rotl(const APInt &rotateAmt) const {
1270
0
  return rotl((unsigned)rotateAmt.getLimitedValue(BitWidth));
1271
0
}
1272
1273
0
APInt APInt::rotl(unsigned rotateAmt) const {
1274
0
  rotateAmt %= BitWidth;
1275
0
  if (rotateAmt == 0)
1276
0
    return *this;
1277
0
  return shl(rotateAmt) | lshr(BitWidth - rotateAmt);
1278
0
}
1279
1280
0
APInt APInt::rotr(const APInt &rotateAmt) const {
1281
0
  return rotr((unsigned)rotateAmt.getLimitedValue(BitWidth));
1282
0
}
1283
1284
0
APInt APInt::rotr(unsigned rotateAmt) const {
1285
0
  rotateAmt %= BitWidth;
1286
0
  if (rotateAmt == 0)
1287
0
    return *this;
1288
0
  return lshr(rotateAmt) | shl(BitWidth - rotateAmt);
1289
0
}
1290
1291
// Square Root - this method computes and returns the square root of "this".
1292
// Three mechanisms are used for computation. For small values (<= 5 bits),
1293
// a table lookup is done. This gets some performance for common cases. For
1294
// values using less than 52 bits, the value is converted to double and then
1295
// the libc sqrt function is called. The result is rounded and then converted
1296
// back to a uint64_t which is then used to construct the result. Finally,
1297
// the Babylonian method for computing square roots is used.
1298
0
APInt APInt::sqrt() const {
1299
1300
  // Determine the magnitude of the value.
1301
0
  unsigned magnitude = getActiveBits();
1302
1303
  // Use a fast table for some small values. This also gets rid of some
1304
  // rounding errors in libc sqrt for small values.
1305
0
  if (magnitude <= 5) {
1306
0
    static const uint8_t results[32] = {
1307
0
      /*     0 */ 0,
1308
0
      /*  1- 2 */ 1, 1,
1309
0
      /*  3- 6 */ 2, 2, 2, 2,
1310
0
      /*  7-12 */ 3, 3, 3, 3, 3, 3,
1311
0
      /* 13-20 */ 4, 4, 4, 4, 4, 4, 4, 4,
1312
0
      /* 21-30 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1313
0
      /*    31 */ 6
1314
0
    };
1315
0
    return APInt(BitWidth, results[ (isSingleWord() ? VAL : pVal[0]) ]);
1316
0
  }
1317
1318
  // If the magnitude of the value fits in less than 52 bits (the precision of
1319
  // an IEEE double precision floating point value), then we can use the
1320
  // libc sqrt function which will probably use a hardware sqrt computation.
1321
  // This should be faster than the algorithm below.
1322
0
  if (magnitude < 52) {
1323
0
    return APInt(BitWidth,
1324
0
                 uint64_t(::round(::sqrt(double(isSingleWord()?VAL:pVal[0])))));
1325
0
  }
1326
1327
  // Okay, all the short cuts are exhausted. We must compute it. The following
1328
  // is a classical Babylonian method for computing the square root. This code
1329
  // was adapted to APInt from a wikipedia article on such computations.
1330
  // See http://www.wikipedia.org/ and go to the page named
1331
  // Calculate_an_integer_square_root.
1332
0
  unsigned nbits = BitWidth, i = 4;
1333
0
  APInt testy(BitWidth, 16);
1334
0
  APInt x_old(BitWidth, 1);
1335
0
  APInt x_new(BitWidth, 0);
1336
0
  APInt two(BitWidth, 2);
1337
1338
  // Select a good starting value using binary logarithms.
1339
0
  for (;; i += 2, testy = testy.shl(2))
1340
0
    if (i >= nbits || this->ule(testy)) {
1341
0
      x_old = x_old.shl(i / 2);
1342
0
      break;
1343
0
    }
1344
1345
  // Use the Babylonian method to arrive at the integer square root:
1346
0
  for (;;) {
1347
0
    x_new = (this->udiv(x_old) + x_old).udiv(two);
1348
0
    if (x_old.ule(x_new))
1349
0
      break;
1350
0
    x_old = x_new;
1351
0
  }
1352
1353
  // Make sure we return the closest approximation
1354
  // NOTE: The rounding calculation below is correct. It will produce an
1355
  // off-by-one discrepancy with results from pari/gp. That discrepancy has been
1356
  // determined to be a rounding issue with pari/gp as it begins to use a
1357
  // floating point representation after 192 bits. There are no discrepancies
1358
  // between this algorithm and pari/gp for bit widths < 192 bits.
1359
0
  APInt square(x_old * x_old);
1360
0
  APInt nextSquare((x_old + 1) * (x_old +1));
1361
0
  if (this->ult(square))
1362
0
    return x_old;
1363
0
  assert(this->ule(nextSquare) && "Error in APInt::sqrt computation");
1364
0
  APInt midpoint((nextSquare - square).udiv(two));
1365
0
  APInt offset(*this - square);
1366
0
  if (offset.ult(midpoint))
1367
0
    return x_old;
1368
0
  return x_old + 1;
1369
0
}
1370
1371
/// Computes the multiplicative inverse of this APInt for a given modulo. The
1372
/// iterative extended Euclidean algorithm is used to solve for this value,
1373
/// however we simplify it to speed up calculating only the inverse, and take
1374
/// advantage of div+rem calculations. We also use some tricks to avoid copying
1375
/// (potentially large) APInts around.
1376
0
APInt APInt::multiplicativeInverse(const APInt& modulo) const {
1377
0
  assert(ult(modulo) && "This APInt must be smaller than the modulo");
1378
1379
  // Using the properties listed at the following web page (accessed 06/21/08):
1380
  //   http://www.numbertheory.org/php/euclid.html
1381
  // (especially the properties numbered 3, 4 and 9) it can be proved that
1382
  // BitWidth bits suffice for all the computations in the algorithm implemented
1383
  // below. More precisely, this number of bits suffice if the multiplicative
1384
  // inverse exists, but may not suffice for the general extended Euclidean
1385
  // algorithm.
1386
1387
0
  APInt r[2] = { modulo, *this };
1388
0
  APInt t[2] = { APInt(BitWidth, 0), APInt(BitWidth, 1) };
1389
0
  APInt q(BitWidth, 0);
1390
1391
0
  unsigned i;
1392
0
  for (i = 0; r[i^1] != 0; i ^= 1) {
1393
    // An overview of the math without the confusing bit-flipping:
1394
    // q = r[i-2] / r[i-1]
1395
    // r[i] = r[i-2] % r[i-1]
1396
    // t[i] = t[i-2] - t[i-1] * q
1397
0
    udivrem(r[i], r[i^1], q, r[i]);
1398
0
    t[i] -= t[i^1] * q;
1399
0
  }
1400
1401
  // If this APInt and the modulo are not coprime, there is no multiplicative
1402
  // inverse, so return 0. We check this by looking at the next-to-last
1403
  // remainder, which is the gcd(*this,modulo) as calculated by the Euclidean
1404
  // algorithm.
1405
0
  if (r[i] != 1)
1406
0
    return APInt(BitWidth, 0);
1407
1408
  // The next-to-last t is the multiplicative inverse.  However, we are
1409
  // interested in a positive inverse. Calcuate a positive one from a negative
1410
  // one if necessary. A simple addition of the modulo suffices because
1411
  // abs(t[i]) is known to be less than *this/2 (see the link above).
1412
0
  return t[i].isNegative() ? t[i] + modulo : t[i];
1413
0
}
1414
1415
/// Calculate the magic numbers required to implement a signed integer division
1416
/// by a constant as a sequence of multiplies, adds and shifts.  Requires that
1417
/// the divisor not be 0, 1, or -1.  Taken from "Hacker's Delight", Henry S.
1418
/// Warren, Jr., chapter 10.
1419
0
APInt::ms APInt::magic() const {
1420
0
  const APInt& d = *this;
1421
0
  unsigned p;
1422
0
  APInt ad, anc, delta, q1, r1, q2, r2, t;
1423
0
  APInt signedMin = APInt::getSignedMinValue(d.getBitWidth());
1424
0
  struct ms mag;
1425
1426
0
  ad = d.abs();
1427
0
  t = signedMin + (d.lshr(d.getBitWidth() - 1));
1428
0
  anc = t - 1 - t.urem(ad);   // absolute value of nc
1429
0
  p = d.getBitWidth() - 1;    // initialize p
1430
0
  q1 = signedMin.udiv(anc);   // initialize q1 = 2p/abs(nc)
1431
0
  r1 = signedMin - q1*anc;    // initialize r1 = rem(2p,abs(nc))
1432
0
  q2 = signedMin.udiv(ad);    // initialize q2 = 2p/abs(d)
1433
0
  r2 = signedMin - q2*ad;     // initialize r2 = rem(2p,abs(d))
1434
0
  do {
1435
0
    p = p + 1;
1436
0
    q1 = q1<<1;          // update q1 = 2p/abs(nc)
1437
0
    r1 = r1<<1;          // update r1 = rem(2p/abs(nc))
1438
0
    if (r1.uge(anc)) {  // must be unsigned comparison
1439
0
      q1 = q1 + 1;
1440
0
      r1 = r1 - anc;
1441
0
    }
1442
0
    q2 = q2<<1;          // update q2 = 2p/abs(d)
1443
0
    r2 = r2<<1;          // update r2 = rem(2p/abs(d))
1444
0
    if (r2.uge(ad)) {   // must be unsigned comparison
1445
0
      q2 = q2 + 1;
1446
0
      r2 = r2 - ad;
1447
0
    }
1448
0
    delta = ad - r2;
1449
0
  } while (q1.ult(delta) || (q1 == delta && r1 == 0));
1450
1451
0
  mag.m = q2 + 1;
1452
0
  if (d.isNegative()) mag.m = -mag.m;   // resulting magic number
1453
0
  mag.s = p - d.getBitWidth();          // resulting shift
1454
0
  return mag;
1455
0
}
1456
1457
/// Calculate the magic numbers required to implement an unsigned integer
1458
/// division by a constant as a sequence of multiplies, adds and shifts.
1459
/// Requires that the divisor not be 0.  Taken from "Hacker's Delight", Henry
1460
/// S. Warren, Jr., chapter 10.
1461
/// LeadingZeros can be used to simplify the calculation if the upper bits
1462
/// of the divided value are known zero.
1463
0
APInt::mu APInt::magicu(unsigned LeadingZeros) const {
1464
0
  const APInt& d = *this;
1465
0
  unsigned p;
1466
0
  APInt nc, delta, q1, r1, q2, r2;
1467
0
  struct mu magu;
1468
0
  magu.a = 0;               // initialize "add" indicator
1469
0
  APInt allOnes = APInt::getAllOnesValue(d.getBitWidth()).lshr(LeadingZeros);
1470
0
  APInt signedMin = APInt::getSignedMinValue(d.getBitWidth());
1471
0
  APInt signedMax = APInt::getSignedMaxValue(d.getBitWidth());
1472
1473
0
  nc = allOnes - (allOnes - d).urem(d);
1474
0
  p = d.getBitWidth() - 1;  // initialize p
1475
0
  q1 = signedMin.udiv(nc);  // initialize q1 = 2p/nc
1476
0
  r1 = signedMin - q1*nc;   // initialize r1 = rem(2p,nc)
1477
0
  q2 = signedMax.udiv(d);   // initialize q2 = (2p-1)/d
1478
0
  r2 = signedMax - q2*d;    // initialize r2 = rem((2p-1),d)
1479
0
  do {
1480
0
    p = p + 1;
1481
0
    if (r1.uge(nc - r1)) {
1482
0
      q1 = q1 + q1 + 1;  // update q1
1483
0
      r1 = r1 + r1 - nc; // update r1
1484
0
    }
1485
0
    else {
1486
0
      q1 = q1+q1; // update q1
1487
0
      r1 = r1+r1; // update r1
1488
0
    }
1489
0
    if ((r2 + 1).uge(d - r2)) {
1490
0
      if (q2.uge(signedMax)) magu.a = 1;
1491
0
      q2 = q2+q2 + 1;     // update q2
1492
0
      r2 = r2+r2 + 1 - d; // update r2
1493
0
    }
1494
0
    else {
1495
0
      if (q2.uge(signedMin)) magu.a = 1;
1496
0
      q2 = q2+q2;     // update q2
1497
0
      r2 = r2+r2 + 1; // update r2
1498
0
    }
1499
0
    delta = d - 1 - r2;
1500
0
  } while (p < d.getBitWidth()*2 &&
1501
0
           (q1.ult(delta) || (q1 == delta && r1 == 0)));
1502
0
  magu.m = q2 + 1; // resulting magic number
1503
0
  magu.s = p - d.getBitWidth();  // resulting shift
1504
0
  return magu;
1505
0
}
1506
1507
/// Implementation of Knuth's Algorithm D (Division of nonnegative integers)
1508
/// from "Art of Computer Programming, Volume 2", section 4.3.1, p. 272. The
1509
/// variables here have the same names as in the algorithm. Comments explain
1510
/// the algorithm and any deviation from it.
1511
static void KnuthDiv(unsigned *u, unsigned *v, unsigned *q, unsigned* r,
1512
0
                     unsigned m, unsigned n) {
1513
0
  assert(u && "Must provide dividend");
1514
0
  assert(v && "Must provide divisor");
1515
0
  assert(q && "Must provide quotient");
1516
0
  assert(u != v && u != q && v != q && "Must use different memory");
1517
0
  assert(n>1 && "n must be > 1");
1518
1519
  // b denotes the base of the number system. In our case b is 2^32.
1520
0
  LLVM_CONSTEXPR uint64_t b = uint64_t(1) << 32;
1521
1522
  // D1. [Normalize.] Set d = b / (v[n-1] + 1) and multiply all the digits of
1523
  // u and v by d. Note that we have taken Knuth's advice here to use a power
1524
  // of 2 value for d such that d * v[n-1] >= b/2 (b is the base). A power of
1525
  // 2 allows us to shift instead of multiply and it is easy to determine the
1526
  // shift amount from the leading zeros.  We are basically normalizing the u
1527
  // and v so that its high bits are shifted to the top of v's range without
1528
  // overflow. Note that this can require an extra word in u so that u must
1529
  // be of length m+n+1.
1530
0
  unsigned shift = countLeadingZeros(v[n-1]);
1531
0
  unsigned v_carry = 0;
1532
0
  unsigned u_carry = 0;
1533
0
  if (shift) {
1534
0
    for (unsigned i = 0; i < m+n; ++i) {
1535
0
      unsigned u_tmp = u[i] >> (32 - shift);
1536
0
      u[i] = (u[i] << shift) | u_carry;
1537
0
      u_carry = u_tmp;
1538
0
    }
1539
0
    for (unsigned i = 0; i < n; ++i) {
1540
0
      unsigned v_tmp = v[i] >> (32 - shift);
1541
0
      v[i] = (v[i] << shift) | v_carry;
1542
0
      v_carry = v_tmp;
1543
0
    }
1544
0
  }
1545
0
  u[m+n] = u_carry;
1546
1547
  // D2. [Initialize j.]  Set j to m. This is the loop counter over the places.
1548
0
  int j = m;
1549
0
  do {
1550
    // D3. [Calculate q'.].
1551
    //     Set qp = (u[j+n]*b + u[j+n-1]) / v[n-1]. (qp=qprime=q')
1552
    //     Set rp = (u[j+n]*b + u[j+n-1]) % v[n-1]. (rp=rprime=r')
1553
    // Now test if qp == b or qp*v[n-2] > b*rp + u[j+n-2]; if so, decrease
1554
    // qp by 1, inrease rp by v[n-1], and repeat this test if rp < b. The test
1555
    // on v[n-2] determines at high speed most of the cases in which the trial
1556
    // value qp is one too large, and it eliminates all cases where qp is two
1557
    // too large.
1558
0
    uint64_t dividend = ((uint64_t(u[j+n]) << 32) + u[j+n-1]);
1559
0
    uint64_t qp = dividend / v[n-1];
1560
0
    uint64_t rp = dividend % v[n-1];
1561
0
    if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) {
1562
0
      qp--;
1563
0
      rp += v[n-1];
1564
0
      if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2]))
1565
0
        qp--;
1566
0
    }
1567
1568
    // D4. [Multiply and subtract.] Replace (u[j+n]u[j+n-1]...u[j]) with
1569
    // (u[j+n]u[j+n-1]..u[j]) - qp * (v[n-1]...v[1]v[0]). This computation
1570
    // consists of a simple multiplication by a one-place number, combined with
1571
    // a subtraction.
1572
    // The digits (u[j+n]...u[j]) should be kept positive; if the result of
1573
    // this step is actually negative, (u[j+n]...u[j]) should be left as the
1574
    // true value plus b**(n+1), namely as the b's complement of
1575
    // the true value, and a "borrow" to the left should be remembered.
1576
0
    int64_t borrow = 0;
1577
0
    for (unsigned i = 0; i < n; ++i) {
1578
0
      uint64_t p = uint64_t(qp) * uint64_t(v[i]);
1579
0
      int64_t subres = int64_t(u[j+i]) - borrow - (unsigned)p;
1580
0
      u[j+i] = (unsigned)subres;
1581
0
      borrow = (p >> 32) - (subres >> 32);
1582
0
    }
1583
0
    bool isNeg = u[j+n] < borrow;
1584
0
    u[j+n] -= (unsigned)borrow;
1585
1586
    // D5. [Test remainder.] Set q[j] = qp. If the result of step D4 was
1587
    // negative, go to step D6; otherwise go on to step D7.
1588
0
    q[j] = (unsigned)qp;
1589
0
    if (isNeg) {
1590
      // D6. [Add back]. The probability that this step is necessary is very
1591
      // small, on the order of only 2/b. Make sure that test data accounts for
1592
      // this possibility. Decrease q[j] by 1
1593
0
      q[j]--;
1594
      // and add (0v[n-1]...v[1]v[0]) to (u[j+n]u[j+n-1]...u[j+1]u[j]).
1595
      // A carry will occur to the left of u[j+n], and it should be ignored
1596
      // since it cancels with the borrow that occurred in D4.
1597
0
      bool carry = false;
1598
0
      for (unsigned i = 0; i < n; i++) {
1599
0
        unsigned limit = std::min(u[j+i],v[i]);
1600
0
        u[j+i] += v[i] + carry;
1601
0
        carry = u[j+i] < limit || (carry && u[j+i] == limit);
1602
0
      }
1603
0
      u[j+n] += carry;
1604
0
    }
1605
1606
  // D7. [Loop on j.]  Decrease j by one. Now if j >= 0, go back to D3.
1607
0
  } while (--j >= 0);
1608
1609
  // D8. [Unnormalize]. Now q[...] is the desired quotient, and the desired
1610
  // remainder may be obtained by dividing u[...] by d. If r is non-null we
1611
  // compute the remainder (urem uses this).
1612
0
  if (r) {
1613
    // The value d is expressed by the "shift" value above since we avoided
1614
    // multiplication by d by using a shift left. So, all we have to do is
1615
    // shift right here. In order to mak
1616
0
    if (shift) {
1617
0
      unsigned carry = 0;
1618
0
      for (int i = n-1; i >= 0; i--) {
1619
0
        r[i] = (u[i] >> shift) | carry;
1620
0
        carry = u[i] << (32 - shift);
1621
0
      }
1622
0
    } else {
1623
0
      for (int i = n-1; i >= 0; i--) {
1624
0
        r[i] = u[i];
1625
0
      }
1626
0
    }
1627
0
  }
1628
0
}
1629
1630
void APInt::divide(const APInt LHS, unsigned lhsWords,
1631
                   const APInt &RHS, unsigned rhsWords,
1632
                   APInt *Quotient, APInt *Remainder)
1633
0
{
1634
0
  assert(lhsWords >= rhsWords && "Fractional result");
1635
1636
  // First, compose the values into an array of 32-bit words instead of
1637
  // 64-bit words. This is a necessity of both the "short division" algorithm
1638
  // and the Knuth "classical algorithm" which requires there to be native
1639
  // operations for +, -, and * on an m bit value with an m*2 bit result. We
1640
  // can't use 64-bit operands here because we don't have native results of
1641
  // 128-bits. Furthermore, casting the 64-bit values to 32-bit values won't
1642
  // work on large-endian machines.
1643
0
  uint64_t mask = ~0ull >> (sizeof(unsigned)*CHAR_BIT);
1644
0
  unsigned n = rhsWords * 2;
1645
0
  unsigned m = (lhsWords * 2) - n;
1646
1647
  // Allocate space for the temporary values we need either on the stack, if
1648
  // it will fit, or on the heap if it won't.
1649
0
  unsigned SPACE[128];
1650
0
  unsigned *U = nullptr;
1651
0
  unsigned *V = nullptr;
1652
0
  unsigned *Q = nullptr;
1653
0
  unsigned *R = nullptr;
1654
0
  if ((Remainder?4:3)*n+2*m+1 <= 128) {
1655
0
    U = &SPACE[0];
1656
0
    V = &SPACE[m+n+1];
1657
0
    Q = &SPACE[(m+n+1) + n];
1658
0
    if (Remainder)
1659
0
      R = &SPACE[(m+n+1) + n + (m+n)];
1660
0
  } else {
1661
0
    U = new unsigned[m + n + 1];
1662
0
    V = new unsigned[n];
1663
0
    Q = new unsigned[m+n];
1664
0
    if (Remainder)
1665
0
      R = new unsigned[n];
1666
0
  }
1667
1668
  // Initialize the dividend
1669
0
  memset(U, 0, (m+n+1)*sizeof(unsigned));
1670
0
  for (unsigned i = 0; i < lhsWords; ++i) {
1671
0
    uint64_t tmp = (LHS.getNumWords() == 1 ? LHS.VAL : LHS.pVal[i]);
1672
0
    U[i * 2] = (unsigned)(tmp & mask);
1673
0
    U[i * 2 + 1] = (unsigned)(tmp >> (sizeof(unsigned)*CHAR_BIT));
1674
0
  }
1675
0
  U[m+n] = 0; // this extra word is for "spill" in the Knuth algorithm.
1676
1677
  // Initialize the divisor
1678
0
  memset(V, 0, (n)*sizeof(unsigned));
1679
0
  for (unsigned i = 0; i < rhsWords; ++i) {
1680
0
    uint64_t tmp = (RHS.getNumWords() == 1 ? RHS.VAL : RHS.pVal[i]);
1681
0
    V[i * 2] = (unsigned)(tmp & mask);
1682
0
    V[i * 2 + 1] = (unsigned)(tmp >> (sizeof(unsigned)*CHAR_BIT));
1683
0
  }
1684
1685
  // initialize the quotient and remainder
1686
0
  memset(Q, 0, (m+n) * sizeof(unsigned));
1687
0
  if (Remainder)
1688
0
    memset(R, 0, n * sizeof(unsigned));
1689
1690
  // Now, adjust m and n for the Knuth division. n is the number of words in
1691
  // the divisor. m is the number of words by which the dividend exceeds the
1692
  // divisor (i.e. m+n is the length of the dividend). These sizes must not
1693
  // contain any zero words or the Knuth algorithm fails.
1694
0
  for (unsigned i = n; i > 0 && V[i-1] == 0; i--) {
1695
0
    n--;
1696
0
    m++;
1697
0
  }
1698
0
  for (unsigned i = m+n; i > 0 && U[i-1] == 0; i--)
1699
0
    m--;
1700
1701
  // If we're left with only a single word for the divisor, Knuth doesn't work
1702
  // so we implement the short division algorithm here. This is much simpler
1703
  // and faster because we are certain that we can divide a 64-bit quantity
1704
  // by a 32-bit quantity at hardware speed and short division is simply a
1705
  // series of such operations. This is just like doing short division but we
1706
  // are using base 2^32 instead of base 10.
1707
0
  assert(n != 0 && "Divide by zero?");
1708
0
  if (n == 1) {
1709
0
    unsigned divisor = V[0];
1710
0
    unsigned remainder = 0;
1711
0
    for (int i = m+n-1; i >= 0; i--) {
1712
0
      uint64_t partial_dividend = uint64_t(remainder) << 32 | U[i];
1713
0
      if (partial_dividend == 0) {
1714
0
        Q[i] = 0;
1715
0
        remainder = 0;
1716
0
      } else if (partial_dividend < divisor) {
1717
0
        Q[i] = 0;
1718
0
        remainder = (unsigned)partial_dividend;
1719
0
      } else if (partial_dividend == divisor) {
1720
0
        Q[i] = 1;
1721
0
        remainder = 0;
1722
0
      } else {
1723
0
        Q[i] = (unsigned)(partial_dividend / divisor);
1724
0
        remainder = (unsigned)(partial_dividend - (Q[i] * divisor));
1725
0
      }
1726
0
    }
1727
0
    if (R)
1728
0
      R[0] = remainder;
1729
0
  } else {
1730
    // Now we're ready to invoke the Knuth classical divide algorithm. In this
1731
    // case n > 1.
1732
0
    KnuthDiv(U, V, Q, R, m, n);
1733
0
  }
1734
1735
  // If the caller wants the quotient
1736
0
  if (Quotient) {
1737
    // Set up the Quotient value's memory.
1738
0
    if (Quotient->BitWidth != LHS.BitWidth) {
1739
0
      if (Quotient->isSingleWord())
1740
0
        Quotient->VAL = 0;
1741
0
      else
1742
0
        delete [] Quotient->pVal;
1743
0
      Quotient->BitWidth = LHS.BitWidth;
1744
0
      if (!Quotient->isSingleWord())
1745
0
        Quotient->pVal = getClearedMemory(Quotient->getNumWords());
1746
0
    } else
1747
0
      Quotient->clearAllBits();
1748
1749
    // The quotient is in Q. Reconstitute the quotient into Quotient's low
1750
    // order words.
1751
    // This case is currently dead as all users of divide() handle trivial cases
1752
    // earlier.
1753
0
    if (lhsWords == 1) {
1754
0
      uint64_t tmp =
1755
0
        uint64_t(Q[0]) | (uint64_t(Q[1]) << (APINT_BITS_PER_WORD / 2));
1756
0
      if (Quotient->isSingleWord())
1757
0
        Quotient->VAL = tmp;
1758
0
      else
1759
0
        Quotient->pVal[0] = tmp;
1760
0
    } else {
1761
0
      assert(!Quotient->isSingleWord() && "Quotient APInt not large enough");
1762
0
      for (unsigned i = 0; i < lhsWords; ++i)
1763
0
        Quotient->pVal[i] =
1764
0
          uint64_t(Q[i*2]) | (uint64_t(Q[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1765
0
    }
1766
0
  }
1767
1768
  // If the caller wants the remainder
1769
0
  if (Remainder) {
1770
    // Set up the Remainder value's memory.
1771
0
    if (Remainder->BitWidth != RHS.BitWidth) {
1772
0
      if (Remainder->isSingleWord())
1773
0
        Remainder->VAL = 0;
1774
0
      else
1775
0
        delete [] Remainder->pVal;
1776
0
      Remainder->BitWidth = RHS.BitWidth;
1777
0
      if (!Remainder->isSingleWord())
1778
0
        Remainder->pVal = getClearedMemory(Remainder->getNumWords());
1779
0
    } else
1780
0
      Remainder->clearAllBits();
1781
1782
    // The remainder is in R. Reconstitute the remainder into Remainder's low
1783
    // order words.
1784
0
    if (rhsWords == 1) {
1785
0
      uint64_t tmp =
1786
0
        uint64_t(R[0]) | (uint64_t(R[1]) << (APINT_BITS_PER_WORD / 2));
1787
0
      if (Remainder->isSingleWord())
1788
0
        Remainder->VAL = tmp;
1789
0
      else
1790
0
        Remainder->pVal[0] = tmp;
1791
0
    } else {
1792
0
      assert(!Remainder->isSingleWord() && "Remainder APInt not large enough");
1793
0
      for (unsigned i = 0; i < rhsWords; ++i)
1794
0
        Remainder->pVal[i] =
1795
0
          uint64_t(R[i*2]) | (uint64_t(R[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1796
0
    }
1797
0
  }
1798
1799
  // Clean up the memory we allocated.
1800
0
  if (U != &SPACE[0]) {
1801
0
    delete [] U;
1802
0
    delete [] V;
1803
0
    delete [] Q;
1804
0
    delete [] R;
1805
0
  }
1806
0
}
1807
1808
0
APInt APInt::udiv(const APInt& RHS) const {
1809
0
  assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
1810
1811
  // First, deal with the easy case
1812
0
  if (isSingleWord()) {
1813
0
    assert(RHS.VAL != 0 && "Divide by zero?");
1814
0
    return APInt(BitWidth, VAL / RHS.VAL);
1815
0
  }
1816
1817
  // Get some facts about the LHS and RHS number of bits and words
1818
0
  unsigned rhsBits = RHS.getActiveBits();
1819
0
  unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1820
0
  assert(rhsWords && "Divided by zero???");
1821
0
  unsigned lhsBits = this->getActiveBits();
1822
0
  unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
1823
1824
  // Deal with some degenerate cases
1825
0
  if (!lhsWords)
1826
    // 0 / X ===> 0
1827
0
    return APInt(BitWidth, 0);
1828
0
  else if (lhsWords < rhsWords || this->ult(RHS)) {
1829
    // X / Y ===> 0, iff X < Y
1830
0
    return APInt(BitWidth, 0);
1831
0
  } else if (*this == RHS) {
1832
    // X / X ===> 1
1833
0
    return APInt(BitWidth, 1);
1834
0
  } else if (lhsWords == 1 && rhsWords == 1) {
1835
    // All high words are zero, just use native divide
1836
0
    return APInt(BitWidth, this->pVal[0] / RHS.pVal[0]);
1837
0
  }
1838
1839
  // We have to compute it the hard way. Invoke the Knuth divide algorithm.
1840
0
  APInt Quotient(1,0); // to hold result.
1841
0
  divide(*this, lhsWords, RHS, rhsWords, &Quotient, nullptr);
1842
0
  return Quotient;
1843
0
}
1844
1845
0
APInt APInt::sdiv(const APInt &RHS) const {
1846
0
  if (isNegative()) {
1847
0
    if (RHS.isNegative())
1848
0
      return (-(*this)).udiv(-RHS);
1849
0
    return -((-(*this)).udiv(RHS));
1850
0
  }
1851
0
  if (RHS.isNegative())
1852
0
    return -(this->udiv(-RHS));
1853
0
  return this->udiv(RHS);
1854
0
}
1855
1856
0
APInt APInt::urem(const APInt& RHS) const {
1857
0
  assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
1858
0
  if (isSingleWord()) {
1859
0
    assert(RHS.VAL != 0 && "Remainder by zero?");
1860
0
    return APInt(BitWidth, VAL % RHS.VAL);
1861
0
  }
1862
1863
  // Get some facts about the LHS
1864
0
  unsigned lhsBits = getActiveBits();
1865
0
  unsigned lhsWords = !lhsBits ? 0 : (whichWord(lhsBits - 1) + 1);
1866
1867
  // Get some facts about the RHS
1868
0
  unsigned rhsBits = RHS.getActiveBits();
1869
0
  unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1870
0
  assert(rhsWords && "Performing remainder operation by zero ???");
1871
1872
  // Check the degenerate cases
1873
0
  if (lhsWords == 0) {
1874
    // 0 % Y ===> 0
1875
0
    return APInt(BitWidth, 0);
1876
0
  } else if (lhsWords < rhsWords || this->ult(RHS)) {
1877
    // X % Y ===> X, iff X < Y
1878
0
    return *this;
1879
0
  } else if (*this == RHS) {
1880
    // X % X == 0;
1881
0
    return APInt(BitWidth, 0);
1882
0
  } else if (lhsWords == 1) {
1883
    // All high words are zero, just use native remainder
1884
0
    return APInt(BitWidth, pVal[0] % RHS.pVal[0]);
1885
0
  }
1886
1887
  // We have to compute it the hard way. Invoke the Knuth divide algorithm.
1888
0
  APInt Remainder(1,0);
1889
0
  divide(*this, lhsWords, RHS, rhsWords, nullptr, &Remainder);
1890
0
  return Remainder;
1891
0
}
1892
1893
0
APInt APInt::srem(const APInt &RHS) const {
1894
0
  if (isNegative()) {
1895
0
    if (RHS.isNegative())
1896
0
      return -((-(*this)).urem(-RHS));
1897
0
    return -((-(*this)).urem(RHS));
1898
0
  }
1899
0
  if (RHS.isNegative())
1900
0
    return this->urem(-RHS);
1901
0
  return this->urem(RHS);
1902
0
}
1903
1904
void APInt::udivrem(const APInt &LHS, const APInt &RHS,
1905
0
                    APInt &Quotient, APInt &Remainder) {
1906
0
  assert(LHS.BitWidth == RHS.BitWidth && "Bit widths must be the same");
1907
1908
  // First, deal with the easy case
1909
0
  if (LHS.isSingleWord()) {
1910
0
    assert(RHS.VAL != 0 && "Divide by zero?");
1911
0
    uint64_t QuotVal = LHS.VAL / RHS.VAL;
1912
0
    uint64_t RemVal = LHS.VAL % RHS.VAL;
1913
0
    Quotient = APInt(LHS.BitWidth, QuotVal);
1914
0
    Remainder = APInt(LHS.BitWidth, RemVal);
1915
0
    return;
1916
0
  }
1917
1918
  // Get some size facts about the dividend and divisor
1919
0
  unsigned lhsBits  = LHS.getActiveBits();
1920
0
  unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
1921
0
  unsigned rhsBits  = RHS.getActiveBits();
1922
0
  unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1923
1924
  // Check the degenerate cases
1925
0
  if (lhsWords == 0) {
1926
0
    Quotient = 0;                // 0 / Y ===> 0
1927
0
    Remainder = 0;               // 0 % Y ===> 0
1928
0
    return;
1929
0
  }
1930
1931
0
  if (lhsWords < rhsWords || LHS.ult(RHS)) {
1932
0
    Remainder = LHS;            // X % Y ===> X, iff X < Y
1933
0
    Quotient = 0;               // X / Y ===> 0, iff X < Y
1934
0
    return;
1935
0
  }
1936
1937
0
  if (LHS == RHS) {
1938
0
    Quotient  = 1;              // X / X ===> 1
1939
0
    Remainder = 0;              // X % X ===> 0;
1940
0
    return;
1941
0
  }
1942
1943
0
  if (lhsWords == 1 && rhsWords == 1) {
1944
    // There is only one word to consider so use the native versions.
1945
0
    uint64_t lhsValue = LHS.isSingleWord() ? LHS.VAL : LHS.pVal[0];
1946
0
    uint64_t rhsValue = RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
1947
0
    Quotient = APInt(LHS.getBitWidth(), lhsValue / rhsValue);
1948
0
    Remainder = APInt(LHS.getBitWidth(), lhsValue % rhsValue);
1949
0
    return;
1950
0
  }
1951
1952
  // Okay, lets do it the long way
1953
0
  divide(LHS, lhsWords, RHS, rhsWords, &Quotient, &Remainder);
1954
0
}
1955
1956
void APInt::sdivrem(const APInt &LHS, const APInt &RHS,
1957
0
                    APInt &Quotient, APInt &Remainder) {
1958
0
  if (LHS.isNegative()) {
1959
0
    if (RHS.isNegative())
1960
0
      APInt::udivrem(-LHS, -RHS, Quotient, Remainder);
1961
0
    else {
1962
0
      APInt::udivrem(-LHS, RHS, Quotient, Remainder);
1963
0
      Quotient = -Quotient;
1964
0
    }
1965
0
    Remainder = -Remainder;
1966
0
  } else if (RHS.isNegative()) {
1967
0
    APInt::udivrem(LHS, -RHS, Quotient, Remainder);
1968
0
    Quotient = -Quotient;
1969
0
  } else {
1970
0
    APInt::udivrem(LHS, RHS, Quotient, Remainder);
1971
0
  }
1972
0
}
1973
1974
0
APInt APInt::sadd_ov(const APInt &RHS, bool &Overflow) const {
1975
0
  APInt Res = *this+RHS;
1976
0
  Overflow = isNonNegative() == RHS.isNonNegative() &&
1977
0
             Res.isNonNegative() != isNonNegative();
1978
0
  return Res;
1979
0
}
1980
1981
0
APInt APInt::uadd_ov(const APInt &RHS, bool &Overflow) const {
1982
0
  APInt Res = *this+RHS;
1983
0
  Overflow = Res.ult(RHS);
1984
0
  return Res;
1985
0
}
1986
1987
0
APInt APInt::ssub_ov(const APInt &RHS, bool &Overflow) const {
1988
0
  APInt Res = *this - RHS;
1989
0
  Overflow = isNonNegative() != RHS.isNonNegative() &&
1990
0
             Res.isNonNegative() != isNonNegative();
1991
0
  return Res;
1992
0
}
1993
1994
0
APInt APInt::usub_ov(const APInt &RHS, bool &Overflow) const {
1995
0
  APInt Res = *this-RHS;
1996
0
  Overflow = Res.ugt(*this);
1997
0
  return Res;
1998
0
}
1999
2000
0
APInt APInt::sdiv_ov(const APInt &RHS, bool &Overflow) const {
2001
  // MININT/-1  -->  overflow.
2002
0
  Overflow = isMinSignedValue() && RHS.isAllOnesValue();
2003
0
  return sdiv(RHS);
2004
0
}
2005
2006
0
APInt APInt::smul_ov(const APInt &RHS, bool &Overflow) const {
2007
0
  APInt Res = *this * RHS;
2008
  
2009
0
  if (*this != 0 && RHS != 0)
2010
0
    Overflow = Res.sdiv(RHS) != *this || Res.sdiv(*this) != RHS;
2011
0
  else
2012
0
    Overflow = false;
2013
0
  return Res;
2014
0
}
2015
2016
0
APInt APInt::umul_ov(const APInt &RHS, bool &Overflow) const {
2017
0
  APInt Res = *this * RHS;
2018
2019
0
  if (*this != 0 && RHS != 0)
2020
0
    Overflow = Res.udiv(RHS) != *this || Res.udiv(*this) != RHS;
2021
0
  else
2022
0
    Overflow = false;
2023
0
  return Res;
2024
0
}
2025
2026
0
APInt APInt::sshl_ov(const APInt &ShAmt, bool &Overflow) const {
2027
0
  Overflow = ShAmt.uge(getBitWidth());
2028
0
  if (Overflow)
2029
0
    return APInt(BitWidth, 0);
2030
2031
0
  if (isNonNegative()) // Don't allow sign change.
2032
0
    Overflow = ShAmt.uge(countLeadingZeros());
2033
0
  else
2034
0
    Overflow = ShAmt.uge(countLeadingOnes());
2035
  
2036
0
  return *this << ShAmt;
2037
0
}
2038
2039
0
APInt APInt::ushl_ov(const APInt &ShAmt, bool &Overflow) const {
2040
0
  Overflow = ShAmt.uge(getBitWidth());
2041
0
  if (Overflow)
2042
0
    return APInt(BitWidth, 0);
2043
2044
0
  Overflow = ShAmt.ugt(countLeadingZeros());
2045
2046
0
  return *this << ShAmt;
2047
0
}
2048
2049
2050
2051
2052
0
void APInt::fromString(unsigned numbits, StringRef str, uint8_t radix) {
2053
  // Check our assumptions here
2054
0
  assert(!str.empty() && "Invalid string length");
2055
0
  assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 || 
2056
0
          radix == 36) &&
2057
0
         "Radix should be 2, 8, 10, 16, or 36!");
2058
2059
0
  StringRef::iterator p = str.begin();
2060
0
  size_t slen = str.size();
2061
0
  bool isNeg = *p == '-';
2062
0
  if (*p == '-' || *p == '+') {
2063
0
    p++;
2064
0
    slen--;
2065
0
    assert(slen && "String is only a sign, needs a value.");
2066
0
  }
2067
0
  assert((slen <= numbits || radix != 2) && "Insufficient bit width");
2068
0
  assert(((slen-1)*3 <= numbits || radix != 8) && "Insufficient bit width");
2069
0
  assert(((slen-1)*4 <= numbits || radix != 16) && "Insufficient bit width");
2070
0
  assert((((slen-1)*64)/22 <= numbits || radix != 10) &&
2071
0
         "Insufficient bit width");
2072
2073
  // Allocate memory
2074
0
  if (!isSingleWord())
2075
0
    pVal = getClearedMemory(getNumWords());
2076
2077
  // Figure out if we can shift instead of multiply
2078
0
  unsigned shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0);
2079
2080
  // Set up an APInt for the digit to add outside the loop so we don't
2081
  // constantly construct/destruct it.
2082
0
  APInt apdigit(getBitWidth(), 0);
2083
0
  APInt apradix(getBitWidth(), radix);
2084
2085
  // Enter digit traversal loop
2086
0
  for (StringRef::iterator e = str.end(); p != e; ++p) {
2087
0
    unsigned digit = getDigit(*p, radix);
2088
0
    assert(digit < radix && "Invalid character in digit string");
2089
2090
    // Shift or multiply the value by the radix
2091
0
    if (slen > 1) {
2092
0
      if (shift)
2093
0
        *this <<= shift;
2094
0
      else
2095
0
        *this *= apradix;
2096
0
    }
2097
2098
    // Add in the digit we just interpreted
2099
0
    if (apdigit.isSingleWord())
2100
0
      apdigit.VAL = digit;
2101
0
    else
2102
0
      apdigit.pVal[0] = digit;
2103
0
    *this += apdigit;
2104
0
  }
2105
  // If its negative, put it in two's complement form
2106
0
  if (isNeg) {
2107
0
    --(*this);
2108
0
    this->flipAllBits();
2109
0
  }
2110
0
}
2111
2112
void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix,
2113
0
                     bool Signed, bool formatAsCLiteral) const {
2114
0
  assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 || 
2115
0
          Radix == 36) &&
2116
0
         "Radix should be 2, 8, 10, 16, or 36!");
2117
2118
0
  const char *Prefix = "";
2119
0
  if (formatAsCLiteral) {
2120
0
    switch (Radix) {
2121
0
      case 2:
2122
        // Binary literals are a non-standard extension added in gcc 4.3:
2123
        // http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Binary-constants.html
2124
0
        Prefix = "0b";
2125
0
        break;
2126
0
      case 8:
2127
0
        Prefix = "0";
2128
0
        break;
2129
0
      case 10:
2130
0
        break; // No prefix
2131
0
      case 16:
2132
0
        Prefix = "0x";
2133
0
        break;
2134
0
      default:
2135
0
        llvm_unreachable("Invalid radix!");
2136
0
    }
2137
0
  }
2138
2139
  // First, check for a zero value and just short circuit the logic below.
2140
0
  if (*this == 0) {
2141
0
    while (*Prefix) {
2142
0
      Str.push_back(*Prefix);
2143
0
      ++Prefix;
2144
0
    };
2145
0
    Str.push_back('0');
2146
0
    return;
2147
0
  }
2148
2149
0
  static const char Digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2150
2151
0
  if (isSingleWord()) {
2152
0
    char Buffer[65];
2153
0
    char *BufPtr = Buffer+65;
2154
2155
0
    uint64_t N;
2156
0
    if (!Signed) {
2157
0
      N = getZExtValue();
2158
0
    } else {
2159
0
      int64_t I = getSExtValue();
2160
0
      if (I >= 0) {
2161
0
        N = I;
2162
0
      } else {
2163
0
        Str.push_back('-');
2164
0
        N = -(uint64_t)I;
2165
0
      }
2166
0
    }
2167
2168
0
    while (*Prefix) {
2169
0
      Str.push_back(*Prefix);
2170
0
      ++Prefix;
2171
0
    };
2172
2173
0
    while (N) {
2174
0
      *--BufPtr = Digits[N % Radix];
2175
0
      N /= Radix;
2176
0
    }
2177
0
    Str.append(BufPtr, Buffer+65);
2178
0
    return;
2179
0
  }
2180
2181
0
  APInt Tmp(*this);
2182
2183
0
  if (Signed && isNegative()) {
2184
    // They want to print the signed version and it is a negative value
2185
    // Flip the bits and add one to turn it into the equivalent positive
2186
    // value and put a '-' in the result.
2187
0
    Tmp.flipAllBits();
2188
0
    ++Tmp;
2189
0
    Str.push_back('-');
2190
0
  }
2191
2192
0
  while (*Prefix) {
2193
0
    Str.push_back(*Prefix);
2194
0
    ++Prefix;
2195
0
  };
2196
2197
  // We insert the digits backward, then reverse them to get the right order.
2198
0
  unsigned StartDig = Str.size();
2199
2200
  // For the 2, 8 and 16 bit cases, we can just shift instead of divide
2201
  // because the number of bits per digit (1, 3 and 4 respectively) divides
2202
  // equaly.  We just shift until the value is zero.
2203
0
  if (Radix == 2 || Radix == 8 || Radix == 16) {
2204
    // Just shift tmp right for each digit width until it becomes zero
2205
0
    unsigned ShiftAmt = (Radix == 16 ? 4 : (Radix == 8 ? 3 : 1));
2206
0
    unsigned MaskAmt = Radix - 1;
2207
2208
0
    while (Tmp != 0) {
2209
0
      unsigned Digit = unsigned(Tmp.getRawData()[0]) & MaskAmt;
2210
0
      Str.push_back(Digits[Digit]);
2211
0
      Tmp = Tmp.lshr(ShiftAmt);
2212
0
    }
2213
0
  } else {
2214
0
    APInt divisor(Radix == 10? 4 : 8, Radix);
2215
0
    while (Tmp != 0) {
2216
0
      APInt APdigit(1, 0);
2217
0
      APInt tmp2(Tmp.getBitWidth(), 0);
2218
0
      divide(Tmp, Tmp.getNumWords(), divisor, divisor.getNumWords(), &tmp2,
2219
0
             &APdigit);
2220
0
      unsigned Digit = (unsigned)APdigit.getZExtValue();
2221
0
      assert(Digit < Radix && "divide failed");
2222
0
      Str.push_back(Digits[Digit]);
2223
0
      Tmp = tmp2;
2224
0
    }
2225
0
  }
2226
2227
  // Reverse the digits before returning.
2228
0
  std::reverse(Str.begin()+StartDig, Str.end());
2229
0
}
2230
2231
/// Returns the APInt as a std::string. Note that this is an inefficient method.
2232
/// It is better to pass in a SmallVector/SmallString to the methods above.
2233
0
std::string APInt::toString(unsigned Radix = 10, bool Signed = true) const {
2234
0
  SmallString<40> S;
2235
0
  toString(S, Radix, Signed, /* formatAsCLiteral = */false);
2236
0
  return S.str();
2237
0
}
2238
2239
2240
0
LLVM_DUMP_METHOD void APInt::dump() const {
2241
0
  SmallString<40> S, U;
2242
0
  this->toStringUnsigned(U);
2243
0
  this->toStringSigned(S);
2244
0
}
2245
2246
0
void APInt::print(raw_ostream &OS, bool isSigned) const {
2247
0
  SmallString<40> S;
2248
0
  this->toString(S, 10, isSigned, /* formatAsCLiteral = */false);
2249
0
  OS << S;
2250
0
}
2251
2252
// This implements a variety of operations on a representation of
2253
// arbitrary precision, two's-complement, bignum integer values.
2254
2255
// Assumed by lowHalf, highHalf, partMSB and partLSB.  A fairly safe
2256
// and unrestricting assumption.
2257
static_assert(integerPartWidth % 2 == 0, "Part width must be divisible by 2!");
2258
2259
/* Some handy functions local to this file.  */
2260
namespace {
2261
2262
  /* Returns the integer part with the least significant BITS set.
2263
     BITS cannot be zero.  */
2264
  static inline integerPart
2265
  lowBitMask(unsigned int bits)
2266
19.1M
  {
2267
19.1M
    assert(bits != 0 && bits <= integerPartWidth);
2268
2269
19.1M
    return ~(integerPart) 0 >> (integerPartWidth - bits);
2270
19.1M
  }
2271
2272
  /* Returns the value of the lower half of PART.  */
2273
  static inline integerPart
2274
  lowHalf(integerPart part)
2275
17.7M
  {
2276
17.7M
    return part & lowBitMask(integerPartWidth / 2);
2277
17.7M
  }
2278
2279
  /* Returns the value of the upper half of PART.  */
2280
  static inline integerPart
2281
  highHalf(integerPart part)
2282
26.6M
  {
2283
26.6M
    return part >> (integerPartWidth / 2);
2284
26.6M
  }
2285
2286
  /* Returns the bit number of the most significant set bit of a part.
2287
     If the input number has no bits set -1U is returned.  */
2288
  static unsigned int
2289
  partMSB(integerPart value)
2290
3.65M
  {
2291
3.65M
    return findLastSet(value, ZB_Max);
2292
3.65M
  }
2293
2294
  /* Returns the bit number of the least significant set bit of a
2295
     part.  If the input number has no bits set -1U is returned.  */
2296
  static unsigned int
2297
  partLSB(integerPart value)
2298
622k
  {
2299
622k
    return findFirstSet(value, ZB_Max);
2300
622k
  }
2301
}
2302
2303
/* Sets the least significant part of a bignum to the input value, and
2304
   zeroes out higher parts.  */
2305
void
2306
APInt::tcSet(integerPart *dst, integerPart part, unsigned int parts)
2307
813k
{
2308
813k
  unsigned int i;
2309
2310
813k
  assert(parts > 0);
2311
2312
813k
  dst[0] = part;
2313
1.24M
  for (i = 1; i < parts; i++)
2314
434k
    dst[i] = 0;
2315
813k
}
2316
2317
/* Assign one bignum to another.  */
2318
void
2319
APInt::tcAssign(integerPart *dst, const integerPart *src, unsigned int parts)
2320
1.46M
{
2321
1.46M
  unsigned int i;
2322
2323
3.20M
  for (i = 0; i < parts; i++)
2324
1.73M
    dst[i] = src[i];
2325
1.46M
}
2326
2327
/* Returns true if a bignum is zero, false otherwise.  */
2328
bool
2329
APInt::tcIsZero(const integerPart *src, unsigned int parts)
2330
1.21M
{
2331
1.21M
  unsigned int i;
2332
2333
1.33M
  for (i = 0; i < parts; i++)
2334
1.32M
    if (src[i])
2335
1.19M
      return false;
2336
2337
13.2k
  return true;
2338
1.21M
}
2339
2340
/* Extract the given bit of a bignum; returns 0 or 1.  */
2341
int
2342
APInt::tcExtractBit(const integerPart *parts, unsigned int bit)
2343
935k
{
2344
935k
  return (parts[bit / integerPartWidth] &
2345
935k
          ((integerPart) 1 << bit % integerPartWidth)) != 0;
2346
935k
}
2347
2348
/* Set the given bit of a bignum. */
2349
void
2350
APInt::tcSetBit(integerPart *parts, unsigned int bit)
2351
16.7M
{
2352
16.7M
  parts[bit / integerPartWidth] |= (integerPart) 1 << (bit % integerPartWidth);
2353
16.7M
}
2354
2355
/* Clears the given bit of a bignum. */
2356
void
2357
APInt::tcClearBit(integerPart *parts, unsigned int bit)
2358
0
{
2359
0
  parts[bit / integerPartWidth] &=
2360
0
    ~((integerPart) 1 << (bit % integerPartWidth));
2361
0
}
2362
2363
/* Returns the bit number of the least significant set bit of a
2364
   number.  If the input number has no bits set -1U is returned.  */
2365
unsigned int
2366
APInt::tcLSB(const integerPart *parts, unsigned int n)
2367
622k
{
2368
622k
  unsigned int i, lsb;
2369
2370
659k
  for (i = 0; i < n; i++) {
2371
659k
      if (parts[i] != 0) {
2372
622k
          lsb = partLSB(parts[i]);
2373
2374
622k
          return lsb + i * integerPartWidth;
2375
622k
      }
2376
659k
  }
2377
2378
0
  return -1U;
2379
622k
}
2380
2381
/* Returns the bit number of the most significant set bit of a number.
2382
   If the input number has no bits set -1U is returned.  */
2383
unsigned int
2384
APInt::tcMSB(const integerPart *parts, unsigned int n)
2385
3.66M
{
2386
3.66M
  unsigned int msb;
2387
2388
3.72M
  do {
2389
3.72M
    --n;
2390
2391
3.72M
    if (parts[n] != 0) {
2392
3.65M
      msb = partMSB(parts[n]);
2393
2394
3.65M
      return msb + n * integerPartWidth;
2395
3.65M
    }
2396
3.72M
  } while (n);
2397
2398
14.4k
  return -1U;
2399
3.66M
}
2400
2401
/* Copy the bit vector of width srcBITS from SRC, starting at bit
2402
   srcLSB, to DST, of dstCOUNT parts, such that the bit srcLSB becomes
2403
   the least significant bit of DST.  All high bits above srcBITS in
2404
   DST are zero-filled.  */
2405
void
2406
APInt::tcExtract(integerPart *dst, unsigned int dstCount,const integerPart *src,
2407
                 unsigned int srcBits, unsigned int srcLSB)
2408
1.38M
{
2409
1.38M
  unsigned int firstSrcPart, dstParts, shift, n;
2410
2411
1.38M
  dstParts = (srcBits + integerPartWidth - 1) / integerPartWidth;
2412
1.38M
  assert(dstParts <= dstCount);
2413
2414
1.38M
  firstSrcPart = srcLSB / integerPartWidth;
2415
1.38M
  tcAssign (dst, src + firstSrcPart, dstParts);
2416
2417
1.38M
  shift = srcLSB % integerPartWidth;
2418
1.38M
  tcShiftRight (dst, dstParts, shift);
2419
2420
  /* We now have (dstParts * integerPartWidth - shift) bits from SRC
2421
     in DST.  If this is less that srcBits, append the rest, else
2422
     clear the high bits.  */
2423
1.38M
  n = dstParts * integerPartWidth - shift;
2424
1.38M
  if (n < srcBits) {
2425
45.2k
    integerPart mask = lowBitMask (srcBits - n);
2426
45.2k
    dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] & mask)
2427
45.2k
                          << n % integerPartWidth);
2428
1.34M
  } else if (n > srcBits) {
2429
1.33M
    if (srcBits % integerPartWidth)
2430
1.33M
      dst[dstParts - 1] &= lowBitMask (srcBits % integerPartWidth);
2431
1.33M
  }
2432
2433
  /* Clear high parts.  */
2434
1.45M
  while (dstParts < dstCount)
2435
68.6k
    dst[dstParts++] = 0;
2436
1.38M
}
2437
2438
/* DST += RHS + C where C is zero or one.  Returns the carry flag.  */
2439
integerPart
2440
APInt::tcAdd(integerPart *dst, const integerPart *rhs,
2441
             integerPart c, unsigned int parts)
2442
0
{
2443
0
  unsigned int i;
2444
2445
0
  assert(c <= 1);
2446
2447
0
  for (i = 0; i < parts; i++) {
2448
0
    integerPart l;
2449
2450
0
    l = dst[i];
2451
0
    if (c) {
2452
0
      dst[i] += rhs[i] + 1;
2453
0
      c = (dst[i] <= l);
2454
0
    } else {
2455
0
      dst[i] += rhs[i];
2456
0
      c = (dst[i] < l);
2457
0
    }
2458
0
  }
2459
2460
0
  return c;
2461
0
}
2462
2463
/* DST -= RHS + C where C is zero or one.  Returns the carry flag.  */
2464
integerPart
2465
APInt::tcSubtract(integerPart *dst, const integerPart *rhs,
2466
                  integerPart c, unsigned int parts)
2467
16.7M
{
2468
16.7M
  unsigned int i;
2469
2470
16.7M
  assert(c <= 1);
2471
2472
266M
  for (i = 0; i < parts; i++) {
2473
250M
    integerPart l;
2474
2475
250M
    l = dst[i];
2476
250M
    if (c) {
2477
59.8M
      dst[i] -= rhs[i] + 1;
2478
59.8M
      c = (dst[i] >= l);
2479
190M
    } else {
2480
190M
      dst[i] -= rhs[i];
2481
190M
      c = (dst[i] > l);
2482
190M
    }
2483
250M
  }
2484
2485
16.7M
  return c;
2486
16.7M
}
2487
2488
/* Negate a bignum in-place.  */
2489
void
2490
APInt::tcNegate(integerPart *dst, unsigned int parts)
2491
0
{
2492
0
  tcComplement(dst, parts);
2493
0
  tcIncrement(dst, parts);
2494
0
}
2495
2496
/*  DST += SRC * MULTIPLIER + CARRY   if add is true
2497
    DST  = SRC * MULTIPLIER + CARRY   if add is false
2498
2499
    Requires 0 <= DSTPARTS <= SRCPARTS + 1.  If DST overlaps SRC
2500
    they must start at the same point, i.e. DST == SRC.
2501
2502
    If DSTPARTS == SRCPARTS + 1 no overflow occurs and zero is
2503
    returned.  Otherwise DST is filled with the least significant
2504
    DSTPARTS parts of the result, and if all of the omitted higher
2505
    parts were zero return zero, otherwise overflow occurred and
2506
    return one.  */
2507
int
2508
APInt::tcMultiplyPart(integerPart *dst, const integerPart *src,
2509
                      integerPart multiplier, integerPart carry,
2510
                      unsigned int srcParts, unsigned int dstParts,
2511
                      bool add)
2512
955k
{
2513
955k
  unsigned int i, n;
2514
2515
  /* Otherwise our writes of DST kill our later reads of SRC.  */
2516
955k
  assert(dst <= src || dst >= src + srcParts);
2517
955k
  assert(dstParts <= srcParts + 1);
2518
2519
  /* N loops; minimum of dstParts and srcParts.  */
2520
955k
  n = dstParts < srcParts ? dstParts: srcParts;
2521
2522
5.80M
  for (i = 0; i < n; i++) {
2523
4.84M
    integerPart low, mid, high, srcPart;
2524
2525
      /* [ LOW, HIGH ] = MULTIPLIER * SRC[i] + DST[i] + CARRY.
2526
2527
         This cannot overflow, because
2528
2529
         (n - 1) * (n - 1) + 2 (n - 1) = (n - 1) * (n + 1)
2530
2531
         which is less than n^2.  */
2532
2533
4.84M
    srcPart = src[i];
2534
2535
4.84M
    if (multiplier == 0 || srcPart == 0)        {
2536
404k
      low = carry;
2537
404k
      high = 0;
2538
4.44M
    } else {
2539
4.44M
      low = lowHalf(srcPart) * lowHalf(multiplier);
2540
4.44M
      high = highHalf(srcPart) * highHalf(multiplier);
2541
2542
4.44M
      mid = lowHalf(srcPart) * highHalf(multiplier);
2543
4.44M
      high += highHalf(mid);
2544
4.44M
      mid <<= integerPartWidth / 2;
2545
4.44M
      if (low + mid < low)
2546
977k
        high++;
2547
4.44M
      low += mid;
2548
2549
4.44M
      mid = highHalf(srcPart) * lowHalf(multiplier);
2550
4.44M
      high += highHalf(mid);
2551
4.44M
      mid <<= integerPartWidth / 2;
2552
4.44M
      if (low + mid < low)
2553
2.07M
        high++;
2554
4.44M
      low += mid;
2555
2556
      /* Now add carry.  */
2557
4.44M
      if (low + carry < low)
2558
905k
        high++;
2559
4.44M
      low += carry;
2560
4.44M
    }
2561
2562
4.84M
    if (add) {
2563
      /* And now DST[i], and store the new low part there.  */
2564
2.11M
      if (low + dst[i] < low)
2565
712k
        high++;
2566
2.11M
      dst[i] += low;
2567
2.11M
    } else
2568
2.73M
      dst[i] = low;
2569
2570
4.84M
    carry = high;
2571
4.84M
  }
2572
2573
955k
  if (i < dstParts) {
2574
    /* Full multiplication, there is no overflow.  */
2575
955k
    assert(i + 1 == dstParts);
2576
955k
    dst[i] = carry;
2577
955k
    return 0;
2578
955k
  } else {
2579
    /* We overflowed if there is carry.  */
2580
0
    if (carry)
2581
0
      return 1;
2582
2583
    /* We would overflow if any significant unwritten parts would be
2584
       non-zero.  This is true if any remaining src parts are non-zero
2585
       and the multiplier is non-zero.  */
2586
0
    if (multiplier)
2587
0
      for (; i < srcParts; i++)
2588
0
        if (src[i])
2589
0
          return 1;
2590
2591
    /* We fitted in the narrow destination.  */
2592
0
    return 0;
2593
0
  }
2594
955k
}
2595
2596
/* DST = LHS * RHS, where DST has the same width as the operands and
2597
   is filled with the least significant parts of the result.  Returns
2598
   one if overflow occurred, otherwise zero.  DST must be disjoint
2599
   from both operands.  */
2600
int
2601
APInt::tcMultiply(integerPart *dst, const integerPart *lhs,
2602
                  const integerPart *rhs, unsigned int parts)
2603
0
{
2604
0
  unsigned int i;
2605
0
  int overflow;
2606
2607
0
  assert(dst != lhs && dst != rhs);
2608
2609
0
  overflow = 0;
2610
0
  tcSet(dst, 0, parts);
2611
2612
0
  for (i = 0; i < parts; i++)
2613
0
    overflow |= tcMultiplyPart(&dst[i], lhs, rhs[i], 0, parts,
2614
0
                               parts - i, true);
2615
2616
0
  return overflow;
2617
0
}
2618
2619
/* DST = LHS * RHS, where DST has width the sum of the widths of the
2620
   operands.  No overflow occurs.  DST must be disjoint from both
2621
   operands.  Returns the number of parts required to hold the
2622
   result.  */
2623
unsigned int
2624
APInt::tcFullMultiply(integerPart *dst, const integerPart *lhs,
2625
                      const integerPart *rhs, unsigned int lhsParts,
2626
                      unsigned int rhsParts)
2627
247k
{
2628
  /* Put the narrower number on the LHS for less loops below.  */
2629
247k
  if (lhsParts > rhsParts) {
2630
0
    return tcFullMultiply (dst, rhs, lhs, rhsParts, lhsParts);
2631
247k
  } else {
2632
247k
    unsigned int n;
2633
2634
247k
    assert(dst != lhs && dst != rhs);
2635
2636
247k
    tcSet(dst, 0, rhsParts);
2637
2638
667k
    for (n = 0; n < lhsParts; n++)
2639
419k
      tcMultiplyPart(&dst[n], rhs, lhs[n], 0, rhsParts, rhsParts + 1, true);
2640
2641
247k
    n = lhsParts + rhsParts;
2642
2643
247k
    return n - (dst[n - 1] == 0);
2644
247k
  }
2645
247k
}
2646
2647
/* If RHS is zero LHS and REMAINDER are left unchanged, return one.
2648
   Otherwise set LHS to LHS / RHS with the fractional part discarded,
2649
   set REMAINDER to the remainder, return zero.  i.e.
2650
2651
   OLD_LHS = RHS * LHS + REMAINDER
2652
2653
   SCRATCH is a bignum of the same size as the operands and result for
2654
   use by the routine; its contents need not be initialized and are
2655
   destroyed.  LHS, REMAINDER and SCRATCH must be distinct.
2656
*/
2657
int
2658
APInt::tcDivide(integerPart *lhs, const integerPart *rhs,
2659
                integerPart *remainder, integerPart *srhs,
2660
                unsigned int parts)
2661
0
{
2662
0
  unsigned int n, shiftCount;
2663
0
  integerPart mask;
2664
2665
0
  assert(lhs != remainder && lhs != srhs && remainder != srhs);
2666
2667
0
  shiftCount = tcMSB(rhs, parts) + 1;
2668
0
  if (shiftCount == 0)
2669
0
    return true;
2670
2671
0
  shiftCount = parts * integerPartWidth - shiftCount;
2672
0
  n = shiftCount / integerPartWidth;
2673
0
  mask = (integerPart) 1 << (shiftCount % integerPartWidth);
2674
2675
0
  tcAssign(srhs, rhs, parts);
2676
0
  tcShiftLeft(srhs, parts, shiftCount);
2677
0
  tcAssign(remainder, lhs, parts);
2678
0
  tcSet(lhs, 0, parts);
2679
2680
  /* Loop, subtracting SRHS if REMAINDER is greater and adding that to
2681
     the total.  */
2682
0
  for (;;) {
2683
0
      int compare;
2684
2685
0
      compare = tcCompare(remainder, srhs, parts);
2686
0
      if (compare >= 0) {
2687
0
        tcSubtract(remainder, srhs, 0, parts);
2688
0
        lhs[n] |= mask;
2689
0
      }
2690
2691
0
      if (shiftCount == 0)
2692
0
        break;
2693
0
      shiftCount--;
2694
0
      tcShiftRight(srhs, parts, 1);
2695
0
      if ((mask >>= 1) == 0)
2696
0
        mask = (integerPart) 1 << (integerPartWidth - 1), n--;
2697
0
  }
2698
2699
0
  return false;
2700
0
}
2701
2702
/* Shift a bignum left COUNT bits in-place.  Shifted in bits are zero.
2703
   There are no restrictions on COUNT.  */
2704
void
2705
APInt::tcShiftLeft(integerPart *dst, unsigned int parts, unsigned int count)
2706
35.0M
{
2707
35.0M
  if (count) {
2708
35.0M
    unsigned int jump, shift;
2709
2710
    /* Jump is the inter-part jump; shift is is intra-part shift.  */
2711
35.0M
    jump = count / integerPartWidth;
2712
35.0M
    shift = count % integerPartWidth;
2713
2714
620M
    while (parts > jump) {
2715
585M
      integerPart part;
2716
2717
585M
      parts--;
2718
2719
      /* dst[i] comes from the two parts src[i - jump] and, if we have
2720
         an intra-part shift, src[i - jump - 1].  */
2721
585M
      part = dst[parts - jump];
2722
585M
      if (shift) {
2723
585M
        part <<= shift;
2724
585M
        if (parts >= jump + 1)
2725
550M
          part |= dst[parts - jump - 1] >> (integerPartWidth - shift);
2726
585M
      }
2727
2728
585M
      dst[parts] = part;
2729
585M
    }
2730
2731
35.1M
    while (parts > 0)
2732
64.5k
      dst[--parts] = 0;
2733
35.0M
  }
2734
35.0M
}
2735
2736
/* Shift a bignum right COUNT bits in-place.  Shifted in bits are
2737
   zero.  There are no restrictions on COUNT.  */
2738
void
2739
APInt::tcShiftRight(integerPart *dst, unsigned int parts, unsigned int count)
2740
1.50M
{
2741
1.50M
  if (count) {
2742
619k
    unsigned int i, jump, shift;
2743
2744
    /* Jump is the inter-part jump; shift is is intra-part shift.  */
2745
619k
    jump = count / integerPartWidth;
2746
619k
    shift = count % integerPartWidth;
2747
2748
    /* Perform the shift.  This leaves the most significant COUNT bits
2749
       of the result at zero.  */
2750
1.37M
    for (i = 0; i < parts; i++) {
2751
754k
      integerPart part;
2752
2753
754k
      if (i + jump >= parts) {
2754
15.5k
        part = 0;
2755
738k
      } else {
2756
738k
        part = dst[i + jump];
2757
738k
        if (shift) {
2758
738k
          part >>= shift;
2759
738k
          if (i + jump + 1 < parts)
2760
137k
            part |= dst[i + jump + 1] << (integerPartWidth - shift);
2761
738k
        }
2762
738k
      }
2763
2764
754k
      dst[i] = part;
2765
754k
    }
2766
619k
  }
2767
1.50M
}
2768
2769
/* Bitwise and of two bignums.  */
2770
void
2771
APInt::tcAnd(integerPart *dst, const integerPart *rhs, unsigned int parts)
2772
0
{
2773
0
  unsigned int i;
2774
2775
0
  for (i = 0; i < parts; i++)
2776
0
    dst[i] &= rhs[i];
2777
0
}
2778
2779
/* Bitwise inclusive or of two bignums.  */
2780
void
2781
APInt::tcOr(integerPart *dst, const integerPart *rhs, unsigned int parts)
2782
0
{
2783
0
  unsigned int i;
2784
2785
0
  for (i = 0; i < parts; i++)
2786
0
    dst[i] |= rhs[i];
2787
0
}
2788
2789
/* Bitwise exclusive or of two bignums.  */
2790
void
2791
APInt::tcXor(integerPart *dst, const integerPart *rhs, unsigned int parts)
2792
0
{
2793
0
  unsigned int i;
2794
2795
0
  for (i = 0; i < parts; i++)
2796
0
    dst[i] ^= rhs[i];
2797
0
}
2798
2799
/* Complement a bignum in-place.  */
2800
void
2801
APInt::tcComplement(integerPart *dst, unsigned int parts)
2802
0
{
2803
0
  unsigned int i;
2804
2805
0
  for (i = 0; i < parts; i++)
2806
0
    dst[i] = ~dst[i];
2807
0
}
2808
2809
/* Comparison (unsigned) of two bignums.  */
2810
int
2811
APInt::tcCompare(const integerPart *lhs, const integerPart *rhs,
2812
                 unsigned int parts)
2813
35.0M
{
2814
35.1M
  while (parts) {
2815
35.1M
      parts--;
2816
35.1M
      if (lhs[parts] == rhs[parts])
2817
104k
        continue;
2818
2819
35.0M
      if (lhs[parts] > rhs[parts])
2820
17.2M
        return 1;
2821
17.8M
      else
2822
17.8M
        return -1;
2823
35.0M
    }
2824
2825
25.6k
  return 0;
2826
35.0M
}
2827
2828
/* Increment a bignum in-place, return the carry flag.  */
2829
integerPart
2830
APInt::tcIncrement(integerPart *dst, unsigned int parts)
2831
364k
{
2832
364k
  unsigned int i;
2833
2834
365k
  for (i = 0; i < parts; i++)
2835
365k
    if (++dst[i] != 0)
2836
364k
      break;
2837
2838
364k
  return i == parts;
2839
364k
}
2840
2841
/* Decrement a bignum in-place, return the borrow flag.  */
2842
integerPart
2843
0
APInt::tcDecrement(integerPart *dst, unsigned int parts) {
2844
0
  for (unsigned int i = 0; i < parts; i++) {
2845
    // If the current word is non-zero, then the decrement has no effect on the
2846
    // higher-order words of the integer and no borrow can occur. Exit early.
2847
0
    if (dst[i]--)
2848
0
      return 0;
2849
0
  }
2850
  // If every word was zero, then there is a borrow.
2851
0
  return 1;
2852
0
}
2853
2854
2855
/* Set the least significant BITS bits of a bignum, clear the
2856
   rest.  */
2857
void
2858
APInt::tcSetLeastSignificantBits(integerPart *dst, unsigned int parts,
2859
                                 unsigned int bits)
2860
0
{
2861
0
  unsigned int i;
2862
2863
0
  i = 0;
2864
0
  while (bits > integerPartWidth) {
2865
0
    dst[i++] = ~(integerPart) 0;
2866
0
    bits -= integerPartWidth;
2867
0
  }
2868
2869
0
  if (bits)
2870
0
    dst[i++] = ~(integerPart) 0 >> (integerPartWidth - bits);
2871
2872
0
  while (i < parts)
2873
0
    dst[i++] = 0;
2874
0
}