{"slug":"readable-verilog-compare-optimize","title":"readable-verilog-compare-optimize","summary":"Use when optimizing Verilog relational or equality predicates, counter thresholds, power-of-two bounds, constant decodes or complementary comparisons for area or timing.","platform":"Claude","tags":[],"authorName":"LLM Mart","authorSlug":"llm-mart","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-12-03T17:18:08.49087Z","repo":{"url":"https://github.com/Eriemon/verilog-generator","stars":348,"forks":23,"license":"Apache-2.0","updatedAt":"2026-12-03T15:34:41Z"},"bodyHtml":"<hr>\n<h2>name: readable-verilog-compare-optimize\ndescription: Use when optimizing Verilog relational or equality predicates, counter thresholds, power-of-two bounds, constant decodes or complementary comparisons for area or timing.</h2>\n<h1>比较判断优化</h1>\n<p>定位判断的所有消费者，区分布尔值输出、过程 <code>if</code> 和条件运算符。根据声明、参数和常量推导实际比较宽度、扩展方式及 signed/unsigned 属性。保留计数器、加法器和寄存器位宽，只改有依据的判断。</p>\n<table>\n<thead>\n<tr>\n<th>判断</th>\n<th>候选与条件</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>无符号 <code>reg [7:0] cnt</code>，<code>cnt &lt; 8'd128</code></td>\n<td>在已知二值域等价于 <code>cnt[7] == 1'b0</code>。对无符号降序向量 <code>[W-1:0]</code>，阈值为可表示的 <span>\\(2^k\\)</span> 且 <span>\\(0 \\le k &lt; W\\)</span> 时，检查 <code>cnt[W-1:k]</code> 是否全零。先核对参数边界、下标方向和常量定宽。</td>\n</tr>\n<tr>\n<td><code>a &lt;= b</code></td>\n<td>相同操作数、宽度和类型下，表达式 <code>!(a &gt; b)</code> 保留四态结果。已有相同的 <code>a &gt; b</code> 时可尝试共享。若改为 <code>if (a &gt; b) B; else A;</code>，须证明判断生效时比较结果已知，或证明未知时两臂效果相同。</td>\n</tr>\n<tr>\n<td><code>cnt == 8'h00</code>、<code>cnt == 8'hff</code></td>\n<td>对同宽 8 位向量，全零和全一检测分别使用归约或非、归约与，保留四态结果。一般常量先找现有的相同译码；缩短相等比较须证明被省略的位在所有生效情况下都匹配，或已由外层条件检查。</td>\n</tr>\n<tr>\n<td>两个变量 <code>a == b</code></td>\n<td>等宽同类型可逐位异或，再检测结果是否全零。仅在已有可共享的逐位异或结果或映射实测有益时采用。保留 <code>==</code> 与 <code>===</code> 各自的未知值语义。</td>\n</tr>\n</tbody>\n</table>\n<p>对应的 Verilog 写法：全零检测为 <code>~|cnt</code>，全一检测为 <code>&amp;cnt</code>，两个等宽变量的相等检测为 <code>~|(a ^ b)</code>。</p>\n<p><strong>可达性。</strong> 当 <code>cnt = 8'b0xxx_xxxx</code> 时，<code>cnt &lt; 8'd128</code> 得 X，最高位检测得 1。若优化依赖“比较时已知”，须从有效复位建立基例，逐条核对计数、保持、回绕、外部装载和部分写入，并证明所有观察点都受对应使能约束。已知起点上的定宽加一与保持可维持已知性；能装入未知数据的路径需独立约束。有复位信号或若干通过的激励不能单独证明该前提。无法建立证明时保留原式。</p>\n<p><strong>终值译码。</strong> 若已证明无符号计数器每次被观察时都是 0 至 10 的已知值，可将 <code>cnt == 8'd10</code> 化为 <code>cnt[3] &amp; cnt[1]</code>。在该集合中，仅 10 同时置位这两位。11 也会命中候选；因此要检查装载、跳计数及非法状态恢复能否使 11 暴露给消费者。一般的部分位译码须证明候选在整个可达集合上只命中原比较接受的值。</p>\n<p><strong>验证与收益。</strong> 小位宽穷举合法输入；较大或跨拍设计用等价检查并列明假设。检查阈值两侧、相等、极值、回绕、复位和 X/Z；用四态比较核对结果或实际分支写入。二态形式证明须配合未知值语义分析。输出原式、候选、适用域、证明或反例。用同一目标、约束和综合设置比较面积及路径；时序结论注明实现阶段。若映射相同，保留较清晰的写法。涉及分支优先级时见<a href=\"../readable-verilog-branch-simplify/SKILL.md\">分支化简</a>。</p>\n","files":[{"path":"SKILL.md","sizeBytes":3204,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"clean","suspicious":0,"notes":0,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-12-03T17:19:31.277879Z","sha256":"810C04C91A599A0F115C4C8494B0C4189B67B070A978EC94EF03389BB0912B9E","sizeBytes":2162},"review":null,"source":{"repositoryUrl":"https://github.com/Eriemon/verilog-generator","path":"skills/readable-verilog-compare-optimize","license":"Apache-2.0","commit":"4738a3c74a2b8a4fec46e6abef7abf66f3eaebea","subtreeSha":"540500BB5F21DAC93E32993E1B8FCFC4720965FE00F39786E42764D681FD749A","lastSyncedAt":"2026-12-03T17:18:07.377421Z"},"reviewedAt":"2026-12-03T17:22:34.827054Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/Eriemon/verilog-generator/tree/main/skills/readable-verilog-compare-optimize"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install eriemon-verilog-generator@llmmart"},{"target":"git","command":"git clone https://github.com/Eriemon/verilog-generator.git"}]}