From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>,
Sakari Ailus <sakari.ailus@linux.intel.com>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
linux-media@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Yong Zhi <yong.zhi@intel.com>, Bingbu Cao <bingbu.cao@intel.com>,
Dan Scally <djrscally@gmail.com>,
Tianshu Qiu <tian.shu.qiu@intel.com>,
Mauro Carvalho Chehab <mchehab@kernel.org>
Subject: [PATCH v1 2/3] lib/sort: Introduce rotate() to circular shift an array of elements
Date: Tue, 24 Aug 2021 16:33:50 +0300 [thread overview]
Message-ID: <20210824133351.88179-2-andriy.shevchenko@linux.intel.com> (raw)
In-Reply-To: <20210824133351.88179-1-andriy.shevchenko@linux.intel.com>
In some cases we want to circular shift an array of elements.
Introduce rotate() helper for that.
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
include/linux/sort.h | 3 +++
lib/sort.c | 61 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 64 insertions(+)
diff --git a/include/linux/sort.h b/include/linux/sort.h
index b5898725fe9d..c881acb12ffc 100644
--- a/include/linux/sort.h
+++ b/include/linux/sort.h
@@ -13,4 +13,7 @@ void sort(void *base, size_t num, size_t size,
cmp_func_t cmp_func,
swap_func_t swap_func);
+void rotate(void *base, size_t num, size_t size, size_t by,
+ swap_func_t swap_func);
+
#endif
diff --git a/lib/sort.c b/lib/sort.c
index d9b2f5b73620..b9243f8db34b 100644
--- a/lib/sort.c
+++ b/lib/sort.c
@@ -14,6 +14,7 @@
#include <linux/types.h>
#include <linux/export.h>
+#include <linux/minmax.h>
#include <linux/sort.h>
/**
@@ -275,3 +276,63 @@ void sort(void *base, size_t num, size_t size,
return sort_r(base, num, size, _CMP_WRAPPER, swap_func, cmp_func);
}
EXPORT_SYMBOL(sort);
+
+/**
+ * rotate - rotate an array of elements by a number of elements
+ * @base: pointer to data to sort
+ * @num: number of elements
+ * @size: size of each element
+ * @by: number of elements to rotate by
+ * @swap_func: pointer to swap function or NULL
+ *
+ * Helper function to advance all the elements of a circular buffer by
+ * @by positions.
+ */
+void rotate(void *base, size_t num, size_t size, size_t by,
+ swap_func_t swap_func)
+{
+ struct {
+ size_t begin, end;
+ } arr[2] = {
+ { .begin = 0, .end = by - 1 },
+ { .begin = by, .end = num - 1 },
+ };
+
+ swap_func = choose_swap_func(swap_func, base, size);
+
+#define CHUNK_SIZE(a) ((a)->end - (a)->begin + 1)
+
+ /* Loop as long as we have out-of-place entries */
+ while (CHUNK_SIZE(&arr[0]) && CHUNK_SIZE(&arr[1])) {
+ size_t size0, i;
+
+ /*
+ * Find the number of entries that can be arranged on this
+ * iteration.
+ */
+ size0 = min(CHUNK_SIZE(&arr[0]), CHUNK_SIZE(&arr[1]));
+
+ /* Swap the entries in two parts of the array */
+ for (i = 0; i < size0; i++) {
+ void *a = base + size * (arr[0].begin + i);
+ void *b = base + size * (arr[1].begin + i);
+
+ do_swap(a, b, size, swap_func);
+ }
+
+ if (CHUNK_SIZE(&arr[0]) > CHUNK_SIZE(&arr[1])) {
+ /* The end of the first array remains unarranged */
+ arr[0].begin += size0;
+ } else {
+ /*
+ * The first array is fully arranged so we proceed
+ * handling the next one.
+ */
+ arr[0].begin = arr[1].begin;
+ arr[0].end = arr[1].begin + size0 - 1;
+ arr[1].begin += size0;
+ }
+ }
+#undef CHUNK_SIZE
+}
+EXPORT_SYMBOL(rotate);
--
2.32.0
next prev parent reply other threads:[~2021-08-24 13:34 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-24 13:33 [PATCH v1 1/3] lib/sort: Split out choose_swap_func() local helper Andy Shevchenko
2021-08-24 13:33 ` Andy Shevchenko [this message]
2021-08-25 7:05 ` [PATCH v1 2/3] lib/sort: Introduce rotate() to circular shift an array of elements Rasmus Villemoes
2021-08-25 8:08 ` Sakari Ailus
2021-08-25 9:29 ` Rasmus Villemoes
2021-08-25 9:55 ` Andy Shevchenko
2021-08-24 13:33 ` [RFT, PATCH v1 3/3] media: ipu3-cio2: Replace custom implementation of rotate() Andy Shevchenko
2021-08-25 8:00 ` [PATCH v1 1/3] lib/sort: Split out choose_swap_func() local helper Sakari Ailus
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210824133351.88179-2-andriy.shevchenko@linux.intel.com \
--to=andriy.shevchenko@linux.intel.com \
--cc=bingbu.cao@intel.com \
--cc=djrscally@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=mchehab+huawei@kernel.org \
--cc=mchehab@kernel.org \
--cc=sakari.ailus@linux.intel.com \
--cc=tian.shu.qiu@intel.com \
--cc=yong.zhi@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome