Is this a guaranteed signed/unsigned conversion?



Consider:

#include <stdlib.h>

/* An elementary optimizer is expected to remove most code */
/* Return the absolute value */
unsigned long ulabs(long j)
{
if (0 == LONG_MIN + LONG_MAX) {
if (j < 0) return -j;
else return j;
}
else if (LONG_MIN == j) return 1U + j + ULONG_MAX;
else if (j < 0) return -j;
else return j;
} /* ulabs */

Is this guaranteed to convert all long values to their absolute
unsigned long equivalent, assuming that the desired result is
representable as an unsigned long. Does it work over 2's, 1's
complement and sign-magnitude? Is there a better method? labs()
is not guaranteed.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

.



Relevant Pages

  • Re: Why is there no difference between buffered and unbuffered file IO?
    ... "If you want to post a followup via groups.google.com, ... the broken "Reply" link at the bottom of the article. ... hardware. ... Prev by Date: ...
    (comp.lang.c)
  • Re: Help Needed - LWP Redirect problemss
    ... If you want to post a followup via groups.google.com, ... don't use the broken "Reply" link at the bottom of the ... Sinan Unur ... Prev by Date: ...
    (comp.lang.perl.misc)
  • Re: auto-stretching in IE
    ... > position: abolute; ... > bottom: 24px; ... BTW, you misspelled "absolute", which could be a major part of your ... Prev by Date: ...
    (alt.html)
  • Re: Embedded Ethernet and Corporate LAN
    ... Off Topic - Google Groups ... "If you want to post a followup via groups.google.com, ... the broken "Reply" link at the bottom of the article. ... Prev by Date: ...
    (comp.dcom.lans.ethernet)
  • Re: help me in port programming
    ... mmquan wrote: ... "If you want to post a followup via groups.google.com, ... the broken "Reply" link at the bottom of the article. ... Prev by Date: ...
    (comp.lang.c)