- // the input value to the bswap. Some observations: 1) if more than one byte
- // is demanded from this input, then it could not be successfully assembled
- // into a byteswap. At least one of the two bytes would not be aligned with
- // their ultimate destination.
- if (!isPowerOf2_32(ByteMask)) return true;
- unsigned InputByteNo = countTrailingZeros(ByteMask);
-
- // 2) The input and ultimate destinations must line up: if byte 3 of an i32
- // is demanded, it needs to go into byte 0 of the result. This means that the
- // byte needs to be shifted until it lands in the right byte bucket. The
- // shift amount depends on the position: if the byte is coming from the high
- // part of the value (e.g. byte 3) then it must be shifted right. If from the
- // low part, it must be shifted left.
- unsigned DestByteNo = InputByteNo + OverallLeftShift;
- if (ByteValues.size()-1-DestByteNo != InputByteNo)
+ // the input value to the bswap/bitreverse. To be part of a bswap or
+ // bitreverse we must be demanding a contiguous range of bits from it.
+ unsigned InputBitLen = BitMask.countPopulation();
+ unsigned InputBitNo = BitMask.countTrailingZeros();
+ if (BitMask.getBitWidth() - BitMask.countLeadingZeros() - InputBitNo !=
+ InputBitLen)
+ // Not a contiguous set range of bits!