From patchwork Fri Mar 15 08:35:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1912415 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=GUcxo3Az; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TwyJL4g8yz1yWn for ; Fri, 15 Mar 2024 19:36:02 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A101A385783C for ; Fri, 15 Mar 2024 08:36:00 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id AD1503857B94 for ; Fri, 15 Mar 2024 08:35:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AD1503857B94 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AD1503857B94 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710491732; cv=none; b=bFwYZfTr65sgH9Y6hOx5XWE/5F/711eMFN9U2+D0q2kzW/CRgIKEkzZBHMdLylz3cguU6AOq6PhdvGw7hvieIJlliQ3f0aOz9o1Pacun25+2BEsFZdX6C585rpjsxJ48wtLoevxeQZg6t/ylXLC18s8xJLACnhNUwiNYC4YR/Dk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710491732; c=relaxed/simple; bh=cbPt7Ghzew/e7k3r/wAKLD/gUWyL2V9sODsT0G5R05U=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=iZg3ZE5OEsv/e6nwOmayKnrvJEaCk5Z91NVWfmQeH6SOT3B4IAiFUKc+gvvxRMrG0Q+54+/1w8vHTkH9IqhR7v3isIClfCykBuQkdc+C+i6O9PlfC/TkstZxVV3TQaITzXMW4zqIMH7F/RLW8HIunxy8GDYblB895xvoGoiatB0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710491730; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=DOEcSLnrtkGqAbU5Ne79eZq1axCCWydB9Lfc+qDHfB0=; b=GUcxo3AzOkm3fTU5MA6WgoPVdmpDsUPZMObBsQrN6DRBpZhwSiNhkeMrjz39gkaMDHHVMV cyjZ12yXFIWHJ8r83Dpdtky2TQEpQy+UQkU/nlwZXs+QKJvVdfNZfQKTYodfbKdLCkS6py Qjx/x0U5CiKUKrxVwOi/b1WSNfqymmQ= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-477-CXZjFo2_NMubVWFeMKeTRA-1; Fri, 15 Mar 2024 04:35:28 -0400 X-MC-Unique: CXZjFo2_NMubVWFeMKeTRA-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A035A8007A1; Fri, 15 Mar 2024 08:35:28 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.225.36]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6377C492BD0; Fri, 15 Mar 2024 08:35:28 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 42F8ZQYd2930573 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 15 Mar 2024 09:35:26 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 42F8ZQ662930572; Fri, 15 Mar 2024 09:35:26 +0100 Date: Fri, 15 Mar 2024 09:35:26 +0100 From: Jakub Jelinek To: Richard Biener , "Joseph S. Myers" Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] libgcc: Fix quotient and/or remainder negation in __divmodbitint4 [PR114327] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! While for __mulbitint3 we actually don't negate anything and perform the multiplication in unsigned style always, for __divmodbitint4 if the operands aren't unsigned and are negative, we negate them first and then try to negate them as needed at the end. quotient is negated if just one of the operands was negated and the other wasn't or vice versa, and remainder is negated if the first operand was negated. The case which doesn't work correctly is if due to limited range of the operands we perform the division/modulo in some smaller number of limbs and then extend it to the desired precision of the quotient and/or remainder results. If they aren't negated, the extension is done with memset to 0, if they are negated, the extension was done with memset to -1. The problem is that if the quotient or remainder is zero, then bitint_negate negates it again to zero (that is ok), but we should then extend with memset to 0, not memset to -1. The following patch achieves that by letting bitint_negate also check if the negated operand is zero and changes the memset argument based on that. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-03-15 Jakub Jelinek PR libgcc/114327 * libgcc2.c (bitint_negate): Return UWtype bitwise or of all the limbs before negation rather than void. (__divmodbitint4): Determine whether to fill in the upper limbs after negation based on whether bitint_negate returned 0 or non-zero, rather then always filling with -1. * gcc.dg/torture/bitint-63.c: New test. Jakub --- libgcc/libgcc2.c.jj 2024-02-02 22:14:14.946684874 +0100 +++ libgcc/libgcc2.c 2024-03-14 13:37:38.227332706 +0100 @@ -1642,19 +1642,22 @@ __mulbitint3 (UBILtype *ret, SItype retp #ifdef L_divmodbitint4 /* D = -S. */ -static void +static UWtype bitint_negate (UBILtype *d, const UBILtype *s, SItype n) { UWtype c = 1; + UWtype r = 0; do { UWtype sv = *s, lo; + r |= sv; s += BITINT_INC; c = __builtin_add_overflow (~sv, c, &lo); *d = lo; d += BITINT_INC; } while (--n); + return r; } /* D -= S * L. */ @@ -1977,10 +1980,10 @@ __divmodbitint4 (UBILtype *q, SItype qpr n = qn; else n = un - vn + 1; - bitint_negate (q + BITINT_END (qn - 1, 0), - q2 + BITINT_END (un - vn, 0), n); + SItype c = bitint_negate (q + BITINT_END (qn - 1, 0), + q2 + BITINT_END (un - vn, 0), n) ? -1 : 0; if (qn > n) - __builtin_memset (q + BITINT_END (0, n), -1, + __builtin_memset (q + BITINT_END (0, n), c, (qn - n) * sizeof (UWtype)); } else @@ -1999,11 +2002,11 @@ __divmodbitint4 (UBILtype *q, SItype qpr if (uprec < 0) { /* Negative remainder. */ - bitint_negate (r + BITINT_END (rn - 1, 0), - r + BITINT_END (rn - 1, 0), - rn > vn ? vn : rn); + SItype c = bitint_negate (r + BITINT_END (rn - 1, 0), + r + BITINT_END (rn - 1, 0), + rn > vn ? vn : rn) ? -1 : 0; if (rn > vn) - __builtin_memset (r + BITINT_END (0, vn), -1, + __builtin_memset (r + BITINT_END (0, vn), c, (rn - vn) * sizeof (UWtype)); } else --- gcc/testsuite/gcc.dg/torture/bitint-63.c.jj 2024-03-14 13:46:31.591938158 +0100 +++ gcc/testsuite/gcc.dg/torture/bitint-63.c 2024-03-14 13:45:58.306399642 +0100 @@ -0,0 +1,30 @@ +/* PR libgcc/114327 */ +/* { dg-do run { target bitint } } */ +/* { dg-options "-std=c23" } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "-flto" } { "" } } */ + +#if __BITINT_MAXWIDTH__ >= 256 +_BitInt(256) +foo (_BitInt(256) b, _BitInt(256) c) +{ + return b % c; +} + +_BitInt(256) +bar (_BitInt(256) b, _BitInt(256) c) +{ + return b / c; +} +#endif + +int +main () +{ +#if __BITINT_MAXWIDTH__ >= 256 + if (foo (-0x9e9b9fe60wb, 1wb)) + __builtin_abort (); + if (bar (1wb, -0x9e9b9fe60wb)) + __builtin_abort (); +#endif +}