This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Inline ASM bug


Hi,

I've run into a slight problem with gcc version 3.2.3. It looks like the automatic register allocation in the extended ASM doesn't work the way it should.
The function maxElement (see below) needs 3 registers, from which 2 are chosen by gcc (array and maximum).
The bug (?) is the following: array and maximum receive the same register (edx in my case).


A programm which illustrates the problem is given below: the maximal value should be 3, but 0 is returned.
Removing "r" from array and maximum and replacing it with "S" and "D" respectivly solves the problem.


------------------------- bug.c -------------------------
#include <stdlib.h>
#include <stdio.h>

#define UINT unsigned int
#define SIZE 4096

float maxElement(float *array, int n) {
    float *maximum, max;
    posix_memalign((void**) &maximum, 16, 16);
    int i = n >> 4; // n/16
    n &= 15; // n%16

    __asm__ __volatile__ (
        "movaps (%0), %%xmm0\n"
        "loop_max_el:\n"
        "movaps (%0), %%xmm1\n"
        "movaps 16(%0), %%xmm2\n"
        "movaps 32(%0), %%xmm3\n"
        "movaps 48(%0), %%xmm4\n"
        "maxps %%xmm4, %%xmm3\n"
        "maxps %%xmm3, %%xmm2\n"
        "maxps %%xmm2, %%xmm1\n"
        "maxps %%xmm1, %%xmm0\n"
        "addl $64, %0\n"
        "subl $1, %%ecx\n"
        "jnz loop_max_el\n"
        "movaps %%xmm0, (%1)\n"
        : "=r" (array)
        : "0" (array), "r" (maximum), "c" (i)
        : "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "memory"
    );

    max = maximum[0];
    for (i=1;i<4;i++) // search for the maximal element
            max = maximum[i]>max?maximum[i]:max;
    return max;
}

int main(int argc, char** argv) {
    float *array, retval;
    posix_memalign((void**) &array, 16, sizeof(*array) * SIZE);

    int i;
    for (i=0;i<SIZE;i++) {
        array[i] = (i%2)==0?2.0:3.0;
    }

    retval = maxElement(array, SIZE);
    printf("value: %f\n", retval);

    return EXIT_SUCCESS;
}
------------------------------------------------------------

This code is generated by compiling with "gcc -c -S bug.c" on my Pentium 4.
[...]
#APP
movaps (%edx), %xmm0 // array = edx
loop_max_el:
movaps (%edx), %xmm1
movaps 16(%edx), %xmm2
movaps 32(%edx), %xmm3
movaps 48(%edx), %xmm4
maxps %xmm4, %xmm3
maxps %xmm3, %xmm2
maxps %xmm2, %xmm1
maxps %xmm1, %xmm0
addl $64, %edx
subl $1, %ecx
jnz loop_max_el
movaps %xmm0, (%edx) // maximum = edx
#NO_APP
[...]


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]