summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby Bingham <koorogi@koorogi.info>2014-06-20 22:55:59 -0500
committerBobby Bingham <koorogi@koorogi.info>2014-06-20 22:55:59 -0500
commit5fa0d5023f0e2935f99b04422f3a338d77264107 (patch)
treed3ce357870a0a816cbda5add42ea14f0fce94053
Initial commit: benchmark musl's qsort
-rw-r--r--Makefile20
-rw-r--r--bench.c72
-rw-r--r--generators.c34
-rw-r--r--generators.h8
-rw-r--r--musl_qsort.c218
-rw-r--r--sorters.c6
-rw-r--r--sorters.h11
7 files changed, 369 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..6271d7c
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,20 @@
+CFLAGS = -O3 -pipe -std=c99 -D_XOPEN_SOURCE=500
+LDFLAGS =
+
+SRCS = $(sort $(wildcard *.c))
+OBJS = $(SRCS:.c=.o)
+BINS = bench
+
+.PHONY: all clean
+
+all: $(BINS)
+
+clean:
+ rm -f $(BINS)
+ rm -f $(OBJS)
+
+bench: $(OBJS)
+ $(CC) $(LDFLAGS) $^ -o $@
+
+%.o: %.c
+ $(CC) $(CFLAGS) -c $< -o $@
diff --git a/bench.c b/bench.c
new file mode 100644
index 0000000..fb46b94
--- /dev/null
+++ b/bench.c
@@ -0,0 +1,72 @@
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#include "generators.h"
+#include "sorters.h"
+
+#define MIN_SIZE 10000
+#define MAX_SIZE 10000000
+
+static int buffer[MAX_SIZE];
+static unsigned long comparisons;
+
+static int compare(const void *a, const void *b)
+{
+ const int *aa = a;
+ const int *bb = b;
+
+ comparisons++;
+ if (*aa < *bb)
+ return -1;
+ else if (*aa > *bb)
+ return 1;
+ else
+ return 0;
+}
+
+#define CMP_WIDTH 12
+#define MS_WIDTH 6
+#define SORT_WIDTH 10
+#define GEN_WIDTH (CMP_WIDTH + MS_WIDTH + 1)
+#define SIZE_WIDTH 10
+
+static inline unsigned long timediff_ms(struct timespec *start, struct timespec *stop)
+{
+ return 1000 * (stop->tv_sec - start->tv_sec) + (stop->tv_nsec - start->tv_nsec) / 1000000;
+}
+
+int main()
+{
+ struct timespec start, stop;
+
+ printf("%-*s ", SORT_WIDTH, "");
+ for (const struct generator *g = generators; g->name; g++)
+ printf("%*s ", GEN_WIDTH, g->name);
+ printf(" %*s\n%-*s ", SIZE_WIDTH, "elements", SORT_WIDTH, "");
+ for (const struct generator *g = generators; g->name; g++)
+ printf("%*s %*s ", CMP_WIDTH, "compares", MS_WIDTH, "ms");
+ puts("");
+
+ for (const struct sorter *s = sorters; s->name; s++) {
+ for (size_t size = MAX_SIZE; size >= MIN_SIZE; size /= 10) {
+ printf("%-*s ", SORT_WIDTH, size == MAX_SIZE ? s->name : "");
+ for (const struct generator *g = generators; g->name; g++) {
+ comparisons = 0;
+ g->func(buffer, size);
+
+ if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start)) abort();
+ s->func(buffer, size, sizeof(int), compare);
+ if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &stop)) abort();
+
+ printf("%*lu %*lu ", CMP_WIDTH, comparisons, MS_WIDTH, timediff_ms(&start, &stop));
+ }
+ printf(" %*zu\n", SIZE_WIDTH, size);
+ }
+ puts("");
+ }
+
+ return 0;
+}
+
diff --git a/generators.c b/generators.c
new file mode 100644
index 0000000..002b128
--- /dev/null
+++ b/generators.c
@@ -0,0 +1,34 @@
+#include <limits.h>
+#include <stddef.h>
+#include <stdlib.h>
+
+#include "generators.h"
+
+static void generate_random(int *buffer, size_t size)
+{
+ srandom(1);
+ for (size_t i = 0; i < size; i++) buffer[i] = random();
+}
+
+static void generate_sorted(int *buffer, size_t size)
+{
+ for (size_t i = 0; i < size; i++) buffer[i] = i;
+}
+
+static void generate_reverse(int *buffer, size_t size)
+{
+ for (size_t i = 0; i < size; i++) buffer[i] = INT_MAX - i;
+}
+
+static void generate_constant(int *buffer, size_t size)
+{
+ for (size_t i = 0; i < size; i++) buffer[i] = 42;
+}
+
+const struct generator generators[] = {
+ { .name = "random", .func = generate_random },
+ { .name = "sorted", .func = generate_sorted },
+ { .name = "reverse", .func = generate_reverse },
+ { .name = "constant", .func = generate_constant },
+ { 0 }
+};
diff --git a/generators.h b/generators.h
new file mode 100644
index 0000000..e08a0be
--- /dev/null
+++ b/generators.h
@@ -0,0 +1,8 @@
+#include <stddef.h>
+
+typedef void (*generatorfn)(int *, size_t);
+
+extern const struct generator {
+ const char *name;
+ generatorfn func;
+} generators[];
diff --git a/musl_qsort.c b/musl_qsort.c
new file mode 100644
index 0000000..21a39bd
--- /dev/null
+++ b/musl_qsort.c
@@ -0,0 +1,218 @@
+/* Copyright (C) 2011 by Valentin Ochs
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+/* Minor changes by Rich Felker for integration in musl, 2011-04-27. */
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "sorters.h"
+
+static inline int ntz(uint64_t x)
+{
+ __asm__( "bsf %1,%0" : "=r"(x) : "r"(x) );
+ return x;
+}
+
+static inline int pntz(size_t p[2]) {
+ int r = ntz(p[0] - 1);
+ if(r != 0 || (r = 8*sizeof(size_t) + ntz(p[1])) != 8*sizeof(size_t)) {
+ return r;
+ }
+ return 0;
+}
+
+static void cycle(size_t width, unsigned char* ar[], int n)
+{
+ unsigned char tmp[256];
+ size_t l;
+ int i;
+
+ if(n < 2) {
+ return;
+ }
+
+ ar[n] = tmp;
+ while(width) {
+ l = sizeof(tmp) < width ? sizeof(tmp) : width;
+ memcpy(ar[n], ar[0], l);
+ for(i = 0; i < n; i++) {
+ memcpy(ar[i], ar[i + 1], l);
+ ar[i] += l;
+ }
+ width -= l;
+ }
+}
+
+/* shl() and shr() need n > 0 */
+static inline void shl(size_t p[2], int n)
+{
+ if(n >= 8 * sizeof(size_t)) {
+ n -= 8 * sizeof(size_t);
+ p[1] = p[0];
+ p[0] = 0;
+ }
+ p[1] <<= n;
+ p[1] |= p[0] >> (sizeof(size_t) * 8 - n);
+ p[0] <<= n;
+}
+
+static inline void shr(size_t p[2], int n)
+{
+ if(n >= 8 * sizeof(size_t)) {
+ n -= 8 * sizeof(size_t);
+ p[0] = p[1];
+ p[1] = 0;
+ }
+ p[0] >>= n;
+ p[0] |= p[1] << (sizeof(size_t) * 8 - n);
+ p[1] >>= n;
+}
+
+static void sift(unsigned char *head, size_t width, cmpfun cmp, int pshift, size_t lp[])
+{
+ unsigned char *rt, *lf;
+ unsigned char *ar[14 * sizeof(size_t) + 1];
+ int i = 1;
+
+ ar[0] = head;
+ while(pshift > 1) {
+ rt = head - width;
+ lf = head - width - lp[pshift - 2];
+
+ if((*cmp)(ar[0], lf) >= 0 && (*cmp)(ar[0], rt) >= 0) {
+ break;
+ }
+ if((*cmp)(lf, rt) >= 0) {
+ ar[i++] = lf;
+ head = lf;
+ pshift -= 1;
+ } else {
+ ar[i++] = rt;
+ head = rt;
+ pshift -= 2;
+ }
+ }
+ cycle(width, ar, i);
+}
+
+static void trinkle(unsigned char *head, size_t width, cmpfun cmp, size_t pp[2], int pshift, int trusty, size_t lp[])
+{
+ unsigned char *stepson,
+ *rt, *lf;
+ size_t p[2];
+ unsigned char *ar[14 * sizeof(size_t) + 1];
+ int i = 1;
+ int trail;
+
+ p[0] = pp[0];
+ p[1] = pp[1];
+
+ ar[0] = head;
+ while(p[0] != 1 || p[1] != 0) {
+ stepson = head - lp[pshift];
+ if((*cmp)(stepson, ar[0]) <= 0) {
+ break;
+ }
+ if(!trusty && pshift > 1) {
+ rt = head - width;
+ lf = head - width - lp[pshift - 2];
+ if((*cmp)(rt, stepson) >= 0 || (*cmp)(lf, stepson) >= 0) {
+ break;
+ }
+ }
+
+ ar[i++] = stepson;
+ head = stepson;
+ trail = pntz(p);
+ shr(p, trail);
+ pshift += trail;
+ trusty = 0;
+ }
+ if(!trusty) {
+ cycle(width, ar, i);
+ sift(head, width, cmp, pshift, lp);
+ }
+}
+
+void qsort_musl(void *base, size_t nel, size_t width, cmpfun cmp)
+{
+ size_t lp[12*sizeof(size_t)];
+ size_t i, size = width * nel;
+ unsigned char *head, *high;
+ size_t p[2] = {1, 0};
+ int pshift = 1;
+ int trail;
+
+ if (!size) return;
+
+ head = base;
+ high = head + size - width;
+
+ /* Precompute Leonardo numbers, scaled by element width */
+ for(lp[0]=lp[1]=width, i=2; (lp[i]=lp[i-2]+lp[i-1]+width) < size; i++);
+
+ while(head < high) {
+ if((p[0] & 3) == 3) {
+ sift(head, width, cmp, pshift, lp);
+ shr(p, 2);
+ pshift += 2;
+ } else {
+ if(lp[pshift - 1] >= high - head) {
+ trinkle(head, width, cmp, p, pshift, 0, lp);
+ } else {
+ sift(head, width, cmp, pshift, lp);
+ }
+
+ if(pshift == 1) {
+ shl(p, 1);
+ pshift = 0;
+ } else {
+ shl(p, pshift - 1);
+ pshift = 1;
+ }
+ }
+
+ p[0] |= 1;
+ head += width;
+ }
+
+ trinkle(head, width, cmp, p, pshift, 0, lp);
+
+ while(pshift != 1 || p[0] != 1 || p[1] != 0) {
+ if(pshift <= 1) {
+ trail = pntz(p);
+ shr(p, trail);
+ pshift += trail;
+ } else {
+ shl(p, 2);
+ pshift -= 2;
+ p[0] ^= 7;
+ shr(p, 1);
+ trinkle(head - lp[pshift] - width, width, cmp, p, pshift + 1, 1, lp);
+ shl(p, 1);
+ p[0] |= 1;
+ trinkle(head - width, width, cmp, p, pshift, 1, lp);
+ }
+ head -= width;
+ }
+}
diff --git a/sorters.c b/sorters.c
new file mode 100644
index 0000000..a630136
--- /dev/null
+++ b/sorters.c
@@ -0,0 +1,6 @@
+#include "sorters.h"
+
+const struct sorter sorters[] = {
+ { .name = "musl", .func = qsort_musl },
+ { 0 }
+};
diff --git a/sorters.h b/sorters.h
new file mode 100644
index 0000000..1a2397a
--- /dev/null
+++ b/sorters.h
@@ -0,0 +1,11 @@
+#include <stddef.h>
+
+typedef int (*cmpfun)(const void *, const void *);
+typedef void (*sorter)(void *, size_t, size_t, cmpfun);
+
+void qsort_musl(void *, size_t, size_t, cmpfun);
+
+extern const struct sorter {
+ const char *name;
+ sorter func;
+} sorters[];