Skip to content

Use windowed shift-and-mask digit extraction in GetDigitAtIndexForBase - #1243

Merged
dsuponitskiy merged 1 commit into
devfrom
issue1240
Aug 5, 2026
Merged

Use windowed shift-and-mask digit extraction in GetDigitAtIndexForBase#1243
dsuponitskiy merged 1 commit into
devfrom
issue1240

Conversation

@pascoec

@pascoec pascoec commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator

The scalar primitive recomputed a floating-point log2 on every call and
assembled each digit one bit at a time; the NativeVector overload then
invoked it per coefficient through the shared library, so nothing hoisted
or vectorized. Extract the digit as a single shift-and-mask window
instead, with the shift and mask hoisted across the vector so the loop
auto-vectorizes. Digits are unchanged everywhere the old code was
defined; windows past the word width now return 0 instead of reading
past it (previously undefined for partial top windows, e.g. a 60-bit
modulus with 13-bit digits).

BV key switching consumes this on every relinearization and rotation.
On Ice Lake (Xeon 8360Y) at 1 thread across {clang-18, gcc-14} x
{native-opt on, off}: the primitive is 46-150x faster,
NativePoly::BaseDecompose 21-150x, DCRTPoly::CRTDecompose 1.45-2.7x,
and BGV BV-mode EvalMult+relin / EvalRotate 1.22-1.90x, with no
regressions; the gains hold at 36 threads under clang (1.19-1.83x).
Verified digit-for-digit against the previous implementation and by
exact reconstruction; all core and pke unit tests pass.

@pascoec pascoec added this to the Release 1.6.0 milestone Aug 5, 2026
@pascoec
pascoec requested a review from dsuponitskiy August 5, 2026 19:28
@pascoec pascoec self-assigned this Aug 5, 2026
@pascoec pascoec added the optimization Improves performance label Aug 5, 2026
@dsuponitskiy
dsuponitskiy merged commit 6726f51 into dev Aug 5, 2026
19 checks passed
@dsuponitskiy
dsuponitskiy deleted the issue1240 branch August 5, 2026 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimization Improves performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GetDigitAtIndexForBase: bit-by-bit extraction dominates BV key switching

3 participants