jeudi 21 mai 2020

How do I create a static library with GCC compiler optimization?

I have a function to compute dot product in C++. I want to compile this function with -O3 compiler optimization. Rest of the codes in my codebase are compiled with -O0. To do this, I have created a static library that contains the function and compiled the library with -O3. Then I have linked the library to my code. But I am not getting the optimization from my library.

The library is compiled with g++ -O3 -std=c++11 -c optimized.cpp and then archived with ar rcs libfast.a optimized.o.

I link this library like this: g++ main.cpp libfast.a -std=c++11. Why is the library not giving me the optimization? What is the other way i can compile my function with -O3 optimization?

Here is the function that I want to optimize:

int multiply(__uint128_t *X1, __uint128_t *Y1, __uint128_t &ans, int input_length)
{
    int i=0;
    ans = 0;
    uint128 a, b, c, d;

    if (input_length > 4)
    {
        for (; i < input_length - 4; i += 4)
        {
            a = X1[i] * Y1[i];
            b = X1[i + 1] * Y1[i + 1];
            c = X1[i + 2] * Y1[i + 2];
            d = X1[i + 3] * Y1[i + 3];
            ans += a + b + c + d;
        }
    }

    for (; i < input_length; i++)
    {
        ans += X1[i] * Y1[i];
    }

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire