How to tell GCC that a pointer argument is always double-word-aligned?

前端 未结 6 1659
花落未央
花落未央 2020-12-01 06:37

In my program I have a function that does a simple vector addition c[0:15] = a[0:15] + b[0:15]. The function prototype is:

void vecadd(float * r         


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 07:17

    gcc versions have been dodgy about align() on simple typedefs and arrays. Typically to do what you want, you would have to wrap the float in a struct, and have the contained float have the alignment restriction.

    With operator overloading you can almost make this painless, but it does assume you can use c++ syntax.

    #include 
    #include 
    
    #define restrict __restrict__
    
    typedef float oldfloat8 __attribute__ ((aligned(8)));
    
    struct float8
    {
        float f __attribute__ ((aligned(8)));
    
        float8 &operator=(float _f) { f = _f; return *this; }
        float8 &operator=(double _f) { f = _f; return *this; }
        float8 &operator=(int _f) { f = _f; return *this; }
    
        operator float() { return f; }
    };
    
    int Myfunc(float8 * restrict a, float8 * restrict b, float8 * restrict c);
    
    int MyFunc(float8 * restrict a, float8 * restrict b, float8 * restrict c)
    {
        return *c = *a* *b;
    }
    
    int main(int argc, char **argv)
    {
        float8 a, b, c;
    
        float8 p[4];
    
        printf("sizeof(oldfloat8) == %d\n", (int)sizeof(oldfloat8));
        printf("sizeof(float8) == %d\n", (int)sizeof(float8));
    
        printf("addr p[0] == %p\n", &p[0] );
        printf("addr p[1] == %p\n", &p[1] );
    
        a = 2.0;
        b = 7.0;
        MyFunc( &a, &b, &c );
        return 0;
    }
    

提交回复
热议问题