🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.help
Article #249

Question on optimizing "abs()"

#249
From: Bob Schmid
Date: Fri, 14 Aug 1992 16:44
33 lines
1234 bytes
I would like to write code for gcc 2.2 that will take the absolute
value of a double in the fastest possible way (for a tight inner
loop).

I currently do this

  double num;
  if (num < 0)
	num *= -1;

but it occurs to me that rather than doing the test, possibly followed
by a multiply, it may be cheaper to just force the IEEE sign bit to be
positive somehow (just one operation).  Any suggestions on how best to
do this for a double, float or int will be most appreciated.

I'm running on a sun IPX, using gcc -O.



Thanks,

-Bob

------------------------------------------------------
R. Schmid     <uunet!cbmvax!schmid>
              <schmid@cbmvax.cbm.commodore.com>

Robert Schmid
VLSI Design
Commodore Technology
1200 Wilson Drive
West Chester, PA  19380
------------------------------------------------------
Technical Details
Message-ID: <34264@cbmvax.commodore.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!rutgers!cbmvax!schmid

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!rutgers!cbmvax!schmid
From: schmid@deneb.cbmvax.cbm.commodore.com (Bob Schmid)
Newsgroups: gnu.gcc.help
Subject: Question on optimizing "abs()"
Message-ID: <34264@cbmvax.commodore.com>
Date: 14 Aug 92 16:44:38 GMT
Sender: news@cbmvax.commodore.com
Distribution: gnu.gcc.help
Organization: Commodore Technology West Chester, PA
Lines: 33