summaryrefslogtreecommitdiff
path: root/test/c/nsievebits.c
diff options
context:
space:
mode:
authorGravatar xleroy <xleroy@fca1b0fc-160b-0410-b1d3-a4f43f01ea2e>2006-09-17 12:04:56 +0000
committerGravatar xleroy <xleroy@fca1b0fc-160b-0410-b1d3-a4f43f01ea2e>2006-09-17 12:04:56 +0000
commit2ec5b3fb2ccb0120be641e077089f3da5e53d8a3 (patch)
treed1e6f6a9a3d99f0095dc96fe320214de68918158 /test/c/nsievebits.c
parente37d620f5b9b05e16563545cba9c538f8d31c746 (diff)
Davantage de tests
git-svn-id: https://yquem.inria.fr/compcert/svn/compcert/trunk@104 fca1b0fc-160b-0410-b1d3-a4f43f01ea2e
Diffstat (limited to 'test/c/nsievebits.c')
-rw-r--r--test/c/nsievebits.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/test/c/nsievebits.c b/test/c/nsievebits.c
new file mode 100644
index 0000000..ad96011
--- /dev/null
+++ b/test/c/nsievebits.c
@@ -0,0 +1,77 @@
+/*
+ * The Great Computer Language Shootout
+ * http://shootout.alioth.debian.org/
+ *
+ * Written by Dima Dorfman, 2004
+ * Compile: gcc -std=c99 -O2 -o nsieve_bits_gcc nsieve_bits.c
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef unsigned int bits;
+#define NBITS (8 * sizeof(bits))
+
+static unsigned long
+nsieve(unsigned long m)
+{
+ unsigned long count, i, j;
+ bits * a;
+ a = malloc((m / NBITS) * sizeof(bits));
+ memset(a, (1 << 8) - 1, (m / NBITS) * sizeof(bits));
+ count = 0;
+ for (i = 2; i < m; ++i)
+ if (a[i / NBITS] & (1 << i % NBITS)) {
+ for (j = i + i; j < m; j += i)
+ a[j / NBITS] &= ~(1 << j % NBITS);
+ ++count;
+ }
+ return (count);
+}
+
+static void
+test(unsigned long n)
+{
+ unsigned long count, m;
+
+ m = (1 << n) * 10000;
+ count = nsieve(m);
+ printf("Primes up to %8ju %8ju\n", m, count);
+}
+
+int
+main(int ac, char **av)
+{
+ unsigned long n;
+ char *cp;
+
+ n = ac < 2 ? 9 : strtoul(av[1], &cp, 10);
+ test(n);
+ if (n >= 1)
+ test(n - 1);
+ if (n >= 2)
+ test(n - 2);
+ exit(0);
+}
+/****
+ build & benchmark results
+
+BUILD COMMANDS FOR: nsievebits.gcc
+
+Fri Sep 15 06:30:15 PDT 2006
+
+/usr/bin/gcc -pipe -Wall -O3 -fomit-frame-pointer -funroll-loops -march=pentium4 nsievebits.c -o nsievebits.gcc_run
+
+=================================================================
+COMMAND LINE (%A is single numeric argument):
+
+nsievebits.gcc_run %A
+
+
+PROGRAM OUTPUT
+==============
+Primes up to 5120000 356244
+Primes up to 2560000 187134
+Primes up to 1280000 98610
+*****/