Skip to content

Commit 791e69d

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 fa06647 commit 791e69d

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

ext/mbstring/mbstring.c

Lines changed: 7 additions & 5 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
@@ -4589,9 +4592,8 @@ MBSTRING_API bool php_mb_check_encoding(const char *input, size_t length, const
45894592

45904593
static bool mb_fast_check_utf8(zend_string *str)
45914594
{
4592-
unsigned char *p = (unsigned char*)ZSTR_VAL(str), *e = p + ZSTR_LEN(str);
4593-
45944595
#ifdef __SSE2__
4596+
unsigned char *p = (unsigned char*)ZSTR_VAL(str), *e = p + ZSTR_LEN(str);
45954597
e -= sizeof(__m128i);
45964598

45974599
/* For checking for illegal bytes 0xF5-FF */

0 commit comments

Comments
 (0)