Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp904678rwe; Wed, 24 Aug 2022 11:00:33 -0700 (PDT) X-Google-Smtp-Source: AA6agR5JvTxzRBHGLe3msZKUL2JhYTK40zWlXuDzlXUqMxwh0y0ESpOmzzUxIC49Y5dgVwHEEejN X-Received: by 2002:a17:906:6086:b0:731:3970:48d0 with SMTP id t6-20020a170906608600b00731397048d0mr103697ejj.16.1661364033020; Wed, 24 Aug 2022 11:00:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661364033; cv=none; d=google.com; s=arc-20160816; b=Y9/5JikQUMUcP5AAprbYUABhdDITdF3xs9sa17aZ6sfrl2XaVzom9RgQNOl3bWrD54 +CdlnYoU89ToLYjDAGbw/c30JsApVcbrLX760I+5TyyVghcwEkGARcI/eS1sRjB5PVx5 g6bUnEAw1N6F56R22bmpef6VStoydgMfK/6ktye/XAV77StwDdT6Yc4jaCnX8VjQala4 IhUHlSq+0Kjnhv0FJCFDzMn0VkLNpBC//EX9jDCE93g65TahutMRHi+5VRJNnbkeuvi6 e2CrViR8WoFJXDsro8nz/amPZc5R0jVj03dtbFkQNKfQs1c9PC9tB0vjd9qGCNRfUXSy tqjg== 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=u0McpXYtOTvCm3oJmLu2/FQ3Yt0NnpKe4YGOkK9vKWQ=; b=gTSs/9VS1hD474of2yNlAKxwoJbRPz1Q5Pl54OePe3fbL1B5OA4nHIprILxkwoRyN/ EtUBjpiZu+qlXvEx//zQL1bVnxTUDJS2/NdkaJdYsOg7NLEFr2eLKzVW4B3Znf9udxm7 dS0pa6T2wwkKOCEOi0uGKvIYjWvf23z4W+cQGLtRvEG3BGILqimpIWXLXX9GnPjB1pF0 ZpK/qRKwieOxtukUis2sO8onG3sbhomhV7IKskpn5pT4iWtJEiKv9HDJyLnvrP32157k 1FTSsG/oqte0iRyN8z7HOUVFrFxRa60hhkoz+YwgEW0GBD0sCbESNadxTWpCs4OZxn7G 49ww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Km1cMWoR; 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 r8-20020aa7da08000000b004479a84fa88si509895eds.519.2022.08.24.11.00.05; Wed, 24 Aug 2022 11:00:33 -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=Km1cMWoR; 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 S239723AbiHXRzf (ORCPT + 99 others); Wed, 24 Aug 2022 13:55:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33430 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239659AbiHXRz1 (ORCPT ); Wed, 24 Aug 2022 13:55:27 -0400 Received: from mail-qv1-xf36.google.com (mail-qv1-xf36.google.com [IPv6:2607:f8b0:4864:20::f36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1996FF5 for ; Wed, 24 Aug 2022 10:55:23 -0700 (PDT) Received: by mail-qv1-xf36.google.com with SMTP id b2so13455823qvp.1 for ; Wed, 24 Aug 2022 10:55:23 -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=u0McpXYtOTvCm3oJmLu2/FQ3Yt0NnpKe4YGOkK9vKWQ=; b=Km1cMWoRnbcrRJy/1JXv49V5CKO98yzrTjYZITA4+o0XeiZwPnlKShBMUBOr1PZsLb BY+zodJ+0sq8OUTk8VmG+6mzRW8IKHKhqFe4Jxz2WwZgc3r9TcMvdBUC9kJHlhYVHwVk SYM61YBpjSrb4IdRiN8QLRL27Ax4ir1RKqBZDZcNF/XQB+UDa0Fhe1pAYL11oufhwZIk 6REeEOjvObWaZyKypSyBr1OzIvu6++v9CNFQZ4qfqtlqP/v4nUaB9Ied66TVI4c7V/zl i0AWdbuDId6TdsnXxh9b0+Clgty2RuIdkUcp7cSJ3CHEjVykXf59zFvEVEbrEyAEvOcs T4uA== 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=u0McpXYtOTvCm3oJmLu2/FQ3Yt0NnpKe4YGOkK9vKWQ=; b=pTdtDlIZ0BV3sAQdSSyzSwd9Tm0vNEGC+PgZAq6XvHCGWxFE0qQ8VpOGGNghQAJQxl zEBrW2RhBmxKXEENigxCB0lW36apTL5+hoZ2HRvUqcMH+x1Bnp5dCnhXZRFlUnidkP3H clvu+pauZRPz3bD8Q7W/9+HimjICBU1YtXJ+Us0Zldm4wbtJg7I/0OgnLhK33sInCfcT +uOFjrCv1hjuyQxr0nPk625rLew6AvoJ0QNfHjqqe5GnKjl3840S3v5smTvsyVKNvACj 9egck4pZhgIUDEKSsmF/3rJo+6fYsuNKUyiCYN6uz6CkqmjOGDbtnw8FLchpRCfTiSv2 pfzQ== X-Gm-Message-State: ACgBeo0o8HX/NK9uCUq2KinS+ShLleDWXAHEFvJTCvh6SQwVmeVXCiPu WuMj/EV0kQk3HxDoZBam0mTtqKDpTKAH9CuK+oE= X-Received: by 2002:a0c:aadb:0:b0:497:1283:c849 with SMTP id g27-20020a0caadb000000b004971283c849mr279833qvb.11.1661363722765; Wed, 24 Aug 2022 10:55:22 -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:54:46 +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 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++) { unsigned long tmp; tmp = (EXPRESSION); if (tmp) { ... } } No? -- With Best Regards, Andy Shevchenko