Commit 7a7b29c397e for php

commit 7a7b29c397ed7f8b10d7e26491a9d21de8fa2338
Author: Daniel Lemire <daniel@lemire.me>
Date:   Sun Nov 15 16:57:58 2026 -0500

    ext/mbstring: Fix UTF-8 validation review notes

    The new test called preg_match('//u') before mb_check_encoding().
    That sets IS_STR_VALID_UTF8, and str_repeat($s, 1) returns the same
    string, so valid input never reached the new kernels. Copy the
    bytes with pack() first, and cover strings longer than 2048 bytes.

    The AVX-512 alignment prologue loaded context through a pointer
    before the zend_string. Read the three bytes from inside the string.
    OR the NEON continuation checks, matching AVX2.

    Record the speedup in NEWS.

diff --git a/NEWS b/NEWS
index aabe78ad2a7..13c2a82bbbb 100644
--- a/NEWS
+++ b/NEWS
@@ -77,6 +77,8 @@ PHP                                                                        NEWS
     (lacatoire)

 - MBString:
+  . Improved performance of UTF-8 validation in mb_check_encoding(),
+    mb_strlen(), mb_substr(), and related functions. (Daniel Lemire)
   . Fixed bug GH-23106 (mb_strpos() reads past the end of a haystack ending in
     a truncated UTF-8 sequence). (Lazizbek Ergashev)
   . Updated Unicode data tables to Unicode 18.0 (Yuya Hamada)
diff --git a/ext/mbstring/mbstring.c b/ext/mbstring/mbstring.c
index 4efcbfc20d7..57ee69ad010 100644
--- a/ext/mbstring/mbstring.c
+++ b/ext/mbstring/mbstring.c
@@ -5196,7 +5196,7 @@ static bool mb_fast_check_utf8_default(zend_string *str)
 			vqtbl1q_u8(byte_1_high, vshrq_n_u8(prev1, 4)), \
 			vqtbl1q_u8(byte_1_low, vandq_u8(prev1, v_0f))), \
 			vqtbl1q_u8(byte_2_high, vshrq_n_u8((input), 4))); \
-		uint8x16_t must23 = veorq_u8( \
+		uint8x16_t must23 = vorrq_u8( \
 			vcgeq_u8(vextq_u8((prev), (input), 14), v_e0), \
 			vcgeq_u8(vextq_u8((prev), (input), 13), v_f0)); \
 		error = vorrq_u8(error, veorq_u8(vandq_u8(must23, v_80), sc)); \
@@ -5821,7 +5821,13 @@ static bool mb_fast_check_utf8_avx512(zend_string *str)
 	const __m512i v_80 = _mm512_set1_epi8((char)0x80);
 	const __m512i v_third = _mm512_set1_epi8((char)(0xE0 - 0x80));
 	const __m512i v_fourth = _mm512_set1_epi8((char)(0xF0 - 0x80));
-	const __m512i max_value = _mm512_setr_epi64(-1, -1, -1, -1, -1, -1, -1, (int64_t)0xBFDFEFFFFFFFFFFFULL);
+	/* A block ending with any of these is incomplete: ... 1111____ 111_____ 11______ */
+	const __m512i max_value = _mm512_setr_epi8(
+		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+		(char)(0xF0 - 1), (char)(0xE0 - 1), (char)(0xC0 - 1));
 	/* Brings the last 16 bytes of `prev` in front of the first 48 bytes of `input` */
 	const __m512i prev_shuffle = _mm512_setr_epi32(28, 29, 30, 31, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);

@@ -5836,7 +5842,12 @@ static bool mb_fast_check_utf8_avx512(zend_string *str)
 		__m512i in = _mm512_loadu_si512((const void*)p);
 		MB_UTF8_CHECK_AVX512(in, prev_input);
 		p += 64 - misalignment;
-		prev_input = _mm512_maskz_loadu_epi8(0xE000000000000000ULL, (const void*)(p - 64));
+		/* The 3 bytes before p belong to the string: misalignment <= 61, so
+		 * p - 3 does not step back past the start. They occupy the last 3 lanes. */
+		uint32_t ctx = 0;
+		memcpy(&ctx, p - 3, 3);
+		__m128i ctxv = _mm_slli_si128(_mm_cvtsi32_si128(ctx), 13);
+		prev_input = _mm512_inserti64x2(_mm512_setzero_si512(), ctxv, 3);
 		prev_incomplete = _mm512_subs_epu8(prev_input, max_value);
 	}

diff --git a/ext/mbstring/tests/mb_check_encoding_utf8_blocks.phpt b/ext/mbstring/tests/mb_check_encoding_utf8_blocks.phpt
index 9d6b32e38e1..068d00714a9 100644
--- a/ext/mbstring/tests/mb_check_encoding_utf8_blocks.phpt
+++ b/ext/mbstring/tests/mb_check_encoding_utf8_blocks.phpt
@@ -26,7 +26,9 @@
                 foreach ([false, true] as $replace) {
                     $s = substr($base, 0, $pos) . $seq . substr($base, $pos + ($replace ? strlen($seq) : 0));
                     $expected = preg_match('//u', $s) === 1;
-                    $s = str_repeat($s, 1); // a fresh string, with no cached validity flag
+                    // preg_match('//u') sets the UTF-8 flag, and str_repeat($s, 1)
+                    // returns that same string. pack() allocates a new one without it.
+                    $s = ($s === "") ? $s : pack("H*", bin2hex($s));
                     if (mb_check_encoding($s, 'UTF-8') !== $expected) {
                         if ($mismatches++ < 10) {
                             echo "Mismatch: ", bin2hex($s), " expected ", var_export($expected, true), "\n";
@@ -38,6 +40,33 @@
         }
     }
 }
+// Lengths past 2048 bytes take the AVX-512 alignment prologue.
+foreach ([2048, 2112, 4095] as $len) {
+    foreach ($fillers as $filler) {
+        $base = str_repeat($filler, intdiv($len, strlen($filler)) + 1);
+        $base = mb_strcut($base, 0, $len, 'UTF-8');
+        $probe = pack('H*', bin2hex($base));
+        if (mb_check_encoding($probe, 'UTF-8') !== (preg_match('//u', $base) === 1)) {
+            if ($mismatches++ < 10) {
+                echo "Mismatch: long ", bin2hex(substr($base, 0, 16)), "... len ", strlen($base), "\n";
+            }
+        }
+        $checked++;
+        if (strlen($base) < 65) {
+            continue;
+        }
+        $s = $probe;
+        $s[64] = "\x80";
+        $expected = preg_match('//u', $s) === 1;
+        $s = pack('H*', bin2hex($s));
+        if (mb_check_encoding($s, 'UTF-8') !== $expected) {
+            if ($mismatches++ < 10) {
+                echo "Mismatch: long mutated len ", strlen($s), "\n";
+            }
+        }
+        $checked++;
+    }
+}
 echo $checked > 100000 ? "enough cases\n" : "too few cases: $checked\n";
 echo "mismatches: $mismatches\n";
 ?>