Divide and C
Christopher Neufeld
neufeld at aurora.physics.utoronto.ca
Sat Mar 30 04:29:19 AEST 1991
On the subject of slow divides, I managed to cut ten percent off the
execution time of a program by doing division of floating points by
powers of two directly. My machine doesn't have a coprocessor (yet). I
wrote a test program to see just how much the divide was costing me:
for (i=1;i<=N;i++)
for (j=1;j<=N;j++)
a[i][j] = b[i][j] / 4.0;
with 'a' and 'b' pointers to pointers to doubles (64-bit IEEE in my
compiler).
I compared that to the following:
int *ptr;
double hold;
for (i=1;i<=N;i++)
for (j=1;j<=N;j++) {
hold = b[i][j];
ptr = (int *) &hold;
ptr += 3;
*ptr -= 0x20;
a[i][j] = hold;
}
and despite all the extra junk in there, it still worked almost three
times faster. Multiplication was faster by about a factor of two. In the
end, I didn't implement this in any code because it's obscure, to say
the least, and because it can fail badly if the operation is being
applied to a number less than about 2^(-1022), or about 1e-306.
Are there compilers which go to the trouble of hardcoding something
like this if they see division or multiplication by integer powers of
two, or do they all get lazy and assume a math coprocessor?
--
Christopher Neufeld....Just a graduate student | Flash: morning star seen
neufeld at aurora.physics.utoronto.ca Ad astra! | in evening! Baffled
cneufeld@{pnet91,pro-cco}.cts.com | astronomers: "could mean
"Don't edit reality for the sake of simplicity" | second coming of Elvis!"
More information about the Comp.lang.c
mailing list