Skip to content

icelake: use unsigned shift base in utf16_to_latin1 tail mask - #976

Merged
lemire merged 1 commit into
simdutf:masterfrom
metsw24-max:icelake-utf16-latin1-unsigned-shift
May 23, 2026
Merged

lemire merged 1 commit into
simdutf:masterfrom
metsw24-max:icelake-utf16-latin1-unsigned-shift

Conversation

@metsw24-max

Copy link
Copy Markdown
Contributor

The masked tail block of icelake_convert_utf16_to_latin1 and
icelake_convert_utf16_to_latin1_with_errors builds the AVX-512
load/store mask from a signed 1:

  • src/icelake/icelake_convert_utf16_to_latin1.inl.cpp:30
  • src/icelake/icelake_convert_utf16_to_latin1.inl.cpp:82
uint32_t mask(uint32_t(1 << (end - buf)) - 1);

After the 32-element main loop, end - buf is in [1, 31]. Any caller
that passes an input length such that the final block has 31 remaining
char16 values (the simplest case is len == 31) reaches the path with
shift count 31. int 1 << 31 places a 1 in the sign bit of a 32-bit
int, which is implementation-defined behaviour in C++17 and was
undefined behaviour before.

The rest of the icelake source already builds these masks from an
unsigned base to avoid exactly this idiom:

  • src/icelake/icelake_ascii_validation.inl.cpp:14: uint64_t(1) << (end - buf)
  • src/icelake/icelake_convert_utf16_to_utf8.inl.cpp:195: (1U << inlen) - 1
  • src/icelake/implementation.cpp:294/323/418/...: (1U << (end - buf)) - 1
  • include/simdutf/internal/isadetection.h:180: uint32_t(1) << 31

The two tail-mask computations in icelake_convert_utf16_to_latin1.inl.cpp
were the remaining outliers.

Fix

Switch both sites to 1U << and drop the now-redundant outer
uint32_t(...) cast around the shift. The value built by the expression
is unchanged on every supported compiler, but the construct no longer
depends on signed-to-unsigned conversion of an int with the sign bit
set.

Reproducer / observation

To exercise the path on icelake-capable hardware:

char16_t buf[31] = {};
char out[31];
simdutf::convert_utf16le_to_latin1(buf, 31, out);

This bypasses the 32-element main loop and enters the if (buf < end)
branch with end - buf == 31, evaluating the shift on 1.

After the 32-element main loop, end - buf is in [1, 31]. With an input
length such that end - buf reaches 31 (e.g. len == 31), the existing
expression evaluates 'int 1 << 31', which places a 1 in the sign bit of
a 32-bit int. That is implementation-defined behaviour in C++17 (and was
undefined before). The rest of the icelake source already builds these
AVX-512 masks from an unsigned base — see e.g.
icelake_ascii_validation.inl.cpp, icelake_convert_utf16_to_utf8.inl.cpp,
and include/simdutf/internal/isadetection.h.

Switch the two tail-mask computations to '1U <<' to bring them in line.
No functional change on supported compilers.
@pauldreik

pauldreik commented May 22, 2026 •

Copy link
Copy Markdown
Collaborator

thanks, this looks good!

the ci does unfortunately not run icelake. will try locally on my machine.

@lemire
lemire merged commit 4f42671 into simdutf:master May 23, 2026
73 checks passed
lemire added a commit that referenced this pull request Jul 21, 2026
Several icelake kernels build AVX-512 lane masks with a signed `1 << n`
base, e.g. `(1 << (remaining_len * 2)) - 1` and
`(1 << (count + count_ones(sp_mask))) - 1`. The shift amount here is a
lane count that can reach 31 or 32, so `1 << 31` / `1 << 32` on a signed
`int` is undefined behavior. Compilers are free to compute a different
mask (or none), which can silently corrupt output. This is the same
class of bug fixed for latin1_to_utf16 (#977) and utf16_to_latin1 (#976),
and is a plausible source of MSVC-only flaky failures in the
utf8_to_utf16 / utf32_to_utf16 tests.

Make the shift base unsigned everywhere:
- `uint64_t(1)` where the shift amount can reach 32
  (utf32_to_utf16 output masks, utf32_to_utf16 error truncation,
   utf8_common utf32_to_utf16 store mask);
- `1U` for the `__mmask16` sites whose shift stays <= 16.

No functional change on well-behaved compilers; removes the UB so the
mask is well-defined regardless of toolchain. All conversion/validation
tests pass on an icelake host.
lemire added a commit that referenced this pull request Jul 21, 2026
…ns (#1004)

Several icelake kernels build AVX-512 lane masks with a signed `1 << n`
base, e.g. `(1 << (remaining_len * 2)) - 1` and
`(1 << (count + count_ones(sp_mask))) - 1`. The shift amount here is a
lane count that can reach 31 or 32, so `1 << 31` / `1 << 32` on a signed
`int` is undefined behavior. Compilers are free to compute a different
mask (or none), which can silently corrupt output. This is the same
class of bug fixed for latin1_to_utf16 (#977) and utf16_to_latin1 (#976),
and is a plausible source of MSVC-only flaky failures in the
utf8_to_utf16 / utf32_to_utf16 tests.

Make the shift base unsigned everywhere:
- `uint64_t(1)` where the shift amount can reach 32
  (utf32_to_utf16 output masks, utf32_to_utf16 error truncation,
   utf8_common utf32_to_utf16 store mask);
- `1U` for the `__mmask16` sites whose shift stays <= 16.

No functional change on well-behaved compilers; removes the UB so the
mask is well-defined regardless of toolchain. All conversion/validation
tests pass on an icelake host.
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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants