Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Melzzzzz <mel@zzzzz.com> |
|---|---|
| Newsgroups | comp.lang.c++, comp.lang.c |
| Subject | Re: Why compiler is able to vectorize on int but not on unsigned |
| Date | 2014-12-05 10:07 +0100 |
| Organization | albasani.net |
| Message-ID | <20141205100724.53b8f315@maxa-pc> (permalink) |
| References | <20141203111909.5ec8c7fc@maxa-pc> <87a934k4b0.fsf@dpt-info.u-strasbg.fr> <547F52D5.8020900@verizon.net> <8761drjfdq.fsf@dpt-info.u-strasbg.fr> |
Cross-posted to 2 groups.
On Thu, 04 Dec 2014 22:04:33 +0100
Alain Ketterlin <alain@dpt-info.u-strasbg.fr> wrote:
> James Kuyper <jameskuyper@verizon.net> writes:
>
> > On 12/03/2014 12:53 PM, Alain Ketterlin wrote:
> >> Melzzzzz <mel@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.)
You are absolutely right. If I change function
to:
int testNormal(const int* ghs, const int* lhs,size_t sz) {
int max = 0;
int tempMax;
for (unsigned k = 0; k < sz; k++) {
tempMax = lhs[k] + ghs[k];
if (max < tempMax) {
max = tempMax;
}
}
return max;
}
it *does not* vectorize as well!
>
> 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.
Your explanation fits what happens with the code. It has to do
with unsigned is smaller then size_t and can overflow.
>
> -- Alain.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Why compiler is able to vectorize on int but not on unsigned Melzzzzz <mel@zzzzz.com> - 2014-12-03 11:19 +0100
Re: Why compiler is able to vectorize on int but not on unsigned James Kuyper <jameskuyper@verizon.net> - 2014-12-03 08:02 -0500
Re: Why compiler is able to vectorize on int but not on unsigned jacob navia <jacob@spamsink.net> - 2014-12-03 14:57 +0100
Re: Why compiler is able to vectorize on int but not on unsigned Tim Prince <tprince@computer.org> - 2014-12-03 09:23 -0500
Re: Why compiler is able to vectorize on int but not on unsigned James Kuyper <jameskuyper@verizon.net> - 2014-12-03 10:03 -0500
Re: Why compiler is able to vectorize on int but not on unsigned Victor Bazarov <v.bazarov@comcast.invalid> - 2014-12-03 09:24 -0500
Re: Why compiler is able to vectorize on int but not on unsigned Richard Heathfield <invalid@see.sig.invalid> - 2014-12-03 14:36 +0000
Re: Why compiler is able to vectorize on int but not on unsigned Melzzzzz <mel@zzzzz.com> - 2014-12-03 16:03 +0100
Re: Why compiler is able to vectorize on int but not on unsigned Victor Bazarov <v.bazarov@comcast.invalid> - 2014-12-03 10:27 -0500
Re: Why compiler is able to vectorize on int but not on unsigned Melzzzzz <mel@zzzzz.com> - 2014-12-03 16:43 +0100
Why compiler is able to vectorize on int but not on unsigned "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2014-12-03 06:35 -0800
Re: Why compiler is able to vectorize on int but not on unsigned Melzzzzz <mel@zzzzz.com> - 2014-12-03 16:09 +0100
Re: Why compiler is able to vectorize on int but not on unsigned Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2014-12-03 18:53 +0100
Re: Why compiler is able to vectorize on int but not on unsigned James Kuyper <jameskuyper@verizon.net> - 2014-12-03 13:13 -0500
Re: Why compiler is able to vectorize on int but not on unsigned Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2014-12-04 22:04 +0100
Re: Why compiler is able to vectorize on int but not on unsigned Melzzzzz <mel@zzzzz.com> - 2014-12-05 10:07 +0100
Re: Why compiler is able to vectorize on int but not on unsigned Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2014-12-05 10:42 +0100
Re: Why compiler is able to vectorize on int but not on unsigned Melzzzzz <mel@zzzzz.com> - 2014-12-05 11:29 +0100
csiph-web