1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // stdlib -- |
---|
8 | // |
---|
9 | // Author : Peter A. Buhr |
---|
10 | // Created On : Thu Jan 28 17:12:35 2016 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Mon Jul 23 07:44:47 2018 |
---|
13 | // Update Count : 341 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <stdlib.h> // *alloc, strto*, ato* |
---|
19 | extern "C" { |
---|
20 | void * memalign( size_t align, size_t size ); // malloc.h |
---|
21 | void * memset( void * dest, int c, size_t size ); // string.h |
---|
22 | void * memcpy( void * dest, const void * src, size_t size ); // string.h |
---|
23 | void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA |
---|
24 | } // extern "C" |
---|
25 | |
---|
26 | //--------------------------------------- |
---|
27 | |
---|
28 | #ifndef EXIT_FAILURE |
---|
29 | #define EXIT_FAILURE 1 // failing exit status |
---|
30 | #define EXIT_SUCCESS 0 // successful exit status |
---|
31 | #endif // ! EXIT_FAILURE |
---|
32 | |
---|
33 | //--------------------------------------- |
---|
34 | |
---|
35 | static inline forall( dtype T | sized(T) ) { |
---|
36 | // C dynamic allocation |
---|
37 | |
---|
38 | T * malloc( void ) { |
---|
39 | return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
40 | } // malloc |
---|
41 | |
---|
42 | // T & malloc( void ) { |
---|
43 | // int & p = *(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
44 | // printf( "& malloc %p\n", &p ); |
---|
45 | // return p; |
---|
46 | // // return (T &)*(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
47 | // } // malloc |
---|
48 | |
---|
49 | T * calloc( size_t dim ) { |
---|
50 | return (T *)(void *)calloc( dim, sizeof(T) ); // C calloc |
---|
51 | } // calloc |
---|
52 | |
---|
53 | T * realloc( T * ptr, size_t size ) { |
---|
54 | return (T *)(void *)realloc( (void *)ptr, size ); |
---|
55 | } // realloc |
---|
56 | |
---|
57 | T * memalign( size_t align ) { |
---|
58 | return (T *)memalign( align, sizeof(T) ); |
---|
59 | } // memalign |
---|
60 | |
---|
61 | T * aligned_alloc( size_t align ) { |
---|
62 | return (T *)aligned_alloc( align, sizeof(T) ); |
---|
63 | } // aligned_alloc |
---|
64 | |
---|
65 | int posix_memalign( T ** ptr, size_t align ) { |
---|
66 | return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign |
---|
67 | } // posix_memalign |
---|
68 | |
---|
69 | |
---|
70 | // Cforall dynamic allocation |
---|
71 | |
---|
72 | T * alloc( void ) { |
---|
73 | return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
74 | } // alloc |
---|
75 | |
---|
76 | T * alloc( char fill ) { |
---|
77 | T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
78 | return (T *)memset( ptr, (int)fill, sizeof(T) ); // initial with fill value |
---|
79 | } // alloc |
---|
80 | |
---|
81 | T * alloc( size_t dim ) { |
---|
82 | return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc |
---|
83 | } // alloc |
---|
84 | |
---|
85 | T * alloc( size_t dim, char fill ) { |
---|
86 | T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc |
---|
87 | return (T *)memset( ptr, (int)fill, dim * sizeof(T) ); // initial with fill value |
---|
88 | } // alloc |
---|
89 | |
---|
90 | T * alloc( T ptr[], size_t dim ) { |
---|
91 | return (T *)(void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc |
---|
92 | } // alloc |
---|
93 | } // distribution |
---|
94 | |
---|
95 | |
---|
96 | forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ); |
---|
97 | |
---|
98 | |
---|
99 | static inline forall( dtype T | sized(T) ) { |
---|
100 | T * align_alloc( size_t align ) { |
---|
101 | return (T *)memalign( align, sizeof(T) ); |
---|
102 | } // align_alloc |
---|
103 | |
---|
104 | T * align_alloc( size_t align, char fill ) { |
---|
105 | T * ptr = (T *)memalign( align, sizeof(T) ); |
---|
106 | return (T *)memset( ptr, (int)fill, sizeof(T) ); |
---|
107 | } // align_alloc |
---|
108 | |
---|
109 | T * align_alloc( size_t align, size_t dim ) { |
---|
110 | return (T *)memalign( align, dim * sizeof(T) ); |
---|
111 | } // align_alloc |
---|
112 | |
---|
113 | T * align_alloc( size_t align, size_t dim, char fill ) { |
---|
114 | T * ptr; |
---|
115 | if ( fill == '\0' ) { |
---|
116 | ptr = (T *)cmemalign( align, dim, sizeof(T) ); |
---|
117 | } else { |
---|
118 | ptr = (T *)memalign( align, dim * sizeof(T) ); |
---|
119 | return (T *)memset( ptr, (int)fill, dim * sizeof(T) ); |
---|
120 | } // if |
---|
121 | return ptr; |
---|
122 | } // align_alloc |
---|
123 | } // distribution |
---|
124 | |
---|
125 | |
---|
126 | static inline forall( dtype T | sized(T) ) { |
---|
127 | // data, non-array types |
---|
128 | |
---|
129 | T * memset( T * dest, char c ) { |
---|
130 | return (T *)memset( dest, c, sizeof(T) ); |
---|
131 | } // memset |
---|
132 | |
---|
133 | T * memcpy( T * dest, const T * src ) { |
---|
134 | return (T *)memcpy( dest, src, sizeof(T) ); |
---|
135 | } // memcpy |
---|
136 | } // distribution |
---|
137 | |
---|
138 | static inline forall( dtype T | sized(T) ) { |
---|
139 | // data, array types |
---|
140 | |
---|
141 | T * memset( T dest[], size_t dim, char c ) { |
---|
142 | return (T *)(void *)memset( dest, c, dim * sizeof(T) ); // C memset |
---|
143 | } // memset |
---|
144 | |
---|
145 | T * memcpy( T dest[], const T src[], size_t dim ) { |
---|
146 | return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy |
---|
147 | } // memcpy |
---|
148 | } // distribution |
---|
149 | |
---|
150 | // allocation/deallocation and constructor/destructor, non-array types |
---|
151 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p ); |
---|
152 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr ); |
---|
153 | forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest ); |
---|
154 | |
---|
155 | // allocation/deallocation and constructor/destructor, array types |
---|
156 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p ); |
---|
157 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] ); |
---|
158 | forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest ); |
---|
159 | |
---|
160 | //--------------------------------------- |
---|
161 | |
---|
162 | static inline { |
---|
163 | int strto( const char * sptr, char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); } |
---|
164 | unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); } |
---|
165 | long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); } |
---|
166 | unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); } |
---|
167 | long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); } |
---|
168 | unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); } |
---|
169 | |
---|
170 | float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); } |
---|
171 | double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); } |
---|
172 | long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); } |
---|
173 | } // distribution |
---|
174 | |
---|
175 | float _Complex strto( const char * sptr, char ** eptr ); |
---|
176 | double _Complex strto( const char * sptr, char ** eptr ); |
---|
177 | long double _Complex strto( const char * sptr, char ** eptr ); |
---|
178 | |
---|
179 | static inline { |
---|
180 | int ato( const char * sptr ) {return (int)strtol( sptr, 0, 10 ); } |
---|
181 | unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); } |
---|
182 | long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); } |
---|
183 | unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); } |
---|
184 | long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); } |
---|
185 | unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); } |
---|
186 | |
---|
187 | float ato( const char * sptr ) { return strtof( sptr, 0 ); } |
---|
188 | double ato( const char * sptr ) { return strtod( sptr, 0 ); } |
---|
189 | long double ato( const char * sptr ) { return strtold( sptr, 0 ); } |
---|
190 | |
---|
191 | float _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } |
---|
192 | double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } |
---|
193 | long double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } |
---|
194 | } // distribution |
---|
195 | |
---|
196 | //--------------------------------------- |
---|
197 | |
---|
198 | forall( otype E | { int ?<?( E, E ); } ) { |
---|
199 | E * bsearch( E key, const E * vals, size_t dim ); |
---|
200 | size_t bsearch( E key, const E * vals, size_t dim ); |
---|
201 | E * bsearchl( E key, const E * vals, size_t dim ); |
---|
202 | size_t bsearchl( E key, const E * vals, size_t dim ); |
---|
203 | E * bsearchu( E key, const E * vals, size_t dim ); |
---|
204 | size_t bsearchu( E key, const E * vals, size_t dim ); |
---|
205 | |
---|
206 | void qsort( E * vals, size_t dim ); |
---|
207 | } // distribution |
---|
208 | |
---|
209 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) { |
---|
210 | E * bsearch( K key, const E * vals, size_t dim ); |
---|
211 | size_t bsearch( K key, const E * vals, size_t dim ); |
---|
212 | E * bsearchl( K key, const E * vals, size_t dim ); |
---|
213 | size_t bsearchl( K key, const E * vals, size_t dim ); |
---|
214 | E * bsearchu( K key, const E * vals, size_t dim ); |
---|
215 | size_t bsearchu( K key, const E * vals, size_t dim ); |
---|
216 | } // distribution |
---|
217 | |
---|
218 | //--------------------------------------- |
---|
219 | |
---|
220 | extern "C" { // override C version |
---|
221 | void srandom( unsigned int seed ); |
---|
222 | long int random( void ); |
---|
223 | } // extern "C" |
---|
224 | |
---|
225 | static inline { |
---|
226 | long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u) |
---|
227 | long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u) |
---|
228 | unsigned long int random( void ) { return lrand48(); } |
---|
229 | unsigned long int random( unsigned long int l, unsigned long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u) |
---|
230 | unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u) |
---|
231 | |
---|
232 | char random( void ) { return (unsigned long int)random(); } |
---|
233 | char random( char u ) { return random( (unsigned long int)u ); } // [0,u) |
---|
234 | char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u) |
---|
235 | int random( void ) { return (long int)random(); } |
---|
236 | int random( int u ) { return random( (long int)u ); } // [0,u] |
---|
237 | int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u) |
---|
238 | unsigned int random( void ) { return (unsigned long int)random(); } |
---|
239 | unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u] |
---|
240 | unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u) |
---|
241 | } // distribution |
---|
242 | |
---|
243 | float random( void ); // [0.0, 1.0) |
---|
244 | double random( void ); // [0.0, 1.0) |
---|
245 | float _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
246 | double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
247 | long double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
248 | |
---|
249 | //--------------------------------------- |
---|
250 | |
---|
251 | #include "common" |
---|
252 | |
---|
253 | // Local Variables: // |
---|
254 | // mode: c // |
---|
255 | // tab-width: 4 // |
---|
256 | // End: // |
---|