0

Is there a way on aarch64 to load a single byte from memory into a register without zero extending it? I know LDRB for example zero extends.

4
  • 2
    Just to clarify: You want to merge it with whatever is in the upper part of the register?
    – Homer512
    Jun 20 at 16:04
  • 1
    ldrsb sign-extends instead if that's what you want. There is no single instruction to merge a byte from memory into another register, ARM64 is a RISC, not like x86 with its CISC partial-register shenanigans; mov al, [rdi] on modern x86 runs as a micro-fused load + ALU merge uop. On AArch64, load separately and bitfield-insert if that's what you want. (IDK the optimal way to do that.) Jun 20 at 17:36
  • 1
    re. what's the optimal bitfield insert: It's likely bfxil
    – Homer512
    Jun 20 at 17:54
  • What exactly do you mean by “loading 1 byte non zero extended?” Could you give an example?
    – fuz
    Jun 21 at 9:04

1 Answer 1

3

Not directly. A general principle of the ARM instruction set is that, for the majority of instructions, the destination register is completely overwritten. One benefit of this is that the instruction does not incur a read dependency on the destination register, making it more efficient to execute out-of-order.

So when you read a single byte from memory, the remaining 56 bits of the destination register has to be overwritten with something. Your options are:

ldrb w0, [x1]  // bits 8:63 of x0 are cleared
ldrsb w0, [x1] // bits 8:31 set equal to bit 7, bits 32:63 are cleared
ldrsb x0, [x1] // bits 8:63 set equal to bit 7

If you want to do something else with the high bits, you'll need one or more extra instructions. For instance, if you want your loaded byte in the low 8 bits of a particular register while keeping bits 8:63 the same, you could do

ldrb w2, [x1]
bfi x0, x2, #0, #8

You can insert it at some other position besides #0 if you want. But you do need a scratch register (here x2) for the load instruction itself. (Note bfi and its relatives are an exception to the principle mentioned above, in that they do "merge" into the destination register.)


On the SIMD side, it is possible to use ld1 to load into a particular lane of a SIMD register, keeping the remaining lanes unchanged. So you could do

ld1 v0.b[11], [x1]

to load a byte into element 11 of SIMD register 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.