Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast multiplication of values in an array

Is there a fast way to multiply values of a float array in C++, to optimize this function (where count is a multiple of 4):

void multiply(float* values, float factor, int count)
{
    for(int i=0; i < count; i++)
    {
        *value *= factor;
        value++;
    }
}

A solution must work on Mac OS X and Windows, Intel and non-Intel. Think SSE, vectorization, compiler (gcc vs. MSVC).

like image 690
Jakob Avatar asked Sep 09 '10 11:09

Jakob


People also ask

How do you multiply two values in an array?

How do you multiply two arrays together? C = A . * B multiplies arrays A and B by multiplying corresponding elements. The sizes of A and B must be the same or be compatible.

How do you multiply an array by itself?

You don't need to make a copy of the array if you just want to square it, you can just multiply each entry with itself and store it where you read it. Additionally, you use index < 10 as a condition, but it's better to have it dynamic by reading the length (the number of elements) of the input array arr .

Can you multiply an array?

A multiplication array is simply an arrangement of rows or columns that matches a multiplication equation. You can make arrays out of objects or pictures, and you can use any sort of shape. For example, here are 3 different arrays that all show 3 × 4.


2 Answers

If you want your code to be cross-platform, then either you're going to have to write platform-independent code, or you're going to have to write a load of #ifdefs.

Have you tried some manual loop unrolling, and seeing if it makes any difference?

like image 141
Oliver Charlesworth Avatar answered Oct 12 '22 23:10

Oliver Charlesworth


Since you know the count is a multiple of 4, you can unroll your loop...

void multiply(float* values, float factor, int count)
{
    count = count >> 2; // count / 4
    for(int i=0; i < count ; i++)
    {
        *value *= factor;
        *(value+1) *= factor;
        *(value+2) *= factor;
        *(value+3) *= factor;
        value += 4;
    }
}
like image 42
st0le Avatar answered Oct 12 '22 23:10

st0le