Why unsigned types are more efficient in arm cpu?

前端 未结 3 1031
醉酒成梦
醉酒成梦 2020-12-01 17:10

I\'m reading an arm manual and come to this suggestion, but the reason is not mentioned.

Why unsigned types are faster?

3条回答
  •  -上瘾入骨i
    2020-12-01 17:46

    The only advantages of unsigned types I can think of are that division and modulo implementations may be slightly faster, and you can do tests like if (unsigned_value < limit) rather than if (signed_value >= 0 && signed_value < limit).

    I suspect your manual may be out of date. Any ARM in use today will have v4 or later of the instruction set, and I'm pretty sure that no instructions are faster or slower depending on signedness.

    On older ARMs, I believe that signed multiplication could be slower; I think that early termination only looked for all zeros in the top bits, not all ones, so multiplications involving negative numbers would always take the maximum time. Although this depended on the value, not on whether the type was signed or unsigned. On at least ARMv4 and later, early termination works for negative values.

    Also, I think very early ARMs couldn't load a single byte, only a word. So you'd need two instructions to load an unsigned byte, and three to load a signed one:

    ldr r0, [r1]
    and r0, r0, #0xff
    

    versus

    ldr r0, [r1]
    mov r0, r0, asl #24
    mov r0, r0, asr #24   ; but this could maybe be combined with later instructions
    

    versus (these days) ldrb r0, [r1] or ldrsb r0, [r1] to do a single-byte load.

    On a modern processor, it's very unlikely that using unsigned types will have a measurable impact on performance. Use whichever type makes most sense, then look at the code in detail once you've identified any performance bottlenecks.

提交回复
热议问题