Move limex specific shuffle utils and ssse3 funcs

This commit is contained in:
Matthew Barr
2016-06-06 11:54:21 +10:00
parent 9f98f4c7b2
commit 4d6934fc77
22 changed files with 182 additions and 371 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Intel Corporation
* Copyright (c) 2015-2016, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -31,8 +31,7 @@
#include "gtest/gtest.h"
#include "util/simd_utils.h"
#include "util/shuffle.h"
#include "util/shuffle_ssse3.h"
#include "nfa/limex_shuffle.h"
namespace {
@@ -50,34 +49,34 @@ Mask setbit(unsigned int bit) {
return cf.simd;
}
TEST(Shuffle, ShuffleDynamic32_1) {
TEST(Shuffle, PackedExtract32_1) {
// Try all possible one-bit masks
for (unsigned int i = 0; i < 32; i++) {
// shuffle a single 1 bit to the front
u32 mask = 1U << i;
EXPECT_EQ(1U, shuffleDynamic32(mask, mask));
EXPECT_EQ(1U, shuffleDynamic32(~0U, mask));
EXPECT_EQ(1U, packedExtract32(mask, mask));
EXPECT_EQ(1U, packedExtract32(~0U, mask));
// we should get zero out of these cases
EXPECT_EQ(0U, shuffleDynamic32(0, mask));
EXPECT_EQ(0U, shuffleDynamic32(~mask, mask));
EXPECT_EQ(0U, packedExtract32(0, mask));
EXPECT_EQ(0U, packedExtract32(~mask, mask));
// we should get zero out of all the other bit positions
for (unsigned int j = 0; (j != i && j < 32); j++) {
EXPECT_EQ(0U, shuffleDynamic32((1U << j), mask));
EXPECT_EQ(0U, packedExtract32((1U << j), mask));
}
}
}
TEST(Shuffle, ShuffleDynamic32_2) {
TEST(Shuffle, PackedExtract32_2) {
// All 32 bits in mask are on
u32 mask = ~0U;
EXPECT_EQ(0U, shuffleDynamic32(0, mask));
EXPECT_EQ(mask, shuffleDynamic32(mask, mask));
EXPECT_EQ(0U, packedExtract32(0, mask));
EXPECT_EQ(mask, packedExtract32(mask, mask));
for (unsigned int i = 0; i < 32; i++) {
EXPECT_EQ(1U << i, shuffleDynamic32(1U << i, mask));
EXPECT_EQ(1U << i, packedExtract32(1U << i, mask));
}
}
TEST(Shuffle, ShuffleDynamic32_3) {
TEST(Shuffle, PackedExtract32_3) {
// Try setting every second bit
u32 mask = 0;
for (unsigned int i = 0; i < 32; i += 2) {
@@ -85,63 +84,63 @@ TEST(Shuffle, ShuffleDynamic32_3) {
}
// Test both cases (all even bits, all odd bits)
EXPECT_EQ((1U << 16) - 1, shuffleDynamic32(mask, mask));
EXPECT_EQ((1U << 16) - 1, shuffleDynamic32(~mask, ~mask));
EXPECT_EQ(0U, shuffleDynamic32(~mask, mask));
EXPECT_EQ(0U, shuffleDynamic32(mask, ~mask));
EXPECT_EQ((1U << 16) - 1, packedExtract32(mask, mask));
EXPECT_EQ((1U << 16) - 1, packedExtract32(~mask, ~mask));
EXPECT_EQ(0U, packedExtract32(~mask, mask));
EXPECT_EQ(0U, packedExtract32(mask, ~mask));
for (unsigned int i = 0; i < 32; i += 2) {
EXPECT_EQ(1U << (i/2), shuffleDynamic32(1U << i, mask));
EXPECT_EQ(0U, shuffleDynamic32(1U << i, ~mask));
EXPECT_EQ(1U << (i/2), shuffleDynamic32(1U << (i+1), ~mask));
EXPECT_EQ(0U, shuffleDynamic32(1U << (i+1), mask));
EXPECT_EQ(1U << (i/2), packedExtract32(1U << i, mask));
EXPECT_EQ(0U, packedExtract32(1U << i, ~mask));
EXPECT_EQ(1U << (i/2), packedExtract32(1U << (i+1), ~mask));
EXPECT_EQ(0U, packedExtract32(1U << (i+1), mask));
}
}
TEST(Shuffle, ShuffleDynamic64_1) {
TEST(Shuffle, PackedExtract64_1) {
// Try all possible one-bit masks
for (unsigned int i = 0; i < 64; i++) {
// shuffle a single 1 bit to the front
u64a mask = 1ULL << i;
EXPECT_EQ(1U, shuffleDynamic64(mask, mask));
EXPECT_EQ(1U, shuffleDynamic64(~0ULL, mask));
EXPECT_EQ(1U, packedExtract64(mask, mask));
EXPECT_EQ(1U, packedExtract64(~0ULL, mask));
// we should get zero out of these cases
EXPECT_EQ(0U, shuffleDynamic64(0, mask));
EXPECT_EQ(0U, shuffleDynamic64(~mask, mask));
EXPECT_EQ(0U, packedExtract64(0, mask));
EXPECT_EQ(0U, packedExtract64(~mask, mask));
// we should get zero out of all the other bit positions
for (unsigned int j = 0; (j != i && j < 64); j++) {
EXPECT_EQ(0U, shuffleDynamic64((1ULL << j), mask));
EXPECT_EQ(0U, packedExtract64((1ULL << j), mask));
}
}
}
TEST(Shuffle, ShuffleDynamic64_2) {
TEST(Shuffle, PackedExtract64_2) {
// Fill first half of mask
u64a mask = 0x00000000ffffffffULL;
EXPECT_EQ(0U, shuffleDynamic64(0, mask));
EXPECT_EQ(0xffffffffU, shuffleDynamic64(mask, mask));
EXPECT_EQ(0U, packedExtract64(0, mask));
EXPECT_EQ(0xffffffffU, packedExtract64(mask, mask));
for (unsigned int i = 0; i < 32; i++) {
EXPECT_EQ(1U << i, shuffleDynamic64(1ULL << i, mask));
EXPECT_EQ(1U << i, packedExtract64(1ULL << i, mask));
}
// Fill second half of mask
mask = 0xffffffff00000000ULL;
EXPECT_EQ(0U, shuffleDynamic64(0, mask));
EXPECT_EQ(0xffffffffU, shuffleDynamic64(mask, mask));
EXPECT_EQ(0U, packedExtract64(0, mask));
EXPECT_EQ(0xffffffffU, packedExtract64(mask, mask));
for (unsigned int i = 32; i < 64; i++) {
EXPECT_EQ(1U << (i - 32), shuffleDynamic64(1ULL << i, mask));
EXPECT_EQ(1U << (i - 32), packedExtract64(1ULL << i, mask));
}
// Try one in the middle
mask = 0x0000ffffffff0000ULL;
EXPECT_EQ(0U, shuffleDynamic64(0, mask));
EXPECT_EQ(0xffffffffU, shuffleDynamic64(mask, mask));
EXPECT_EQ(0U, packedExtract64(0, mask));
EXPECT_EQ(0xffffffffU, packedExtract64(mask, mask));
for (unsigned int i = 16; i < 48; i++) {
EXPECT_EQ(1U << (i - 16), shuffleDynamic64(1ULL << i, mask));
EXPECT_EQ(1U << (i - 16), packedExtract64(1ULL << i, mask));
}
}
TEST(Shuffle, ShuffleDynamic64_3) {
TEST(Shuffle, PackedExtract64_3) {
// Try setting every second bit (note: 32 bits, the max we can shuffle)
u64a mask = 0;
for (unsigned int i = 0; i < 64; i += 2) {
@@ -149,46 +148,69 @@ TEST(Shuffle, ShuffleDynamic64_3) {
}
// Test both cases (all even bits, all odd bits)
EXPECT_EQ(0xffffffffU, shuffleDynamic64(mask, mask));
EXPECT_EQ(0xffffffffU, shuffleDynamic64(~mask, ~mask));
EXPECT_EQ(0U, shuffleDynamic64(~mask, mask));
EXPECT_EQ(0U, shuffleDynamic64(mask, ~mask));
EXPECT_EQ(0xffffffffU, packedExtract64(mask, mask));
EXPECT_EQ(0xffffffffU, packedExtract64(~mask, ~mask));
EXPECT_EQ(0U, packedExtract64(~mask, mask));
EXPECT_EQ(0U, packedExtract64(mask, ~mask));
for (unsigned int i = 0; i < 64; i += 2) {
EXPECT_EQ(1U << (i/2), shuffleDynamic64(1ULL << i, mask));
EXPECT_EQ(0U, shuffleDynamic64(1ULL << i, ~mask));
EXPECT_EQ(1U << (i/2), shuffleDynamic64(1ULL << (i+1), ~mask));
EXPECT_EQ(0U, shuffleDynamic64(1ULL << (i+1), mask));
EXPECT_EQ(1U << (i/2), packedExtract64(1ULL << i, mask));
EXPECT_EQ(0U, packedExtract64(1ULL << i, ~mask));
EXPECT_EQ(1U << (i/2), packedExtract64(1ULL << (i+1), ~mask));
EXPECT_EQ(0U, packedExtract64(1ULL << (i+1), mask));
}
}
template<typename T>
static
void build_pshufb_masks_onebit(unsigned int bit, m128 *permute, m128 *compare) {
void build_pshufb_masks_onebit(unsigned int bit, T *permute, T *compare) {
static_assert(sizeof(T) == sizeof(m128) || sizeof(T) == sizeof(m256),
"should be valid type");
// permute mask has 0x80 in all bytes except the one we care about
memset(permute, 0x80, sizeof(*permute));
memset(compare, 0, sizeof(*compare));
char *pmsk = (char *)permute;
char *cmsk = (char *)compare;
pmsk[0] = bit/8;
cmsk[0] = ~(1 << (bit % 8));
u8 off = (bit >= 128) ? 0x10 : 0;
pmsk[off] = bit/8;
cmsk[off] = ~(1 << (bit % 8));
}
TEST(Shuffle, ShufflePshufb128_1) {
TEST(Shuffle, PackedExtract128_1) {
// Try all possible one-bit masks
for (unsigned int i = 0; i < 128; i++) {
// shuffle a single 1 bit to the front
m128 permute, compare;
build_pshufb_masks_onebit(i, &permute, &compare);
EXPECT_EQ(1U, shufflePshufb128(setbit<m128>(i), permute, compare));
EXPECT_EQ(1U, shufflePshufb128(ones128(), permute, compare));
EXPECT_EQ(1U, packedExtract128(setbit<m128>(i), permute, compare));
EXPECT_EQ(1U, packedExtract128(ones128(), permute, compare));
// we should get zero out of these cases
EXPECT_EQ(0U, shufflePshufb128(zeroes128(), permute, compare));
EXPECT_EQ(0U, shufflePshufb128(not128(setbit<m128>(i)), permute, compare));
EXPECT_EQ(0U, packedExtract128(zeroes128(), permute, compare));
EXPECT_EQ(0U, packedExtract128(not128(setbit<m128>(i)), permute, compare));
// we should get zero out of all the other bit positions
for (unsigned int j = 0; (j != i && j < 128); j++) {
EXPECT_EQ(0U, shufflePshufb128(setbit<m128>(j), permute, compare));
EXPECT_EQ(0U, packedExtract128(setbit<m128>(j), permute, compare));
}
}
}
#if defined(__AVX2__)
TEST(Shuffle, PackedExtract256_1) {
// Try all possible one-bit masks
for (unsigned int i = 0; i < 256; i++) {
// shuffle a single 1 bit to the front
m256 permute, compare;
build_pshufb_masks_onebit(i, &permute, &compare);
EXPECT_EQ(1U, packedExtract256(setbit<m256>(i), permute, compare));
EXPECT_EQ(1U, packedExtract256(ones256(), permute, compare));
// we should get zero out of these cases
EXPECT_EQ(0U, packedExtract256(zeroes256(), permute, compare));
EXPECT_EQ(0U, packedExtract256(not256(setbit<m256>(i)), permute, compare));
// we should get zero out of all the other bit positions
for (unsigned int j = 0; (j != i && j < 256); j++) {
EXPECT_EQ(0U, packedExtract256(setbit<m256>(j), permute, compare));
}
}
}
#endif
} // namespace

View File

@@ -32,7 +32,6 @@
#include "util/alloc.h"
#include "util/make_unique.h"
#include "util/simd_utils.h"
#include "util/simd_utils_ssse3.h"
using namespace std;
using namespace ue2;