Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@ PHP 8.7 UPGRADE NOTES
- Standard:
. Improved performance of array_splice() when inserting without removing
elements.
. Improved performance of str_rot13().
24 changes: 8 additions & 16 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -6054,34 +6054,26 @@ static zend_string *php_str_rot13(zend_string *str)
gt = _mm_cmpgt_epi8(in, a_minus_1);
lt = _mm_cmplt_epi8(in, m_plus_1);
cmp = _mm_and_si128(lt, gt);
if (_mm_movemask_epi8(cmp)) {
cmp = _mm_and_si128(cmp, add);
delta = _mm_or_si128(delta, cmp);
}
cmp = _mm_and_si128(cmp, add);
delta = _mm_or_si128(delta, cmp);

gt = _mm_cmpgt_epi8(in, n_minus_1);
lt = _mm_cmplt_epi8(in, z_plus_1);
cmp = _mm_and_si128(lt, gt);
if (_mm_movemask_epi8(cmp)) {
cmp = _mm_and_si128(cmp, sub);
delta = _mm_or_si128(delta, cmp);
}
cmp = _mm_and_si128(cmp, sub);
delta = _mm_or_si128(delta, cmp);

gt = _mm_cmpgt_epi8(in, A_minus_1);
lt = _mm_cmplt_epi8(in, M_plus_1);
cmp = _mm_and_si128(lt, gt);
if (_mm_movemask_epi8(cmp)) {
cmp = _mm_and_si128(cmp, add);
delta = _mm_or_si128(delta, cmp);
}
cmp = _mm_and_si128(cmp, add);
delta = _mm_or_si128(delta, cmp);

gt = _mm_cmpgt_epi8(in, N_minus_1);
lt = _mm_cmplt_epi8(in, Z_plus_1);
cmp = _mm_and_si128(lt, gt);
if (_mm_movemask_epi8(cmp)) {
cmp = _mm_and_si128(cmp, sub);
delta = _mm_or_si128(delta, cmp);
}
cmp = _mm_and_si128(cmp, sub);
delta = _mm_or_si128(delta, cmp);
Comment on lines 6054 to +6076

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c | 0x20 maps A-Z onto a-z and moves nothing else into that range (it only touches bit 5, and bytes >= 0x80 stay negative under the signed compare), so the four range tests collapse into one plus a midpoint test:

const __m128i lowbit = _mm_set1_epi8(0x20);
const __m128i a_minus_1 = _mm_set1_epi8('a' - 1);
const __m128i z_plus_1 = _mm_set1_epi8('z' + 1);
const __m128i n = _mm_set1_epi8('n');
const __m128i add = _mm_set1_epi8(13);
const __m128i sub = _mm_set1_epi8(-13);

do {
	__m128i in = _mm_loadu_si128((__m128i *)p);
	__m128i low = _mm_or_si128(in, lowbit);
	__m128i alpha = _mm_and_si128(_mm_cmpgt_epi8(low, a_minus_1), _mm_cmplt_epi8(low, z_plus_1));
	__m128i first = _mm_cmplt_epi8(low, n);
	__m128i delta = _mm_and_si128(_mm_or_si128(_mm_and_si128(first, add), _mm_andnot_si128(first, sub)), alpha);

	_mm_storeu_si128((__m128i *)target, _mm_add_epi8(in, delta));

	p += 16;
	target += 16;
} while (e - p > 15);

gcc 11.4 -O2: the compiled loop body goes from 28 SIMD instructions to 15, and the 4-deep serial OR chain on delta goes away. Best of 7 over 1 MiB against your version: 1.9x on all-lowercase, 1.9x on digits, 2.4x on mixed case. Byte-identical to the scalar fallback for every byte value in every lane.

Separately, for the description: 1.2-1.3x is the floor, not the typical case. Random bytes at 1 MiB measure 7.9x against master here, since the four branches mispredict. Small buffers hide that, the predictor just memorizes a short repeating pattern.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I also confirmed the speed-up on my production server. @iliaal Is it mergeable now?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The branch still has the original commit, so the variant isn't in. Fine to merge as is if you'd rather keep this PR to the branch removal; either way please make the UPGRADING line "Improved performance of str_rot13()." like its neighbours.


in = _mm_add_epi8(in, delta);
_mm_storeu_si128((__m128i *)target, in);
Expand Down
Loading