LSL 但用 1 而不是 0 更新正确的位

Hol*_*dad 1 assembly arm64

我需要一个类似于 LSL 的指令,但右边的位必须填充 1 而不是 0。类似于:

mov x0, 1
XXX x0, 3 -> here I should have 1111 in x0.
Run Code Online (Sandbox Code Playgroud)

Unn*_*Unn 5

不幸的是,没有,没有一条指令可以做到这一点。从您的示例中,很难说您是否想要基于最低有效位(根据 LSb 的值是一或零)填充的算术右移之类的东西,还是总是用 1 而不是零填充。无论哪种方式,您都可以在 2/3 条指令中获得类似的结果:

MOV x0, #1

/* For the fill with LSb case */
RBIT x0, x0  /* reverse the bit order of the register */
ASR x0, x0, #3 /* use arithmetic right shift to do the shift, it will fill with the old LSb, now MSb */
RBIT x0, x0 /* fill bits back */ 

/* For the fill with 1s case */
MVN x0, x0 /* bitwise not the value of the register */
MVN x0, x0, LSL #3 /* shift the register value, filling with 0s, then invert the register again, restoring the original bits and flipping the filled 0s to 1s */

/* From the comments, it looks like OP wants the shift to come from another register and not a constant like in their post so the above needs an extra instruction */
MOV x1, #3 /* load the shift amount into a register */
MVN x0, x0
LSL x0, x0, x1 /* need a separate instruction to use a register instead of a constant as the shift amount */
MVN x0, x0
Run Code Online (Sandbox Code Playgroud)

  • 请注意,AArch64 灵活的第二个操作数仅适用于恒定移位计数。对于OP的运行时变量移位的实际问题(在注释中),您需要在“mvn”之间有一个单独的“lsl”指令。但好主意,这对于总指令数来说更好(对于关键路径延迟来说等于或更好),而不是我构建 ORR 值的想法。 (2认同)