Skip to content

Reduce code duplication without sacrificing performance#5

Draft
WXbet wants to merge 1 commit into
oe-mirrors:masterfrom
WXbet:master
Draft

Reduce code duplication without sacrificing performance#5
WXbet wants to merge 1 commit into
oe-mirrors:masterfrom
WXbet:master

Conversation

@WXbet

@WXbet WXbet commented Mar 21, 2026

Copy link
Copy Markdown

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):

  • 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.

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>
@WXbet WXbet marked this pull request as draft March 21, 2026 22:54
@WXbet WXbet mentioned this pull request Mar 21, 2026
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