From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 32704 invoked by alias); 11 Aug 2011 20:14:36 -0000 Received: (qmail 32695 invoked by uid 22791); 11 Aug 2011 20:14:35 -0000 X-SWARE-Spam-Status: No, hits=-2.2 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,TW_LV X-Spam-Check-By: sourceware.org Received: from mail-yx0-f175.google.com (HELO mail-yx0-f175.google.com) (209.85.213.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 11 Aug 2011 20:14:21 +0000 Received: by yxi19 with SMTP id 19so1662743yxi.20 for ; Thu, 11 Aug 2011 13:14:20 -0700 (PDT) MIME-Version: 1.0 Received: by 10.150.235.17 with SMTP id i17mr1067714ybh.67.1313093660732; Thu, 11 Aug 2011 13:14:20 -0700 (PDT) Received: by 10.151.6.18 with HTTP; Thu, 11 Aug 2011 13:14:20 -0700 (PDT) In-Reply-To: References: <201108111715.14240.florian.merz@kit.edu> <201108111805.19582.florian.merz@kit.edu> <20110811171301.GA11774@synopsys.com> Date: Thu, 11 Aug 2011 20:14:00 -0000 Message-ID: Subject: Re: [LLVMdev] Handling of pointer difference in llvm-gcc and clang From: Gabriel Dos Reis To: Richard Guenther Cc: Joe Buck , Florian Merz , "gcc@gcc.gnu.org" Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org X-SW-Source: 2011-08/txt/msg00233.txt.bz2 On Thu, Aug 11, 2011 at 12:15 PM, Richard Guenther wrote: > On Thu, Aug 11, 2011 at 7:13 PM, Joe Buck wrote: >> On Thu, Aug 11, 2011 at 09:05:19AM -0700, Florian Merz wrote: >>> If I remember the standard correctly, pointer subtraction is valid if b= oth >>> pointers point to elements of the same array or to one past the last el= ement >>> of the array. According to this 0x80000000 - 0x7FFFFFFF should be a val= id >>> pointer subtraction with the result 0x00000001. >>> >>> But if the subtraction is treated as a signed, this would be an signed = integer >>> overflow, as we subtract INT_MAX from INT_MIN, which surely must overfl= ow, and >>> the result therefore would be undefined. >> >> It is true that the C and C++ languages make signed integer overflow >> undefined, but that's for actual integer types as declared by the user. >> For pointers, though the subtraction has to be signed (because, for two >> pointers, either can can come later in the address space), this signed >> subtraction has to be defined to work in a two's complement fashion (so >> the wraparound in your example case works reliably). > > Of course GCC can't (yet) do both at the same time. yes, but GCC should mark its internal artifacts so that it surely distingui= shes user-provided abstractions (which may be subjected to harsh treatments) from its own blessed babies. > =A0Thus we have to > use unsigned arithmetic when we want two's complement arithmetic.