Newsgroups: comp.lang.c Path: utzoo!utgpu!news-server.csri.toronto.edu!helios.physics.utoronto.ca!aurora.physics.utoronto.ca!neufeld From: neufeld@aurora.physics.utoronto.ca (Christopher Neufeld) Subject: Re: Divide and C Message-ID: <1991Mar29.182919.20728@helios.physics.utoronto.ca> Sender: news@helios.physics.utoronto.ca (News Administrator) Nntp-Posting-Host: aurora.physics.utoronto.ca Organization: University of Toronto Physics/Astronomy/CITA References: <1991Mar27.185804.7221@uunet.uu.net> <639@taumet.com> <1991Mar29.163422.14672@uunet.uu.net> Date: Fri, 29 Mar 1991 18:29:19 GMT 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@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!"