Daniel Carvalho has uploaded this change for review. ( https://gem5-review.googlesource.com/9022

Change subject: tests: Add test program for primes search
......................................................................

tests: Add test program for primes search

Simple program that uses the Sieve of Eratosthenes to search for
all primes up to a certain value.

Change-Id: I3c9501a67e5736d54ff9962395984a67734475c1
---
A tests/test-progs/primes/bin/x86/linux/primes
A tests/test-progs/primes/src/Makefile
A tests/test-progs/primes/src/primes.cpp
3 files changed, 87 insertions(+), 0 deletions(-)



diff --git a/tests/test-progs/primes/bin/x86/linux/primes b/tests/test-progs/primes/bin/x86/linux/primes
new file mode 100755
index 0000000..5075504
--- /dev/null
+++ b/tests/test-progs/primes/bin/x86/linux/primes
Binary files differ
diff --git a/tests/test-progs/primes/src/Makefile b/tests/test-progs/primes/src/Makefile
new file mode 100644
index 0000000..6f1341a
--- /dev/null
+++ b/tests/test-progs/primes/src/Makefile
@@ -0,0 +1,2 @@
+../bin/x86/linux/primes: primes.cpp
+       g++ -o ../bin/x86/linux/primes primes.cpp
diff --git a/tests/test-progs/primes/src/primes.cpp b/tests/test-progs/primes/src/primes.cpp
new file mode 100644
index 0000000..d30ef90
--- /dev/null
+++ b/tests/test-progs/primes/src/primes.cpp
@@ -0,0 +1,85 @@
+/**
+ * Copyright (c) 2018 Inria
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Daniel Carvalho
+ */
+
+/**
+ * @file
+ * Test program to find the Sieve of Eratosthenes.
+ */
+
+#include <iostream>
+#include <iterator>
+#include <set>
+#include <sstream>
+
+// Typedef for convenience. We use sets to keep increasing order.
+typedef std::set<unsigned> Sieve;
+
+int main(int argc, char *argv[])
+{
+    // Maximum value of sieve, that is, the number up to which we
+    // will find primes
+    unsigned limit;
+
+    // Get Sieve's maximum value
+    if (argc == 1) {
+        // Use 10k as default max. This generates 1229 prime numbers
+        limit = 10000;
+    } else {
+        std::istringstream ss(argv[1]);
+
+        if (argc != 2 || !(ss >> limit)) {
+            std::cerr << "Usage: " << argv[0] << " [max_prime_value]\n";
+            return 1;
+        }
+    }
+
+    // Initialize sieve set with all values up to limit
+    Sieve sieve;
+    for (unsigned i = 2; i < limit; ++i)
+        sieve.insert(i);
+
+    // Search for primes
+    for (Sieve::iterator it = sieve.begin(); it != sieve.end(); ++it) {
+        // If number has not been deleted it is a prime
+        unsigned prime = *it;
+
+        // Delete all numbers that are divisable by current prime
+        Sieve::iterator deleter = it;
+        while (++deleter != sieve.end())
+            if (((*deleter) % prime) == 0)
+                sieve.erase(deleter);
+    }
+
+    // Print all primes
+    std::copy(sieve.begin(), sieve.end(),
+              std::ostream_iterator<int>(std::cout, "\n"));
+
+    return 0;
+}

--
To view, visit https://gem5-review.googlesource.com/9022
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: master
Gerrit-Change-Id: I3c9501a67e5736d54ff9962395984a67734475c1
Gerrit-Change-Number: 9022
Gerrit-PatchSet: 1
Gerrit-Owner: Daniel Carvalho <oda...@yahoo.com.br>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list
gem5-dev@gem5.org
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to