From patchwork Wed Nov 22 09:33:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sandiford X-Patchwork-Id: 119442 Delivered-To: patch@linaro.org Received: by 10.140.22.164 with SMTP id 33csp6456821qgn; Wed, 22 Nov 2017 01:34:08 -0800 (PST) X-Google-Smtp-Source: AGs4zMZG+JW9729peheFsMyIdhVIY0mWfMEiAu0Qr6ug/RnsXQ7Iz19yIVtTwQn2s8gmQuAXYpRf X-Received: by 10.98.155.129 with SMTP id e1mr18840943pfk.119.1511343248770; Wed, 22 Nov 2017 01:34:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511343248; cv=none; d=google.com; s=arc-20160816; b=EFb4cGgYJfoTDUNechNfdLPj7WlLNxAW5GSHey6Tg+Uv5rBSC4h3UXmHuEPVcmk2/I pbrSdE2M85LdXfOEzwiccIfcyqs8S2KSGN5Egd56gZJ1MOH1pbJKY5uwlezQJcjh48/g toOFEmJtYTvJFWLX2S+0vioD2Es2ek9UHG+UReAIZM7b13uQSzso6eNQz3cbHPJM9fe4 344lm2xTHlOXfHsmoj43CTeKhiQv1dEg7bd0t1HojfG5WD87v4iwDeS/Vg+kg1EITWVp p1YOF4TYQWR02F3+GE1Fb20x6g2sJdOBBg9XCLN2jFdn6NkYW/IwkLoS7tEMrFBaBULd Vw/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:user-agent:message-id:date:subject:mail-followup-to:to :from:delivered-to:sender:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=nrhIBuFZYI31bpo4jnREJO7A3l5CeoPBQ/LZ6Hth9P0=; b=zM1q3K0rqWcPFdtZCmqgVyhiNxgaOenKiD01g23BUlybRKQEpElcXEZ7N0lqyCcUIM /P9aPiG2h0uQhkuEO5XGPcpbjBrgi36MgmyNLrMka9Sk092TyAST3nI9encxp3re7thz G5o9D8UfsdzxArTFXPL2gLwV1tsPMmKQ5rx/dHxFVxck7em349sQ/6g0TJowwDWS9/gf noDoUM1a8nLpJhIxj3XIwosR25mcN0HSx9j3cFVBQ4TZjCUFJwDPvnuxclyFGeNYAOmX IFPjpXwncKwEYQT51KrDssWD36AMcBuxmToyamnNV67Q+dI1Rk2WaIIdDY3kuv+p5scF iB/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=CsYpQvXF; spf=pass (google.com: domain of gcc-patches-return-467644-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-467644-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id v3si13250165plb.589.2017.11.22.01.34.08 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 Nov 2017 01:34:08 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-467644-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=CsYpQvXF; spf=pass (google.com: domain of gcc-patches-return-467644-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-467644-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version:content-type; q=dns; s= default; b=DaZG2VKko4GLg/FkDdwAyTxgYPpCMd4UDIm+Xek4pqpzbeRyaJy/i kaMeLgH6YkA+DlBkMnAszFvqp1BBoBWZzD3pr1lvUD/ZDH6svPrRpTUwu/tz0ylI dIekNpp+Y7NwVGixV/iAKbTwldl2/FYks6YJWp09URVCdotp3YSgII= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version:content-type; s= default; bh=kZww3EwZ5F9hYELvHoN9a6LlGQk=; b=CsYpQvXF2yRZYc87thbD kZXpiPvEAU9cyc262cad3mrxhzEWRzv0UR+wJH00IPuOJBUwq2H8JTmZjCFkcoBX fbmwa8LJzAkEd4YPJhxV9gbbWaIaB173Y7X6HnDgAssxiAyfKUjxZMX3FYwZppcf 3gr30P4au+RyxEw3MMbl9WU= Received: (qmail 40247 invoked by alias); 22 Nov 2017 09:33:55 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 37076 invoked by uid 89); 22 Nov 2017 09:33:54 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KB_WAM_FROM_NAME_SINGLEWORD, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=sgn, sum X-HELO: mail-wr0-f181.google.com Received: from mail-wr0-f181.google.com (HELO mail-wr0-f181.google.com) (209.85.128.181) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 22 Nov 2017 09:33:52 +0000 Received: by mail-wr0-f181.google.com with SMTP id r2so7447955wra.13 for ; Wed, 22 Nov 2017 01:33:52 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:mail-followup-to:subject:date:message-id :user-agent:mime-version; bh=nrhIBuFZYI31bpo4jnREJO7A3l5CeoPBQ/LZ6Hth9P0=; b=EgmzL3TPOU+FUs5gyttnFVyDmd71jfyifxSiggXTJy3k1POfpwmzlrDqhuvipFfX+K cSUURUf6j58oOQmXNZQMDyEiCAqu2lV5sUtSU6qQ2FOmpl6Icypcr+tN9d3JtIPZjbDX 8eiU0BbZyGgttwc6kh6F176kGu51nWQd8vOr/xeUSKuu0h8Ti6oFj8FgGc8CyNXYA2rf P+2kdxogP1xg8tCS35uL9i2fjZfwupJxgbmzBZzb2N7RoyAyGqLuaPo2TWx5tMY+oo6C yvLDnISgtcGD0wqBucu3Z8vBTvSChKzRrLWaOfDGzW0Uzg8SCTPtgy/2EGgaPvGBja88 B3XA== X-Gm-Message-State: AJaThX54Jdlu4KKJO7F8DMPC2wZq2dOFEY9WHNWyrxCGKMRu3N06wooR i7rxdelcPc969hV8FnDJPyMh06GkEB4= X-Received: by 10.223.178.87 with SMTP id y23mr4335474wra.3.1511343230450; Wed, 22 Nov 2017 01:33:50 -0800 (PST) Received: from localhost ([2.25.234.120]) by smtp.gmail.com with ESMTPSA id c3sm638900wrf.21.2017.11.22.01.33.49 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 22 Nov 2017 01:33:49 -0800 (PST) From: Richard Sandiford To: gcc-patches@gcc.gnu.org Mail-Followup-To: gcc-patches@gcc.gnu.org, richard.sandiford@linaro.org Subject: PR82547: Undetected overflow for UNSIGNED wide_ints Date: Wed, 22 Nov 2017 09:33:47 +0000 Message-ID: <871skq1wuc.fsf@linaro.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux) MIME-Version: 1.0 wi::add_large and wi::sub_large weren't setting the overflow bit correctly for unsigned operations if the result needed fewer HWIs than the precision. Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64le-linux-gnu. OK to install? Richard 2017-11-21 Richard Sandiford gcc/ PR middle-end/82547 * wide-int.cc (wi::add_large, wi::sub_large): Fix overflow detection for unsigned values with fewer HWIs than the precision. (test_overflow): New function. (wide_int_cc_tests): Call it. Index: gcc/wide-int.cc =================================================================== --- gcc/wide-int.cc 2017-11-22 09:21:12.516930172 +0000 +++ gcc/wide-int.cc 2017-11-22 09:33:00.427739376 +0000 @@ -1158,7 +1158,7 @@ wi::add_large (HOST_WIDE_INT *val, const val[len] = mask0 + mask1 + carry; len++; if (overflow) - *overflow = false; + *overflow = (sgn == UNSIGNED && carry); } else if (overflow) { @@ -1552,7 +1552,7 @@ wi::sub_large (HOST_WIDE_INT *val, const val[len] = mask0 - mask1 - borrow; len++; if (overflow) - *overflow = false; + *overflow = (sgn == UNSIGNED && borrow); } else if (overflow) { @@ -2345,14 +2345,54 @@ static void run_all_wide_int_tests () test_comparisons (); } +/* Test overflow conditions. */ + +static void +test_overflow () +{ + static int precs[] = { 31, 32, 33, 63, 64, 65, 127, 128 }; + static int offsets[] = { 16, 1, 0 }; + for (unsigned int i = 0; i < ARRAY_SIZE (precs); ++i) + for (unsigned int j = 0; j < ARRAY_SIZE (offsets); ++j) + { + int prec = precs[i]; + int offset = offsets[j]; + bool overflow; + wide_int sum, diff; + + sum = wi::add (wi::max_value (prec, UNSIGNED) - offset, 1, + UNSIGNED, &overflow); + ASSERT_EQ (sum, -offset); + ASSERT_EQ (overflow, offset == 0); + + sum = wi::add (1, wi::max_value (prec, UNSIGNED) - offset, + UNSIGNED, &overflow); + ASSERT_EQ (sum, -offset); + ASSERT_EQ (overflow, offset == 0); + + diff = wi::sub (wi::max_value (prec, UNSIGNED) - offset, + wi::max_value (prec, UNSIGNED), + UNSIGNED, &overflow); + ASSERT_EQ (diff, -offset); + ASSERT_EQ (overflow, offset != 0); + + diff = wi::sub (wi::max_value (prec, UNSIGNED) - offset, + wi::max_value (prec, UNSIGNED) - 1, + UNSIGNED, &overflow); + ASSERT_EQ (diff, 1 - offset); + ASSERT_EQ (overflow, offset > 1); + } +} + /* Run all of the selftests within this file, for all value types. */ void wide_int_cc_tests () { - run_all_wide_int_tests (); - run_all_wide_int_tests (); - run_all_wide_int_tests (); + run_all_wide_int_tests (); + run_all_wide_int_tests (); + run_all_wide_int_tests (); + test_overflow (); } } // namespace selftest