Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!purdue!decwrl!sun-barr!lll-winken!xanth!mcnc!rti!xyzzy!tiktok!meissner From: meissner@tiktok.dg.com (Michael Meissner) Newsgroups: comp.lang.c Subject: Re: Type-independent ABS Keywords: abs labs Message-ID: <1752@xyzzy.UUCP> Date: 10 Oct 89 14:42:10 GMT References: <1392@cipc1.Dayton.NCR.COM> <20042@mimsy.UUCP> Sender: usenet@xyzzy.UUCP Reply-To: meissner@tiktok.UUCP (Michael Meissner) Organization: Data General (Languages @ Research Triangle Park, NC.) Lines: 32 In article <20042@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes: | In general, the latter is true. The proposed ANSI C standard requires | that side effects take place exactly once, which effectively prohibits | a straightforward macro version of abs() and labs(). One can still | write, e.g., | | int abs(int); | #define abs(x) __abs(x) | static __inline int __abs(int x) { return x < 0 ? -x : x; } | | in GCC. I have used this in to make putc() readable (`#ifdef'ed | on __GNUC__, of course). I know the above example is to demostrate the use of __inline, but with GNU C, it does support a builtin abs function (though for some reason it's not documented). You can simplify the above to: #ifdef __GNUC__ extern int abs(int); #define abs(x) __builtin_abs(x) #endif Some machines have fast ways of doing abs (as compared to the branch method above). Gnu C also has the following builtin functions related to abs: __builtin_labs(long) __builtin_fabs(double) -- Michael Meissner, Data General. Uucp: ...!mcnc!rti!xyzzy!meissner If compiles were much Internet: meissner@dg-rtp.DG.COM faster, when would we Old Internet: meissner%dg-rtp.DG.COM@relay.cs.net have time for netnews?