AArch64 Bitfield Move (BFM) Instruction

The sixth part of my series on decoding UTF-8 was a pretty uninteresting one: in it, I described a basic non-validating UTF-8 decoder. When examining the generated assembly, I saw three different instructions for shifting bits: LSL, BFI and BFXIL. As it turns out, BFI and BFXIL are both aliases of the same instruction: BFM - bitfield move. I decided to learn more about BFM and share my findings here 1.

A little history first (shocking, I know). Arm32 had the following instructions for moving bitfields: BFI, BFC, UBFX, and SBFX. Unlike their Arm64 namesakes, they are instructions on their own and not aliases of other instructions. The 64-bit Arm introduces three separate, but related instructions: BFM, UBFM and SBFM. There are a number of aliases that are easier to use and are implemented in terms of the three basic instructions.

If I really knew anything about chip design, I would probably speculate that all three core bitfield move instructions share most of the common implementation, but as a software person, I’ll leave the implementation details to the knowledgeable folks.

To keep the post relatively short, I’ll focus on the BFM instruction. Once we understand how it works, the rest of them will be easy enough to understand from the documentation.

BFM - Bitfield Move

The BFM instruction copies a bitfield (a collection of adjacent bits within a machine word) from the source register to a position in the destination register without changing any other bits.

According to the official documentation:

Bitfield Move copies any number of low-order bits from a source register into the same number of adjacent bits at any position in the destination register, leaving other bits unchanged.

BFM Rd, Rn, #immr, #imms

Where:

Rd is the destination register ( Xd for the 64-bit operation, Wd for the 32-bit variant).

Rn is the source register ( Xn for the 64-bit operation, Wn for the 32-bit variant).

immr is the right rotate amount.

imms is the leftmost bit number to b…

添加评论
点赞收藏
点踩分享查看原文
评论
?
参与讨论