Message ID | 87d273w2gl.fsf@codesourcery.com |
---|---|
State | New, archived |
Headers |
Received: (qmail 12649 invoked by alias); 29 Dec 2014 03:38:13 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: <gdb-patches.sourceware.org> List-Unsubscribe: <mailto:gdb-patches-unsubscribe-##L=##H@sourceware.org> List-Subscribe: <mailto:gdb-patches-subscribe@sourceware.org> List-Archive: <http://sourceware.org/ml/gdb-patches/> List-Post: <mailto:gdb-patches@sourceware.org> List-Help: <mailto:gdb-patches-help@sourceware.org>, <http://sourceware.org/ml/#faqs> Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 12626 invoked by uid 89); 29 Dec 2014 03:38:10 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.7 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 X-HELO: relay1.mentorg.com Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 29 Dec 2014 03:38:07 +0000 Received: from svr-orw-fem-02x.mgc.mentorg.com ([147.34.96.206] helo=SVR-ORW-FEM-02.mgc.mentorg.com) by relay1.mentorg.com with esmtp id 1Y5R9f-0004UZ-2l from Yao_Qi@mentor.com ; Sun, 28 Dec 2014 19:38:03 -0800 Received: from GreenOnly (147.34.91.1) by svr-orw-fem-02.mgc.mentorg.com (147.34.96.168) with Microsoft SMTP Server id 14.3.224.2; Sun, 28 Dec 2014 19:38:02 -0800 From: Yao Qi <yao@codesourcery.com> To: Joel Brobecker <brobecker@adacore.com> CC: <gdb-patches@sourceware.org> Subject: Re: [PATCH] Clear upper bits during sign extension References: <1419815569-21854-1-git-send-email-yao@codesourcery.com> <20141229030659.GB2123@adacore.com> Date: Mon, 29 Dec 2014 11:37:46 +0800 In-Reply-To: <20141229030659.GB2123@adacore.com> (Joel Brobecker's message of "Mon, 29 Dec 2014 07:06:59 +0400") Message-ID: <87d273w2gl.fsf@codesourcery.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes |
Commit Message
Yao Qi
Dec. 29, 2014, 3:37 a.m. UTC
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.
Comments
> Yeah, that is better, or even we can left shift signbit by one. Indeed! > 2014-12-29 Yao Qi <yao@codesourcery.com> > > * utils.c (gdb_sign_extend): Clear bits from BIT in VALUE. Looks good to me. > 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; > }
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.
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; }