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

前端 未结 6 1673
花落未央
花落未央 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 06:58

    Alignment specifications usually only work for alignments that are smaller than the base type of a pointer, not larger.

    I think easiest is to declare your whole array with an alignment specification, something like

    typedef float myvector[16];
    typedef myvector alignedVector __attribute__((aligned (8));
    

    (The syntax might not be correct, I always have difficulties to know where to put these __attribute__s)

    And use that type throughout your code. For your function definition I'd try

    void vecadd(alignedVector * restrict a, alignedVector * restrict b, alignedVector * restrict c);
    

    This gives you an additional indirection but this is only syntax. Something like *a is just a noop and only reinterprets the pointer as a pointer to the first element.

提交回复
热议问题