Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!seismo!brl-adm!adm!rgenter@j.bbn.com
From: rgenter@j.bbn.com (Rick Genter)
Newsgroups: comp.lang.c
Subject: Re: fabs(x) vs. (x) < 0 ? -(x) : (x)
Message-ID: <2197@brl-adm.ARPA>
Date: Wed, 7-Jan-87 13:45:00 EST
Article-I.D.: brl-adm.2197
Posted: Wed Jan  7 13:45:00 1987
Date-Received: Wed, 7-Jan-87 23:06:14 EST
Sender: news@brl-adm.ARPA
Lines: 39

On machines which do not support floating-point instructions,
libraries which implement the DEC G-format floating point (I think
that's right) format can implement fabs() much more efficiently as a
subroutine than as a macro.  If you implement it as a macro you'll end
up generating something like:

	push	address of x
	push	address of constant 0
	call	fp-compare
	jumpge	L1
	push	address of x
	push	address of temp
	call	fp-negate-copy
	jump	L2

L1:
	push	address of x
	push	address of temp
	call	fp-copy

L2:

whereas the routine fabs() could be:

fabs:
	bitclr	x,#BIT_HIGH
	return

Obviously optimizations can be made depending on the intelligence of
your compiler, whether registers (or register-pairs) can hold floating
point values, etc.  Still, it would be hard to beat a single bit-clear
(or AND) operation.

					- Rick
--------
Rick Genter 				BBN Laboratories Inc.
(617) 497-3848				10 Moulton St.  6/512
rgenter@bbn.COM  (Internet new)		Cambridge, MA   02238
rgenter@bbnj.ARPA (Internet old)	seismo!bbn.com!rgenter (UUCP)