no noalias not negligible - a difference between C and Fortran - long

ark at alice.UUCP ark at alice.UUCP
Sun May 22 13:00:15 AEST 1988


In article <54080 at sun.uucp>, dgh at dgh.UUCP writes:

> What is to be done?

Before writing off C for lacking noalias, try working on the
program a little harder.  Here is the original program:

	daxpy(n, da, dx, dy )
		double          dx[], dy[], da;
		int             n;
	{
		int             i;

		i = 0;
		do {
			dy[i] = dy[i] + da * dx[i];
		} while (i++ < n);
	}

I don't really think there's much here that noalias would help:
the object code has to fetch dy[i], add da, multiply by
dx[i], and store it in dy[i] and aliasing is irrelevant to
each of these operations.

Four things could speed it up, though, on many C implementations:

	1. Make `i', `n', and `da' into register variables.

	2. Use pointers instead of subscripts.

	3. Use the += operator to avoid fetching and then storing dy[i].

	4. Replace i++<n by the faster, but equivalent, ++i<=n.

Doing all these things gives the following revised program:

	daxpy(n, da, dx, dy)
		register double *dx, *dy, da;
		int n;
	{
		register double *dylim = dy + n;

		do *dy += da * *dx++;
		while (++dy <= dylim);
	}

Why not try this and let us know the results?



More information about the Comp.lang.c mailing list