How to add two floating point numbers using inline x86 assembly?

Viewed 194

First: I know this is pointless, I'm going to write more complex stuff later (it's an assignment), this is a starting point.

Here is the code I wrote:

#![feature(asm)]
fn my_add(a: f32, b: f32) -> f32 {
    let x: f32;
    unsafe {
        asm!(
        "addss {0}, {1}",
        inlateout(xmm_reg) a => x,
        in(xmm_reg) b,
        );
    };
    x
}

This compiles to:

example::my_add:
        sub     rsp, 4

        addss   xmm0, xmm1

        movss   dword ptr [rsp], xmm0
        movss   xmm0, dword ptr [rsp]
        add     rsp, 4
        ret

Where does these movss come from ? Why is it writing to memory and then reading it again ?

Moreover, this only works for f32 and not for f64, and I can't see why: the return value is the value of the first argument I failed to notice that I was using addss instead of addsd for f64.

I built this with rustc 1.57.0-nightly (25ec82738 2021-10-05).

0 Answers
Related