Skip to content

refactor: shift 单遍融合内核化并整体 constexpr - #5

Merged
holtwood merged 1 commit into
masterfrom
refactor/shift-fused-constexpr
Aug 20, 2026
Merged

refactor: shift 单遍融合内核化并整体 constexpr#5
holtwood merged 1 commit into
masterfrom
refactor/shift-fused-constexpr

Conversation

@holtwood

Copy link
Copy Markdown
Collaborator

背景

shift_left / shift_right 原先「先整块拷贝 + 原地移位」,数据被扫 2~3 遍,位移循环的 carry 在字间串行传播,且 shift_*_arraymemset 是「变换类算法整体非 constexpr」这一已知限制的直接来源。

改动

  • detail/word_ops.hpp:删除 shift_left_array / shift_right_array,新增 constexpr 单遍融合内核 shift_left_fused / shift_right_fused(读入写出,每输出字只依赖两个独立输入字,无串行 carry 依赖链)。count==0、count>=N*64、bit_shift==0(短路防 >>64 UB)均由公式天然正确处理,无特判分支;assert(out != in) 声明不承诺重叠。删除不再使用的 <cstring>
  • algorithms.hppcompose_shifted_block 删预拷贝循环、改 constexpr;shift_left / shift_right(span 与 bit_block 两种形态)整体 constexpr。
  • tests/test_bitcal.cpp:新增 constexpr 编译期断言(static_assert,覆盖 count=64/65/256)+ 128 位词内位移极端(count=1/63)+ 纯词移(count=64)确定性用例 + 512 位随机对照。共 49 用例。
  • NOTES.md:已知限制缩窄为「仅 bit_* 非 constexpr」;CHANGELOG.md:加 ♻️ 重构 / 🧪 测试 / 📚 文档条目。

验证

  • AVX2 与 scalar(-mno-avx2)两套构建 ctest 100% 通过——constexpr static_assert 在两个配置编译,验证「constexpr 与后端无关」。
  • clang-format 通过。
  • benchmark_compare(count=65):256/512 位与 std::bitset 持平(0.92~0.99x),无回退。

公开 API 签名与语义不变,非破坏性变更。

@holtwood
holtwood merged commit 2d08501 into master Aug 20, 2026
4 checks passed
@holtwood
holtwood deleted the refactor/shift-fused-constexpr branch August 20, 2026 09:30
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