1 /* Copyright (C) 2010 The Trustees of Indiana University.                  */
2 /*                                                                         */
3 /* Use, modification and distribution is subject to the Boost Software     */
4 /* License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at */
5 /* http://www.boost.org/LICENSE_1_0.txt)                                   */
6 /*                                                                         */
7 /*  Authors: Jeremiah Willcock                                             */
8 /*           Andrew Lumsdaine                                              */
9 
10 #ifndef UTILS_H
11 #define UTILS_H
12 
13 #include <stddef.h>
14 #include <stdint.h>
15 #include "splittable_mrg.h"
16 #ifdef __MTA__
17 #include <sys/mta_task.h>
18 #endif
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 void* xmalloc(size_t n);
25 void* xcalloc(size_t n, size_t k);
26 uint_fast64_t random_up_to(mrg_state* st, uint_fast64_t n);
27 void make_mrg_seed(uint64_t userseed1, uint64_t userseed2, uint_fast32_t* seed);
28 
29 /* Compare-and-swap; return 1 if successful or 0 otherwise. */
30 #ifdef __MTA__
31 #pragma mta inline
int64_t_cas(volatile int64_t * p,int64_t oldval,int64_t newval)32 static inline int int64_t_cas(volatile int64_t* p, int64_t oldval, int64_t newval) {
33   int64_t val = readfe(p);
34   if (val == oldval) {
35     writeef(p, newval);
36     return 1;
37   } else {
38     writeef(p, val);
39     return 0;
40   }
41 }
42 #elif defined(GRAPH_GENERATOR_MPI) || defined(GRAPH_GENERATOR_SEQ)
43 /* Sequential */
44 #ifdef _MSC_VER
int64_t_cas(int64_t * p,int64_t oldval,int64_t newval)45 static _inline int int64_t_cas(int64_t* p, int64_t oldval, int64_t newval){
46 #else
47 static inline int int64_t_cas(int64_t* p, int64_t oldval, int64_t newval) {
48 #endif
49   if (*p == oldval) {
50     *p = newval;
51     return 1;
52   } else {
53     return 0;
54   }
55 }
56 #elif defined(GRAPH_GENERATOR_OMP)
57 #ifndef _MSC_VER
58 /* GCC intrinsic */
59 static inline int int64_t_cas(volatile int64_t* p, int64_t oldval, int64_t newval) {
60   return __sync_bool_compare_and_swap(p, oldval, newval);
61 }
62 #else
63 static _inline int int64_t_cas(volatile int64_t* p, int64_t oldval, int64_t newval) {
64   if (*p == oldval)
65     {
66         *p = newval;
67         return 1;
68     } else
69     {
70         return 0;
71     }
72 }
73 #endif
74 #else
75 #ifndef _MSC_VER
76 static inline int int64_t_cas(volatile int64_t* p, int64_t oldval, int64_t newval) {
77   if (*p == oldval)
78     {
79         *p = newval;
80         return 1;
81     } else
82     {
83         return 0;
84     }
85 }
86 #else
87 static _inline int int64_t_cas(volatile int64_t* p, int64_t oldval, int64_t newval) {
88   if (*p == oldval)
89     {
90         *p = newval;
91         return 1;
92     } else
93     {
94         return 0;
95     }
96 }
97 #endif
98 #endif
99 
100 #ifdef __cplusplus
101 }
102 #endif
103 
104 #endif /* UTILS_H */
105