[llvm-commits] CVS: llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/ary3.cpp echo.cpp except.cpp hash.cpp hash2.cpp heapsort.cpp hello.cpp matrix.cpp methcall.cpp moments.cpp nestedloop.cpp objinst.cpp prodcons.cpp random.cpp sieve.cpp strcat.cpp sumcol.cpp wc.cpp

Chris Lattner lattner at cs.uiuc.edu
Mon Oct 6 20:13:01 PDT 2003


Changes in directory llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++:

ary3.cpp added (r1.1)
echo.cpp added (r1.1)
except.cpp added (r1.1)
hash.cpp added (r1.1)
hash2.cpp added (r1.1)
heapsort.cpp added (r1.1)
hello.cpp added (r1.1)
matrix.cpp added (r1.1)
methcall.cpp added (r1.1)
moments.cpp added (r1.1)
nestedloop.cpp added (r1.1)
objinst.cpp added (r1.1)
prodcons.cpp added (r1.1)
random.cpp added (r1.1)
sieve.cpp added (r1.1)
strcat.cpp added (r1.1)
sumcol.cpp added (r1.1)
wc.cpp added (r1.1)

---
Log message:

Add tests which did not have to be "ported" to our C++ fe


---
Diffs of the changes:  (+968 -36)

Index: llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/ary3.cpp
diff -c /dev/null llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/ary3.cpp:1.1
*** /dev/null	Mon Oct  6 20:12:13 2003
--- llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/ary3.cpp	Mon Oct  6 20:11:33 2003
***************
*** 0 ****
--- 1,26 ----
+ // -*- mode: c++ -*-
+ // $Id: ary3.cpp,v 1.1 2003/10/07 01:11:33 lattner Exp $
+ // http://www.bagley.org/~doug/shootout/
+ 
+ #include <iostream>
+ #include <vector>
+ 
+ using namespace std;
+ 
+ int main(int argc, char *argv[]) {
+     int i, k, n = ((argc == 2) ? atoi(argv[1]) : 1);
+     typedef vector<int> ARY;
+     ARY x(n);
+     ARY y(n);
+ 
+     for (i=0; i<n; i++) {
+ 	x[i] = i + 1;
+     }
+     for (k=0; k<1000; k++) {
+ 	for (int i = n - 1; i >= 0; --i) {
+ 	    y[i] += x[i];
+ 	}
+     }
+ 
+     cout << y[0] << " " << y.back() << endl;
+ }


Index: llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/echo.cpp
diff -c /dev/null llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/echo.cpp:1.1
*** /dev/null	Mon Oct  6 20:12:13 2003
--- llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/echo.cpp	Mon Oct  6 20:11:33 2003
***************
*** 0 ****
--- 1,152 ----
+ // -*- mode: c++ -*-
+ // $Id: echo.cpp,v 1.1 2003/10/07 01:11:33 lattner Exp $
+ // http://www.bagley.org/~doug/shootout/
+ 
+ #include <stdio.h>
+ #include <stdlib.h>
+ #include <string.h>
+ #include <unistd.h>
+ #include <signal.h>
+ #include <errno.h>
+ #include <sys/types.h>
+ #include <sys/socket.h>
+ #include <sys/wait.h>
+ #include <netinet/in.h>
+ 
+ using namespace std;
+ 
+ #define DATA "Hello there sailor\n"
+ 
+ void myabort (char *m) { fprintf(stderr, "%s\n", m); exit(1); }
+ void sysabort (char *m) { perror(m); exit(1); }
+ 
+ int sigchld = 0;
+ void reaper (int sig) { sigchld = 1; }
+ 
+ int
+ server_sock () {
+     int ss, optval = 1;
+     struct sockaddr_in sin;
+     if ((ss = socket(PF_INET, SOCK_STREAM, 0)) == -1)
+ 	sysabort("server/socket");
+     if (setsockopt(ss, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1)
+ 	sysabort("server/setsockopt");
+     memset(&sin,0,sizeof(sin));
+     sin.sin_family = AF_INET;
+     sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+     sin.sin_port = 0;
+     if (bind(ss, (sockaddr *)&sin, sizeof(sin)) == -1)
+ 	sysabort("server/bind");
+     listen(ss, 2);
+     return(ss);
+ }
+ 
+ 
+ int
+ get_port (int sock) {
+     struct sockaddr_in sin;
+     socklen_t slen = sizeof(sin);
+     if (getsockname(sock, (sockaddr *)&sin, &slen) == -1)
+ 	sysabort("server/getsockname");
+     return(sin.sin_port);
+ }    
+ 
+ 
+ int
+ client_sock (int port) {
+     struct sockaddr_in sin;
+     int sock;
+     if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1)
+ 	sysabort("client/socket");
+     sin.sin_family = AF_INET;
+     sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+     sin.sin_port = port;
+     if (connect(sock, (sockaddr *)&sin, sizeof(sin)) == -1)
+ 	sysabort("client/connect");
+     return(sock);
+ }
+ 
+ 
+ void
+ echo_client (int n, int port) {
+     int i, sock, olen, len, nwritten, nread;
+     char *offset, obuf[64], ibuf[64];
+     char *end = ibuf + sizeof(ibuf);
+ 
+     sock = client_sock(port);
+     strcpy(obuf, DATA);
+     olen = strlen(obuf);
+     for (i=0; i<n; i++) {
+ 	len = olen;
+ 	offset = obuf;
+ 	while (len > 0) {
+ 	    if ((nwritten = write(sock, offset, len)) == -1)
+ 		sysabort("client/write");
+ 	    offset += nwritten;
+ 	    len -= nwritten;
+ 	}
+ 	offset = ibuf;
+ 	while ((nread = read(sock, offset, (end - offset))) > 0) {
+ 	    offset += nread;
+ 	    if (*(offset-1) == '\n') break;
+ 	}
+ 	if (nread == -1)
+ 	    sysabort("client/read");
+ 	*offset = 0;
+ 	if ((strcmp(obuf, ibuf)) != 0) {
+ 	    char mbuf[128];
+ 	    sprintf(mbuf, "client: \"%s\" ne \"%s\"", obuf, ibuf);
+ 	    myabort(mbuf);
+ 	}
+     }
+     close(sock);
+ }
+ 
+ 
+ void
+ echo_server (int n) {
+     int ssock, csock, len, nwritten, total_bytes;
+     pid_t pid;
+     char buf[64], *offset;
+     struct sockaddr_in sin;
+     socklen_t slen = sizeof(sin);
+     int status;
+ 
+     ssock = server_sock();
+     signal(SIGCHLD, reaper);
+     if ((pid = fork()) == -1)
+ 	sysabort("server/fork");
+     if (pid) {
+ 	/* parent is server */
+ 	if ((csock = accept(ssock, (sockaddr *)&sin, &slen)) == -1)
+ 	    sysabort("server/accept");
+ 	total_bytes = 0;
+ 	while ((len = read(csock, buf, sizeof(buf))) > 0) {
+ 	    if (sigchld) myabort("server/sigchld");
+ 	    offset = buf;
+ 	    total_bytes += len;
+ 	    while (len > 0) {
+ 		if ((nwritten = write(csock, offset, len)) == -1)
+ 		    sysabort("server/write");
+ 		offset += nwritten;
+ 		len -= nwritten;
+ 	    }
+ 	}
+ 	if (len == -1)
+ 	    sysabort("server/read");
+ 	close(csock);
+ 	fprintf(stdout, "server processed %d bytes\n", total_bytes);
+     } else {
+ 	/* child is client */
+ 	echo_client(n, get_port(ssock));
+     }
+     wait(&status);
+ }
+ 
+ 
+ int
+ main(int argc, char *argv[]) {
+     int n = ((argc == 2) ? atoi(argv[1]) : 1);
+     echo_server(n);
+     return(0);
+ }


Index: llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/except.cpp
diff -c /dev/null llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/except.cpp:1.1
*** /dev/null	Mon Oct  6 20:12:13 2003
--- llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/except.cpp	Mon Oct  6 20:11:33 2003
***************
*** 0 ****
--- 1,69 ----
+ // -*- mode: c++ -*-
+ // $Id: except.cpp,v 1.1 2003/10/07 01:11:33 lattner Exp $
+ // http://www.bagley.org/~doug/shootout/
+ // from Bill Lear
+ 
+ #include <iostream>
+ #include <cstdlib>
+ #include <cstdio>
+ 
+ using namespace std;
+ 
+ size_t HI = 0;
+ size_t LO = 0;
+ 
+ class Hi_exception {
+ public:
+     explicit Hi_exception(size_t _n) : n(_n) {}
+     const char* what() { sprintf(N, "%d", n); return N; }
+ private:
+     size_t n; char N[8];
+ };
+ 
+ class Lo_exception {
+ public:
+     explicit Lo_exception(size_t _n) : n(_n) {}
+     const char* what() { sprintf(N, "%d", n); return N; }
+ private:
+     size_t n; char N[8];
+ };
+ 
+ void blowup(size_t num) {
+     if (num % 2) {
+         throw Lo_exception(num);
+     }
+     throw Hi_exception(num);
+ }
+ 
+ void lo_function(size_t num) {
+     try {
+         blowup(num);
+     } catch(const Lo_exception& ex) {
+         ++LO;
+     }
+ }
+ 
+ void hi_function(size_t num) {
+     try {
+         lo_function(num);
+     } catch(const Hi_exception& ex) {
+         ++HI;
+     }
+ }
+ 
+ void some_function(size_t num) {
+     try {
+         hi_function(num);
+     } catch (...) {
+         cerr << "We shouldn't get here\n"; exit(1);
+     }
+ }
+ 
+ int
+ main(int argc, char* argv[]) {
+     size_t NUM = (argc == 2 ? (atoi(argv[1]) < 1 ? 1 : atoi(argv[1])): 1);
+     while (NUM--) {
+         some_function(NUM);
+     }
+     cout << "Exceptions: HI=" << HI << " / " << "LO=" << LO << endl;
+ }


Index: llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/hash.cpp
diff -c /dev/null llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/hash.cpp:1.1
*** /dev/null	Mon Oct  6 20:12:13 2003
--- llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/hash.cpp	Mon Oct  6 20:11:33 2003
***************
*** 0 ****
--- 1,36 ----
+ // -*- mode: c++ -*-
+ // $Id: hash.cpp,v 1.1 2003/10/07 01:11:33 lattner Exp $
+ // http://www.bagley.org/~doug/shootout/
+ 
+ #include <stdio.h>
+ #include <iostream>
+ #include <hash_map.h>
+ 
+ using namespace std;
+ 
+ struct eqstr {
+     bool operator()(const char* s1, const char* s2) const {
+ 	return strcmp(s1, s2) == 0;
+     }
+ };
+ 
+ int
+ main(int argc, char *argv[]) {
+     int n = ((argc == 2) ? atoi(argv[1]) : 1);
+     char buf[16];
+     typedef hash_map<const char*, int, hash<const char*>, eqstr> HM;
+     HM X;
+ 
+     for (int i=1; i<=n; i++) {
+ 	sprintf(buf, "%x", i);
+ 	X[strdup(buf)] = i;
+     }
+ 
+     int c = 0;
+     for (int i=n; i>0; i--) {
+ 	sprintf(buf, "%d", i);
+ 	if (X[strdup(buf)]) c++;
+     }
+ 
+     cout << c << endl;
+ }


Index: llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/hash2.cpp
diff -c /dev/null llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/hash2.cpp:1.1
*** /dev/null	Mon Oct  6 20:12:13 2003
--- llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/hash2.cpp	Mon Oct  6 20:11:33 2003
***************
*** 0 ****
--- 1,35 ----
+ // -*- mode: c++ -*-
+ // $Id: hash2.cpp,v 1.1 2003/10/07 01:11:33 lattner Exp $
+ // http://www.bagley.org/~doug/shootout/
+ 
+ #include <stdio.h>
+ #include <iostream>
+ #include <hash_map.h>
+ 
+ using namespace std;
+ 
+ struct eqstr {
+     bool operator()(const char* s1, const char* s2) const {
+ 	return strcmp(s1, s2) == 0;
+     }
+ };
+ 
+ int
+ main(int argc, char *argv[]) {
+     int n = ((argc == 2) ? atoi(argv[1]) : 1);
+     char buf[16];
+     typedef hash_map<const char*, int, hash<const char*>, eqstr> HM;
+     HM hash1, hash2;
+ 
+     for (int i=0; i<10000; i++) {
+ 	sprintf(buf, "foo_%d", i);
+ 	hash1[strdup(buf)] = i;
+     }
+     for (int i=0; i<n; i++) {
+ 	for (HM::iterator k = hash1.begin(); k != hash1.end(); ++k) {
+ 	    hash2[(*k).first] += hash1[(*k).first];
+ 	}
+     }
+     cout << hash1["foo_1"] << " " << hash1["foo_9999"] << " "
+ 	 << hash2["foo_1"] << " " << hash2["foo_9999"] << endl;
+ }


Index: llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/heapsort.cpp
diff -c /dev/null llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/heapsort.cpp:1.1
*** /dev/null	Mon Oct  6 20:12:13 2003
--- llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/heapsort.cpp	Mon Oct  6 20:11:33 2003
***************
*** 0 ****
--- 1,72 ----
+ // -*- mode: c++ -*-
+ // $Id: heapsort.cpp,v 1.1 2003/10/07 01:11:33 lattner Exp $
+ // http://www.bagley.org/~doug/shootout/
+ 
+ #include <iostream>
+ #include <stdlib.h>
+ #include <math.h>
+ 
+ using namespace std;
+ 
+ #define IM 139968
+ #define IA   3877
+ #define IC  29573
+ 
+ double
+ gen_random(double max) {
+     static long last = 42;
+     return( max * (last = (last * IA + IC) % IM) / IM );
+ }
+ 
+ void
+ heapsort(int n, double *ra) {
+     int i, j;
+     int ir = n;
+     int l = (n >> 1) + 1;
+     double rra;
+ 
+     for (;;) {
+ 	if (l > 1) {
+ 	    rra = ra[--l];
+ 	} else {
+ 	    rra = ra[ir];
+ 	    ra[ir] = ra[1];
+ 	    if (--ir == 1) {
+ 		ra[1] = rra;
+ 		return;
+ 	    }
+ 	}
+ 	i = l;
+ 	j = l << 1;
+ 	while (j <= ir) {
+ 	    if (j < ir && ra[j] < ra[j+1]) { ++j; }
+ 	    if (rra < ra[j]) {
+ 		ra[i] = ra[j];
+ 		j += (i = j);
+ 	    } else {
+ 		j = ir + 1;
+ 	    }
+ 	}
+ 	ra[i] = rra;
+     }
+ }
+ 
+ int
+ main(int argc, char *argv[]) {
+     int N = ((argc == 2) ? atoi(argv[1]) : 1);
+     double *ary;
+     int i;
+     
+     /* create an array of N random doubles */
+     ary = (double *)malloc((N+1) * sizeof(double));
+     for (i=1; i<=N; i++) {
+ 	ary[i] = gen_random(1);
+     }
+ 
+     heapsort(N, ary);
+ 
+     printf("%.10f\n", ary[N]);
+     free(ary);
+     return(0);
+ }
+ 


Index: llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/hello.cpp
diff -c /dev/null llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/hello.cpp:1.1
*** /dev/null	Mon Oct  6 20:12:13 2003
--- llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/hello.cpp	Mon Oct  6 20:11:33 2003
***************
*** 0 ****
--- 1,12 ----
+ // -*- mode: c++ -*-
+ // $Id: hello.cpp,v 1.1 2003/10/07 01:11:33 lattner Exp $
+ // http://www.bagley.org/~doug/shootout/
+ 
+ #include <iostream>
+ 
+ using namespace std;
+ 
+ int main() {
+     cout << "hello world" << endl;
+     return(0);
+ }


Index: llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/matrix.cpp
diff -c /dev/null llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/matrix.cpp:1.1
*** /dev/null	Mon Oct  6 20:12:13 2003
--- llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/matrix.cpp	Mon Oct  6 20:11:33 2003
***************
*** 0 ****
--- 1,66 ----
+ // -*- mode: c++ -*-
+ // $Id: matrix.cpp,v 1.1 2003/10/07 01:11:33 lattner Exp $
+ // http://www.bagley.org/~doug/shootout/
+ 
+ #include <iostream>
+ #include <stdlib.h>
+ 
+ using namespace std;
+ 
+ #define SIZE 30
+ 
+ int **mkmatrix(int rows, int cols) {
+     int i, j, count = 1;
+     int **m = (int **) malloc(rows * sizeof(int *));
+     for (i=0; i<rows; i++) {
+ 	m[i] = (int *) malloc(cols * sizeof(int));
+ 	for (j=0; j<cols; j++) {
+ 	    m[i][j] = count++;
+ 	}
+     }
+     return(m);
+ }
+ 
+ void zeromatrix(int rows, int cols, int **m) {
+     int i, j;
+     for (i=0; i<rows; i++)
+ 	for (j=0; j<cols; j++)
+ 	    m[i][j] = 0;
+ }
+ 
+ void freematrix(int rows, int **m) {
+     while (--rows > -1) { free(m[rows]); }
+     free(m);
+ }
+ 
+ int **mmult(int rows, int cols, int **m1, int **m2, int **m3) {
+     int i, j, k, val;
+     for (i=0; i<rows; i++) {
+ 	for (j=0; j<cols; j++) {
+ 	    val = 0;
+ 	    for (k=0; k<cols; k++) {
+ 		val += m1[i][k] * m2[k][j];
+ 	    }
+ 	    m3[i][j] = val;
+ 	}
+     }
+     return(m3);
+ }
+ 
+ int main(int argc, char *argv[]) {
+     int i, n = ((argc == 2) ? atoi(argv[1]) : 1);
+ 	
+     int **m1 = mkmatrix(SIZE, SIZE);
+     int **m2 = mkmatrix(SIZE, SIZE);
+     int **mm = mkmatrix(SIZE, SIZE);
+ 
+     for (i=0; i<n; i++) {
+ 	mm = mmult(SIZE, SIZE, m1, m2, mm);
+     }
+     cout << mm[0][0] << " " << mm[2][3] << " " << mm[3][2] << " " << mm[4][4] << endl;
+ 
+     freematrix(SIZE, m1);
+     freematrix(SIZE, m2);
+     freematrix(SIZE, mm);
+     return(0);
+ }


Index: llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/methcall.cpp
diff -c /dev/null llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/methcall.cpp:1.1
*** /dev/null	Mon Oct  6 20:12:13 2003
--- llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/methcall.cpp	Mon Oct  6 20:11:33 2003
***************
*** 0 ****
--- 1,65 ----
+ // -*- mode: c++ -*-
+ // $Id: methcall.cpp,v 1.1 2003/10/07 01:11:33 lattner Exp $
+ // http://www.bagley.org/~doug/shootout/
+ 
+ // with some help from Bill Lear
+ 
+ #include <stdlib.h>
+ #include <iostream>
+ 
+ using namespace std;
+ 
+ class Toggle {
+ public:
+     Toggle(bool start_state) : state(start_state) { }
+     virtual ~Toggle() {  }
+     bool value() {
+ 	return(state);
+     }
+     virtual Toggle& activate() {
+ 	state = !state;
+ 	return(*this);
+     }
+     bool state;
+ };
+ 
+ class NthToggle : public Toggle {
+ public:
+     NthToggle(bool start_state, int max_counter) :
+ 	Toggle(start_state), count_max(max_counter), counter(0) {
+     }
+     Toggle& activate() {
+ 	if (++this->counter >= this->count_max) {
+ 	    state = !state;
+ 	    counter = 0;
+ 	}
+ 	return(*this);
+     }
+ private:
+     int count_max;
+     int counter;
+ };
+ 
+ 
+ int
+ main(int argc, char *argv[]) {
+     int n = ((argc == 2) ? atoi(argv[1]) : 1);
+ 
+     bool val = true;
+     Toggle *toggle = new Toggle(val);
+     for (int i=0; i<n; i++) {
+ 	val = toggle->activate().value();
+     }
+     cout << ((val) ? "true" : "false") << endl;
+     delete toggle;
+ 
+     val = true;
+     NthToggle *ntoggle = new NthToggle(val, 3);
+     for (int i=0; i<n; i++) {
+ 	val = ntoggle->activate().value();
+     }
+     cout << ((val) ? "true" : "false") << endl;
+     delete ntoggle;
+ 
+     return 0;
+ }


Index: llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/moments.cpp
diff -c /dev/null llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/moments.cpp:1.1
*** /dev/null	Mon Oct  6 20:12:13 2003
--- llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/moments.cpp	Mon Oct  6 20:11:33 2003
***************
*** 0 ****
--- 1,85 ----
+ // -*- mode: c++ -*-
+ // $Id
+ // http://www.bagley.org/~doug/shootout/
+ // Calculate statistical moments of a region, from Bill Lear
+ // Modified by Tamás Benkõ
+ // Further modified by Tom Hyer
+ 
+ #include <vector>
+ #include <numeric>
+ #include <iterator>
+ #include <algorithm>
+ #include <cstdio>
+ #include <cstdlib>
+ #include <cmath>
+ 
+ using namespace std;
+ 
+ template <class T>
+ struct moments {
+ public:
+     template <class InputIterator>
+     moments(InputIterator begin, InputIterator end)
+         : median(0.0), mean(0.0), average_deviation(0.0),
+           standard_deviation(0.0), variance(0.0),
+           skew(0.0), kurtosis(0.0)
+         {
+             T sum = accumulate(begin, end, 0.0);
+             size_t N = end - begin;
+             mean = sum / N;
+             for (InputIterator i = begin; i != end; ++i) {
+                 T deviation = *i - mean;
+                 average_deviation += fabs(deviation);
+ 				T temp = deviation * deviation;
+                 variance += temp;
+ 				temp *= deviation;
+                 skew += temp;
+                 kurtosis += temp * deviation;
+             }
+             average_deviation /= N;
+             variance /= (N - 1);
+             standard_deviation = sqrt(variance);
+ 
+             if (variance) {
+                 skew /= (N * variance * standard_deviation);
+                 kurtosis = kurtosis/(N * variance * variance) - 3.0;
+             }
+ 
+             InputIterator mid = begin+N/2;
+             nth_element(begin, mid, end);
+             if (N % 2 == 0) {
+ 				InputIterator next_biggest = max_element(begin, 
+ mid);
+                 median = (*mid+*next_biggest)/2;
+             }
+ 			else
+ 				median = *mid;
+         }
+ 
+     T median;
+     T mean;
+     T average_deviation;
+     T standard_deviation;
+     T variance;
+     T skew;
+     T kurtosis;
+ };
+ 
+ int main() {
+     vector<double> v;
+     double d;
+ 
+     while (scanf(" %lf", &d) == 1) v.push_back(d);
+     moments<double> m(v.begin(), v.end());
+ 
+     printf("n:                  %d\n", v.end() - v.begin());
+     printf("median:             %f\n", m.median);
+     printf("mean:               %f\n", m.mean);
+     printf("average_deviation:  %f\n", m.average_deviation);
+     printf("standard_deviation: %f\n", m.standard_deviation);
+     printf("variance:           %f\n", m.variance);
+     printf("skew:               %f\n", m.skew);
+     printf("kurtosis:           %f\n", m.kurtosis);
+ 
+     return 0;
+ }


Index: llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/nestedloop.cpp
diff -c /dev/null llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/nestedloop.cpp:1.1
*** /dev/null	Mon Oct  6 20:12:13 2003
--- llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/nestedloop.cpp	Mon Oct  6 20:11:33 2003
***************
*** 0 ****
--- 1,24 ----
+ // -*- mode: c++ -*-
+ // $Id: nestedloop.cpp,v 1.1 2003/10/07 01:11:33 lattner Exp $
+ // http://www.bagley.org/~doug/shootout/
+ 
+ #include <iostream>
+ #include <stdlib.h>
+ 
+ using namespace std;
+ 
+ int main(int argc, char *argv[]) {
+     int n = ((argc == 2) ? atoi(argv[1]) : 1);
+     int a, b, c, d, e, f, x=0;
+ 	
+     for (a=0; a<n; a++)
+ 	for (b=0; b<n; b++)
+ 	    for (c=0; c<n; c++)
+ 		for (d=0; d<n; d++)
+ 		    for (e=0; e<n; e++)
+ 			for (f=0; f<n; f++)
+ 			    x++;
+ 
+     cout << x << endl;
+     return(0);
+ }


Index: llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/objinst.cpp
diff -c /dev/null llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/objinst.cpp:1.1
*** /dev/null	Mon Oct  6 20:12:13 2003
--- llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/objinst.cpp	Mon Oct  6 20:11:33 2003
***************
*** 0 ****
--- 1,67 ----
+ // -*- mode: c++ -*-
+ // $Id: objinst.cpp,v 1.1 2003/10/07 01:11:33 lattner Exp $
+ // http://www.bagley.org/~doug/shootout/
+ 
+ #include <stdlib.h>
+ #include <iostream>
+ 
+ using namespace std;
+ 
+ class Toggle {
+ public:
+     Toggle(bool start_state) : state(start_state) { }
+     virtual ~Toggle() {  }
+     bool value() {
+ 	return(state);
+     }
+     virtual Toggle& activate() {
+ 	state = !state;
+ 	return(*this);
+     }
+     bool state;
+ };
+ 
+ class NthToggle : public Toggle {
+ public:
+     NthToggle(bool start_state, int max_counter) :
+ 	Toggle(start_state), count_max(max_counter), counter(0) {
+     }
+     Toggle& activate() {
+ 	if (++this->counter >= this->count_max) {
+ 	    state = !state;
+ 	    counter = 0;
+ 	}
+ 	return(*this);
+     }
+ private:
+     int count_max;
+     int counter;
+ };
+ 
+ int
+ main(int argc, char *argv[]) {
+     int n = ((argc == 2) ? atoi(argv[1]) : 1);
+ 
+     Toggle *toggle1 = new Toggle(true);
+     for (int i=0; i<5; i++) {
+ 	cout << ((toggle1->activate().value()) ? "true" : "false") << endl;
+     }
+     delete toggle1;
+     for (int i=0; i<n; i++) {
+ 	Toggle *toggle = new Toggle(true);
+ 	delete toggle;
+     }
+     
+     cout << endl;
+     
+     NthToggle *ntoggle1 = new NthToggle(true, 3);
+     for (int i=0; i<8; i++) {
+ 	cout << ((ntoggle1->activate().value()) ? "true" : "false") << endl;
+     }
+     delete ntoggle1;
+     for (int i=0; i<n; i++) {
+ 	NthToggle *ntoggle = new NthToggle(true, 3);
+ 	delete ntoggle;
+     }
+     return 0;
+ }


Index: llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/prodcons.cpp
diff -c /dev/null llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/prodcons.cpp:1.1
*** /dev/null	Mon Oct  6 20:12:13 2003
--- llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/prodcons.cpp	Mon Oct  6 20:11:33 2003
***************
*** 0 ****
--- 1,88 ----
+ // -*- mode: c++ -*-
+ // $Id: prodcons.cpp,v 1.1 2003/10/07 01:11:33 lattner Exp $
+ // http://www.bagley.org/~doug/shootout/
+ 
+ #include <iostream>
+ #include <stdio.h>
+ #include <stdlib.h>
+ #include <string.h>
+ #include <unistd.h>
+ #include <signal.h>
+ #include <errno.h>
+ #include <sys/types.h>
+ #include <pthread.h>
+ 
+ using namespace std;
+ 
+ pthread_mutex_t mutex;
+ pthread_cond_t control;
+ void producer(int *arg);
+ void consumer(int *arg);
+ int pcount, data, consumed, produced;
+ 
+ 
+ int
+ main(int argc, char *argv[]) {
+     int n = ((argc == 2) ? atoi(argv[1]) : 1);
+     pthread_t t1, t2;
+     
+     pcount = data = consumed = produced = 0;
+ 
+     if (pthread_mutex_init(&mutex, NULL)) {
+ 	perror("pthread_mutex_init");
+ 	exit(1);
+     }
+     if (pthread_cond_init(&control, NULL)) {
+ 	perror("pthread_cond_init");
+ 	exit(1);
+     }
+     if (pthread_create(&t1, (pthread_attr_t *)NULL,
+ 		       (void * (*)(void *))producer, (void *)&n)) {
+ 	perror("pthread_create");
+ 	exit(1);
+     }
+     if (pthread_create(&t2, (pthread_attr_t *)NULL,
+ 		       (void * (*)(void *))consumer, (void *)&n)) {
+ 	perror("pthread_create");
+ 	exit(1);
+     }
+   
+     pthread_join(t1, NULL);
+     pthread_join(t2, NULL);
+     cout << produced << " " << consumed << endl;
+     return(0);
+ }
+ 
+ 
+ void producer(int *arg) {
+     int i, n = *arg;
+     for (i=1; i<=n; i++) {
+ 	pthread_mutex_lock(&mutex);
+ 	while (pcount == 1) {
+ 	    pthread_cond_wait(&control, &mutex);
+ 	}
+ 	data = i;
+ 	pcount = 1;
+ 	pthread_cond_signal(&control);
+ 	pthread_mutex_unlock(&mutex);
+ 	produced++;
+     }
+ }
+  
+ 
+ void consumer(int *arg) {
+     int i = 0, n = *arg;
+     while (1) {
+ 	pthread_mutex_lock(&mutex);
+ 	while (pcount == 0) {
+ 	    pthread_cond_wait(&control, &mutex);
+ 	}
+ 	i = data;
+ 	pcount = 0;
+ 	pthread_cond_signal(&control);
+ 	pthread_mutex_unlock(&mutex);
+ 	consumed++;
+ 	if (i == n) return;
+     }
+ }
+ 


Index: llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/random.cpp
diff -c /dev/null llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/random.cpp:1.1
*** /dev/null	Mon Oct  6 20:12:13 2003
--- llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/random.cpp	Mon Oct  6 20:11:33 2003
***************
*** 0 ****
--- 1,33 ----
+ // -*- mode: c++ -*-
+ // $Id: random.cpp,v 1.1 2003/10/07 01:11:33 lattner Exp $
+ // http://www.bagley.org/~doug/shootout/
+ 
+ #include <iostream>
+ #include <stdlib.h>
+ #include <math.h>
+ 
+ using namespace std;
+ 
+ #define IM 139968
+ #define IA 3877
+ #define IC 29573
+ 
+ inline double gen_random(double max) {
+     static long last = 42;
+     last = (last * IA + IC) % IM;
+     return( max * last / IM );
+ }
+ 
+ int main(int argc, char *argv[]) {
+     int N = ((argc == 2) ? atoi(argv[1]) : 1);
+     double result = 0;
+     
+     while (N--) {
+ 	result = gen_random(100.0);
+     }
+     cout.precision(9);
+     cout.setf(ios::fixed);
+     cout << result << endl;
+     return(0);
+ }
+ 


Index: llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/sieve.cpp
diff -c /dev/null llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/sieve.cpp:1.1
*** /dev/null	Mon Oct  6 20:12:13 2003
--- llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/sieve.cpp	Mon Oct  6 20:11:33 2003
***************
*** 0 ****
--- 1,44 ----
+ #include <iostream>
+ #include <vector>
+ #include <list>
+ #include <cstdlib>
+ 
+ using namespace std;
+ 
+ void sieve(list<int>& unknown, vector<int>& primes)
+ {
+ 	while (!unknown.empty())
+ 	{
+ 		int p = unknown.front();
+ 		unknown.pop_front();
+ 		list<int>::iterator i = unknown.begin();
+ 		while (i != unknown.end())
+ 		{
+ 			if (*i % p)
+ 				++i;
+ 			else
+ 				i = unknown.erase(i);
+ 		}
+ 		primes.push_back(p);
+ 	}
+ }
+ 
+ int main(int argc, char *argv[]) 
+ {
+     size_t NUM = (argc == 2 ? (atoi(argv[1]) < 1 ? 1 : atoi(argv[1])): 
+ 1);
+ 
+ 	vector<int> primes;
+ 
+ 	// run the sieve repeatedly
+     while (NUM--) {
+ 		list<int> integers;
+ 		for (int i = 2; i < 8192; ++i)
+ 			integers.push_back(i);
+ 		primes.clear();
+ 		sieve(integers, primes);
+     }
+ 
+     cout << "Count: " << primes.size() << endl;
+ 	return 0;
+ }


Index: llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/strcat.cpp
diff -c /dev/null llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/strcat.cpp:1.1
*** /dev/null	Mon Oct  6 20:12:13 2003
--- llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/strcat.cpp	Mon Oct  6 20:11:33 2003
***************
*** 0 ****
--- 1,29 ----
+ // -*- mode: c++ -*-
+ // $Id: strcat.cpp,v 1.1 2003/10/07 01:11:33 lattner Exp $
+ // http://www.bagley.org/~doug/shootout/
+ // with help from PeterB
+ 
+ #include <iostream>
+ #include <string>
+ using namespace std;
+ 
+ int main(int argc, char *argv[])
+ {
+     int i, n = ((argc == 2) ? atoi(argv[1]) : 1);
+     string str;
+     size_t capacity = 31;
+     str.reserve(capacity); // as per C-string
+     size_t newLength = 6;
+     for (i = 0; i < n; i++)
+     {
+ 	if(newLength > capacity)
+ 	{
+ 	    capacity *= 2;
+ 	    str.reserve(capacity);
+ 	}
+ 	str += "hello\n";
+ 	newLength += 6;
+     }
+     cout << str.length() << endl;
+     return 0;
+ }


Index: llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/sumcol.cpp
diff -c /dev/null llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/sumcol.cpp:1.1
*** /dev/null	Mon Oct  6 20:12:13 2003
--- llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/sumcol.cpp	Mon Oct  6 20:11:33 2003
***************
*** 0 ****
--- 1,25 ----
+ // -*- mode: c++ -*-
+ // $Id: sumcol.cpp,v 1.1 2003/10/07 01:11:33 lattner Exp $
+ // http://www.bagley.org/~doug/shootout/
+ // with help from Waldek Hebisch
+ 
+ #include <iostream>
+ #include <fstream>
+ #include <stdlib.h>
+ #include <stdio.h>
+ 
+ using namespace std;
+ 
+ #define MAXLINELEN 128
+ 
+ int main(int argc, char * * argv) {
+     char line[MAXLINELEN];
+     int sum = 0;
+     char buff[4096];
+     cin.rdbuf()->pubsetbuf(buff, 4096); // enable buffering
+ 
+     while (cin.getline(line, MAXLINELEN)) {
+         sum += atoi(line);
+     }
+     cout << sum << '\n';
+ }


Index: llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/wc.cpp
diff -c /dev/null llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/wc.cpp:1.1
*** /dev/null	Mon Oct  6 20:12:13 2003
--- llvm/test/Programs/SingleSource/Benchmarks/Shootout-C++/wc.cpp	Mon Oct  6 20:11:33 2003
***************
*** 0 ****
--- 1,40 ----
+ // -*- mode: c++ -*-
+ // $Id: wc.cpp,v 1.1 2003/10/07 01:11:33 lattner Exp $
+ // http://www.bagley.org/~doug/shootout/
+ // with help from Tom Widmer
+ 
+ #include <iostream>
+ #include <vector>
+ 
+ using namespace std;
+ 
+ enum {
+     OUT,			/* outside a word */
+     IN				/* inside a word */
+ };
+ 
+ int
+ main(int argc, char *argv[]) {
+     char c;
+     int nl, nw, nc, state;
+     char buff[4096];
+     cin.rdbuf()->pubsetbuf(buff, 4096); // enable buffering
+ 
+     state = OUT;
+     nl = nw = nc = 0;
+     int intc;
+     streambuf* sbuf = cin.rdbuf();
+     while ((intc = sbuf->sbumpc()) != EOF) {
+         c = (char)intc;
+ 	++nc;
+ 	if (c == '\n')
+ 	    ++nl;
+ 	if (c == ' ' || c == '\n' || c == '\t')
+ 	    state = OUT;
+ 	else if (state == OUT) {
+ 	    state = IN;
+ 	    ++nw;
+ 	}
+     }
+     cout << nl << " " << nw << " " << nc << endl;
+ }





More information about the llvm-commits mailing list