-
Notifications
You must be signed in to change notification settings - Fork 0
simd_half: TD-SIMD-8 — F16C-vectorized F16↔f32 batch conversion #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+315
−9
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cast_f32_to_f16_batch_f16ccurrently uses_mm256_cvtps_ph::<0>, which performs round-to-nearest-even but does not request exception suppression. This means conversions of NaN/Inf/overflow/underflow inputs can set MXCSR exception flags (and can trap if FP exceptions are unmasked), which is a behavior change from the previous pure bit-manipulation scalar path and contradicts the function-level contract that says “no exceptions.” Use the_MM_FROUND_NO_EXCvariant (imm8 with bit 3 set) to preserve non-trapping behavior.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks — the underlying concern (the F16C path can set MXCSR flags that the scalar bit-fiddle path doesn't) is valid. The proposed fix isn't quite right though:
_mm256_cvtps_ph'sIMM8is constrained to 3 bits in Rust stdarch (static_assert_uimm_bits!(IMM8, 3)— fails to compile forIMM8 = 8), and in the underlying VCVTPS2PH spec bit 3 selectsMXCSR.RMnot_MM_FROUND_NO_EXC(NO_EXC is an AVX-512 convention; F16C predates the SAE family). Only valid IMM8 values here are 0..=3 (the four rounding modes).The right fix is MXCSR save/restore via inline
asm!(stmxcsr/ldmxcsr)— landed in1a73c37. STMXCSR before the SIMD region, LDMXCSR after, preserves every bit of the saved control/status word including the exception flags the SIMD path may have set. Net effect: callers observe zero MXCSR change vs. the scalar path. Inline asm rather than_mm_getcsr/_mm_setcsrbecause those wrappers are deprecated on Rust 1.95 stable (unsoundness across thread MXCSR visibility; the deprecation notice explicitly recommends inline asm).Same fix applied to
cast_f16_to_f32_batch_f16csince_mm256_cvtph_pscan also raise#I/#Don SNaN/denormal F16 inputs. New testf16c_cast_preserves_mxcsrexercises both directions with inputs that trigger every relevant exception (overflow/underflow/precision/invalid/denormal); snapshots MXCSR before and after viastmxcsr, asserts byte-equal. Test passes.This fix preserves the MXCSR FLAG state. It does not prevent traps when the caller has unmasked FP exceptions before invoking us — those would fire from the SIMD ops themselves and bypass our restore. That's the same trap behaviour as any plain
a + bon overflow-prone f32, and the default OS-set MXCSR has all exception masks set so it's a non-issue for the common case.Generated by Claude Code