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

firth at sei.cmu.edu.UUCP firth at sei.cmu.edu.UUCP
Mon Feb 9 23:46:38 AEST 1987


In article <2181 at batcomputer.tn.cornell.edu> braner at batcomputer.UUCP (braner) writes:
>THE COMPLETE FP LIBRARY MUST BE WRITTEN IN HAND-OPTIMIZED ASSEMBLER LANGUAGE!!!
>
>- Moshe Braner

Let me scoond this cry!  Most C mathlibs are indeed very bad.
I recall running a mathematical benchmark through a supposedly
optimising compiler and finding negligible difference in runtime
relative to the portable-C compiler generated code.  Turned out
the benchmark was spending 95% of its time computing sqrt.  We
recoded sqrt in Assembler and reduced its runtime by a factor
of 20.  As a bonus, it even returned the correct results.  I
then looked at sin, cos, log &c and... and you really don't
want to know what they were like.

As a very small nitpick with Moshe, your hand-optimised fabs
should of course check for infinity and NaNs rather than just
clearing the "sign bit".  On the PDP-11, for instance, the BIC
trick turns "reserved operand" into "zero", and some programmer
who cared about detecting uninitialised variables loses badly.



More information about the Comp.lang.c mailing list