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

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

    If the attributes don't work, or aren't an option ....

    I'm not sure, but try this:

    void vecadd (float * restrict a, float * restrict b, float * restrict c)
    {
       a = __builtin_assume_aligned (a, 8);
       b = __builtin_assume_aligned (b, 8);
       c = __builtin_assume_aligned (c, 8);
    
       for ....
    

    That should tell GCC that the pointers are aligned. From that whether it does what you want depends on whether the compiler can use that information effectively; it might not be smart enough: these optimizations aren't easy.

    Another option might be to wrap the float inside a union that must be 8-byte aligned:

    typedef union {
      float f;
      long long dummy;
    } aligned_float;
    
    void vedadd (aligned_float * a, ......
    

    I think that should enforce 8-byte alignment, but again, I don't know if the compiler is smart enough to use it.

提交回复
热议问题