[[ext:zbb]] === ext:zbb[] Extension for Basic Bit Manipulation ==== Logical with negate [[norm:Zbb_logical_insns_tbl]] [%header,cols="^1,^1,4,8"] |=== |RV32 |RV64 |Mnemonic |Instruction |{check} |{check} |insn:andn[rd,rs1,rs2] |<<#insns-andn>> |{check} |{check} |insn:orn[rd,rs1,rs2] |<<#insns-orn>> |{check} |{check} |insn:xnor[rd,rs1,rs2] |<<#insns-xnor>> |=== .Implementation Hint [NOTE, caption="Imp" ] =============================================================== The Logical with Negate instructions can be implemented by inverting the _rs2_ inputs to the base-required AND, OR, and XOR logic instructions. In some implementations, the inverter on rs2 used for subtraction can be reused for this purpose. =============================================================== ==== Count leading/trailing zero bits [[norm:Zbb_count_zeros_insns_tbl]] [%header,cols="^1,^1,4,8"] |=== |RV32 |RV64 |Mnemonic |Instruction |{check} |{check} |insn:clz[rd,rs] |<<#insns-clz>> | |{check} |insn:clzw[rd,rs] |<<#insns-clzw>> |{check} |{check} |insn:ctz[rd,rs] |<<#insns-ctz>> | |{check} |insn:ctzw[rd,rs] |<<#insns-ctzw>> |=== ==== Count population These instructions count the number of set bits (1-bits). This is also commonly referred to as population count. [[norm:Zbb_cpop_insns_tbl]] [%header,cols="^1,^1,4,8"] |=== |RV32 |RV64 |Mnemonic |Instruction |{check} |{check} |insn:cpop[rd,rs] |<<#insns-cpop>> | |{check} |insn:cpopw[rd,rs] |<<#insns-cpopw>> |=== ==== Integer minimum/maximum The integer minimum/maximum instructions are arithmetic R-type instructions that return the smaller/larger of two operands. [[norm:Zbb_min_max_insns_tbl]] [%header,cols="^1,^1,4,8"] |=== |RV32 |RV64 |Mnemonic |Instruction |{check} |{check} |insn:max[rd,rs1,rs2] |<<#insns-max>> |{check} |{check} |insn:maxu[rd,rs1,rs2] |<<#insns-maxu>> |{check} |{check} |insn:min[rd,rs1,rs2] |<<#insns-min>> |{check} |{check} |insn:minu[rd,rs1,rs2] |<<#insns-minu>> |=== ==== Sign extension and zero extension [#norm:Zbb_sign_zero_extension_function]#These instructions perform the sign extension or zero extension of the least-significant 8 bits or 16 bits of the source register.# These instructions replace the generalized idioms insn:slli[rd,rs,(XLEN-)] + insn:srai[rd,rd,(XLEN-)] for sign extension of 8-bit and 16-bit quantities and insn:slli[] + insn:srli[] for zero extension of 16-bit quantities. [[norm:Zbb_sext_zext_insns_tbl]] [%header,cols="^1,^1,4,8"] |=== |RV32 |RV64 |Mnemonic |Instruction |{check} |{check} |insn:sext.b[rd,rs] |<<#insns-sext_b>> |{check} |{check} |insn:sext.h[rd,rs] |<<#insns-sext_h>> |{check} |{check} |insn:zext.h[rd,rs] |<<#insns-zext_h>> |=== ==== Bitwise rotation Bitwise rotation instructions are similar to the shift-logical operations from the base spec. However, [#norm:Zbb_rotate_semantics]#where the shift-logical instructions shift in zeros, the rotate instructions shift in the bits that were shifted out of the other side of the value.# Such operations are also referred to as "`circular shifts`". [[norm:Zbb_rotate_insns_tbl]] [%header,cols="^1,^1,4,8"] |=== |RV32 |RV64 |Mnemonic |Instruction |{check} |{check} |insn:rol[rd,rs1,rs2] |<<#insns-rol>> | |{check} |insn:rolw[rd,rs1,rs2] |<<#insns-rolw>> |{check} |{check} |insn:ror[rd,rs1,rs2] |<<#insns-ror>> |{check} |{check} |insn:rori[rd,rs1,shamt] |<<#insns-rori>> | |{check} |insn:roriw[rd,rs1,shamt] |<<#insns-roriw>> | |{check} |insn:rorw[rd,rs1,rs2] |<<#insns-rorw>> |=== .Architecture Explanation [NOTE, caption="AE" ] =============================================================== The rotate instructions were included to replace a common four-instruction sequence to achieve the same effect (insn:neg[]; insn:sll[]/insn:srl[]; insn:srl[]/insn:sll[]; insn:or[]) =============================================================== ==== OR Combine [#norm:Zbb_orc-b_semantics]#insn:orc.b[] sets the bits of each byte in the result _rd_ to all zeros if no bit within the respective byte of _rs_ is set, or to all ones if any bit within the respective byte of _rs_ is set.# One use-case is string-processing functions, such as `strlen` and `strcpy`, which can use insn:orc.b[] to test for the terminating zero byte by counting the set bits in leading non-zero bytes in a word. [[norm:Zbb_orc-b_insns_tbl]] [%header,cols="^1,^1,4,8"] |=== |RV32 |RV64 |Mnemonic |Instruction |{check} |{check} |insn:orc.b[rd,rs] |<<#insns-orc_b>> |=== ==== Byte-reverse insn:rev8[] reverses the byte-ordering of _rs_. [%header,cols="^1,^1,4,8"] |==== |RV32 |RV64 |Mnemonic |Instruction |{check} |{check} |insn:rev8[rd,rs] |<<#insns-rev8>> |==== //