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

mouse at mcgill-vision.UUCP mouse at mcgill-vision.UUCP
Wed Feb 11 17:02:48 AEST 1987


In article <1315 at ho95e.ATT.COM>, wcs at ho95e.ATT.COM (#Bill.Stewart) writes:
> 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).

> 	#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.

I would be very surprised to find the SVR2 (ho95e *is* SVR2 on a 3B20,
isn't it?) C compiler that badly broken.  Or maybe you just posted
without bothering to test?  Anyway....

	% cat -n x.c
	     1	#define foo(x) ((x) & 0xffff7fff)
	     2	
	     3	main()
	     4	{
	     5	 float f;
	     6	
	     7	 f = -12.34;
	     8	 f = foo(f);
	     9	 printf("f = %g\n",f);
	    10	}
	% cc x.c
	"x.c", line 8: operands of & have incompatible types
	%

>> 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.

I would really like a C compiler that produces anything that could
*touch* the "Kahan's magic square root" code of the 4.3 VAX libm.  Or
the generation of exceptions in the correct manner, which usually
requires code the compiler simply can't generate.  You wind up writing
it in assembly and putting it in a C file full of asm()s, which is an
exercise in stupidity.  I tend to agree with braner.

Or is your definition of an "adequate" compiler one which can recognize
"oh, this code is trying to do a square root, so let's plug in the good
square root code"?  I think you will find there aren't any such.

					der Mouse

USA: {ihnp4,decvax,akgua,utzoo,etc}!utcsri!mcgill-vision!mouse
     think!mosart!mcgill-vision!mouse
Europe: mcvax!decvax!utcsri!mcgill-vision!mouse
ARPAnet: think!mosart!mcgill-vision!mouse at harvard.harvard.edu



More information about the Comp.lang.c mailing list