RISCV-LEARN

“B” Extension for Bit Manipulation, Version 1.0.0

"B" Extension for Bit Manipulation

1 Zb*

RISC-V的B扩展(Bit Manipulation Extension)是一组用于位操作的指令集扩展,旨在提供代码大小优化、性能提升和能耗降低。B扩展由多个子扩展(Zba、Zbb、Zbs等)组成,每个子扩展专注于特定类型的位操作。B扩展适用于RV32和RV64架构。

子扩展有:

2 Zba : Address generation

Zba用于加速地址生成,比如数组索引计算(优化内存访问中的地址计算,如 array[i]的地址计算)。

其包含的指令如下:

1755227090997

3 Zbb : Basic bit-manipulation

3-1 Logical with negate

与非、或非和异或非:

1755227268612

3-2 Count leading/trailing zero bits

前导零计数和尾随零技术:

1755227282826

3-3 Count population

置1位数统计:

1755227297473

3-4 Integer minimum/maximum

返回两个数中较小/较大的那个数

1755227354525

3-5 Sign extension and zero extension

实现符号扩展和零扩展,替代了:

1755227408702

3-6 Bitwise rotation

循环移位(circular shifts),左/右旋转和立即数右旋:

1755227585718

3-7 OR Combine

按字节或合并,可用于优化strlen和strcpy等字符串处理函数:

1755227691598

bitwise:逐位/按位,granule:颗粒

3-8 Byte-reverse

字节反转:

1755227735795

4 Zbc : Carry-less multiplication

支持无进位乘法,其中clmulr反转无进位乘法可用于CRC加速:

1755227878976

应用场景: CRC, AES-GCM, ECC

5 Zbs : Single-bit instructions

对寄存器中的单个位进行设置、清楚、翻转或提取:

1755227910951

6 Zbkb : Bit-manipulation for Cryptography

为加密算法提供专用位操作:

1755227930095

7 Zbkc : Carry-less multiplication for Cryptography

精简版Zbc,仅包含clmul和clmulh,专为GHASH(AES-GCM)优化:

1755227996969

8 Zbkx : Crossbar permutations

实现跨字节/半字节的查找表操作,用于加密中的S盒替换:

1755228025846

9 优化案例

strlen(字符串长度计算)

strcmp(字符串比较)