CopyBlock<kBlockSize>(dst, src); // Copy first block
// Copy aligned blocks
- size_t offset = kBlockSize - offset_from_last_aligned<kBlockSize>(dst);
- for (; offset + kBlockSize < count; offset += kBlockSize)
- CopyBlock<kBlockSize>(dst + offset, src + offset);
+ const size_t ofla = offset_from_last_aligned<kBlockSize>(dst);
+ const size_t limit = count + ofla - kBlockSize;
+ for (size_t offset = kBlockSize; offset < limit; offset += kBlockSize)
+ CopyBlock<kBlockSize>(dst - ofla + offset, src - ofla + offset);
CopyLastBlock<kBlockSize>(dst, src, count); // Copy last block
}
TEST(MemcpyUtilsTest, CopyAlignedBlocks) {
auto &trace = GetTrace();
- // Destination is aligned already.
+ // Destination is aligned and multiple of alignment.
+ // "1111"
+ trace.Clear();
+ CopyAlignedBlocks<4>(I(0), I(0), 4);
+ EXPECT_STREQ(trace.Write(), "2222");
+ EXPECT_STREQ(trace.Read(), "2222");
+
+ // Destination is aligned and multiple of alignment.
+ // "11110000"
+ // + "00001111"
+ // = "11111111"
+ trace.Clear();
+ CopyAlignedBlocks<4>(I(0), I(0), 8);
+ EXPECT_STREQ(trace.Write(), "11111111");
+ EXPECT_STREQ(trace.Read(), "11111111");
+
+ // Destination is aligned already overlap at end.
// "1111000000000"
// + "0000111100000"
// + "0000000011110"
EXPECT_STREQ(trace.Write(), "1111111112221");
EXPECT_STREQ(trace.Read(), "1111111112221");
- // Misaligned destination
+ // Misaligned destination.
// "01111000000000"
// + "00001111000000"
// + "00000000111100"
CopyAlignedBlocks<4>(I(1), I(0), 13);
EXPECT_STREQ(trace.Write(), "01112111112211");
EXPECT_STREQ(trace.Read(), "1112111112211");
+
+ // Misaligned destination aligned at end.
+ // "011110000000"
+ // + "000011110000"
+ // + "000000001111"
+ // = "011121111111"
+ trace.Clear();
+ CopyAlignedBlocks<4>(I(1), I(0), 11);
+ EXPECT_STREQ(trace.Write(), "011121111111");
+ EXPECT_STREQ(trace.Read(), "11121111111");
}
TEST(MemcpyUtilsTest, MaxReloads) {