From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4041 invoked by alias); 4 Dec 2009 20:34:32 -0000 Received: (qmail 4029 invoked by uid 22791); 4 Dec 2009 20:34:30 -0000 X-SWARE-Spam-Status: No, hits=-2.5 required=5.0 tests=AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from gate.crashing.org (HELO gate.crashing.org) (63.228.1.57) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 04 Dec 2009 20:34:25 +0000 Received: from gate.crashing.org (localhost.localdomain [127.0.0.1]) by gate.crashing.org (8.14.1/8.13.8) with ESMTP id nB4KYLOC002796; Fri, 4 Dec 2009 14:34:21 -0600 Received: (from apache@localhost) by gate.crashing.org (8.14.1/8.14.1/Submit) id nB4KYKvo002795; Fri, 4 Dec 2009 21:34:20 +0100 Received: from 84.105.60.153 (SquirrelMail authenticated user segher) by gate.crashing.org with HTTP; Fri, 4 Dec 2009 21:34:20 +0100 (CET) Message-ID: <55182.84.105.60.153.1259958860.squirrel@gate.crashing.org> In-Reply-To: <4B1963DC.7080108@redhat.com> References: <5c6f2a5d0911291151j46dc9d28h568b87f57228b28e@mail.gmail.com> <87ljhpt5lu.fsf@mid.deneb.enyo.de> <5c6f2a5d0911291400t63b84bdak3c37b8523040c50c@mail.gmail.com> <87fx7w4iyi.fsf@mid.deneb.enyo.de> <5c6f2a5d0911300726r7b902fdeuf905f698c55407f2@mail.gmail.com> <87my23nasp.fsf@mid.deneb.enyo.de> <29bd08b70911301639u70eeb87xa49e6c92740817c2@mail.gmail.com> <5c6f2a5d0912010300k49636f40l693db2c206dfc3d9@mail.gmail.com> <87ljhm8lga.fsf@mid.deneb.enyo.de> <4B163525.8010104@redhat.com> <873a3qvaan.fsf@mid.deneb.enyo.de> <4B195E8A.90304@redhat.com> <87ljhituan.fsf@mid.deneb.enyo.de> <4B1963DC.7080108@redhat.com> Date: Fri, 04 Dec 2009 21:04:00 -0000 Subject: Re: Efficient detection of signed overflow? From: "Segher Boessenkool" To: "Andrew Haley" Cc: "Florian Weimer" , "Mark Dickinson" , "Lawrence Crowl" , "me22" , "GCC-help" User-Agent: SquirrelMail/1.4.10a-1.fc6 MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2009-12/txt/msg00108.txt.bz2 >>> The test was, if I recall correctly >>> >>> x = a + b; >>> if ((x ^ a) & (x ^ b)) < 0) >>> >>> all you have to do is convert everything to unsigned values, then >>> >>> ux = ua + ub; >>> if ((ux ^ ua) & (ux ^ ub)) & (unsigned)INT_MIN)) >>> goto deal_with_overflow; >>> // we now know there is no overflow >>> x = ux; >>> >>> which is exactly the same test as before, but perfectly compliant. >> >> The comment is wrong. The code checks for signed overflow, but the >> following assignment still overflwos when ux is larger than INT_MAX. >> So this version is usable exactly under the same circumstances as the >> first one. > > Ahhh, I see. Hmm, there must be a decent way to do this. Oh, it needs to be absolutely portable? Well, you're already assuming two's complement. if (ux <= LONG_MAX) /* or whatever the type was */ x = ux; else { x = ux + LONG_MIN; x += LONG_MIN; } should do the trick I think? Segher