1 /*
2  * Copyright 1997, Regents of the University of Minnesota
3  *
4  * macros.h
5  *
6  * This file contains macros used in multilevel
7  *
8  * Started 9/25/94
9  * George
10  *
11  * $Id: macros.h,v 1.1 1998/11/27 17:59:18 karypis Exp $
12  *
13  */
14 
15 
16 /*************************************************************************
17 * The following macro returns a random number in the specified range
18 **************************************************************************/
19 #define RandomInRange(u) ((rand()>>3)%(u))
20 #define RandomInRangeFast(u) ((rand()>>3)%(u))
21 
22 
23 
24 #define amax(a, b) ((a) >= (b) ? (a) : (b))
25 #define amin(a, b) ((a) >= (b) ? (b) : (a))
26 
27 #define AND(a, b) ((a) < 0 ? ((-(a))&(b)) : ((a)&(b)))
28 #define OR(a, b) ((a) < 0 ? -((-(a))|(b)) : ((a)|(b)))
29 #define XOR(a, b) ((a) < 0 ? -((-(a))^(b)) : ((a)^(b)))
30 
31 #define SWAP(a, b, tmp)  \
32                  do {(tmp) = (a); (a) = (b); (b) = (tmp);} while(0)
33 
34 #define INC_DEC(a, b, val) \
35                  do {(a) += (val); (b) -= (val);} while(0)
36 
37 
38 #define scopy(n, a, b) (float *)memcpy((void *)(b), (void *)(a), sizeof(float)*(n))
39 #define idxcopy(n, a, b) (idxtype *)memcpy((void *)(b), (void *)(a), sizeof(idxtype)*(n))
40 
41 #define HASHFCT(key, size) ((key)%(size))
42 
43 
44 /*************************************************************************
45 * Timer macros
46 **************************************************************************/
47 #define cleartimer(tmr) (tmr = 0.0)
48 #define starttimer(tmr) (tmr -= seconds())
49 #define stoptimer(tmr) (tmr += seconds())
50 #define gettimer(tmr) (tmr)
51 
52 
53 /*************************************************************************
54 * This macro is used to handle dbglvl
55 **************************************************************************/
56 #define IFSET(a, flag, cmd) if ((a)&(flag)) (cmd);
57 
58 /*************************************************************************
59 * These macros are used for debuging memory leaks
60 **************************************************************************/
61 #ifdef DMALLOC
62 #define imalloc(n, msg) (malloc(sizeof(int)*(n)))
63 #define fmalloc(n, msg) (malloc(sizeof(float)*(n)))
64 #define idxmalloc(n, msg) (malloc(sizeof(idxtype)*(n)))
65 #define ismalloc(n, val, msg) (iset((n), (val), malloc(sizeof(int)*(n))))
66 #define idxsmalloc(n, val, msg) (idxset((n), (val), malloc(sizeof(idxtype)*(n))))
67 #define GKmalloc(a, b) (malloc((a)))
68 #endif
69 
70 #ifdef DMALLOC
71 #   define MALLOC_CHECK(ptr)                                          \
72     if (malloc_verify((ptr)) == DMALLOC_VERIFY_ERROR) {  \
73         printf("***MALLOC_CHECK failed on line %d of file %s: " #ptr "\n", \
74               __LINE__, __FILE__);                               \
75         abort();                                                \
76     }
77 #else
78 #   define MALLOC_CHECK(ptr) ;
79 #endif
80 
81 
82 
83 /*************************************************************************
84 * This macro converts a length array in a CSR one
85 **************************************************************************/
86 #define MAKECSR(i, n, a) \
87    do { \
88      for (i=1; i<n; i++) a[i] += a[i-1]; \
89      for (i=n; i>0; i--) a[i] = a[i-1]; \
90      a[0] = 0; \
91    } while(0)
92 
93 
94 /*************************************************************************
95 * These macros insert and remove nodes from the boundary list
96 **************************************************************************/
97 #define BNDInsert(nbnd, bndind, bndptr, vtx) \
98    do { \
99      ASSERT(bndptr[vtx] == -1); \
100      bndind[nbnd] = vtx; \
101      bndptr[vtx] = nbnd++;\
102    } while(0)
103 
104 #define BNDDelete(nbnd, bndind, bndptr, vtx) \
105    do { \
106      ASSERT(bndptr[vtx] != -1); \
107      bndind[bndptr[vtx]] = bndind[--nbnd]; \
108      bndptr[bndind[nbnd]] = bndptr[vtx]; \
109      bndptr[vtx] = -1; \
110    } while(0)
111 
112 
113 
114 /*************************************************************************
115 * These are debugging macros
116 **************************************************************************/
117 #ifdef DEBUG
118 #   define ASSERT(expr)                                          \
119     if (!(expr)) {                                               \
120         printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
121               __LINE__, __FILE__);                               \
122         abort();                                                \
123     }
124 #else
125 #   define ASSERT(expr) ;
126 #endif
127 
128 #ifdef DEBUG
129 #   define ASSERTP(expr, msg)                                          \
130     if (!(expr)) {                                               \
131         printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
132               __LINE__, __FILE__);                               \
133         printf msg ; \
134         abort();                                                \
135     }
136 #else
137 #   define ASSERTP(expr, msg) ;
138 #endif
139