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

Robert Firth firth at sei.cmu.edu
Thu Jan 8 06:50:40 AEST 1987


In article <2197 at brl-adm.ARPA> rgenter at j.bbn.com (Rick Genter) writes:
>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.
...
>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.

If you really are implementing the VAX G data type, you must first
check whether X is a reserved operand.  That's another couple of
instructions.

By the way, you should clear not the high bit of X but the high bit
of the low-order longword.



More information about the Comp.lang.c mailing list