Commit 4f81c873 authored by miloyip's avatar miloyip

Optimize SkipWhitespace_SIMD()

Do unaligned non-SIMD matching first, and then SIMD matching later.
Also add a fast path for no skipping.
parent fdc2b569
...@@ -242,57 +242,34 @@ void SkipWhitespace(InputStream& is) { ...@@ -242,57 +242,34 @@ void SkipWhitespace(InputStream& is) {
#ifdef RAPIDJSON_SSE42 #ifdef RAPIDJSON_SSE42
//! Skip whitespace with SSE 4.2 pcmpistrm instruction, testing 16 8-byte characters at once. //! Skip whitespace with SSE 4.2 pcmpistrm instruction, testing 16 8-byte characters at once.
inline const char *SkipWhitespace_SIMD(const char* p) { inline const char *SkipWhitespace_SIMD(const char* p) {
static const char whitespace[16] = " \n\r\t"; // Fast return for single non-whitespace
static const char whitespaces[4][17] = { if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')
" ", ++p;
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", else
"\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r", return p;
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"};
// 16-byte align to the next boundary
// 16-byte align to the lower boundary const char* nextAligned = reinterpret_cast<const char*>((reinterpret_cast<size_t>(p) + 15) & ~15);
const char* ap = reinterpret_cast<const char*>(reinterpret_cast<size_t>(p) & ~15); while (p != nextAligned)
if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')
// Test first unaligned characters ++p;
// Cannot make use of _mm_cmpistrm() because it stops when encounters '\0' before p else
if (ap != p) { return p;
const __m128i w0 = _mm_loadu_si128((const __m128i *)&whitespaces[0][0]);
const __m128i w1 = _mm_loadu_si128((const __m128i *)&whitespaces[1][0]); // The rest of string using SIMD
const __m128i w2 = _mm_loadu_si128((const __m128i *)&whitespaces[2][0]); static const char whitespace[16] = " \n\r\t";
const __m128i w3 = _mm_loadu_si128((const __m128i *)&whitespaces[3][0]); const __m128i w = _mm_loadu_si128((const __m128i *)&whitespace[0]);
unsigned char shift = reinterpret_cast<size_t>(p) & 15; for (;; p += 16) {
const __m128i s = _mm_load_si128(reinterpret_cast<const __m128i*>(ap)); const __m128i s = _mm_load_si128((const __m128i *)p);
__m128i x = _mm_cmpeq_epi8(s, w0);
x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w1));
x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w2));
x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w3));
unsigned short r = (unsigned short)~_mm_movemask_epi8(x);
r = r >> shift << shift; // Clear results before p
if (r != 0) {
#ifdef _MSC_VER // Find the index of first non-whitespace
unsigned long offset;
_BitScanForward(&offset, r);
return ap + offset;
#else
return ap + __builtin_ffs(r) - 1;
#endif
}
ap += 16;
}
const __m128i w = _mm_loadu_si128((const __m128i *)&whitespace[0]);
// The rest of string
for (;; ap += 16) {
const __m128i s = _mm_load_si128((const __m128i *)ap);
const unsigned r = _mm_cvtsi128_si32(_mm_cmpistrm(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK | _SIDD_NEGATIVE_POLARITY)); const unsigned r = _mm_cvtsi128_si32(_mm_cmpistrm(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK | _SIDD_NEGATIVE_POLARITY));
if (r != 0) { // some of characters is non-whitespace if (r != 0) { // some of characters is non-whitespace
#ifdef _MSC_VER // Find the index of first non-whitespace #ifdef _MSC_VER // Find the index of first non-whitespace
unsigned long offset; unsigned long offset;
_BitScanForward(&offset, r); _BitScanForward(&offset, r);
return ap + offset; return p + offset;
#else #else
return ap + __builtin_ffs(r) - 1; return p + __builtin_ffs(r) - 1;
#endif #endif
} }
} }
...@@ -302,45 +279,34 @@ inline const char *SkipWhitespace_SIMD(const char* p) { ...@@ -302,45 +279,34 @@ inline const char *SkipWhitespace_SIMD(const char* p) {
//! Skip whitespace with SSE2 instructions, testing 16 8-byte characters at once. //! Skip whitespace with SSE2 instructions, testing 16 8-byte characters at once.
inline const char *SkipWhitespace_SIMD(const char* p) { inline const char *SkipWhitespace_SIMD(const char* p) {
static const char whitespaces[4][17] = { // Fast return for single non-whitespace
" ", if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", ++p;
"\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r", else
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"}; return p;
const __m128i w0 = _mm_loadu_si128((const __m128i *)&whitespaces[0][0]); // 16-byte align to the next boundary
const __m128i w1 = _mm_loadu_si128((const __m128i *)&whitespaces[1][0]); const char* nextAligned = reinterpret_cast<const char*>((reinterpret_cast<size_t>(p) + 15) & ~15);
const __m128i w2 = _mm_loadu_si128((const __m128i *)&whitespaces[2][0]); while (p != nextAligned)
const __m128i w3 = _mm_loadu_si128((const __m128i *)&whitespaces[3][0]); if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')
++p;
// 16-byte align to the lower boundary else
const char* ap = reinterpret_cast<const char*>(reinterpret_cast<size_t>(p) & ~15); return p;
// Test first unaligned characters
if (ap != p) {
unsigned char shift = reinterpret_cast<size_t>(p) & 15;
const __m128i s = _mm_load_si128(reinterpret_cast<const __m128i*>(ap));
__m128i x = _mm_cmpeq_epi8(s, w0);
x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w1));
x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w2));
x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w3));
unsigned short r = (unsigned short)~_mm_movemask_epi8(x);
r = r >> shift << shift; // Clear results before p
if (r != 0) {
#ifdef _MSC_VER // Find the index of first non-whitespace
unsigned long offset;
_BitScanForward(&offset, r);
return ap + offset;
#else
return ap + __builtin_ffs(r) - 1;
#endif
}
ap += 16;
}
// The rest of string // The rest of string
for (;; ap += 16) { static const char whitespaces[4][17] = {
const __m128i s = _mm_load_si128((const __m128i *)ap); " ",
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
"\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r",
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"};
const __m128i w0 = _mm_loadu_si128((const __m128i *)&whitespaces[0][0]);
const __m128i w1 = _mm_loadu_si128((const __m128i *)&whitespaces[1][0]);
const __m128i w2 = _mm_loadu_si128((const __m128i *)&whitespaces[2][0]);
const __m128i w3 = _mm_loadu_si128((const __m128i *)&whitespaces[3][0]);
for (;; p += 16) {
const __m128i s = _mm_load_si128((const __m128i *)p);
__m128i x = _mm_cmpeq_epi8(s, w0); __m128i x = _mm_cmpeq_epi8(s, w0);
x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w1)); x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w1));
x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w2)); x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w2));
...@@ -350,9 +316,9 @@ inline const char *SkipWhitespace_SIMD(const char* p) { ...@@ -350,9 +316,9 @@ inline const char *SkipWhitespace_SIMD(const char* p) {
#ifdef _MSC_VER // Find the index of first non-whitespace #ifdef _MSC_VER // Find the index of first non-whitespace
unsigned long offset; unsigned long offset;
_BitScanForward(&offset, r); _BitScanForward(&offset, r);
return ap + offset; return p + offset;
#else #else
return ap + __builtin_ffs(r) - 1; return p + __builtin_ffs(r) - 1;
#endif #endif
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment