From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 197253858D20 for ; Thu, 16 Nov 2023 13:18:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 197253858D20 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 197253858D20 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=1700140714; cv=none; b=q9v3BkRo75NiXldn06NQ+E4VvtPLInJWxl01OhjyAabInnT8Y+u+5jgM8FPFFPqTyY0oFFMLuOwYx6fNddIZGNTKo4+xwEbIoZBcjI5lK49XyUBw5QTs45Yp29PBbMPmqWPSLg9CpLs11+cPKbl7bkb//4BfevenLEG5NqF61LE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700140714; c=relaxed/simple; bh=gV4QofZqp/PeWumZmBvCPe2yYey3ddPFQJSe9QT943w=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=xqrD6ZiCopiXatK3toHJw2KMGdxYC7xEAdP9SUG2uKa31T0r0O12vyGpKW0/qKBxxjzyCl/Fn0f3ZKVocrSkErUCQGo5r4tJW8/TT1AzlcIRqXFRYhQ3tGrXKJHAnzjRUFti+ieTEKL//TXvSzLG2g563BDCyeq5aOL0TitDmLo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700140703; 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:in-reply-to:in-reply-to: references:references; bh=erTw+seZz6uuejuAH6e6EOzfZpY3SiJhPUR1teApRXw=; b=VULodABcpvgtaKie2T7V6YvyI5TJAn56AQsv4dWNDpnjQAlxMadq/q8TamRcxuXrNO5fZW zNg6IEMgLQ0sNvN2hMySRG3SufZVjKVzSqVEFtEBBO+yoM1ddqdXMV/gq5RIbA/7p57LKP 2onm4fxj+PCB1CLzGwgcvlZhvnxiwlQ= 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-615-5MIcqQVtMIq8OEgO4iwGbw-1; Thu, 16 Nov 2023 08:18:18 -0500 X-MC-Unique: 5MIcqQVtMIq8OEgO4iwGbw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (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 0EFB1185A784; Thu, 16 Nov 2023 13:18:18 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.194.53]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9076140C6EB9; Thu, 16 Nov 2023 13:18:17 +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 3AGDIE0W2088107 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 16 Nov 2023 14:18:15 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3AGDIDfI2088104; Thu, 16 Nov 2023 14:18:13 +0100 Date: Thu, 16 Nov 2023 14:18:13 +0100 From: Jakub Jelinek To: tavianator@tavianator.com Cc: libc-alpha@sourceware.org, joseph@codesourcery.com Subject: Re: [RFC] stdbit.h implementation Message-ID: Reply-To: Jakub Jelinek References: <20231115191111.208350-1-tavianator@tavianator.com> MIME-Version: 1.0 In-Reply-To: <20231115191111.208350-1-tavianator@tavianator.com> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3.4 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 List-Id: On Wed, Nov 15, 2023 at 02:11:11PM -0500, tavianator@tavianator.com wrote: > On Tue, 14 Nov 2023, Jakub Jelinek wrote: > > > I think the fallback _Generic versions satisfy that, but the > > __builtin_{clz,ctz,popcount}g unfortunately do use ({ }) in 3 of them. > > In one case it is just an optimization attempt and it isn't strictly > > necessary, stdc_bit_width can be implemented as popcount (value) == 1 > > You mean stdc_has_single_bit()? Sure. > > or as value && (value & (value - 1)) == 0 or as > > value && (value & -value) == value > > You might also consider > > (value ^ (value - 1)) > value - 1 > > or > > (value ^ -value) < -value > > which avoid the && and so optimize a bit better. Indeed. I'll see what I can do on the GCC side. For _BitInt larger than unsigned long long, I'd say the best is to use __builtin_popcountg (value) == 1 and if needed optimize that during the popcount _BitInt lowering (differentiate 3 cases, 0 bits set, 1 bit set, more than one bit set), for the last case early out (the whole value isn't single bit), the the former two add 0/1 to sum and again early exit if it is 2 or more, because even ignoring the multiple evaluation stuff, (value & (value - 1)) > value - 1 needs 2 loops, subtraction needs to operate from least significant limb to most, & doesn't care and > needs to iterate from most significant limb to least. For that case value != 0 && (value & (value - 1)) == 0 would be actually better as that can be done in one loop. Anyway, I think it is a good idea to use (value ^ (value - 1)) > value - 1 in the __hsb*_inline functions, that is just 3 cheap operations rather than 5 and && isn't that cheap on various arches. --- stdlib/stdbit.h.jj 2023-11-14 20:54:58.788102241 +0100 +++ stdlib/stdbit.h 2023-11-16 14:02:54.533538355 +0100 @@ -572,30 +572,33 @@ extern _Bool stdc_has_single_bit_ul (uns __extension__ extern _Bool stdc_has_single_bit_ull (unsigned long long int __x) __THROW __attribute_const__; -#define stdc_has_single_bit(x) (stdc_has_single_bit_ull (x)) +#define stdc_has_single_bit(x) \ + (sizeof (x) <= sizeof (unsigned int) \ + ? stdc_has_single_bit_ui (x) \ + : stdc_has_single_bit_ull (x)) static __always_inline _Bool __hsb64_inline (uint64_t __x) { - return __x == (__x & -__x) && __x != 0; + return (__x ^ (__x - 1)) > __x - 1; } static __always_inline _Bool __hsb32_inline (uint32_t __x) { - return __x == (__x & -__x) && __x != 0; + return (__x ^ (__x - 1)) > __x - 1; } static __always_inline _Bool __hsb16_inline (uint16_t __x) { - return __x == (__x & -__x) && __x != 0; + return (__x ^ (__x - 1)) > __x - 1; } static __always_inline _Bool __hsb8_inline (uint8_t __x) { - return __x == (__x & -__x) && __x != 0; + return (__x ^ (__x - 1)) > __x - 1; } #define stdc_has_single_bit_uc(x) (__hsb8_inline (x)) Jakub