-#ifdef __SSE2__
- /* SSE2 vectorized implementation for x86_64. This speeds up LZX
- * decompression by about 5-8% overall. (Usually --- the performance
- * actually regresses slightly in the degenerate case that the data
- * consists entirely of 0xe8 bytes. Also, this optimization affects
- * compression as well, but the percentage improvement is less because
- * LZX compression is much slower than LZX decompression. ) */
- if (size >= 32 && (uintptr_t)p % 16 == 0) {
-
- u32 valid_mask = 0xFFFFFFFF;
-
- u8 * const vec_end = p + (size & ~15) - 16;
-
- /* Create a vector of all 0xe8 bytes */
- const __m128i e8_bytes = _mm_set1_epi8(0xe8);
-
- /* Iterate through the 16-byte vectors in the input. */
- do {
- /* Compare the current 16-byte vector with the vector of
- * all 0xe8 bytes. This produces 0xff where the byte is
- * 0xe8 and 0x00 where it is not. */
- __m128i cmpresult = _mm_cmpeq_epi8(*(const __m128i *)p,
- e8_bytes);
-
- /* Map the comparison results into a single 16-bit
- * number. It will contain a 1 bit when the
- * corresponding byte in the current 16-byte vector is
- * an e8 byte. Note: the low-order bit corresponds to
- * the first (lowest address) byte. */
- u32 e8_mask = _mm_movemask_epi8(cmpresult);
-
- if (!e8_mask) {
- /* If e8_mask is 0, then none of these 16 bytes
- * have value 0xe8. No e8 translation is
- * needed, and there is no restriction that
- * carries over to the next 16 bytes. */
- valid_mask = 0xFFFFFFFF;
- } else {
- /* At least one byte has value 0xe8.
- *
- * The AND with valid_mask accounts for the fact
- * that we can't start an e8 translation that
- * overlaps the previous one. */
- while ((e8_mask &= valid_mask)) {
-
- /* Count the number of trailing zeroes
- * in e8_mask. This will produce the
- * index of the byte, within the 16, at
- * which the next e8 translation should
- * be done. */
- int bit = ffs32(e8_mask);
-
- /* Do (or undo) the e8 translation. */
- (*process_target)(p + bit + 1,
- p + bit - data);
-
- /* Don't start an e8 translation in the
- * next 4 bytes. */
- valid_mask &= ~((u32)0x1F << bit);
+ u64 valid_mask = ~0;
+
+ if (size <= 10)
+ return;
+#ifdef __AVX2__
+# define ALIGNMENT_REQUIRED 32
+#else
+# define ALIGNMENT_REQUIRED 16
+#endif
+
+ /* Process one byte at a time until the pointer is properly aligned. */
+ while ((uintptr_t)p % ALIGNMENT_REQUIRED != 0) {
+ if (p >= data + size - 10)
+ return;
+ if (*p == 0xE8 && (valid_mask & 1)) {
+ (*process_target)(p + 1, p - data);
+ valid_mask &= ~0x1F;
+ }
+ p++;
+ valid_mask >>= 1;
+ valid_mask |= (u64)1 << 63;
+ }
+
+ if (data + size - p >= 64) {
+
+ /* Vectorized processing */
+
+ /* Note: we use a "trap" E8 byte to eliminate the need to check
+ * for end-of-buffer in the inner loop. This byte is carefully
+ * positioned so that it will never be changed by a previous
+ * translation before it is detected. */
+
+ u8 *trap = p + ((data + size - p) & ~31) - 32 + 4;
+ u8 saved_byte = *trap;
+ *trap = 0xE8;
+
+ for (;;) {
+ u32 e8_mask;
+ u8 *orig_p = p;
+ #ifdef __SSE2__
+ const __m128i e8_bytes = _mm_set1_epi8(0xE8);
+ for (;;) {
+ /* Read the next 32 bytes of data and test them
+ * for E8 bytes. */
+ __m128i bytes1 = *(const __m128i *)p;
+ __m128i bytes2 = *(const __m128i *)(p + 16);
+ __m128i cmpresult1 = _mm_cmpeq_epi8(bytes1, e8_bytes);
+ __m128i cmpresult2 = _mm_cmpeq_epi8(bytes2, e8_bytes);
+ u32 mask1 = _mm_movemask_epi8(cmpresult1);
+ u32 mask2 = _mm_movemask_epi8(cmpresult2);
+ /* The masks have a bit set for each E8 byte.
+ * We stay in this fast inner loop as long as
+ * there are no E8 bytes. */
+ if (mask1 | mask2) {
+ e8_mask = mask1 | (mask2 << 16);
+ break;