Reduce code duplication without sacrificing performance#5
Draft
WXbet wants to merge 1 commit into
Draft
Conversation
Inspired by PR oe-mirrors#4 (lukasz1992), this commit eliminates duplicated code across the stream cipher kernel and key scheduling functions while preserving the original runtime performance characteristics. Stream cipher kernel (dvbcsa_bs_stream_kernel.inc): - Replace the #ifdef DVBCSA_BS_STREAM_KERNEL_INIT / #else / #endif double-include trick with a single unified function _dvbcsa_bs_stream_cipher_kernel(const int init, regs). - The function is marked static DVBCSA_INLINE inline, which maps to __attribute__((always_inline)) on GCC. Combined with the macro wrappers that pass literal constants (0 or 1), the compiler is guaranteed to inline and eliminate the dead branches via constant propagation — producing identical machine code to the original two-function approach. - The sbox0 computation (4x unrolled Z/E carry chain) is intentionally kept unrolled. Extracting it into a loop function would break the regs->r "ultimate carry" calculation, which depends on tmp1/tmp3 values from the final iteration remaining in scope. Stream cipher includes (dvbcsa_bs_stream.c): - The .inc file is now included only once instead of the previous define/undef/include-twice pattern. Kernel dispatch macros (dvbcsa_bs_stream_kernel.h): - dvbcsa_bs_stream_cipher_kernel_init(regs) and dvbcsa_bs_stream_cipher_kernel(regs) are now macros that call _dvbcsa_bs_stream_cipher_kernel with compile-time constant 1 or 0. Key scheduling (dvbcsa_key.c): - dvbcsa_key_schedule_block() now delegates to dvbcsa_key_schedule_block_ecm(0, cw, kk), removing the duplicated loop body. Little-endian load (dvbcsa_pv.h): - dvbcsa_load_le64(p) is now a macro expanding to dvbcsa_load_le64_ecm(0, p). The compiler sees ecm=0 != 4 at compile time and eliminates the csa_block_perm_ecm lookup path entirely. Co-Authored-By: lukasz1992 <lukasz1992@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Inspired by PR #4 (lukasz1992), this commit eliminates duplicated code across the stream cipher kernel and key scheduling functions while preserving the original runtime performance characteristics.
Stream cipher kernel (dvbcsa_bs_stream_kernel.inc):
Stream cipher includes (dvbcsa_bs_stream.c):
Kernel dispatch macros (dvbcsa_bs_stream_kernel.h):
Key scheduling (dvbcsa_key.c):
Little-endian load (dvbcsa_pv.h):