[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-gsl] array, vector indices out-of-bounds in "linalg/bidiag.c"

From: Brian Gough
Subject: Re: [Bug-gsl] array, vector indices out-of-bounds in "linalg/bidiag.c"
Date: Wed, 23 Apr 2008 15:14:07 +0100
User-agent: Wanderlust/2.14.0 (Africa) Emacs/22.1 Mule/5.0 (SAKAKI)

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 &&

Brian Gough

reply via email to

[Prev in Thread] Current Thread [Next in Thread]