File: duffs_device.c

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (61 lines) | stat: -rw-r--r-- 1,859 bytes parent folder | download | duplicates (17)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# /* Copyright (C) 2002
#  * Housemarque Oy
#  * http://www.housemarque.com
#  *
#  * Distributed under the Boost Software License, Version 1.0. (See
#  * accompanying file LICENSE_1_0.txt or copy at
#  * http://www.boost.org/LICENSE_1_0.txt)
#  */
#
# /* Revised by Paul Mensonides (2002) */
#
# /* See http://www.boost.org for most recent version. */
#
# /* This example uses the preprocessor library to implement a generalized
#  * macro for implementing Duff's Device.
#  *
#  * This example was inspired by an original generalized macro for
#  * for implementing Duff's Device written by Joerg Walter.
#  */
#
# include <assert.h>
#
# include <boost/preprocessor/repetition/repeat.hpp>
# include <boost/preprocessor/tuple/elem.hpp>
#
# /* Expands to a Duff's Device. */
# define DUFFS_DEVICE(UNROLLING_FACTOR, COUNTER_TYPE, N, STATEMENT) \
   do { \
      COUNTER_TYPE duffs_device_initial_cnt = (N); \
      if (duffs_device_initial_cnt > 0) { \
         COUNTER_TYPE 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(Z, 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, 0, ++i;);
   assert(i == 0);
   DUFFS_DEVICE(UNROLLING_FACTOR, int, N, ++i;);
   assert(i == N);
   return 0;
}