Skip to content

Commit b189aaa

Browse files
committed
Tweaks for accelerated implementation of mb_strlen for UTF-8
On longer strings, this gives a small speed boost of 10% or less.
1 parent 3ae4779 commit b189aaa

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

ext/mbstring/mbstring.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,9 +1748,11 @@ static size_t mb_fast_strlen_utf8(unsigned char *p, size_t len)
17481748

17491749
#ifdef __SSE2__
17501750
if (len >= sizeof(__m128i)) {
1751+
e -= sizeof(__m128i);
1752+
17511753
const __m128i threshold = _mm_set1_epi8(-64);
17521754
const __m128i delta = _mm_set1_epi8(1);
1753-
__m128i counter = _mm_set1_epi8(0); /* Vector of 16 continuation-byte counters */
1755+
__m128i counter = _mm_setzero_si128(); /* Vector of 16 continuation-byte counters */
17541756

17551757
int reset_counter = 255;
17561758
do {
@@ -1762,13 +1764,14 @@ static size_t mb_fast_strlen_utf8(unsigned char *p, size_t len)
17621764
* and reset them to zero */
17631765
if (--reset_counter == 0) {
17641766
len -= _mm_sum_epu8(counter);
1765-
counter = _mm_set1_epi8(0);
1767+
counter = _mm_setzero_si128();
17661768
reset_counter = 255;
17671769
}
17681770

17691771
p += sizeof(__m128i);
1770-
} while (p + sizeof(__m128i) <= e);
1772+
} while (p <= e);
17711773

1774+
e += sizeof(__m128i);
17721775
len -= _mm_sum_epu8(counter); /* Fold in any remaining non-zero values in the 16 counters */
17731776
}
17741777
#endif

0 commit comments

Comments
 (0)