llvm.org GIT mirror llvm / 154c0ad
Revert "[BitVector] Add operator<<= and operator>>=." This is causing test failures on Linux / BSD systems. Reverting while I investigate. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@300852 91177308-0d34-0410-b5e6-96231b3b80d8 Zachary Turner 2 years ago
3 changed file(s) with 0 addition(s) and 287 deletion(s). Raw diff Collapse all Expand all
1313 #ifndef LLVM_ADT_BITVECTOR_H
1414 #define LLVM_ADT_BITVECTOR_H
1515
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/STLExtras.h"
1816 #include "llvm/Support/MathExtras.h"
1917 #include
2018 #include
453451 resize(RHS.size());
454452 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
455453 Bits[i] ^= RHS.Bits[i];
456 return *this;
457 }
458
459 BitVector &operator>>=(unsigned N) {
460 assert(N <= Size);
461 if (LLVM_UNLIKELY(empty() || N == 0))
462 return *this;
463
464 unsigned NumWords = NumBitWords(Size);
465 assert(NumWords >= 1);
466
467 wordShr(N / BITWORD_SIZE);
468
469 unsigned BitDistance = N % BITWORD_SIZE;
470 if (BitDistance == 0)
471 return *this;
472
473 // When the shift size is not a multiple of the word size, then we have
474 // a tricky situation where each word in succession needs to extract some
475 // of the bits from the next word and or them into this word while
476 // shifting this word to make room for the new bits. This has to be done
477 // for every word in the array.
478
479 // Since we're shifting each word right, some bits will fall off the end
480 // of each word to the right, and empty space will be created on the left.
481 // The final word in the array will lose bits permanently, so starting at
482 // the beginning, work forwards shifting each word to the right, and
483 // OR'ing in the bits from the end of the next word to the beginning of
484 // the current word.
485
486 // Example:
487 // Starting with {0xAABBCCDD, 0xEEFF0011, 0x22334455} and shifting right
488 // by 4 bits.
489 // Step 1: Word[0] >>= 4 ; 0x0ABBCCDD
490 // Step 2: Word[0] |= 0x10000000 ; 0x1ABBCCDD
491 // Step 3: Word[1] >>= 4 ; 0x0EEFF001
492 // Step 4: Word[1] |= 0x50000000 ; 0x5EEFF001
493 // Step 5: Word[2] >>= 4 ; 0x02334455
494 // Result: { 0x1ABBCCDD, 0x5EEFF001, 0x02334455 }
495 const unsigned Mask = maskTrailingOnes(BitDistance);
496 const unsigned LSH = BITWORD_SIZE - BitDistance;
497
498 for (unsigned I = 0; I < NumWords - 1; ++I) {
499 Bits[I] >>= BitDistance;
500 Bits[I] |= (Bits[I + 1] & Mask) << LSH;
501 }
502
503 Bits[NumWords - 1] >>= BitDistance;
504
505 return *this;
506 }
507
508 BitVector &operator<<=(unsigned N) {
509 assert(N <= Size);
510 if (LLVM_UNLIKELY(empty() || N == 0))
511 return *this;
512
513 unsigned NumWords = NumBitWords(Size);
514 assert(NumWords >= 1);
515
516 wordShl(N / BITWORD_SIZE);
517
518 unsigned BitDistance = N % BITWORD_SIZE;
519 if (BitDistance == 0)
520 return *this;
521
522 // When the shift size is not a multiple of the word size, then we have
523 // a tricky situation where each word in succession needs to extract some
524 // of the bits from the previous word and or them into this word while
525 // shifting this word to make room for the new bits. This has to be done
526 // for every word in the array. This is similar to the algorithm outlined
527 // in operator>>=, but backwards.
528
529 // Since we're shifting each word left, some bits will fall off the end
530 // of each word to the left, and empty space will be created on the right.
531 // The first word in the array will lose bits permanently, so starting at
532 // the end, work backwards shifting each word to the left, and OR'ing
533 // in the bits from the end of the next word to the beginning of the
534 // current word.
535
536 // Example:
537 // Starting with {0xAABBCCDD, 0xEEFF0011, 0x22334455} and shifting left
538 // by 4 bits.
539 // Step 1: Word[2] <<= 4 ; 0x23344550
540 // Step 2: Word[2] |= 0x0000000E ; 0x2334455E
541 // Step 3: Word[1] <<= 4 ; 0xEFF00110
542 // Step 4: Word[1] |= 0x0000000A ; 0xEFF0011A
543 // Step 5: Word[0] <<= 4 ; 0xABBCCDD0
544 // Result: { 0xABBCCDD0, 0xEFF0011A, 0x2334455E }
545 const unsigned Mask = maskLeadingOnes(BitDistance);
546 const unsigned RSH = BITWORD_SIZE - BitDistance;
547
548 for (int I = NumWords - 1; I > 0; --I) {
549 Bits[I] <<= BitDistance;
550 Bits[I] |= (Bits[I - 1] & Mask) >> RSH;
551 }
552 Bits[0] <<= BitDistance;
553 clear_unused_bits();
554
555454 return *this;
556455 }
557456
638537 }
639538
640539 private:
641 /// \brief Perform a logical left shift of \p Count words by moving everything
642 /// \p Count words to the right in memory.
643 ///
644 /// While confusing, words are stored from least significant at Bits[0] to
645 /// most significant at Bits[NumWords-1]. A logical shift left, however,
646 /// moves the current least significant bit to a higher logical index, and
647 /// fills the previous least significant bits with 0. Thus, we actually
648 /// need to move the bytes of the memory to the right, not to the left.
649 /// Example:
650 /// Words = [0xBBBBAAAA, 0xDDDDFFFF, 0x00000000, 0xDDDD0000]
651 /// represents a BitVector where 0xBBBBAAAA contain the least significant
652 /// bits. So if we want to shift the BitVector left by 2 words, we need to
653 /// turn this into 0x00000000 0x00000000 0xBBBBAAAA 0xDDDDFFFF by using a
654 /// memmove which moves right, not left.
655 void wordShl(uint32_t Count) {
656 if (Count == 0)
657 return;
658
659 uint32_t NumWords = NumBitWords(Size);
660
661 auto Src = ArrayRef(Bits, NumWords).drop_back(Count);
662 auto Dest = MutableArrayRef(Bits, NumWords).drop_front(Count);
663
664 // Since we always move Word-sized chunks of data with src and dest both
665 // aligned to a word-boundary, we don't need to worry about endianness
666 // here.
667 std::memmove(Dest.begin(), Src.begin(), Dest.size() * sizeof(BitWord));
668 std::memset(Bits, 0, Count * sizeof(BitWord));
669 clear_unused_bits();
670 }
671
672 /// \brief Perform a logical right shift of \p Count words by moving those
673 /// words to the left in memory. See wordShl for more information.
674 ///
675 void wordShr(uint32_t Count) {
676 if (Count == 0)
677 return;
678
679 uint32_t NumWords = NumBitWords(Size);
680
681 auto Src = ArrayRef(Bits, NumWords).drop_front(Count);
682 auto Dest = MutableArrayRef(Bits, NumWords).drop_back(Count);
683 assert(Dest.size() == Src.size());
684
685 std::memmove(Dest.begin(), Src.begin(), Dest.size() * sizeof(BitWord));
686 std::memset(Dest.end(), 0, Count * sizeof(BitWord));
687 }
688
689540 int next_unset_in_word(int WordIndex, BitWord Word) const {
690541 unsigned Result = WordIndex * BITWORD_SIZE + countTrailingOnes(Word);
691542 return Result < size() ? Result : -1;
507507 return *this;
508508 }
509509
510 SmallBitVector &operator<<=(unsigned N) {
511 if (isSmall())
512 setSmallBits(getSmallBits() << N);
513 else
514 getPointer()->operator<<=(N);
515 return *this;
516 }
517
518 SmallBitVector &operator>>=(unsigned N) {
519 if (isSmall())
520 setSmallBits(getSmallBits() >> N);
521 else
522 getPointer()->operator>>=(N);
523 return *this;
524 }
525
526510 // Assignment operator.
527511 const SmallBitVector &operator=(const SmallBitVector &RHS) {
528512 if (isSmall()) {
344344 EXPECT_FALSE(B.anyCommon(A));
345345 }
346346
347 typedef std::vector> RangeList;
348
349 template
350 static inline VecType createBitVector(uint32_t Size,
351 const RangeList &setRanges) {
352 VecType V;
353 V.resize(Size);
354 for (auto &R : setRanges)
355 V.set(R.first, R.second);
356 return V;
357 }
358
359 TYPED_TEST(BitVectorTest, ShiftOpsSingleWord) {
360 // Test that shift ops work when the desired shift amount is less
361 // than one word.
362
363 // 1. Case where the number of bits in the BitVector also fit into a single
364 // word.
365 TypeParam A = createBitVector(12, {{2, 4}, {8, 10}});
366 TypeParam B = A;
367
368 EXPECT_EQ(4U, A.count());
369 EXPECT_TRUE(A.test(2));
370 EXPECT_TRUE(A.test(3));
371 EXPECT_TRUE(A.test(8));
372 EXPECT_TRUE(A.test(9));
373
374 A >>= 1;
375 EXPECT_EQ(createBitVector(12, {{1, 3}, {7, 9}}), A);
376
377 A <<= 1;
378 EXPECT_EQ(B, A);
379
380 A >>= 10;
381 EXPECT_EQ(createBitVector(12, {}), A);
382
383 A = B;
384 A <<= 10;
385 EXPECT_EQ(createBitVector(12, {}), A);
386
387 // 2. Case where the number of bits in the BitVector do not fit into a single
388 // word.
389
390 // 31----------------------------------------------------------------------0
391 // XXXXXXXX XXXXXXXX XXXXXXXX 00000111 | 11111110 00000000 00001111 11111111
392 A = createBitVector(40, {{0, 12}, {25, 35}});
393 EXPECT_EQ(40U, A.size());
394 EXPECT_EQ(22U, A.count());
395
396 // 2a. Make sure that left shifting some 1 bits out of the vector works.
397 // 31----------------------------------------------------------------------0
398 // Before:
399 // XXXXXXXX XXXXXXXX XXXXXXXX 00000111 | 11111110 00000000 00001111 11111111
400 // After:
401 // XXXXXXXX XXXXXXXX XXXXXXXX 11111100 | 00000000 00011111 11111110 00000000
402 A <<= 9;
403 EXPECT_EQ(createBitVector(40, {{9, 21}, {34, 40}}), A);
404
405 // 2b. Make sure that keeping the number of one bits unchanged works.
406 // 31----------------------------------------------------------------------0
407 // Before:
408 // XXXXXXXX XXXXXXXX XXXXXXXX 11111100 | 00000000 00011111 11111110 00000000
409 // After:
410 // XXXXXXXX XXXXXXXX XXXXXXXX 00000011 | 11110000 00000000 01111111 11111000
411 A >>= 6;
412 EXPECT_EQ(createBitVector(40, {{3, 15}, {28, 34}}), A);
413
414 // 2c. Make sure that right shifting some 1 bits out of the vector works.
415 // 31----------------------------------------------------------------------0
416 // Before:
417 // XXXXXXXX XXXXXXXX XXXXXXXX 00000011 | 11110000 00000000 01111111 11111000
418 // After:
419 // XXXXXXXX XXXXXXXX XXXXXXXX 00000000 | 00000000 11111100 00000000 00011111
420 A >>= 10;
421 EXPECT_EQ(createBitVector(40, {{0, 5}, {18, 24}}), A);
422
423 // 3. Big test.
424 A = createBitVector(300, {{1, 30}, {60, 95}, {200, 275}});
425 A <<= 29;
426 EXPECT_EQ(createBitVector(
427 300, {{1 + 29, 30 + 29}, {60 + 29, 95 + 29}, {200 + 29, 300}}),
428 A);
429 }
430
431 TYPED_TEST(BitVectorTest, ShiftOpsMultiWord) {
432 // Test that shift ops work when the desired shift amount is greater than or
433 // equal to the size of a single word.
434 auto A = createBitVector(300, {{1, 30}, {60, 95}, {200, 275}});
435
436 // Make a copy so we can re-use it later.
437 auto B = A;
438
439 // 1. Shift left by an exact multiple of the word size. This should invoke
440 // only a memmove and no per-word bit operations.
441 A <<= 64;
442 auto Expected = createBitVector(
443 300, {{1 + 64, 30 + 64}, {60 + 64, 95 + 64}, {200 + 64, 300}});
444 EXPECT_EQ(Expected, A);
445
446 // 2. Shift left by a non multiple of the word size. This should invoke both
447 // a memmove and per-word bit operations.
448 A = B;
449 A <<= 93;
450 EXPECT_EQ(createBitVector(
451 300, {{1 + 93, 30 + 93}, {60 + 93, 95 + 93}, {200 + 93, 300}}),
452 A);
453
454 // 1. Shift right by an exact multiple of the word size. This should invoke
455 // only a memmove and no per-word bit operations.
456 A = B;
457 A >>= 64;
458 EXPECT_EQ(
459 createBitVector(300, {{0, 95 - 64}, {200 - 64, 275 - 64}}), A);
460
461 // 2. Shift left by a non multiple of the word size. This should invoke both
462 // a memmove and per-word bit operations.
463 A = B;
464 A >>= 93;
465 EXPECT_EQ(
466 createBitVector(300, {{0, 95 - 93}, {200 - 93, 275 - 93}}), A);
467 }
468
469347 TYPED_TEST(BitVectorTest, RangeOps) {
470348 TypeParam A;
471349 A.resize(256);