Search code examples
user-defined-functionsvectorizationintelicc

How to allow user-defined functions in intel's auto-vectorization?


Assuming I have the following piece of code:

class Vec3
{
    // ... deleted, not needed...

    public:
        Vec3 operator+(const Vec3 &rh) const;
};

void test(Vec3 *a, Vec3 *b, Vec3 *c, size_t len)
{
    for(size_t i = 0; i < len; i++)
        c[i] = a[i] + b[i];
}

This seems to be an ordinary example for vectorization, but because operator+() is a user-defined function this loop will not auto-vectorize. I also tried adding __declspec(vector) as suggested here, but this leads to an compilation error:

error: return type with class/struct/union is not supported in vector function

Maybe I've missunderstood what this prefix is doing. How can I make the function vectorizable?

Thanks in advance!


Solution

  • A traditional function can only be converted when the parameter to the function falls in one of the following catagories (This is a current limitation documented at https://software.intel.com/sites/products/documentation/doclib/iss/2013/compiler/cpp-lin/GUID-42986DEF-8710-453A-9DAC-2086EE55F1F5.htm under "Restrictions on using vector declaration"):

    • (un)signed 8, 16, 32, or 64-bit integer
    • 32 or 64-bit floating point 64 or 128-bit complex
      a pointer (C++ reference is considered a pointer data type)

    Your program is using a user defined data type Vec3 which doesn't fall in the above four catagories.