Skip to content

Commit 5434ebf

Browse files
committed
Improve: Fewer registers for e280xx masks in SVE2
1 parent bd9ddf5 commit 5434ebf

1 file changed

Lines changed: 15 additions & 19 deletions

File tree

include/stringzilla/utf8.h

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,9 @@ SZ_PUBLIC sz_cptr_t sz_utf8_find_whitespace_sve2(sz_cptr_t text, sz_size_t lengt
13981398
svdupq_n_u8(' ', '\t', '\n', '\v', '\f', '\r', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
13991399
svuint8_t multi_byte_prefix_set =
14001400
svdupq_n_u8(0xC2, 0xE1, 0xE2, 0xE3, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2);
1401+
// Valid third bytes for E2 80 XX: U+2000-U+200D (0x80-0x8D), U+2028 (0xA8), U+2029 (0xA9), U+202F (0xAF)
1402+
svuint8_t e280_third_bytes =
1403+
svdupq_n_u8(0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0xA8, 0xA9);
14011404

14021405
// Early return for short inputs
14031406
if (length < 3) return sz_utf8_find_whitespace_serial(text, length, matched_length);
@@ -1449,18 +1452,11 @@ SZ_PUBLIC sz_cptr_t sz_utf8_find_whitespace_sve2(sz_cptr_t text, sz_size_t lengt
14491452
svbool_t ogham_mask = svand_b_z(pg, svand_b_z(pg, svcmpeq_n_u8(pg, text0, 0xE1), svcmpeq_n_u8(pg, text1, 0x9A)),
14501453
svcmpeq_n_u8(pg, text2, 0x80));
14511454

1452-
// 3-byte: E2 80 XX - various Unicode spaces
1455+
// 3-byte: E2 80 XX - various Unicode spaces (U+2000-U+200D, U+2028, U+2029, U+202F)
14531456
svbool_t x_e2_mask = svcmpeq_n_u8(pg, text0, 0xE2);
1454-
svbool_t x_80_mask = svcmpeq_n_u8(pg, text1, 0x80);
1455-
svbool_t x_e280_mask = svand_b_z(pg, x_e2_mask, x_80_mask);
1456-
// U+2000 to U+200D: E2 80 [80-8D]
1457-
svbool_t range_e280_mask =
1458-
svand_b_z(pg, x_e280_mask, svand_b_z(pg, svcmpge_n_u8(pg, text2, 0x80), svcmple_n_u8(pg, text2, 0x8D)));
1459-
// U+2028: E2 80 A8 (LINE SEPARATOR)
1460-
svbool_t line_mask = svand_b_z(pg, x_e280_mask, svcmpeq_n_u8(pg, text2, 0xA8));
1461-
// U+2029: E2 80 A9 (PARAGRAPH SEPARATOR)
1462-
svbool_t paragraph_mask = svand_b_z(pg, x_e280_mask, svcmpeq_n_u8(pg, text2, 0xA9));
1463-
// U+202F: E2 80 AF (NARROW NO-BREAK SPACE)
1457+
svbool_t x_e280_mask = svand_b_z(pg, x_e2_mask, svcmpeq_n_u8(pg, text1, 0x80));
1458+
svbool_t x_e280xx_mask = svand_b_z(pg, x_e280_mask, svmatch_u8(pg, text2, e280_third_bytes));
1459+
// U+202F: E2 80 AF (NARROW NO-BREAK SPACE) - doesn't fit in the 16-byte set
14641460
svbool_t nnbsp_mask = svand_b_z(pg, x_e280_mask, svcmpeq_n_u8(pg, text2, 0xAF));
14651461
// U+205F: E2 81 9F (MEDIUM MATHEMATICAL SPACE)
14661462
svbool_t mmsp_mask =
@@ -1471,19 +1467,19 @@ SZ_PUBLIC sz_cptr_t sz_utf8_find_whitespace_sve2(sz_cptr_t text, sz_size_t lengt
14711467
svand_b_z(pg, svand_b_z(pg, svcmpeq_n_u8(pg, text0, 0xE3), svcmpeq_n_u8(pg, text1, 0x80)),
14721468
svcmpeq_n_u8(pg, text2, 0x80));
14731469

1474-
svbool_t three_byte_mask = svorr_b_z(
1475-
pg, svorr_b_z(pg, svorr_b_z(pg, ogham_mask, range_e280_mask), svorr_b_z(pg, line_mask, paragraph_mask)),
1476-
svorr_b_z(pg, svorr_b_z(pg, nnbsp_mask, mmsp_mask), ideographic_mask));
1470+
svbool_t three_byte_mask = svorr_b_z(pg, svorr_b_z(pg, ogham_mask, svorr_b_z(pg, x_e280xx_mask, nnbsp_mask)),
1471+
svorr_b_z(pg, mmsp_mask, ideographic_mask));
14771472
svbool_t combined_mask = svorr_b_z(pg, one_byte_mask, svorr_b_z(pg, two_byte_mask, three_byte_mask));
14781473

14791474
if (svptest_any(pg, combined_mask)) {
14801475
sz_size_t pos = svcntp_b8(pg, svbrkb_b_z(pg, combined_mask));
14811476
svbool_t at_pos = svcmpeq_n_u8(svptrue_b8(), svindex_u8(0, 1), (sz_u8_t)pos);
1482-
1483-
if (svptest_any(at_pos, three_byte_mask)) { *matched_length = 3; }
1484-
else if (svptest_any(at_pos, two_byte_mask)) { *matched_length = 2; }
1485-
else { *matched_length = 1; }
1486-
1477+
sz_size_t has_two_byte = svptest_any(at_pos, two_byte_mask);
1478+
sz_size_t has_three_byte = svptest_any(at_pos, three_byte_mask);
1479+
sz_size_t length_value = 1;
1480+
length_value += has_two_byte | has_three_byte;
1481+
length_value += has_three_byte;
1482+
*matched_length = length_value;
14871483
return (sz_cptr_t)(text_u8 + offset + pos);
14881484
}
14891485
offset += step;

0 commit comments

Comments
 (0)