Commit 19f7f9ec366 for php

commit 19f7f9ec366ab9a31cac1a1eb32e973a95dece49
Author: Daniel Lemire <daniel@lemire.me>
Date:   Mon Nov 23 08:40:53 2026 -0500

    ext/mbstring: Branch hints and more long-string tests for UTF-8 validation

    Mark the all-ASCII branch as expected in the NEON and AVX-512 block
    loops, and the periodic error checks as unexpected in all three kernels.
    The AVX2 ASCII branch is left without a hint: with GCC 16 it makes
    the compiler spill registers in the non-ASCII path, and UTF-8 text gets
    about 12% slower, for a gain of about 1% on ASCII.

    Test lengths around 1024 and 1088 bytes, where the kernels check the
    accumulated errors, as well as 2047-4097 bytes. Corrupt bytes at
    offsets around 64, 1024, 1088 and 2048, and in the last 6 bytes.

diff --git a/ext/mbstring/mbstring.c b/ext/mbstring/mbstring.c
index 451c776366d..4277f081a6e 100644
--- a/ext/mbstring/mbstring.c
+++ b/ext/mbstring/mbstring.c
@@ -5230,7 +5230,7 @@ static bool mb_fast_check_utf8_default(zend_string *str)
 			uint8x16_t in1 = vld1q_u8(p + 16);
 			uint8x16_t in2 = vld1q_u8(p + 32);
 			uint8x16_t in3 = vld1q_u8(p + 48);
-			if (vmaxvq_u8(vorrq_u8(vorrq_u8(in0, in1), vorrq_u8(in2, in3))) < 0x80) {
+			if (EXPECTED(vmaxvq_u8(vorrq_u8(vorrq_u8(in0, in1), vorrq_u8(in2, in3))) < 0x80)) {
 				/* All ASCII; only need to check that the previous block was complete */
 				error = vorrq_u8(error, prev_incomplete);
 			} else {
@@ -5243,7 +5243,7 @@ static bool mb_fast_check_utf8_default(zend_string *str)
 			}
 			p += 64;
 		} while (p < stop);
-		if (vmaxvq_u8(error)) {
+		if (UNEXPECTED(vmaxvq_u8(error))) {
 			return false;
 		}
 	}
@@ -5727,7 +5727,7 @@ static bool mb_fast_check_utf8_avx2(zend_string *str)
 			}
 			p += 64;
 		} while (p < stop);
-		if (!_mm256_testz_si256(error, error)) {
+		if (UNEXPECTED(!_mm256_testz_si256(error, error))) {
 			return false;
 		}
 	}
@@ -5851,7 +5851,7 @@ static bool mb_fast_check_utf8_avx512(zend_string *str)
 		const unsigned char *stop = (e - p >= 1024 + 64) ? p + 1024 : e - 63;
 		do {
 			__m512i in = _mm512_loadu_si512((const void*)p);
-			if (!_mm512_movepi8_mask(in)) {
+			if (EXPECTED(!_mm512_movepi8_mask(in))) {
 				error = _mm512_or_si512(error, prev_incomplete);
 			} else {
 				MB_UTF8_CHECK_AVX512(in, prev_input);
@@ -5860,7 +5860,7 @@ static bool mb_fast_check_utf8_avx512(zend_string *str)
 			}
 			p += 64;
 		} while (p < stop);
-		if (_mm512_test_epi8_mask(error, error)) {
+		if (UNEXPECTED(_mm512_test_epi8_mask(error, error))) {
 			return false;
 		}
 	}
diff --git a/ext/mbstring/tests/mb_check_encoding_utf8_blocks.phpt b/ext/mbstring/tests/mb_check_encoding_utf8_blocks.phpt
index 068d00714a9..d4bfe27edab 100644
--- a/ext/mbstring/tests/mb_check_encoding_utf8_blocks.phpt
+++ b/ext/mbstring/tests/mb_check_encoding_utf8_blocks.phpt
@@ -40,31 +40,42 @@
         }
     }
 }
-// Lengths past 2048 bytes take the AVX-512 alignment prologue.
-foreach ([2048, 2112, 4095] as $len) {
+// Long strings: errors are only tested every 1024 bytes, and lengths of 2048
+// bytes or more take the AVX-512 alignment prologue.
+$lengths = [1000, 1020, 1023, 1024, 1025, 1028, 1087, 1088, 1089, 1100,
+            2047, 2048, 2049, 2112, 3000, 4095, 4096, 4097];
+$bad = ["\x80", "\xC3", "\xED", "\xF0", "\xF4", "\xFF"];
+foreach ($lengths as $len) {
+    $offsets = [0, 1, 2, 3, 61, 62, 63, 64, 65, 66, 127, 128, 129,
+                1021, 1022, 1023, 1024, 1025, 1026, 1086, 1087, 1088, 1089,
+                2045, 2046, 2047, 2048, 2049, 2050];
+    for ($i = 1; $i <= 6; $i++) {
+        $offsets[] = $len - $i;
+    }
     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";
+        $n = strlen($base);
+        $variants = [$base];
+        foreach ($offsets as $pos) {
+            if ($pos >= 0 && $pos < $n) {
+                foreach ($bad as $byte) {
+                    $s = $base;
+                    $s[$pos] = $byte;
+                    $variants[] = $s;
+                }
             }
         }
-        $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";
+        foreach ($variants as $s) {
+            $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 len ", strlen($s), " expected ", var_export($expected, true), "\n";
+                }
             }
+            $checked++;
         }
-        $checked++;
     }
 }
 echo $checked > 100000 ? "enough cases\n" : "too few cases: $checked\n";