From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by sourceware.org (Postfix) with ESMTPS id 5FBEE3858D20 for ; Wed, 10 Apr 2024 09:16:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5FBEE3858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=ispras.ru Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=ispras.ru ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5FBEE3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=83.149.199.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712740601; cv=none; b=PsLmup6CXebFIWFdMhrFwAkAMZqtMClxPq+4EbfhPlyUU8pAD5Voqqy6aJo1rWTHG8Du4VmgxMXySw0bVS7fprM68y0sLMz/6sGa/uQUkley+nh10fcHT1kQMM1kq6ne6jCyOKg9pHeP6deW1O/S/wVNXpmNn2Wg1wqrOneCZss= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712740601; c=relaxed/simple; bh=ntJxYsns3itnWyzK7eVQ6DS9mIFR+HOSJG+UNQ9zX0k=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=ptSoBoVGh4VtlgNUZXIL4w+1AdNowVziA9O1nIRi7ztCkXFMLyPbuvknpXp/eQy14tE7jxZUE/riZj26fFJCmq3DmKgH15veZ6ynO1LvT6kONI+gtr32jf5Mp7UW30wAta7YkPgmsoJdUt9vf9e8XDsDI0d34prJq5NyPGdLVE8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from [10.10.3.121] (unknown [10.10.3.121]) by mail.ispras.ru (Postfix) with ESMTPS id 0918A40769D4; Wed, 10 Apr 2024 09:16:37 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.ispras.ru 0918A40769D4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ispras.ru; s=default; t=1712740597; bh=iJek1uac6fPmVjmpG2HVAyBH03hkZMzezFSrL18dfRs=; h=Date:From:To:cc:Subject:In-Reply-To:References:From; b=cDQXpTCq0lQKER51ab9jE86Itm+nhX2qJp71Tpct2penQGelITDxpFUvlcJwrtFrp 35Jm399PLC35cDe6Bwm9L+QorIvOa20Y6GzmbahwkyCPJZ/CSgwJB7Y9AOvJnPIJue kb0Ou+iUqFLsqBEzvOwNEyxZfSVdHus09tg0AZrU= Date: Wed, 10 Apr 2024 12:16:36 +0300 (MSK) From: Alexander Monakov To: stefan@franke.ms cc: gcc-help@gcc.gnu.org Subject: Re: optimizer discards sign information In-Reply-To: <016501da8b24$735d5170$5a17f450$@franke.ms> Message-ID: References: <016501da8b24$735d5170$5a17f450$@franke.ms> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="8323328-1745424296-1712740597=:9686" X-Spam-Status: No, score=-2.7 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: This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323328-1745424296-1712740597=:9686 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT On Wed, 10 Apr 2024, stefan@franke.ms wrote: > Hi all, > > I just stumbled over an issue, which is present in almost all gcc versions. > I worked around using inline assembly… > Maybe gcc behaves correct and I am wrong? Here is the code: > > https://godbolt.org/z/cW8jcdh56 > > typedef unsigned long long int u64; > typedef unsigned int u32; > typedef unsigned short u16; > > u64 foo(u16 a, u16 b) { >     u32 x = a * b; >     u64 r = x; >     return r; > } > > And on gcc 13.2 x86.64 you get > > foo: >         movzx   esi, si >         movzx   edi, di >         imul    edi, esi >         movsx   rax, edi >         ret > > > There is a sign extension! The optimizer step discards the information > >  x_6 = (u32) _3; > > and uses _3 directly instead, which is signed. > > Am I wrong or is it gcc? GCC is not wrong. When your code computes x: u32 x = a * b; 'a' and 'b' are first promoted to int according to C language rules, and the multiplication happens in the signed int type, with UB on overflow. The compiler deduces the range of signed int temporary holding the result of the multiplication is [0, 0x7fffffff], which allows to propagate it to the assignment of 'r' (which in the end produces a sign extension, as you observed, so the propagation did not turn out to be useful). u16 * u16 is a famous footgun for sure. I'd suggest 'x = 1u * a * b' as a fix for the code. Alexander --8323328-1745424296-1712740597=:9686--