James Kuyper <
james...@verizon.net> writes:
> On 12/03/2014 12:53 PM, Alain Ketterlin wrote:
>> Melzzzzz <
m...@zzzzz.com> writes:
>>
>>> int testNormal(const vector_t & ghs, const vector_t & lhs) {
>>> int max = 0;
>>> int tempMax;
>>> for (unsigned k = 0; k < ghs.size(); k++) {
>>> tempMax = lhs[k] + ghs[k];
>>> if (max < tempMax) {
>>> max = tempMax;
>>> }
>>> }
>>> return max;
>>> }
>>>
>>> where: typedef vector<int> vector_t;
>>> (could be also plain array so I post this to clc as well)
>>
>> A plain array would have been better, because the explanation has
>> nothing to do with vectors/templates/whatnot
Sorry for the late answer, I was busy.
> He's already posted (nearly three hours ago) a message indicating that
> the problem does not occur when using plain arrays,
You're right. Some people stop reading in the middle of posts when they
meet a template. I admit I did stop reading in the middle of the thread
for lack of time.
(I come back to plain arrays below.)
> so the explanation must indeed have something to do with
> vectors/templates/whatnot - to be more precise, it seems to be
> connected to the use of ghs.size().
And your suggestion is?
Here is mine: ghs.size() (probably) has return type size_t, which is
(probably) bigger than unsigned on the target machine. (Actually size()
return size_type which is typedefed inside vector, usually as size_t bt
I'm not sure this is mandatory.)
Therefore, the loop may never end (if k wraps around---which it is
allowed to when unsigned), and this is enough for the compiler to decide
*not* to vectorize (because manipulating the loop trip count in that
case is tricky). The same thing does not happen when k is signed: this
is explained in the link I posted earlier.
So, why does it work with plain arrays? Well, it does not, but
Melzzzzz's modified program was vectorized because the size was now an
unsigned (not a size_t) coming in as a parameter, and the risk of
overflow during iteration was gone.
So why do I say it does not work with a "plain-array-like" version of
vector? Because a (rough) translation of vector<int> using a plain array
would be a structure like:
struct vectorlike {
size_t sz;
int * data;
};
and bam! the same would happen in C if the sz field were used as a loop
bound. Also, in the version of Melzzzzz if the parameters were "real"
plain arrays but the size were passed in as a size_t (instead of
unsigned, as it should I think), the same would happen. (And this make
the cross-posting to comp.lang.c not completely irrelevant.)
I'm speculating, because I haven't looked into the respective
vectorizers, but my experience with that unsigned/signed mess in
compilers makes me reasonably sure the problem lies somewhere there. I
maintain what I said, but of course if James or anybody else has a
better explanation I would love to read it.
-- Alain.