Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/nfa/accel.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,12 @@ const u8 *run_accel(const union AccelAux *accel, const u8 *c, const u8 *c_end) {
return c;
}

/* need to stop one early to get an accurate end state */
/* Shufti double handles its own ending boundary checks internally
* to correctly identify matches at the last byte. */
rv = shuftiDoubleExec(accel->dshufti.lo1,
accel->dshufti.hi1,
accel->dshufti.lo2,
accel->dshufti.hi2, c, c_end - 1);
accel->dshufti.hi2, c, c_end);
break;

case ACCEL_RED_TAPE:
Expand Down
3 changes: 1 addition & 2 deletions src/nfa/shufti_simd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ const u8 *check_last_byte(SuperVector<S> mask2_lo, SuperVector<S> mask2_hi,
uint8_t last_elem = mask.u.u8[mask_len - 1];

SuperVector<S> reduce = mask2_lo | mask2_hi;
for(uint16_t i = S; i > 2; i/=2) {
for(uint16_t i = S; i >= 2; i/=2) {
reduce = reduce | reduce.vshr(i/2);
}
uint8_t match_inverted = reduce.u.u8[0] | last_elem;
Expand Down Expand Up @@ -260,7 +260,6 @@ const u8 *shuftiDoubleExecReal(m128 mask1_lo, m128 mask1_hi, m128 mask2_lo, m128
first_char_mask.print8("inout_c1 shifted");
}

first_char_mask = SuperVector<S>::Ones();
while(d + S <= buf_end) {
__builtin_prefetch(d + 64);
DEBUG_PRINTF("d %p \n", d);
Expand Down
88 changes: 88 additions & 0 deletions unit/internal/shufti.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,94 @@ TEST(DoubleShufti, ExecNoOverreadPageBoundary) {
munmap(pages, page_size);
}

TEST(DoubleShufti, ExecMatchVectorEdge) {
m128 lo1, hi1, lo2, hi2;

flat_set<pair<u8, u8>> lits;
lits.insert(make_pair('a', 'b'));

bool ret = shuftiBuildDoubleMasks(CharReach(), lits, reinterpret_cast<u8 *>(&lo1), reinterpret_cast<u8 *>(&hi1),
reinterpret_cast<u8 *>(&lo2), reinterpret_cast<u8 *>(&hi2));
ASSERT_TRUE(ret);

const int len = 80;
const int vector_size = 16;
for (size_t start = 1; start < vector_size; start++) {
char t[len];
memset(t, 'z', len);
char *buf = t + start;
int buf_len = len - start;

uintptr_t aligned = (reinterpret_cast<uintptr_t>(buf) + vector_size - 1) &
~static_cast<uintptr_t>(vector_size - 1);
ptrdiff_t boundary = reinterpret_cast<const char *>(aligned) - buf;

if (boundary < 1 || boundary >= buf_len - 1) continue;

buf[boundary - 1] = 'a';
buf[boundary] = 'b';

const u8 *rv = shuftiDoubleExec(lo1, hi1, lo2, hi2,
reinterpret_cast<u8 *>(buf),
reinterpret_cast<u8 *>(buf) + buf_len);

ASSERT_EQ(reinterpret_cast<const u8 *>(buf + boundary - 1), rv)
<< "Failed for start=" << start << " boundary=" << boundary;
}
}

TEST(DoubleShufti, ExecNoMatchLastByte) {
m128 lo1, hi1, lo2, hi2;

flat_set<pair<u8, u8>> lits;
lits.insert(make_pair('x', 'y'));

bool ret = shuftiBuildDoubleMasks(CharReach(), lits, reinterpret_cast<u8 *>(&lo1), reinterpret_cast<u8 *>(&hi1),
reinterpret_cast<u8 *>(&lo2), reinterpret_cast<u8 *>(&hi2));
ASSERT_TRUE(ret);

const int maxlen = 80;
char t1[maxlen + 1];
for (int len = 17; len < maxlen; len++) {
memset(t1, 'b', len + 1);
t1[len - 1] = 'x';

const u8 *rv = shuftiDoubleExec(lo1, hi1, lo2, hi2,
reinterpret_cast<u8 *>(t1),
reinterpret_cast<u8 *>(t1) + len);

ASSERT_EQ(reinterpret_cast<const u8 *>(t1 + len), rv)
<< "Failed for len=" << len;
}
}

TEST(DoubleShufti, ExecMatchLastByte) {
m128 lo1, hi1, lo2, hi2;

CharReach onebyte;
flat_set<pair<u8, u8>> twobyte;

onebyte.set('a');
twobyte.insert(make_pair('x', 'y'));

bool ret = shuftiBuildDoubleMasks(onebyte, twobyte, reinterpret_cast<u8 *>(&lo1), reinterpret_cast<u8 *>(&hi1),
reinterpret_cast<u8 *>(&lo2), reinterpret_cast<u8 *>(&hi2));
ASSERT_TRUE(ret);

const int maxlen = 80;
char t1[maxlen + 1];
for (int len = 17; len < maxlen; len++) {
memset(t1, 'b', len + 1);
t1[len - 1] = 'a';

const u8 *rv = shuftiDoubleExec(lo1, hi1, lo2, hi2,
reinterpret_cast<u8 *>(t1),
reinterpret_cast<u8 *>(t1) + len);

ASSERT_EQ(reinterpret_cast<const u8 *>(t1 + len - 1), rv);
}
}

TEST(ReverseShufti, ExecNoMatch1) {
m128 lo, hi;

Expand Down