Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp260707pxb; Wed, 25 Aug 2021 02:31:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz03DX7O3cHeUO2LWYA+VO8fa7Wp64vELNhw8vjWKLxiS0m1j018dDQ39i1uCZff61QVcyb X-Received: by 2002:aa7:c7c2:: with SMTP id o2mr47917239eds.166.1629883914925; Wed, 25 Aug 2021 02:31:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629883914; cv=none; d=google.com; s=arc-20160816; b=SDqMfQ2VYXRdzDCeBMfwIiLEC4nVzZe6nkOQcuPdGS1O0BzXpEa/OO4RXAwACFB7PE gnWzGzA3vKbihTzBTFm2ZsHni9uN2mzjMrsU8LmTdT+UYzlP0qXqrz3zLF7BMGz/7HuY LBvPZkNthV/mOyp/ScxsRFxsJG12wCDB+J5Snp8/3jnQIImgOAU7OFvIQiD6H6HL7ZW3 qQThjNHYkKl20X6rl12f3gYMak/srvqRCWtLDEz5uAvUT6uQGR4qxAlSFygKNUF+Wn7a 47K3xKzHG1/qY191VmOJx1VAj2yh5k9VACqqNMYYKiNwn+KXtEZ9DETABRatVPMONAng GdJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=J2/OQEffn/ha/SHF1AP1mfEI0+u4GaQpxzyiwpPvr08=; b=YLKQ7i49c3FdkwklLSBM6TJZY5oQnG0zm6c1u0vtpLQVtBYp0lhmKRChiLZsuQ75YX Qg7k3zZUJ8FVVq7//HMkqNXxTFGqeY8zHe+PF4K5hGNonJu9sabiFjcWjARASvjzGLAd f0rXWeXTfI5OPI0sMNX1etqbZYvFkSLXL4NXJvbaDvauT6Y+InauT3sBEOisaE4bCXtf 95UQxWDmDaT8sw38NejDCKDBgio/GXyapVcOTJmbWcMDE+v/GSWkFuZktICX0aC9d+Mu uk4DqJsAXyth58hIPm2lB0PhP7xDhw/OgyIKITlpYwaieLfdXUmKTFBA+RmaUQY5rR5y UwZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=HgnFsVv3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l4si15276900edj.282.2021.08.25.02.31.31; Wed, 25 Aug 2021 02:31:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=HgnFsVv3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239525AbhHYJaB (ORCPT + 99 others); Wed, 25 Aug 2021 05:30:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231994AbhHYJaB (ORCPT ); Wed, 25 Aug 2021 05:30:01 -0400 Received: from mail-lj1-x231.google.com (mail-lj1-x231.google.com [IPv6:2a00:1450:4864:20::231]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 689EBC061757 for ; Wed, 25 Aug 2021 02:29:15 -0700 (PDT) Received: by mail-lj1-x231.google.com with SMTP id w4so40974032ljh.13 for ; Wed, 25 Aug 2021 02:29:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=J2/OQEffn/ha/SHF1AP1mfEI0+u4GaQpxzyiwpPvr08=; b=HgnFsVv3taOdV0Fwq/Gg1EnGRV5idZHsFWLEzxJQs2i3eqnAirD/0prcm+4Z0uJQ4n C4cYOQh246/Ec6+zrhFhMl04DJ2tAbNoPi1TCYbZi6IYhjvvlsI02UtvUJ/VPx67l9lR oRVrRPAwyMg9+MbM5wzBmnGRncsDS+KNKYh6A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=J2/OQEffn/ha/SHF1AP1mfEI0+u4GaQpxzyiwpPvr08=; b=pvpX5eMkONGDkwLXiS/DzwV9BkYqxUwCe98yg5vXQ+tM+OdxDwOrHh4JVshh7TMBWy w2l4Heh+DrKRD2aYbZo+QJ2PxO4PlkFnb+QQ92JqIjgMwed2gry21sHBpHrLl1WTfuXH 5dt4NzhVpJtA6v7Erv1aochQygzXGtqcC97vGGQwc8DvCjjd2HtmLOSduKJKyu+7Hvpp PCbxkHD8ymVSJuJj6iVMK/xM++Tn1utZn7/U2YCnUgsHp/HPBF2YakXrf0FH0O7a0ier 9b+U6v6nSCYuww9qhyYUgqmD4cgCDWRdvRrRbEa9HDj2vj4POjtqbxK08pzWVffp8KY7 rrsA== X-Gm-Message-State: AOAM533g6ciglucq/128pMIJrNqyC2yoTlFAnFW1WGAPeR+T8MklM1Pw rVld3iR11sRXg3Bo+OWRarloNQ== X-Received: by 2002:a2e:7005:: with SMTP id l5mr22066141ljc.355.1629883753790; Wed, 25 Aug 2021 02:29:13 -0700 (PDT) Received: from [172.16.11.1] ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id u20sm490639lfr.272.2021.08.25.02.29.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 25 Aug 2021 02:29:13 -0700 (PDT) Subject: Re: [PATCH v1 2/3] lib/sort: Introduce rotate() to circular shift an array of elements To: Sakari Ailus Cc: Andy Shevchenko , Mauro Carvalho Chehab , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Yong Zhi , Bingbu Cao , Dan Scally , Tianshu Qiu , Mauro Carvalho Chehab References: <20210824133351.88179-1-andriy.shevchenko@linux.intel.com> <20210824133351.88179-2-andriy.shevchenko@linux.intel.com> <4078b7a3-2ec2-ba87-d23c-b8daed7386fe@rasmusvillemoes.dk> <20210825080832.GN3@paasikivi.fi.intel.com> From: Rasmus Villemoes Message-ID: <8bc8d977-5204-6f5b-8a1c-f2338c141993@rasmusvillemoes.dk> Date: Wed, 25 Aug 2021 11:29:12 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <20210825080832.GN3@paasikivi.fi.intel.com> Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 25/08/2021 10.08, Sakari Ailus wrote: > Hi Rasmus, Andy, > >>> + * @num: number of elements >>> + * @size: size of each element >>> + * @by: number of elements to rotate by >> >> Perhaps add (0 <= @by < @num) or something like that, and/or start the >> implementation with "if (num <= 1) return; if (by >= num) by %= num;" > > The latter could be done unconditionally. Yes (provided num is tested at least for being non-zero first, but then it's mostly free to check <= 1 instead), but in the vast majority of cases the caller would pass a sane value of by, and an unconditional %= would thus waste 100+ clock cycles for nothing. >>> + struct { >>> + size_t begin, end; >>> + } arr[2] = { >>> + { .begin = 0, .end = by - 1 }, >>> + { .begin = by, .end = num - 1 }, >>> + }; >> >> I see you just copied-and-adapted, but I think the code would be much >> easier to read without all those plus/minus ones all over. > > Now that I think about it, they can be just removed. In that case end > refers to the element following end, rather than the last element. Yes, as we almost always do array indexing in C... the math simply ends up coming out more naturally that way in the majority of cases. >> Perhaps add a small self-test, it's not at all obvious how this works >> (perhaps it's some standard CS101 algorithm for rotating in-place, I >> don't know, but even then an implementation can have off-by-ones and >> corner cases). > > I don't know, I wrote this to fix a bug in the ipu3-cio2 driver. ;-) The > hardware, and so the arguments, were static. Nice to see it would be useful > elsewhere almost as-is. Well, Andy hasn't actually shown that it would be useful anywhere else. I think I'd like to see another user. Just doing "move this helper to lib/ because we can reuse choose-a-proper-swap-func and thus implement this perhaps a tiny bit faster" without considering whether it's even performance-critical in the sole user is not a good idea IMO. Especially since it can affect code generation of the much more important (at least, has many more users) sort() function - the do_swap() function grows another user, so could make the compiler end up choosing not to inline it anymore. There's another slightly simpler way to implement rotate(), which might end up having more users (though I can't find any currently): Add a reverse() helper, then rotate() can be done as reverse(a, 0, n); reverse(a, 0, k); reverse(a, k, n-k);. If my math is right, the current suggested rotate() ends up doing n-gcd(n,k) swaps, while the implementation in terms of a reverse() would do n-1 if either n or k is odd, otherwise n, calls to swap(). Rasmus