Test the logic of ByteSwapSimd control flow using specified SIMD routine with an input/output buffer. CpuFlag parameter indicates SIMD routine to be tested: CpuInfo::SSSE3 for SimdByteSwap::ByteSwapSSE_Unit; CpuInfo::AVX2 for SimdByteSwap::ByteSwapAVX2_Unit; CpuFlag == 0 for BitUtil::ByteSwap; buf_size parameter indicates the size of input/output buffer.
| 174 | // CpuFlag == 0 for BitUtil::ByteSwap; |
| 175 | // buf_size parameter indicates the size of input/output buffer. |
| 176 | void TestByteSwapSimd(const int64_t CpuFlag, const int buf_size) { |
| 177 | if (buf_size <= 0) return; |
| 178 | uint8_t src_buf[buf_size]; |
| 179 | uint8_t dst_buf[buf_size]; |
| 180 | std::iota(src_buf, src_buf + buf_size, 0); |
| 181 | |
| 182 | int start_size = 0; |
| 183 | if (IS_AARCH64 || CpuFlag == CpuInfo::SSSE3) { |
| 184 | start_size = 16; |
| 185 | } else if (CpuFlag == CpuInfo::AVX2) { |
| 186 | start_size = 32; |
| 187 | } |
| 188 | |
| 189 | for (int i = start_size; i < buf_size; ++i) { |
| 190 | // Initialize dst buffer and swap i bytes. |
| 191 | memset(dst_buf, 0, buf_size); |
| 192 | if (IS_AARCH64 || CpuFlag == CpuInfo::SSSE3) { |
| 193 | SimdByteSwap::ByteSwapSimd<16>(src_buf, i, dst_buf); |
| 194 | } else if (CpuFlag == CpuInfo::AVX2) { |
| 195 | SimdByteSwap::ByteSwapSimd<32>(src_buf, i, dst_buf); |
| 196 | } else { |
| 197 | // CpuFlag == 0: test the internal logic of BitUtil::ByteSwap |
| 198 | ASSERT_EQ(CpuFlag, 0); |
| 199 | BitUtil::ByteSwap(dst_buf, src_buf, i); |
| 200 | } |
| 201 | |
| 202 | // Validate the swap results. |
| 203 | for (int j = 0; j < i; ++j) { |
| 204 | EXPECT_EQ(dst_buf[j], i - j - 1); |
| 205 | EXPECT_EQ(dst_buf[j], src_buf[i - j - 1]); |
| 206 | } |
| 207 | // Check that the dst buffer is otherwise unmodified. |
| 208 | for (int j = i; j < buf_size; ++j) { |
| 209 | EXPECT_EQ(dst_buf[j], 0); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | TEST(BitUtil, ByteSwap) { |
| 215 | EXPECT_EQ(BitUtil::ByteSwap(static_cast<uint32_t>(0)), 0); |