fabs(x) vs. (x) < 0 ? -(x) : (x)

wcs at ho95e.UUCP wcs at ho95e.UUCP
Fri Feb 6 12:50:25 AEST 1987


In article <2181 at batcomputer.tn.cornell.edu> braner at batcomputer.UUCP (braner) writes:
>Somebody already said this a LONG time ago:  Any macro definition of
>fabs() requires floating-point arithmetic (e.g. comparision with 0).
>That is A LOT slower than a dedicated fabs() function, written in
>assembly language of course, that simply clears the sign bit.

	#define	fabs(x) ( (x) & 0x7FFF )

is probably a *lot* faster than your assembly language function, since
it doesn't need a function call.  You'll have to #ifdef it to get the right bit
pattern for your machine, and worry about details for double and double-extended
versions, but for no-more-bits-than-a-long floating point  it's ok.

>[various complaints about most C compilers' FP code quality]
Yeah, a lot of it's not exciting.  The standard complaint is that everything is
really done in double precision, when you can often get by with single.  I
suspect the 80*8* world is heading back in the PDP-11 direction though, since
the 8087 gives you fairly wide arithmetic.

>THE COMPLETE FP LIBRARY MUST BE WRITTEN IN HAND-OPTIMIZED ASSEMBLER LANGUAGE!!!
Only if your compilers are inadequate for your machines, though some
hand-tuning helps.
-- 
# Bill Stewart, AT&T Bell Labs 2G-202, Holmdel NJ 1-201-949-0705 ihnp4!ho95c!wcs



More information about the Comp.lang.c mailing list