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 6AFB23858C31 for ; Thu, 14 Mar 2024 06:04:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6AFB23858C31 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 6AFB23858C31 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=1710396255; cv=none; b=kiBHI+NQi77i4fYovgiwqWkHRGgaTZevBG5lPBA3u+PJ6r1Veyu7Pdu4AgvfP7BX2DmRSzsCD7AyTmehuwpMxtkX/0g29zEINsl1d/uBgx4KxSQ8dISHYxnANK+EXoheE3fV+XxeOdxm5GMeMTa7Vz2vvsQRinFYjqgvSrRJjxU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710396255; c=relaxed/simple; bh=JOm3G3mPR4QDas75avzEwvhJ0Yvb1MNFDaXtNCB8TjU=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=V7cDh1Z9Z8VDPhuamQKCoJZzjkEYKGEFEeOiUBjyl8IeNpzchxHwwT8FuPG099JcmX8dMeXN7E43Wv1X6LAS211n0dNUyu58W5oEBXYn1/JW7wYDrilz8iGPWq2JkkcAFX1UFBtWqJYh25X+eLwI32st2lepSA7b0c7q7GkPiXo= 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 D448340AC4FB; Thu, 14 Mar 2024 06:04:10 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.ispras.ru D448340AC4FB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ispras.ru; s=default; t=1710396250; bh=P2sOvBOOc5fmeOKTbX7jrQRFZVVS+hNGDBQt5W1S/3A=; h=Date:From:To:cc:Subject:In-Reply-To:References:From; b=AjDjsXVKNkFx+WjMzNBOCzZZcV9i2nxO9YOi5ag8wfEyiadJ2R/vd68eHMYCJP8a+ ajZ9Xy8H2OOwrG0V3kFtzTRsOwLp3uRY15+Bfv7aG7pInXPqPyFAk64oUggHeMHUB+ h2QAWjVsYQ6ZKQxM24bgS+WG4E9vYcjcYakM/l3E= Date: Thu, 14 Mar 2024 09:04:10 +0300 (MSK) From: Alexander Monakov To: Andrew Cooper cc: gcc@gcc.gnu.org Subject: Re: Builtin for consulting value analysis (better ffs() code gen) In-Reply-To: <06d7af49-c4a9-43d5-a18f-266439c7f82d@citrix.com> Message-ID: <12b2c99d-993d-ba8d-75ff-b107de2eba67@ispras.ru> References: <06d7af49-c4a9-43d5-a18f-266439c7f82d@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS,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 List-Id: On Thu, 14 Mar 2024, Andrew Cooper via Gcc wrote: > I suppose that what I'm looking for is something a little like > __builtin_constant_p() which can either be used in a straight if(), or > in a __builtin_choose_expr(). > > Anyway - is there a way of doing this that I've managed to overlook? I am missing what is lacking for you with __builtin_constant_p, I would do it like this: unsigned ffs(unsigned x) { unsigned res; unsigned nonzero = x != 0; if (__builtin_constant_p(nonzero) && nonzero) asm("bsf %1, %0" : "=r"(res) : "rm"(x)); else { res = -1; asm("bsf %1, %0" : "+r"(res) : "rm"(x)); } return res; } or with handling known-zero-input case like this: unsigned ffs(unsigned x) { unsigned res; unsigned nonzero = x != 0; if (!__builtin_constant_p(nonzero)) { res = -1; asm("bsf %1, %0" : "+r"(res) : "rm"(x)); } else if (nonzero) { asm("bsf %1, %0" : "=r"(res) : "rm"(x)); } else { res = -1; } return res; } Does it work for you? HTH Alexander