modernc.org/ccgo/v3@v3.16.14/lib/testdata/CompCert-3.6/test/c/nsievebits.c (about)

     1  /*
     2   * The Great Computer Language Shootout
     3   * http://shootout.alioth.debian.org/
     4   *
     5   * Written by Dima Dorfman, 2004
     6   * Compile: gcc -std=c99 -O2 -o nsieve_bits_gcc nsieve_bits.c
     7   */
     8  
     9  #include <stdio.h>
    10  #include <stdlib.h>
    11  #include <string.h>
    12  
    13  typedef unsigned int bits;
    14  #define	NBITS	(8 * sizeof(bits))
    15  
    16  static unsigned int
    17  nsieve(unsigned int m)
    18  {
    19  	unsigned int count, i, j;
    20  	bits * a;
    21          a = malloc((m / NBITS) * sizeof(bits));
    22  	memset(a, (1 << 8) - 1, (m / NBITS) * sizeof(bits));
    23  	count = 0;
    24  	for (i = 2; i < m; ++i)
    25  		if (a[i / NBITS] & (1 << i % NBITS)) {
    26  			for (j = i + i; j < m; j += i)
    27  				a[j / NBITS] &= ~(1 << j % NBITS);
    28  			++count;
    29  		}
    30  	return (count);
    31  }
    32  
    33  #define NITER 2
    34  
    35  static void
    36  test(unsigned int n)
    37  {
    38  	unsigned int count, m;
    39          int i;
    40  
    41  	m = (1 << n) * 10000;
    42          for (i = 0; i < NITER; i++) { count = nsieve(m); }
    43  	printf("Primes up to %8u %8u\n", m, count);
    44  }
    45  
    46  int
    47  main(int ac, char **av)
    48  {
    49  	unsigned int n;
    50  
    51  	n = ac < 2 ? 9 : atoi(av[1]);
    52  	test(n);
    53  	if (n >= 1)
    54  		test(n - 1);
    55  	if (n >= 2)
    56  		test(n - 2);
    57  	exit(0);
    58  }
    59  /****
    60   build & benchmark results
    61  
    62  BUILD COMMANDS FOR: nsievebits.gcc
    63  
    64  Fri Sep 15 06:30:15 PDT 2006
    65  
    66  /usr/bin/gcc -pipe -Wall -O3 -fomit-frame-pointer -funroll-loops -march=pentium4  nsievebits.c -o nsievebits.gcc_run
    67  
    68  =================================================================
    69  COMMAND LINE (%A is single numeric argument):
    70  
    71  nsievebits.gcc_run %A
    72  
    73  
    74  PROGRAM OUTPUT
    75  ==============
    76  Primes up to  5120000   356244
    77  Primes up to  2560000   187134
    78  Primes up to  1280000    98610
    79  *****/