From 8a557b560bdd7ddc859246a1d380d663313ba640 Mon Sep 17 00:00:00 2001
From: Vesa Karvonen
Date: Wed, 27 Feb 2002 05:54:07 +0000
Subject: [PATCH] Added Duff's Device example
[SVN r12949]
---
doc/reference/repeat.htm | 1 +
example/duffs_device.c | 61 +++++++++++++++++++++++++++
include/boost/preprocessor/repeat.hpp | 1 +
3 files changed, 63 insertions(+)
create mode 100644 example/duffs_device.c
diff --git a/doc/reference/repeat.htm b/doc/reference/repeat.htm
index 6895289..fae87b2 100644
--- a/doc/reference/repeat.htm
+++ b/doc/reference/repeat.htm
@@ -42,6 +42,7 @@
Example
diff --git a/example/duffs_device.c b/example/duffs_device.c
new file mode 100644
index 0000000..d1c7f9c
--- /dev/null
+++ b/example/duffs_device.c
@@ -0,0 +1,61 @@
+/* Copyright (C) 2002
+ * Housemarque Oy
+ * http://www.housemarque.com
+ *
+ * Permission to copy, use, modify, sell and distribute this software is
+ * granted provided this copyright notice appears in all copies. This
+ * software is provided "as is" without express or implied warranty, and
+ * with no claim as to its suitability for any purpose.
+ *
+ * See http://www.boost.org for most recent version.
+ */
+
+/* This example uses the preprocessor library to implement a generalized
+ * macro for implementing a Duff's Device.
+ *
+ * This example was inspired by an original generalized macro for
+ * implementing Duff's Device written by Joerg Walter.
+ */
+
+#include
+#include
+#include
+
+/* Expands to a Duff's Device. */
+#define DUFFS_DEVICE(UNROLLING_FACTOR,COUNTER_TYPE,N,STATEMENT)\
+ do\
+ {\
+ COUNTER_TYPE\
+ duffs_device_initial_cnt = (N),\
+ duffs_device_running_cnt = (duffs_device_initial_cnt + (UNROLLING_FACTOR-1))/UNROLLING_FACTOR;\
+ switch (duffs_device_initial_cnt % UNROLLING_FACTOR)\
+ {\
+ do\
+ {\
+ BOOST_PP_REPEAT(UNROLLING_FACTOR,DUFFS_DEVICE_C,(UNROLLING_FACTOR,{STATEMENT}))\
+ } while (--duffs_device_running_cnt);\
+ }\
+ } while (0)
+
+#define DUFFS_DEVICE_C(I,UNROLLING_FACTOR_STATEMENT)\
+ case (I?BOOST_PP_TUPLE_ELEM(2,0,UNROLLING_FACTOR_STATEMENT)-I:0): BOOST_PP_TUPLE_ELEM(2,1,UNROLLING_FACTOR_STATEMENT);
+
+
+#ifndef UNROLLING_FACTOR
+#define UNROLLING_FACTOR 16
+#endif
+
+#ifndef N
+#define N 1000
+#endif
+
+int main(void)
+{
+ int i=0;
+
+ DUFFS_DEVICE(UNROLLING_FACTOR, int, N, ++i;);
+
+ assert(i == N);
+
+ return 0;
+}
diff --git a/include/boost/preprocessor/repeat.hpp b/include/boost/preprocessor/repeat.hpp
index 7e23544..0478320 100644
--- a/include/boost/preprocessor/repeat.hpp
+++ b/include/boost/preprocessor/repeat.hpp
@@ -28,6 +28,7 @@ BOOST_PP_REPEAT_3RD() macros.
Example