Skip to content

refactor(sha): decouple BSWAP masks from SHA round-constant arrays#80

Merged
Xor-el merged 1 commit into
masterfrom
refactor/decouple-simd-bswap-masks
Jun 20, 2026
Merged

refactor(sha): decouple BSWAP masks from SHA round-constant arrays#80
Xor-el merged 1 commit into
masterfrom
refactor/decouple-simd-bswap-masks

Conversation

@Xor-el

@Xor-el Xor-el commented Jun 20, 2026

Copy link
Copy Markdown
Owner

The x86 SIMD kernels previously appended a pshufb byte-swap mask to the end of the K256/K512/K_SHA1 round-constant arrays and reached it via an offset from the constants pointer (e.g. [r9+$100]). This conflated two unrelated things: the algorithm's round constants and an x86-only implementation detail, and it bloated the public K arrays with values that are not part of the SHA specification.
Separate the two concerns:

  • Shrink K256 [0..63], K512 [0..79] and K_SHA1 [0..15] back to exactly the spec-defined round constants.
  • Introduce dedicated BSWAP32_MASK / BSWAP64_MASK constants, guarded by {$IFDEF HASHLIB_X86_SIMD} since they are an x86 SIMD concern only (ARM byte-swaps natively via REV32/REV64 and needs no mask table).
  • Pass the mask as a separate 5th pointer argument: the SSSE3/AVX2/SHA-NI compress procedures now use SimdProc5Begin instead of SimdProc4Begin, and the kernels load the mask from the dedicated pointer (r10 on x86-64, ecx on i386) instead of an offset past the constants. SSE2 backends are unchanged (they byte-swap without a mask).

The x86 SIMD kernels previously appended a pshufb byte-swap mask to the end
of the K256/K512/K_SHA1 round-constant arrays and reached it via an offset
from the constants pointer (e.g. [r9+$100]). This conflated two unrelated
things: the algorithm's round constants and an x86-only implementation
detail, and it bloated the public K arrays with values that are not part of
the SHA specification.
Separate the two concerns:
- Shrink K256 [0..63], K512 [0..79] and K_SHA1 [0..15] back to exactly the
  spec-defined round constants.
- Introduce dedicated BSWAP32_MASK / BSWAP64_MASK constants, guarded by
  {$IFDEF HASHLIB_X86_SIMD} since they are an x86 SIMD concern only (ARM
  byte-swaps natively via REV32/REV64 and needs no mask table).
- Pass the mask as a separate 5th pointer argument: the SSSE3/AVX2/SHA-NI
  compress procedures now use SimdProc5Begin instead of SimdProc4Begin, and
  the kernels load the mask from the dedicated pointer (r10 on x86-64, ecx
  on i386) instead of an offset past the constants. SSE2 backends are
  unchanged (they byte-swap without a mask).
@Xor-el Xor-el merged commit d54fd7e into master Jun 20, 2026
24 checks passed
@Xor-el Xor-el deleted the refactor/decouple-simd-bswap-masks branch June 20, 2026 22:10
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.

1 participant