views:

136

answers:

1

This problem is driving me a bit crazy. The code seems to be segmentation faulting for no good reason:

#define MULT_FLOAT4(X, Y) ({ \
    asm volatile ( \
        "movups (%0), %%xmm0\n\t" \
        "mulps (%1), %%xmm0\n\t" \
        "movups %%xmm0, (%1)" \
        :: "r" (X), "r" (Y)); })

int main(void)
{
    int before;
    float a[4] = { 10, 20, 30, 40 };
    float b[4] = { 0.1, 0.1, 0.1, 0.1 };

    /* Segmentation faults if I uncomment this line below, otherwise works fine. Why?? */
    int after;

    MULT_FLOAT4(a, b);

    return 0;
}

Note that so long as I have defined both the 'before' and 'after' variables it segmentation faults. If I just have 'before' or just have 'after' then it works fine.

I'm on Ubuntu Hardy (8.04), GCC version 4.2.4 (Ubuntu 4.2.4-1ubuntu4). Linux kernel: 2.6.24-16-generic.

+3  A: 

Check the address of a and b. I suspect you'll find that they must be aligned to a 16 byte boundary in order to avoid a segfault. Adding __ attribute __((aligned(16))) after their declarations should do the trick.

That's two underscores on each side of attribute and connected to it, BTW.

George Phillips
hmmm i thought movups was for unaligned data - and movaps was for aligned? :)
banister
it works! thanks! ...though im still confused as to why it's segfaulting when im using movups which specifically is for unaligned data?
banister
It's the mulps trying to access b. gdb should be able to show which instruction triggers the segfault.
George Phillips