logoalt Hacker News

timerol • yesterday at 7:35 PM • 0 replies • view on HN

Loading a float value of 3.1 does not have only one translation into assembly. On Godbolt, flipping through various ARM 32-bit compilers, I define a function that returns 3.1 as a float:

    float constant(void) {
      return 3.1f;
    }
And can get a handful of different results. I trimmed the function setup and teardown to simplify things.

Option 1 (loading high 16 bits and low 16 bits, then moving to fp register):

    movw     r3, #26214
    movt     r3, 16454
    vmov     s15, r3
    vmov.f32 s0, s15
Option 2 (hardcoding the hex value into program text and loading it into r0 via r3, no specific fp conversion):

    ldr     r3, .L3
    mov     r0, r3
    .L3:
        .word   1078355558
Option 3 (similar to 2, but using the load for SIMD and FP registers):

    vldr.32 s0, .L3
    .L3:
        .word   1078355558