public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Doug Evans <dje@google.com>
To: Yao Qi <yao@codesourcery.com>
Cc: Joel Brobecker <brobecker@adacore.com>,
	gdb-patches <gdb-patches@sourceware.org>
Subject: Re: [PATCH] Clear upper bits during sign extension
Date: Mon, 29 Dec 2014 05:29:00 -0000	[thread overview]
Message-ID: <CADPb22S97uP=UbNp4XZvoLtrN0msnFNwEKxoHU88nrDMpwx=rQ@mail.gmail.com> (raw)
In-Reply-To: <87d273w2gl.fsf@codesourcery.com>

On Sun, Dec 28, 2014 at 7:37 PM, Yao Qi <yao@codesourcery.com> wrote:
> Joel Brobecker <brobecker@adacore.com> writes:
>
>> I think you can simplify the above with just:
>>
>>         value &= ((LONGEST) 1 << bit) - 1;
>>
>> ? I don't know if the cast to LONGEST is really necessary, but we use
>> it for signbit, so I did the same for the mask...
>
> Yeah, that is better, or even we can left shift signbit by one.
>
> --
> Yao (齐尧)
>
> gdb:
>
> 2014-12-29  Yao Qi  <yao@codesourcery.com>
>
>         * utils.c (gdb_sign_extend): Clear bits from BIT in VALUE.
>
> diff --git a/gdb/utils.c b/gdb/utils.c
> index 47adb67..61873f7 100644
> --- a/gdb/utils.c
> +++ b/gdb/utils.c
> @@ -3032,6 +3032,9 @@ gdb_sign_extend (LONGEST value, int bit)
>      {
>        LONGEST signbit = ((LONGEST) 1) << (bit - 1);
>
> +      /* Clear upper bits from bit BIT.  */
> +      value &= (signbit << 1) - 1;
> +
>        value = (value ^ signbit) - signbit;
>      }

It's not immediately clear to this reader that undefined behaviour is
avoided here.
E.g., what if sizeof (LONGEST) == 8 && bit == 64.

There's an assert at the beginning of the function that bit could be 64.

  parent reply	other threads:[~2014-12-29  5:29 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-29  1:13 Yao Qi
2014-12-29  3:07 ` Joel Brobecker
2014-12-29  3:38   ` Yao Qi
2014-12-29  3:53     ` Joel Brobecker
2014-12-29  5:29     ` Doug Evans [this message]
2014-12-29  6:27       ` Yao Qi
2014-12-29 10:48 ` Pedro Alves
2014-12-30  9:20   ` Yao Qi
2014-12-30 12:20     ` Pedro Alves
2014-12-30 13:47       ` Yao Qi
2015-01-08  5:40         ` Yao Qi
2015-01-08 10:42           ` Pedro Alves
2015-01-08 13:06             ` Yao Qi

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='CADPb22S97uP=UbNp4XZvoLtrN0msnFNwEKxoHU88nrDMpwx=rQ@mail.gmail.com' \
    --to=dje@google.com \
    --cc=brobecker@adacore.com \
    --cc=gdb-patches@sourceware.org \
    --cc=yao@codesourcery.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).