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).
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.
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 .
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.
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 #ifdef
s.
Have you tried some manual loop unrolling, and seeing if it makes any difference?
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With