Skip to content

arm64: test comparison masks with shrn + fcmp instead of a reduction - #1013

Merged
lemire merged 1 commit into
masterfrom
arm64-shrn-fcmp-zero-check
Jul 28, 2026
Merged

lemire merged 1 commit into
masterfrom
arm64-shrn-fcmp-zero-check

Conversation

@lemire

@lemire lemire commented Jul 27, 2026

Copy link
Copy Markdown
Member

Checking whether a NEON comparison mask has any lane set was done with a reduction (vmaxvq_*) followed by a move to a general-purpose register. On Apple hardware both instructions have a latency of about 3 cycles, so the test costs ~6 cycles.

When the vector is the result of a lane-wise comparison (every byte is 0x00 or 0xff), a narrowing shift by 4 preserves 'is non-zero', and the result can be compared against zero directly in the floating-point register file:

shrn.8b	v0, v0, #4
fcmp	d0, #0.0

This is two instructions and no cross-domain transfer. Both steps require the input to be a comparison mask: shrn #4 keeps bits 4..11 of each 16-bit lane, and the only non-zero bit pattern that compares equal to 0.0 is -0.0, whose most significant byte (0x80) cannot be produced from a mask.

This adds simd::any_lane_set() and uses it where the operand is already known to be a comparison mask: simd32::any() (which covers the generic UTF-32 validation loop), the utf16fix illsequencing test, the surrogate tests in UTF-16 to UTF-8/UTF-32, and the forbidden-code-point tests in UTF-32 to UTF-8/UTF-16.

In arm_validate_utf32le, veorq(vmaxq(a, b), b) is non-zero exactly when a > b, so it is replaced by vcgtq_u32; the _with_errors loop then tests both accumulators at once and only tells the two error kinds apart on the unlikely error path.

This is a micro-optimization that might not deliver an obvious win in general, but it saves a bit of latency here and there.

@lemire lemire added the NEON label Jul 27, 2026
Checking whether a NEON comparison mask has any lane set was done with a
reduction (vmaxvq_*) followed by a move to a general-purpose register. On
Apple hardware both instructions have a latency of about 3 cycles, so the
test costs ~6 cycles.

When the vector is the result of a lane-wise comparison (every byte is
0x00 or 0xff), a narrowing shift by 4 preserves 'is non-zero', and the
result can be compared against zero directly in the floating-point
register file:

	shrn.8b	v0, v0, #4
	fcmp	d0, #0.0

This is two instructions and no cross-domain transfer. Both steps require
the input to be a comparison mask: shrn #4 keeps bits 4..11 of each 16-bit
lane, and the only non-zero bit pattern that compares equal to 0.0 is -0.0,
whose most significant byte (0x80) cannot be produced from a mask.

This adds simd::any_lane_set() and uses it where the operand is already
known to be a comparison mask: simd32<bool>::any() (which covers the
generic UTF-32 validation loop), the utf16fix illsequencing test, the
surrogate tests in UTF-16 to UTF-8/UTF-32, and the forbidden-code-point
tests in UTF-32 to UTF-8/UTF-16.

In arm_validate_utf32le, veorq(vmaxq(a, b), b) is non-zero exactly when
a > b, so it is replaced by vcgtq_u32; the _with_errors loop then tests
both accumulators at once and only tells the two error kinds apart on the
unlikely error path.

The count of umaxv instructions in the library drops from 87 to 51.

Deliberately left alone: simd8<uint8_t>::any_bits_set_anywhere(), whose
caller utf8_checker::errors() accumulates lookup-table results (values such
as 0x01), not comparison masks. The narrowing shift would drop those bits.
@lemire
lemire force-pushed the arm64-shrn-fcmp-zero-check branch from 885cee5 to bfe24b7 Compare July 28, 2026 00:18
@lemire
lemire merged commit 611becc into master Jul 28, 2026
97 checks passed
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
simdutf 9.1.0

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## What's Changed
* scripts/add_function.py: fixes by @BenjaminBucher in simdutf/simdutf#971
* workaround missing std::unreachable by @pauldreik in simdutf/simdutf#966
* update to cmake 3.18 everywhere by @pauldreik in simdutf/simdutf#967
* Remove redundant checks in scalar utf8 validation by @BenjaminBucher in simdutf/simdutf#970
* test: strengthen conversion result coverage by @parasol-aser in simdutf/simdutf#972
* add test where rewind causes integer underflow by @BenjaminBucher in simdutf/simdutf#975
* icelake: use unsigned shift base in utf16_to_latin1 tail mask by @metsw24-max in simdutf/simdutf#976
* icelake: use unsigned shift base in latin1_to_utf16 tail mask by @metsw24-max in simdutf/simdutf#977
* fix doubled length in binary_length_from_base64 for char16 input by @metsw24-max in simdutf/simdutf#978
* removing <algorithm> header by @lemire in simdutf/simdutf#979
* scripts/add_function.py: Better #if detection by @BenjaminBucher in simdutf/simdutf#985
* fixing issue 989 by @lemire in simdutf/simdutf#991
* fuzz: add binary_length_from_base64 coverage to base64 fuzzer by @aryanputta in simdutf/simdutf#987
* upgrading our 'unused' macros + silence silly warnings on 3 functions by @lemire in simdutf/simdutf#993
* Fix some out-of-bounds warnings from clang 23 by @erikcorry in simdutf/simdutf#995
* using __builtin_popcountll by @lemire in simdutf/simdutf#997
* add missing includes by @pauldreik in simdutf/simdutf#998
* Add RVV vectorized base64 encoding by @rajeshgangam in simdutf/simdutf#996
* arm64/base64: reuse scalar base64 tables for encoding by @tantei3 in simdutf/simdutf#1000
* arm64/base64: optimize insert_line_feed16 by @tantei3 in simdutf/simdutf#1001
* icelake: use unsigned shift base in all remaining tail/mask expressions by @lemire in simdutf/simdutf#1004
* tools/fastbase64: fix size_t -> int conversion warning by @lemire in simdutf/simdutf#1005
* potential fix for vs issue by @lemire in simdutf/simdutf#1006
* adding optimization from PR 1001 to westmere and haswell by @lemire in simdutf/simdutf#1003
* include what you use - std::abort by @pauldreik in simdutf/simdutf#1007
* add missing include for std::memcpy by @pauldreik in simdutf/simdutf#1009
* Avoid including algorithm just for std::min by @pauldreik in simdutf/simdutf#1010
* optimize the utf16 to utf8 transcoding. by @lemire in simdutf/simdutf#1011
* icelake/base64: optimized newline encoding for line_len < 64 by @tantei3 in simdutf/simdutf#1012
* arm64: test comparison masks with shrn + fcmp instead of a reduction by @lemire in simdutf/simdutf#1013
* icelake/base64: Add SIMD tail for binary_length_from_base64 by @tantei3 in simdutf/simdutf#1015
* [doc] document the cost of _safe variants by @lemire in simdutf/simdutf#1020
* Add convert_utf16_to_utf8_with_replacement to the C API by @lemire in simdutf/simdutf#1021
* utf16_to_utf8: report bytes written on error, drop the utf8_length re-walk by @lemire in simdutf/simdutf#1022

## New Contributors
* @BenjaminBucher made their first contribution in simdutf/simdutf#971
* @parasol-aser made their first contribution in simdutf/simdutf#972
* @metsw24-max made their first contribution in simdutf/simdutf#976
* @aryanputta made their first contribution in simdutf/simdutf#987
* @rajeshgangam made their first contribution in simdutf/simdutf#996

**Full Changelog**: https://github.com/simdutf/simdutf/compare/v9.0.0...v9.1.0</pre>
  <p>View the full release notes at <a href="https://github.com/simdutf/simdutf/releases/tag/v9.1.0">https://github.com/simdutf/simdutf/releases/tag/v9.1.0</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!17087
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant