From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by sourceware.org (Postfix) with ESMTPS id 626933849AC6 for ; Fri, 19 Apr 2024 06:33:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 626933849AC6 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 626933849AC6 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713508400; cv=none; b=iStgdcVE9d/ccODmMPsft0KcUV5ZCrO9l0SzbPY9K6clI2+WCAVAbZ+XZVviXIjMqZZRQNDUzOCa86ql9xI+aRL7LtGq0V4Mm2Tv8fmPRROxmanINvleAYKiZI6iPqaMRJYGizI9FXhNtIqBrHMLsH4sO5zZ2NQwKzmoXYJExhU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713508400; c=relaxed/simple; bh=emEvQwoKUAZ3QQDEJhn/fc8ouYumkeFp3IiEbe/MZI8=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:Message-ID:MIME-Version; b=sS5jFq3AUHCh9rpPrzJJAqkoddwUk/LL/WCmCu7OgpKyy4tXuZHGysCD9XEnx+oA3rgY8PzKUSX0InHLMUT2k1EqekUDKiPR+uB2mHgAVUzQpAAgu8F8LIzXEMLCZi/HanpCEpwD9/VTuZXoY7ScLvbGDwdWIV8ynWIDZUanLaM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from [10.168.5.241] (unknown [10.168.5.241]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 3E8B45D389; Fri, 19 Apr 2024 06:33:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1713508390; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=O95JtAsy6DWJdEzRdJyTtI4Y3/84ZxWVfNp/gtXxkn0=; b=bH119EuIYTP4ixBynfv3FA3MU1VUg/WsjWCc5PX0FncW2EOB2C2C4TfHzghwawBch+eKZw pxlJgGJxVdtJL6E4CsVadpQ57SlgJFYHmRT5gUTBvIA4IBUNcRHShcUPah5YcBQ5/SSalV DHdIiUp6h/r/mj6hJNX8KnLeGLq9wug= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1713508390; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=O95JtAsy6DWJdEzRdJyTtI4Y3/84ZxWVfNp/gtXxkn0=; b=R10WVgaCZmvoVfzxgDaVzVRcLPjoQuCjOWzfvBYJuW4ZwfgBHptvoPKAUACvFNzHIQOEvR whTUjfD1t/fbKWCA== Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1713508390; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=O95JtAsy6DWJdEzRdJyTtI4Y3/84ZxWVfNp/gtXxkn0=; b=bH119EuIYTP4ixBynfv3FA3MU1VUg/WsjWCc5PX0FncW2EOB2C2C4TfHzghwawBch+eKZw pxlJgGJxVdtJL6E4CsVadpQ57SlgJFYHmRT5gUTBvIA4IBUNcRHShcUPah5YcBQ5/SSalV DHdIiUp6h/r/mj6hJNX8KnLeGLq9wug= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1713508390; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=O95JtAsy6DWJdEzRdJyTtI4Y3/84ZxWVfNp/gtXxkn0=; b=R10WVgaCZmvoVfzxgDaVzVRcLPjoQuCjOWzfvBYJuW4ZwfgBHptvoPKAUACvFNzHIQOEvR whTUjfD1t/fbKWCA== Date: Fri, 19 Apr 2024 08:33:10 +0200 (CEST) From: Richard Biener To: Jakub Jelinek cc: "Joseph S. Myers" , gcc-patches@gcc.gnu.org Subject: Re: [PATCH] libgcc: Another __divmodbitint4 bug fix [PR114762] In-Reply-To: Message-ID: <829n4sp6-s1r5-9339-2r57-n997q5n45qs7@fhfr.qr> References: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Score: -4.30 X-Spam-Level: X-Spamd-Result: default: False [-4.30 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-0.20)[-0.999]; MIME_GOOD(-0.10)[text/plain]; MISSING_XM_UA(0.00)[]; MIME_TRACE(0.00)[0:+]; TO_DN_SOME(0.00)[]; ARC_NA(0.00)[]; RCVD_COUNT_ZERO(0.00)[0]; FROM_HAS_DN(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FROM_EQ_ENVFROM(0.00)[]; RCPT_COUNT_THREE(0.00)[3]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:email] X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Fri, 19 Apr 2024, Jakub Jelinek wrote: > Hi! > > The following testcase is miscompiled because the code to decrement > vn on negative value with all ones in most significant limb (even partial) > and 0 in most significant bit of the second most significant limb doesn't > take into account the case where all bits below the most significant limb > are zero. This has been a problem both in the version before yesterday's > commit where it has been done only if un was one shorter than vn before this > decrement, and is now problem even more often when it is done earlier. > When we decrement vn in such case and negate it, we end up with all 0s in > the v2 value, so have both the problems with UB on __builtin_clz* and the > expectations of the algorithm that the divisor has most significant bit set > after shifting, plus when the decremented vn is 1 it can SIGFPE on division > by zero even when it is not division by zero etc. Other values shouldn't > get 0 in the new most significant limb after negation, because the > bitint_reduce_prec canonicalization should reduce prec if the second most > significant limb is all ones and if that limb is all zeros, if at least > one limb below it is non-zero, carry in will make it non-zero. > > The following patch fixes it by checking if at least one bit below the > most significant limb is non-zero, in that case it decrements, otherwise > it will do nothing (but e.g. for the un < vn case that also means the > divisor is large enough that the result should be q 0 r u). > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? OK. Richard. > 2024-04-19 Jakub Jelinek > > PR libgcc/114762 > * libgcc2.c (__divmodbitint4): Don't decrement vn if all bits > below the most significant limb are zero. > > * gcc.dg/torture/bitint-70.c: New test. > > --- libgcc/libgcc2.c.jj 2024-04-18 09:48:55.172538667 +0200 > +++ libgcc/libgcc2.c 2024-04-18 12:17:28.893616007 +0200 > @@ -1715,11 +1715,18 @@ __divmodbitint4 (UBILtype *q, SItype qpr > && vn > 1 > && (Wtype) v[BITINT_END (1, vn - 2)] >= 0) > { > - vp = 0; > - --vn; > + /* Unless all bits below the most significant limb are zero. */ > + SItype vn2; > + for (vn2 = vn - 2; vn2 >= 0; --vn2) > + if (v[BITINT_END (vn - 1 - vn2, vn2)]) > + { > + vp = 0; > + --vn; > #if __LIBGCC_BITINT_ORDER__ == __ORDER_BIG_ENDIAN__ > - ++v; > + ++v; > #endif > + break; > + } > } > if (__builtin_expect (un < vn, 0)) > { > --- gcc/testsuite/gcc.dg/torture/bitint-70.c.jj 2024-04-18 12:26:09.406383158 +0200 > +++ gcc/testsuite/gcc.dg/torture/bitint-70.c 2024-04-18 12:26:57.253718287 +0200 > @@ -0,0 +1,22 @@ > +/* PR libgcc/114762 */ > +/* { 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__ >= 255 > +__attribute__((__noipa__)) signed _BitInt(255) > +foo (signed _BitInt(255) a, signed _BitInt(65) b) > +{ > + return a / b; > +} > +#endif > + > +int > +main () > +{ > +#if __BITINT_MAXWIDTH__ >= 255 > + if (foo (1, -0xffffffffffffffffwb - 1wb)) > + __builtin_abort (); > +#endif > +} > > Jakub > > -- Richard Biener SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, Germany; GF: Ivo Totev, Andrew McDonald, Werner Knoblich; (HRB 36809, AG Nuernberg)