At Tue, 22 Apr 2008 11:50:15 +0200,
Julian Seward wrote:
> Ok.  So it's clear that is what the original authors intended.
> And I fully sympathise with wanting to avoid unsigned integer 
> wraparound problems in such loops.
> 
> However, regarding 
> http://www.gnu.org/software/gsl/design/gsl-design.html#SEC31
> 
> not only is the clever version
> 
>   for (i = N; i > 0 && i--;) { ... }
> 
> confusing, it also generates worse code than the simple
> version:
> 
>   for (i = 0; i < N; i++) { j = N - i; ... }
> 

Thanks for that interesting analysis!  The loop unrolling point is a
good one which I must have overlooked at the time (although I don't
know if gcc did loop unrolling back then - it was almost ten years ago
I think).

That part as written when I was translating a lot of code from fortran
and I was trying to avoid introducing additional variables, to
minimise the number of indices I mentally needed to keep track of.

I've played with a few variations and found that the alternative

  for (i = N; i-- > 0;)  

does get unrolled (with gcc-4.2), while 

  for (i = N; i > 0 && i--; ) 

does not. Unfortunately the former is perhaps still somewhat
unintuitive, but I think I will update the design document and code to
use it.

I never really liked code that depends on the distinction between --i
and i--, being burned a few times by subtle bugs from not noticing the
wrong one being used, hence the tendency to write things like i>0 &&
i--.

-- 
Brian Gough


_______________________________________________
Bug-gsl mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/bug-gsl

Reply via email to