All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wolfgang Denk <wd@denx.de>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 2/5] Add qsort - add support for sorting data arrays
Date: Sat, 17 Jul 2010 21:45:45 +0200	[thread overview]
Message-ID: <1279395948-25864-3-git-send-email-wd@denx.de> (raw)
In-Reply-To: <1279395948-25864-1-git-send-email-wd@denx.de>

Code adapted from uClibc-0.9.30.3

Signed-off-by: Wolfgang Denk <wd@denx.de>
---
 include/common.h |    4 +++
 lib/Makefile     |    1 +
 lib/qsort.c      |   69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 74 insertions(+), 0 deletions(-)
 create mode 100644 lib/qsort.c

diff --git a/include/common.h b/include/common.h
index eddec22..e4b4ec0 100644
--- a/include/common.h
+++ b/include/common.h
@@ -631,6 +631,10 @@ static inline IPaddr_t getenv_IPaddr (char *var)
 	return (string_to_ip(getenv(var)));
 }
 
+/* lib/qsort.c */
+void qsort(void *base, size_t nmemb, size_t size,
+	   int(*compar)(const void *, const void *));
+
 /* lib/time.c */
 void	udelay        (unsigned long);
 
diff --git a/lib/Makefile b/lib/Makefile
index c26536d..2d969a3 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -43,6 +43,7 @@ COBJS-$(CONFIG_LMB) += lmb.o
 COBJS-y += ldiv.o
 COBJS-$(CONFIG_MD5) += md5.o
 COBJS-y += net_utils.o
+COBJS-y += qsort.o
 COBJS-$(CONFIG_SHA1) += sha1.o
 COBJS-$(CONFIG_SHA256) += sha256.o
 COBJS-y += string.o
diff --git a/lib/qsort.c b/lib/qsort.c
new file mode 100644
index 0000000..bb47319
--- /dev/null
+++ b/lib/qsort.c
@@ -0,0 +1,69 @@
+/*
+ * Code adapted from uClibc-0.9.30.3
+ *
+ * It is therefore covered by the GNU LESSER GENERAL PUBLIC LICENSE
+ * Version 2.1, February 1999
+ *
+ * Wolfgang Denk <wd@denx.de>
+ */
+
+/* This code is derived from a public domain shell sort routine by
+ * Ray Gardner and found in Bob Stout's snippets collection.  The
+ * original code is included below in an #if 0/#endif block.
+ *
+ * I modified it to avoid the possibility of overflow in the wgap
+ * calculation, as well as to reduce the generated code size with
+ * bcc and gcc. */
+
+#include <linux/types.h>
+#if 0
+#include <assert.h>
+#else
+#define assert(arg)
+#endif
+
+void qsort(void  *base,
+           size_t nel,
+           size_t width,
+           int (*comp)(const void *, const void *))
+{
+	size_t wgap, i, j, k;
+	char tmp;
+
+	if ((nel > 1) && (width > 0)) {
+		assert(nel <= ((size_t)(-1)) / width); /* check for overflow */
+		wgap = 0;
+		do {
+			wgap = 3 * wgap + 1;
+		} while (wgap < (nel-1)/3);
+		/* From the above, we know that either wgap == 1 < nel or */
+		/* ((wgap-1)/3 < (int) ((nel-1)/3) <= (nel-1)/3 ==> wgap <  nel. */
+		wgap *= width;			/* So this can not overflow if wnel doesn't. */
+		nel *= width;			/* Convert nel to 'wnel' */
+		do {
+			i = wgap;
+			do {
+				j = i;
+				do {
+					register char *a;
+					register char *b;
+
+					j -= wgap;
+					a = j + ((char *)base);
+					b = a + wgap;
+					if ((*comp)(a, b) <= 0) {
+						break;
+					}
+					k = width;
+					do {
+						tmp = *a;
+						*a++ = *b;
+						*b++ = tmp;
+					} while (--k);
+				} while (j >= wgap);
+				i += width;
+			} while (i < nel);
+			wgap = (wgap - width)/3;
+		} while (wgap);
+	}
+}
-- 
1.7.1.1

  parent reply	other threads:[~2010-07-17 19:45 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-07-17 19:45 [U-Boot] [PATCH 0/5] New environment code Wolfgang Denk
2010-07-17 19:45 ` [U-Boot] [PATCH 1/5] Add basic errno support Wolfgang Denk
2010-07-17 21:17   ` Mike Frysinger
2010-07-17 21:34     ` Wolfgang Denk
2010-07-18 12:51     ` Jerry Van Baren
2010-07-18 13:03       ` Wolfgang Denk
2010-09-12 19:16   ` Wolfgang Denk
2010-07-17 19:45 ` Wolfgang Denk [this message]
2010-09-12 19:16   ` [U-Boot] [PATCH 2/5] Add qsort - add support for sorting data arrays Wolfgang Denk
2010-07-17 19:45 ` [U-Boot] [PATCH 3/5] Add hash table support as base for new environment code Wolfgang Denk
2010-09-12 19:16   ` Wolfgang Denk
2010-12-08  9:44   ` Mike Frysinger
2010-12-08 10:02     ` Wolfgang Denk
2010-12-08 10:52       ` Mike Frysinger
2010-07-17 19:45 ` [U-Boot] [PATCH 4/5] Remove support for CONFIG_HAS_UID and "forceenv" command Wolfgang Denk
2010-07-17 22:12   ` Sergey Kubushyn
2010-09-12 19:18   ` Wolfgang Denk
2010-07-17 19:45 ` [U-Boot] [PATCH 5/5] New implementation for internal handling of environment variables Wolfgang Denk
2010-07-17 22:48   ` [U-Boot] [PATCH] new env: fix off-by-one error in setenv command Wolfgang Denk
2010-07-20  0:38   ` [U-Boot] [PATCH 5/5] New implementation for internal handling of environment variables Kim Phillips
2010-07-20  9:40     ` Wolfgang Denk
2010-07-20 18:36       ` Kim Phillips
2010-07-20 19:01         ` Wolfgang Denk
2010-07-20 20:09           ` Wolfgang Denk
     [not found]             ` <1279658019.5685.125.camel@thunk>
2010-07-20 20:35               ` Wolfgang Denk
2010-07-20 21:08             ` Kim Phillips
2010-07-20 21:43               ` Wolfgang Denk
2010-07-20 22:00                 ` Kim Phillips
2010-07-25 21:45                   ` Wolfgang Denk
2010-07-26 23:18                     ` Kim Phillips
2010-07-20 19:11         ` Wolfgang Denk
2010-07-26 14:52   ` Matthias Fuchs
2010-07-28 21:17     ` Wolfgang Denk
2010-07-29  9:16       ` Matthias Fuchs
2010-08-03 22:48         ` Wolfgang Denk
2010-09-12 19:19   ` Wolfgang Denk
2010-12-30  1:53   ` [U-Boot] env_flash.c:saveenv() broken when env is smaller than a sector Mike Frysinger
2010-12-30  2:39     ` Mike Frysinger
2011-01-17 20:23       ` Wolfgang Denk
2010-07-17 19:49 ` [U-Boot] [PATCH 0/5] New environment code Wolfgang Denk
2010-07-17 20:56 ` Reinhard Meyer
2010-07-17 21:28   ` Mike Frysinger
2010-07-17 21:41     ` Wolfgang Denk
2010-07-18  2:18       ` Mike Frysinger
2010-07-17 21:31   ` Wolfgang Denk
2010-07-17 21:55 ` Wolfgang Denk
2010-07-18  5:32   ` Reinhard Meyer
2010-07-18 10:26     ` Wolfgang Denk
2010-10-20  8:08 ` Mike Frysinger
2010-10-20  8:19   ` Wolfgang Denk
2010-12-08  9:56 ` Mike Frysinger
2010-12-08 10:04   ` Wolfgang Denk
2010-12-08 10:19     ` Mike Frysinger

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=1279395948-25864-3-git-send-email-wd@denx.de \
    --to=wd@denx.de \
    --cc=u-boot@lists.denx.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.