Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp905010rwe; Wed, 24 Aug 2022 11:00:52 -0700 (PDT) X-Google-Smtp-Source: AA6agR6lnoo5XD/3k4hPlPaRUAEHp9LTud6fz0NmsRMErFCU9co4AwvHa0H625PqhSNfrCZ2JU6x X-Received: by 2002:a17:906:d7a9:b0:731:2189:4f58 with SMTP id pk9-20020a170906d7a900b0073121894f58mr72120ejb.471.1661364051738; Wed, 24 Aug 2022 11:00:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661364051; cv=none; d=google.com; s=arc-20160816; b=myWXj1CUfKS5p8zbUOXDvVi6Dfxz59DCCg3hK7Mst1KRUAeihsP1hG34AeTah6k0CL jSPkecSwIuCkKZI1rCv9hnEdD5HgudvfqkzzuGWYoZ6fcyXtINM8WkXh+Ymh/P/Zh+aH A3rKUQ2AYIL7FryJeyQoccinuS4yADTdJnfeut/xIiSeLdZpYpwaRAObvCHtqq38Fi2e BSCKS5ql0nNtBf2INgrEY6JagpN5gGuCtUeB03f6p/3ONwQW42KGsdv9OXcKKF2/1KqK fOY12SPHSjDwHjYTA+abJcqPbqBLNmlwx+IU51dyCsdA04FTTmXGKPvlDGQEb71dnrBo zMCg== 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=9CXiN/f38xrjnVFAJBLJLmpUxNRcRVRzq1k058bxfcw=; b=IXijMVbua5dQfKwwd05IpgocKgAlNSPF7ZC+u+8b1i0ppI/+gjYlwCNZadIAxCHTsv CO+25KydVGR2Rcdjzgc57Ra+Dny0QFhsBOuTfgUHJG3ma731xdvb4fW1IfnTRfFiiX2M 4XttKn9hIZuNKbuQZYqxt0KKLVIH9rvM999SpyxzyIlrldJVANnbcJB6uFRpR9X5ibBy ES61oJ48AWRIElZND8z+0q+VGNfjQExVlRIqpEE/4vJW7Dhf/LqEvwH7YqdV3ZUDUlsm LEeDcUGhFZh7Gv4ROUr5FvssR0zz5SM07PRp0NS1xfpm/JDC8NwikoP1qB2CbJ0QaXOQ iHNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=eF7ZWWTC; 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 yr19-20020a170907335300b0072b12db5f1csi2278565ejb.451.2022.08.24.11.00.23; Wed, 24 Aug 2022 11:00:51 -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=eF7ZWWTC; 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 S239605AbiHXR46 (ORCPT + 99 others); Wed, 24 Aug 2022 13:56:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240368AbiHXR4l (ORCPT ); Wed, 24 Aug 2022 13:56:41 -0400 Received: from mail-qv1-xf2d.google.com (mail-qv1-xf2d.google.com [IPv6:2607:f8b0:4864:20::f2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4AC912316B for ; Wed, 24 Aug 2022 10:56:39 -0700 (PDT) Received: by mail-qv1-xf2d.google.com with SMTP id m5so10622664qvo.4 for ; Wed, 24 Aug 2022 10:56: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=9CXiN/f38xrjnVFAJBLJLmpUxNRcRVRzq1k058bxfcw=; b=eF7ZWWTCO2FIyRuX+ks4C8xUfsfE+qppB9UMsrM9mAcoAHSGzFbBXhjxOfhJOyNcae cRm92PQak1NihUM2n8USl2GwycUmw+0poGQVHLbfQ77BvE5aTuUCeDabGsKvXvR7E+0w /lu/KaIIo+1PRB8TXvJU68jligK4yuZYrgylmK3+qU7WPKRu+1SgEM1c/czEI+iMPpPt jzYsAyqvUAeGojgjmxoFj60NCD/B9Yiqcgt4x2Oec7/ahns+jIqFLFnch6h8s5o3frrP GjbTbbvPimGWNdQf5a1hkZTjmc4srCjL4huMrYOenQefkyLRbqmb2OFF0QqtJrcy0zPd 2vIA== 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=9CXiN/f38xrjnVFAJBLJLmpUxNRcRVRzq1k058bxfcw=; b=yArZYELUwIbmT6Dejj2E7mDieb1rf70rjrn7qhajnnfXlEJEpfetxy4perTHQ0Bxx2 wueQOiub7qDgDWLd6Wzz+tp7d4XyNH+gApzrbkviXptEyi+ElzNt15Rsg1uA3hLQl/QN qhIMlvhKv2R8ETRSxehxtDKZ5EqOGPOB1HsSYb2RftOvs2eowCxd7/G57jaBjkg8yM9d XRBPQUfAfDl0kBpns6lsS8zqpKeeDesx4y78xfbvuxHl9Jf7mieMFgNguOEDulFX4Sc2 tQGUIi+f7jVFRr88GvxzodC9/jPzjDrhWBxK8EHJ9/WHxPUMcvwdHpNmkNp1IHzsYHKu 3NZA== X-Gm-Message-State: ACgBeo3GvNFaP8CDKdI/zlDrQ2qjEimL6dXcGWEpLwL7oqHwAU9K4aov RY+JUqk9zrF4d5Z9WMThtiPOT0pOuZNp1Na+BHw= X-Received: by 2002:ad4:4eaf:0:b0:496:ac46:2d9c with SMTP id ed15-20020ad44eaf000000b00496ac462d9cmr231066qvb.82.1661363798458; Wed, 24 Aug 2022 10:56:38 -0700 (PDT) MIME-Version: 1.0 References: <20220824012624.2826445-1-yury.norov@gmail.com> <20220824012624.2826445-4-yury.norov@gmail.com> In-Reply-To: From: Andy Shevchenko Date: Wed, 24 Aug 2022 20:56:02 +0300 Message-ID: Subject: Re: [PATCH v2 3/3] lib/find_bit: optimize find_next_bit() functions 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 8:54 PM Andy Shevchenko wrote: > On Wed, Aug 24, 2022 at 4:53 PM Yury Norov wrote: > > On Wed, Aug 24, 2022 at 12:19:05PM +0300, Andy Shevchenko wrote: > > > On Wed, Aug 24, 2022 at 4:56 AM Yury Norov wrote: ... > > > > +#define FIND_NEXT_BIT(EXPRESSION, size, start) \ > > > > +({ \ > > > > + unsigned long mask, idx, tmp, sz = (size), __start = (start); \ > > > > + \ > > > > + if (unlikely(__start >= sz)) \ > > > > + goto out; \ > > > > + \ > > > > + mask = word_op(BITMAP_FIRST_WORD_MASK(__start)); \ > > > > + idx = __start / BITS_PER_LONG; \ > > > > + \ > > > > + for (tmp = (EXPRESSION) & mask; !tmp; tmp = (EXPRESSION)) { \ > > > > > > for (unsigned long tmp ...; > > > But hey, why not loop over idx (which probably should be named as > > > offset) > > > > Offset in structure, index in array, isn't? > > > > > as I proposed in the first patch? You will drop a lot of > > > divisions / multiplications, no? > > > > Those divisions and multiplications are optimized away, and > > what you suggested blows up the EXPRESSION. > > > > I tried like this: > > mask = word_op(BITMAP_FIRST_WORD_MASK(__start)); > > idx = __start / BITS_PER_LONG; > > tmp = (EXPRESSION); > > > > while (1) { > > if (tmp) { > > sz = min(idx * BITS_PER_LONG + __ffs(word_op(tmp)), sz); > > break; > > } > > > > if (++idx > sz) > > break; > > > > tmp = (EXPRESSION); > > } > > > > And it generated the same code, but looks less expressive to me. > > If you have some elegant approach in mind - can you please share > > it, and how the generated code looks? > > for (unsigned long idx = 0; idx < sz; idx++) { Of source 0 should be changed to whatever start you have there. > unsigned long tmp; > > tmp = (EXPRESSION); > if (tmp) { > ... > } > } > > No? -- With Best Regards, Andy Shevchenko