Skip to content

refactor(backend): narrow unsafe scopes in SIMD kernels - #25

Merged
tisonkun merged 3 commits into
fast:mainfrom
cijiugechu:refactor/narrow-simd-unsafe
Nov 23, 2026
Merged

tisonkun merged 3 commits into
fast:mainfrom
cijiugechu:refactor/narrow-simd-unsafe

Conversation

@cijiugechu

@cijiugechu cijiugechu commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Convert pure SIMD value helpers, x86 encode_chunk_* / decode_chunk_* chunk helpers, and slice-based encode_neon / encode_ssse3 / encode_avx2 functions to safe #[target_feature] functions under the Rust 1.88 MSRV, reducing unsafe fn count across arch/{aarch64,x86}.rs from 48 to 15 (leaving only raw-pointer decode_* entry points shared with decode_in_place and partial-width tail load/store helpers).
  • Narrow unsafe blocks inside raw-pointer SIMD kernels to cover only memory loads/stores and pointer advancement while leaving vector arithmetic in safe code, and load static [T; N] lookup tables via typed array helpers (load_16, load_16_i8, load_8_i16).
  • Deduplicate Base32 and Base64 nibble masks in x86.rs against parent backend constants, drop an unused tuple element in Base32 encode_128_consts, and tighten // SAFETY: comments and # Safety documentation across all six modified backend files.

cijiugechu and others added 3 commits November 20, 2026 22:30
Convert the three `encode_neon` functions to slice-taking safe `#[target_feature]` functions matching `encode_ssse3` and `encode_avx2`, and pass loaded SIMD vectors by value into x86 `encode_chunk_*` and `decode_chunk_*` helpers so they become safe `fn`s without multi-line `(unsafe { ... })` call sites, reducing `unsafe fn` count across `arch/{aarch64,x86}.rs` from 27 to 15.

Add fixed-size array load helpers (`load_16`, `load_16_i8`, `load_8_i16`) for static lookup tables, deduplicate Base32 and Base64 nibble masks in `x86.rs`, drop the unused 9th tuple element in Base32 `encode_128_consts`, deduplicate NEON Base16 16-byte encoding, and coalesce adjacent store-and-advance `unsafe` blocks.

Signed-off-by: tison <wander4096@gmail.com>

@tisonkun tisonkun left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the cleanup, @cijiugechu! I merged main and pushed a small follow-up commit (1daa238) with Gemini 4 Carbon to take the same idea a step further:

  1. Converted the three AArch64 encode_neon functions to slice-taking safe #[target_feature] functions to match encode_ssse3 / encode_avx2 (and factored out encode_16_neon for Base16).
  2. Changed the nine x86 encode_chunk_* and decode_chunk_* helpers to take SIMD vectors by value as safe fns, matching AArch64/WASM and dropping the remaining unsafe fn count from 27 to 15.
  3. Added typed [T; N] array helpers (load_16, load_16_i8, load_8_i16) for loading static lookup tables without raw .as_ptr() unsafe blocks.
  4. Deduplicated the Base32 and Base64 nibble masks in x86.rs against the parent backend constants.
  5. Removed the unused 9th tuple element in Base32 encode_128_consts and merged adjacent store-and-advance unsafe blocks in the decode loops.

LGTM! Welcome to drop a review on the follow-up commit before we merge.

@tisonkun

Copy link
Copy Markdown
Contributor

I'm going to merge this later today, and any review after merge will be welcome.

@cijiugechu

Copy link
Copy Markdown
Contributor Author

Looks clean, thanks for taking the cleanup further!

@tisonkun
tisonkun merged commit 197c8f8 into fast:main Nov 23, 2026
13 checks passed
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.

2 participants