fabs(x) vs. (x) < 0 ? -(x) : (x)
Rick Genter
rgenter at j.bbn.com
Thu Jan 8 04:45:00 AEST 1987
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 at bbn.COM (Internet new) Cambridge, MA 02238
rgenter at bbnj.ARPA (Internet old) seismo!bbn.com!rgenter (UUCP)
More information about the Comp.lang.c
mailing list