public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Option to make unsigned->signed conversion always well-defined?
@ 2011-10-05 22:56 Ulf Magnusson
  2011-10-06  8:25 ` Ulf Magnusson
                   ` (3 more replies)
  0 siblings, 4 replies; 18+ messages in thread
From: Ulf Magnusson @ 2011-10-05 22:56 UTC (permalink / raw)
  To: gcc

Hi,

I've been experimenting with different methods for emulating the
signed overflow of an 8-bit CPU. The method I've found that seems to
generate the most efficient code on both ARM and x86 is

bool overflow(unsigned int a, unsigned int b) {
    const unsigned int sum = (int8_t)a + (int8_t)b;
    return (int8_t)sum != sum;
}

(The real function would probably be 'inline', of course. Regs are
stored in overlong variables, hence 'unsigned int'.)

Looking at the spec, it unfortunately seems the behavior of this
function is undefined, as it relies on signed int addition wrapping,
and that (int8_t)sum truncates bits. Is there some way to make this
guaranteed safe with GCC without resorting to inline asm? Locally
enabling -fwrap takes care of the addition, but that still leaves the
conversion.

/Ulf

^ permalink raw reply	[flat|nested] 18+ messages in thread
* Re: Option to make unsigned->signed conversion always well-defined?
@ 2011-10-06 18:24 Jeremy Hall
  0 siblings, 0 replies; 18+ messages in thread
From: Jeremy Hall @ 2011-10-06 18:24 UTC (permalink / raw)
  To: gcc

Hi.

Instead of all the clever bit twiddling I have used code similar to

   sum > UINT8_MAX

which just generates

      cmp  ax,255
      seta  al

which seems to be far more efficient (even the signed version gets optimized
down to the above single check).
Please could someone tell me if I have missed something here????

Signed check:

bool overflow(int16_t a, int16_t b)
{
   const int16_t sum = a + b;
   return sum > INT8_MAX || sum < INT8_MIN;
}

Unsigned check

bool overflow(uint16_t a, uint16_t b)
{
   const uint16_t sum = a + b;
   return sum > UINT8_MAX;
}

Jeremy

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2011-10-07 19:36 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-05 22:56 Option to make unsigned->signed conversion always well-defined? Ulf Magnusson
2011-10-06  8:25 ` Ulf Magnusson
2011-10-06  9:04 ` Pedro Pedruzzi
2011-10-06 10:52   ` Ulf Magnusson
2011-10-06 14:42     ` Ulf Magnusson
2011-10-06 14:45       ` Miles Bader
2011-10-06 14:48         ` Ulf Magnusson
2011-10-06 21:31           ` Pedro Pedruzzi
2011-10-07 11:52             ` Miles Bader
2011-10-07 17:20               ` Pedro Pedruzzi
2011-10-07 17:35                 ` Miles Bader
     [not found] ` <4E8DBF3C.1020700@redhat.com>
2011-10-06 22:08   ` Ulf Magnusson
2011-10-06 22:46 ` Florian Weimer
2011-10-07 19:36   ` Ulf Magnusson
2011-10-07 22:24     ` Florian Weimer
2011-10-07 22:48       ` Ulf Magnusson
2011-10-07 23:36         ` Florian Weimer
2011-10-06 18:24 Jeremy Hall

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).