Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp364966rwe; Wed, 24 Aug 2022 02:15:58 -0700 (PDT) X-Google-Smtp-Source: AA6agR7GC/jpj0yMMzQGUynZqd/apKkoHSoJvvlLl9u8aeQqvoyChCGVbJfuIGsl86Y2eHxJhAJQ X-Received: by 2002:a63:8548:0:b0:42a:206b:b6fd with SMTP id u69-20020a638548000000b0042a206bb6fdmr22191888pgd.556.1661332557990; Wed, 24 Aug 2022 02:15:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661332557; cv=none; d=google.com; s=arc-20160816; b=w7LKbzNF/rvsHpQ/AfS7CxEPumnWTS5GFkMexc5c4auQ8ZSB5xNMEKeqS9hc6oibsH iarO3VLvO7Cbp9gL9/tYuY7AhcLAvZrYMnhXCeGUh2mPtERWuf8Fwp2TfQuE9VHyH/P1 up3/jrwJEnAk3wstRxCqhgKDfQDyDhdasYk0NHFniK2kEgGt9P8w9V1bMlYV58Rs3dnP tPNhNPHK1Bl19sqW2f0MMhUroxPXrrZjZ/qqHyXskj2B0oqZ/2A4wGnDz2yjg736dQHK uEqYbjyEhEUKavWpzR2CVJ1vEZzmBYMwy8a2id+cPPS87P4VZLyPPqvNCKspqBXJzIiD rbzg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=qp5U4hlmW1coNM2H6xZQ1GZizUdeX5ho/eYaDjMVqY0=; b=MkWDN7lmP8SLlzlg+xeHWAiUX1tYOgMSxs0tfDxbacaBQ4PeFEGU9NFTMTEqTE9/i6 IarRH9Cu+h9bCQFk+EDtPbQ0Yo+tTQ3T2Ny/IfA+7CfXj10yVs4amIZSzRgqXLzWGp+R SzJQEDd0R8NuewjipfDxcexgq0viPCnEr/1JEqIzc4DeC42kutIYO8pyLDfMdB+9VyAo N09iJVr5p1jmX5AcAGe9Eo1H+i5pgKnDBHWQrfC4fqqZ3WK/r99nFhv9l4ulhtvxn/3r /yFjM8jjLh9fNK7MA70QqkvIkWWa+99y5VymLfj+lYTETUP0DgUM7TjYFozQbN0JrXOD moDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=esvYOuK4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p13-20020a17090a428d00b001efbdf44c12si744217pjg.97.2022.08.24.02.15.46; Wed, 24 Aug 2022 02:15:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=esvYOuK4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235470AbiHXJKl (ORCPT + 99 others); Wed, 24 Aug 2022 05:10:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234852AbiHXJKk (ORCPT ); Wed, 24 Aug 2022 05:10:40 -0400 Received: from mail-qv1-xf29.google.com (mail-qv1-xf29.google.com [IPv6:2607:f8b0:4864:20::f29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31D9B3FA02 for ; Wed, 24 Aug 2022 02:10:39 -0700 (PDT) Received: by mail-qv1-xf29.google.com with SMTP id e4so12391052qvr.2 for ; Wed, 24 Aug 2022 02:10:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc; bh=qp5U4hlmW1coNM2H6xZQ1GZizUdeX5ho/eYaDjMVqY0=; b=esvYOuK4gh+mq2fW9go+4DjZ7T6fOpQzZkckuZ5y9mpQtZSUJri1HxYNVcDhsWD1aC LIDLhIs0sZMkpuw50YoGBx+5PlpbDGD4OeEGytcOOy/SrK2OTBE6i8y4GG+QEhkoTpgU jv72bkrJrln2HawFU4ZLdoeCFxMJSKbitk4yZgEHrLdgh+tMkWWqj1wIx7P3Vk+qp8Gx PvL9zdqx5uORFYM8H0KnnE8IGf31GakqCrrfr1MnS6oGfA2wSJUBPmlHejUySP1C9ja6 WkAqd5/ZH0Eu639lwkjg19vdGPF9ahGqtCX6Rjz4t4UjA+ASsUXQ0nhC3VRU5tR0TOj0 7BMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=qp5U4hlmW1coNM2H6xZQ1GZizUdeX5ho/eYaDjMVqY0=; b=d8WeiQlhs4iV9wHCz7bMfkeIado+wzijSFGVg07joxtuAjX4VKPFSu4sOXV0b/pBuK a/z4nkJnmFpTL25gtZK9L3lwTTLtaHrkikb9YpfY4pavOg+7W7XkirRfOUKgVT6RzkwM vN+vqhL0yNvTcfGXQhsKMAX5r7bDrgZn3u1k1pQB58Og5VQ5VbFSK9y/oMYTeScmeGHC WVg9hBKykj4sNQgi6+kr12XSB4h75SE0Rq+x5dqcg6Kj9HQWs5RdN/hTv2jgJv4p/qkM l8n/J+uYH+rRumN2wx0zdUrla6pQakSNUZCLRhqNRR/vQ2nbP8ryrKgLlookCiL2BCEW AzVQ== X-Gm-Message-State: ACgBeo1tEPUQVZM7b6rSkKkzds7vy76yLT1Wr4pDiS7EL5cAm0K0lmlB 8olvvN/DJe9z0aR6HoZ8T9D5vUyo5q8ZFivkn/M= X-Received: by 2002:ad4:5baf:0:b0:476:6a9e:b5e1 with SMTP id 15-20020ad45baf000000b004766a9eb5e1mr23122067qvq.64.1661332238224; Wed, 24 Aug 2022 02:10:38 -0700 (PDT) MIME-Version: 1.0 References: <20220824012624.2826445-1-yury.norov@gmail.com> <20220824012624.2826445-2-yury.norov@gmail.com> In-Reply-To: <20220824012624.2826445-2-yury.norov@gmail.com> From: Andy Shevchenko Date: Wed, 24 Aug 2022 12:10:02 +0300 Message-ID: Subject: Re: [PATCH v2 1/3] lib/find_bit: introduce FIND_FIRST_BIT() macro To: Yury Norov Cc: Linus Torvalds , Linux Kernel Mailing List , Guenter Roeck , Dennis Zhou , Russell King , Catalin Marinas , Andy Shevchenko , Rasmus Villemoes , Alexey Klimov , Kees Cook , Andy Whitcroft Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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 lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 24, 2022 at 4:51 AM Yury Norov wrote: > > Now that we have many flavors of find_first_bit(), and expect even more, > it's better to have one macro that generates optimal code for all and makes > maintaining of slightly different functions simpler. > > The logic common to all versions is moved to the new macro, and all the > flavors are generated by providing an EXPRESSION macro-parameter, like > in this example: > > #define FIND_FIRST_BIT(EXPRESSION, size) ... > > find_first_ornot_and_bit(addr1, addr2, addr3, size) > { > return FIND_NEXT_BIT(addr1[idx] | ~addr2[idx] & addr3[idx], size); > } > > The EXPRESSION may be of any complexity, as soon as it only refers > the bitmap(s) and an iterator idx. > > The 'word_op' is here to allow the macro to generate code for _le > versions on big-endian machines; used in the following patches. ... > +#ifndef word_op > +#define word_op > +#endif Not sure about the naming without namespace. Perhaps __ffs_word_op? > +#define FIND_FIRST_BIT(EXPRESSION, size) \ > +({ \ > + unsigned long idx, val, sz = (size); \ > + \ > + for (idx = 0; idx * BITS_PER_LONG < sz; idx++) { \ I think we can do slightly better: for (unsigned long idx = 0; idx < sz; idx += BITS_PER_LONG) { unsigned long val; > + val = (EXPRESSION); \ > + if (val) { \ > + sz = min(idx * BITS_PER_LONG + __ffs(word_op(val)), sz);\ sz = min(idx + __ffs(...)); > + break; \ > + } \ > + } \ > + \ > + sz; \ > +}) -- With Best Regards, Andy Shevchenko