Path: utzoo!mnetor!uunet!mcvax!dik From: dik@cwi.nl (Dik T. Winter) Newsgroups: comp.lang.c Subject: Re: More on noalias Message-ID: <7488@boring.cwi.nl> Date: 12 Jan 88 22:28:52 GMT References: <10129@mimsy.UUCP> <3433@megaron.arizona.edu> Organization: CWI, Amsterdam Lines: 49 In article <3433@megaron.arizona.edu> mike@arizona.edu (Mike Coffin) writes: > void > vector_add /* maybe */ (int *a, int i, int j, int k, int n) > { > if (i==j || i==k) return; > > /* now the compiler can infer that i!=j and i!=k */ > for (; --n >= 0; i++, j++, k++) > a[i] = a[j] + a[k]; > } > and supposes that this allows vectorization (just like the parent article by Chris Torek). First the conditions are wrong (if i equals both j and k, vectorization is possible; no vectorization is possible if (j-n) < i < j, or similar for i and k). But that is beside the point; the equivalent Fortran code will not vectorize on any machine that I know (Cray, CDC 205, NEC SX/2, Fujitsu), because of this aliasing problem. A better example is: void vector_add /* maybe */ (int *a, *b, *c, int n) { int i; for(i = 0; i < n; i++) a[i] = b[i] + c[i]; } This will vectorize in Fortran, because the standard states that aliasing is not allowed here if a is involved. It will not vectorize in C. Adding a noalias for a will solve the problem (if I understand it right). Of course the statement if((a > b && a < b+n) || (a > c && a < c+n)) exit(1); (or EXIT_FAILURE) will help also, but the existence of such a statement (or a similar one) is very difficult to check, because although in this case the condition is extremely simple, it might be much more complex in other cases. (What is the condition if the loop reads: int i, j; for(i = 0, j = 0; i < n; j += i, i++) a[j] = b[j] + c[j]; not very common, but also not unheard of. It vectorizes on a number of machines.) Note that this example also clarifies Doug Gwyn's remark that noalias changes the semantics. Especially if the programmer lies. (Without noalias the result is well-defined, with it is undefined.) -- dik t. winter, cwi, amsterdam, nederland INTERNET : dik@cwi.nl BITNET/EARN: dik@mcvax