public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Pedro Pedruzzi <pedro.pedruzzi@gmail.com>
To: Ulf Magnusson <ulfalizer@gmail.com>
Cc: Miles Bader <miles@gnu.org>, gcc@gcc.gnu.org
Subject: Re: Option to make unsigned->signed conversion always well-defined?
Date: Thu, 06 Oct 2011 21:31:00 -0000	[thread overview]
Message-ID: <4E8DDA85.4090302@gmail.com> (raw)
In-Reply-To: <CAFkk2KQQaAsq3ci5Quq6tN3bn8JSsDJiZBea+51RDzkdNuGm=A@mail.gmail.com>

On Thu, Oct 6, 2011 at 11:04 AM, Miles Bader <miles@gnu.org> wrote:
> How about:
>
>   bool overflowbit2(unsigned int a, unsigned int b)
>   {
>       const unsigned int sum = a + b;
>       return ~(a ^ b) & sum & 0x80;
>   }
>
> ?
>
> I thinnnnk it has the same results as your function...
> [I just made a table of all 8 possibilities, and checked!]

Miles, it is not the same. Take for example (0xff, 0xff). In 8-bit 2's
complement, this is (-1, -1) and does not overflow. Your function says
it does.


Em 06-10-2011 12:23, Jeremy Hall escreveu:
> bool overflow(int16_t a, int16_t b)
> {
>    const int16_t sum = a + b;
>    return sum > INT8_MAX || sum < INT8_MIN;
> }

Jeremy, here you are ignoring the problem of converting from the
unsigned int (in the range 0 to 0xff) to the signed integer that it
represents in 8-bit two's complement. Example: 0xff -> -1.

In practice, casting the unsigned int to int8_t works in most cases, but
it is compiler-defined. We are trying to find a always well-defined
approach that is efficient as well.


> Ops, should have been
> 
> return ~(a ^ b) & (a ^ sum) & 0x80
> 
> ~(a ^ b) gives 1 in the sign bit position if the signs are the same,
> and (a ^ sum) gives 1 if it's different in the sum.

This is good. Do you think this is suboptimal? How are you evaluating
efficiency? In x86 this generates pretty small code.

<overflow2>:
  400524:   8d 04 3e                lea    (%rsi,%rdi,1),%eax
  400527:   31 f8                   xor    %edi,%eax
  400529:   31 f7                   xor    %esi,%edi
  40052b:   f7 d7                   not    %edi
  40052d:   21 f8                   and    %edi,%eax
  40052f:   25 80 00 00 00          and    $0x80,%eax
  400534:   c3                      retq

-- 
Pedro Pedruzzi

  reply	other threads:[~2011-10-06 16:43 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-05 22:56 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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4E8DDA85.4090302@gmail.com \
    --to=pedro.pedruzzi@gmail.com \
    --cc=gcc@gcc.gnu.org \
    --cc=miles@gnu.org \
    --cc=ulfalizer@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).