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 20 19:04:33 2020 |
---|
13 | // Update Count : 472 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include "bits/defs.hfa" |
---|
19 | #include "bits/align.hfa" |
---|
20 | |
---|
21 | #include <stdlib.h> // *alloc, strto*, ato* |
---|
22 | #include <heap.hfa> |
---|
23 | |
---|
24 | // Reduce includes by explicitly defining these routines. |
---|
25 | extern "C" { |
---|
26 | void * memalign( size_t alignment, size_t size ); // malloc.h |
---|
27 | void * pvalloc( size_t size ); // malloc.h |
---|
28 | void * memset( void * dest, int fill, size_t size ); // string.h |
---|
29 | void * memcpy( void * dest, const void * src, size_t size ); // string.h |
---|
30 | } // extern "C" |
---|
31 | |
---|
32 | //--------------------------------------- |
---|
33 | |
---|
34 | #ifndef EXIT_FAILURE |
---|
35 | #define EXIT_FAILURE 1 // failing exit status |
---|
36 | #define EXIT_SUCCESS 0 // successful exit status |
---|
37 | #endif // ! EXIT_FAILURE |
---|
38 | |
---|
39 | //--------------------------------------- |
---|
40 | |
---|
41 | // Macro because of returns |
---|
42 | #define $VAR_ALLOC( allocation, alignment ) \ |
---|
43 | if ( _Alignof(T) <= libAlign() ) return (T *)(void *)allocation( (size_t)sizeof(T) ); /* C allocation */ \ |
---|
44 | else return (T *)alignment( _Alignof(T), sizeof(T) ) |
---|
45 | |
---|
46 | #define $ARRAY_ALLOC( allocation, alignment, dim ) \ |
---|
47 | if ( _Alignof(T) <= libAlign() ) return (T *)(void *)allocation( dim, (size_t)sizeof(T) ); /* C allocation */ \ |
---|
48 | else return (T *)alignment( _Alignof(T), dim, sizeof(T) ) |
---|
49 | |
---|
50 | #define $RE_SPECIALS( ptr, size, allocation, alignment ) \ |
---|
51 | if ( unlikely( size == 0 ) || unlikely( ptr == 0p ) ) { \ |
---|
52 | if ( unlikely( size == 0 ) ) free( ptr ); \ |
---|
53 | $VAR_ALLOC( malloc, memalign ); \ |
---|
54 | } /* if */ |
---|
55 | |
---|
56 | static inline forall( dtype T | sized(T) ) { |
---|
57 | // Cforall safe equivalents, i.e., implicit size specification |
---|
58 | |
---|
59 | T * malloc( void ) { |
---|
60 | $VAR_ALLOC( malloc, memalign ); |
---|
61 | } // malloc |
---|
62 | |
---|
63 | T * aalloc( size_t dim ) { |
---|
64 | $ARRAY_ALLOC( aalloc, amemalign, dim ); |
---|
65 | } // aalloc |
---|
66 | |
---|
67 | T * calloc( size_t dim ) { |
---|
68 | $ARRAY_ALLOC( calloc, cmemalign, dim ); |
---|
69 | } // calloc |
---|
70 | |
---|
71 | T * resize( T * ptr, size_t size ) { // CFA resize, eliminate return-type cast |
---|
72 | $RE_SPECIALS( ptr, size, malloc, memalign ); |
---|
73 | return (T *)(void *)resize( (void *)ptr, size ); // CFA resize |
---|
74 | } // resize |
---|
75 | |
---|
76 | T * realloc( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast |
---|
77 | $RE_SPECIALS( ptr, size, malloc, memalign ); |
---|
78 | return (T *)(void *)realloc( (void *)ptr, size ); // C realloc |
---|
79 | } // realloc |
---|
80 | |
---|
81 | T * memalign( size_t align ) { |
---|
82 | return (T *)memalign( align, sizeof(T) ); // C memalign |
---|
83 | } // memalign |
---|
84 | |
---|
85 | T * amemalign( size_t align, size_t dim ) { |
---|
86 | return (T *)amemalign( align, dim, sizeof(T) ); // CFA amemalign |
---|
87 | } // amemalign |
---|
88 | |
---|
89 | T * cmemalign( size_t align, size_t dim ) { |
---|
90 | return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign |
---|
91 | } // cmemalign |
---|
92 | |
---|
93 | T * aligned_alloc( size_t align ) { |
---|
94 | return (T *)aligned_alloc( align, sizeof(T) ); // C aligned_alloc |
---|
95 | } // aligned_alloc |
---|
96 | |
---|
97 | int posix_memalign( T ** ptr, size_t align ) { |
---|
98 | return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign |
---|
99 | } // posix_memalign |
---|
100 | |
---|
101 | T * valloc( void ) { |
---|
102 | return (T *)valloc( sizeof(T) ); // C valloc |
---|
103 | } // valloc |
---|
104 | |
---|
105 | T * pvalloc( void ) { |
---|
106 | return (T *)pvalloc( sizeof(T) ); // C pvalloc |
---|
107 | } // pvalloc |
---|
108 | } // distribution |
---|
109 | |
---|
110 | static inline forall( dtype T | sized(T) ) { |
---|
111 | // Cforall safe general allocation, fill, resize, array |
---|
112 | |
---|
113 | T * alloc( void ) { |
---|
114 | return malloc(); |
---|
115 | } // alloc |
---|
116 | |
---|
117 | T * alloc( size_t dim ) { |
---|
118 | return aalloc( dim ); |
---|
119 | } // alloc |
---|
120 | |
---|
121 | forall( dtype S | sized(S) ) |
---|
122 | T * alloc( S ptr[], size_t dim = 1 ) { // singleton/array resize |
---|
123 | size_t len = malloc_usable_size( ptr ); // current bucket size |
---|
124 | if ( sizeof(T) * dim > len ) { // not enough space ? |
---|
125 | T * temp = alloc( dim ); // new storage |
---|
126 | free( ptr ); // free old storage |
---|
127 | return temp; |
---|
128 | } else { |
---|
129 | return (T *)ptr; |
---|
130 | } // if |
---|
131 | } // alloc |
---|
132 | |
---|
133 | T * alloc( T ptr[], size_t dim, bool copy = true ) { |
---|
134 | if ( copy ) { |
---|
135 | return realloc( ptr, dim * sizeof(T) ); // CFA realloc |
---|
136 | } else { |
---|
137 | return resize( ptr, dim * sizeof(T) ); // CFA resize |
---|
138 | } // if |
---|
139 | } // alloc |
---|
140 | |
---|
141 | T * alloc_set( char fill ) { |
---|
142 | return (T *)memset( (T *)alloc(), (int)fill, sizeof(T) ); // initialize with fill value |
---|
143 | } // alloc |
---|
144 | |
---|
145 | T * alloc_set( T fill ) { |
---|
146 | return (T *)memcpy( (T *)alloc(), &fill, sizeof(T) ); // initialize with fill value |
---|
147 | } // alloc |
---|
148 | |
---|
149 | T * alloc_set( size_t dim, char fill ) { |
---|
150 | return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value |
---|
151 | } // alloc |
---|
152 | |
---|
153 | T * alloc_set( size_t dim, T fill ) { |
---|
154 | T * r = (T *)alloc( dim ); |
---|
155 | for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value |
---|
156 | return r; |
---|
157 | } // alloc |
---|
158 | |
---|
159 | T * alloc_set( size_t dim, const T fill[] ) { |
---|
160 | return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value |
---|
161 | } // alloc |
---|
162 | |
---|
163 | T * alloc_set( T ptr[], size_t dim, char fill ) { // realloc array with fill |
---|
164 | size_t osize = malloc_size( ptr ); // current allocation |
---|
165 | T * nptr = realloc( ptr, dim * sizeof(T) ); // CFA realloc |
---|
166 | size_t nsize = malloc_size( nptr ); // new allocation |
---|
167 | if ( nsize > osize ) { // larger ? |
---|
168 | memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage |
---|
169 | } // if |
---|
170 | return (T *)nptr; |
---|
171 | } // alloc_set |
---|
172 | |
---|
173 | T * alloc_set( T ptr[], size_t dim, T & fill ) { // realloc array with fill |
---|
174 | size_t odim = malloc_size( ptr ) / sizeof(T); // current allocation |
---|
175 | T * nptr = realloc( ptr, dim * sizeof(T) ); // CFA realloc |
---|
176 | size_t ndim = malloc_size( nptr ) / sizeof(T); // new allocation |
---|
177 | if ( ndim > odim ) { // larger ? |
---|
178 | for ( i; odim ~ ndim ) { |
---|
179 | memcpy( &nptr[i], &fill, sizeof(T) ); // initialize with fill value |
---|
180 | } // for |
---|
181 | } // if |
---|
182 | return (T *)nptr; |
---|
183 | } // alloc_align_set |
---|
184 | } // distribution |
---|
185 | |
---|
186 | static inline forall( dtype T | sized(T) ) { |
---|
187 | T * alloc_align( size_t align ) { |
---|
188 | return (T *)memalign( align, sizeof(T) ); |
---|
189 | } // alloc_align |
---|
190 | |
---|
191 | T * alloc_align( size_t align, size_t dim ) { |
---|
192 | return (T *)memalign( align, dim * sizeof(T) ); |
---|
193 | } // alloc_align |
---|
194 | |
---|
195 | T * alloc_align( T * ptr, size_t align ) { // aligned realloc array |
---|
196 | return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc |
---|
197 | } // alloc_align |
---|
198 | |
---|
199 | forall( dtype S | sized(S) ) |
---|
200 | T * alloc_align( S ptr[], size_t align ) { // aligned reuse array |
---|
201 | return (T *)(void *)resize( (void *)ptr, align, sizeof(T) ); // CFA realloc |
---|
202 | } // alloc_align |
---|
203 | |
---|
204 | T * alloc_align( T ptr[], size_t align, size_t dim ) { // aligned realloc array |
---|
205 | return (T *)(void *)realloc( (void *)ptr, align, dim * sizeof(T) ); // CFA realloc |
---|
206 | } // alloc_align |
---|
207 | |
---|
208 | T * alloc_align_set( size_t align, char fill ) { |
---|
209 | return (T *)memset( (T *)alloc_align( align ), (int)fill, sizeof(T) ); // initialize with fill value |
---|
210 | } // alloc_align |
---|
211 | |
---|
212 | T * alloc_align_set( size_t align, T fill ) { |
---|
213 | return (T *)memcpy( (T *)alloc_align( align ), &fill, sizeof(T) ); // initialize with fill value |
---|
214 | } // alloc_align |
---|
215 | |
---|
216 | T * alloc_align_set( size_t align, size_t dim, char fill ) { |
---|
217 | return (T *)memset( (T *)alloc_align( align, dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value |
---|
218 | } // alloc_align |
---|
219 | |
---|
220 | T * alloc_align_set( size_t align, size_t dim, T fill ) { |
---|
221 | T * r = (T *)alloc_align( align, dim ); |
---|
222 | for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value |
---|
223 | return r; |
---|
224 | } // alloc_align |
---|
225 | |
---|
226 | T * alloc_align_set( size_t align, size_t dim, const T fill[] ) { |
---|
227 | return (T *)memcpy( (T *)alloc_align( align, dim ), fill, dim * sizeof(T) ); |
---|
228 | } // alloc_align |
---|
229 | |
---|
230 | T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ) { |
---|
231 | size_t osize = malloc_size( ptr ); // current allocation |
---|
232 | T * nptr = realloc( ptr, align, dim * sizeof(T) ); // CFA realloc |
---|
233 | size_t nsize = malloc_size( nptr ); // new allocation |
---|
234 | if ( nsize > osize ) { // larger ? |
---|
235 | memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage |
---|
236 | } // if |
---|
237 | return (T *)nptr; |
---|
238 | } // alloc_align_set |
---|
239 | |
---|
240 | T * alloc_align_set( T ptr[], size_t align, size_t dim, T & fill ) { |
---|
241 | size_t odim = malloc_size( ptr ) / sizeof(T); // current allocation |
---|
242 | T * nptr = realloc( ptr, align, dim * sizeof(T) ); // CFA realloc |
---|
243 | size_t ndim = malloc_size( nptr ); // new allocation |
---|
244 | if ( ndim > odim ) { // larger ? |
---|
245 | for ( i; odim ~ ndim ) { |
---|
246 | memcpy( &nptr[i], &fill, sizeof(T) ); // initialize with fill value |
---|
247 | } // for |
---|
248 | } // if |
---|
249 | return (T *)nptr; |
---|
250 | } // alloc_align_set |
---|
251 | } // distribution |
---|
252 | |
---|
253 | static inline forall( dtype T | sized(T) ) { |
---|
254 | // Cforall safe initialization/copy, i.e., implicit size specification, non-array types |
---|
255 | T * memset( T * dest, char fill ) { |
---|
256 | return (T *)memset( dest, fill, sizeof(T) ); |
---|
257 | } // memset |
---|
258 | |
---|
259 | T * memcpy( T * dest, const T * src ) { |
---|
260 | return (T *)memcpy( dest, src, sizeof(T) ); |
---|
261 | } // memcpy |
---|
262 | } // distribution |
---|
263 | |
---|
264 | static inline forall( dtype T | sized(T) ) { |
---|
265 | // Cforall safe initialization/copy, i.e., implicit size specification, array types |
---|
266 | T * amemset( T dest[], char fill, size_t dim ) { |
---|
267 | return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset |
---|
268 | } // amemset |
---|
269 | |
---|
270 | T * amemcpy( T dest[], const T src[], size_t dim ) { |
---|
271 | return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy |
---|
272 | } // amemcpy |
---|
273 | } // distribution |
---|
274 | |
---|
275 | // Cforall allocation/deallocation and constructor/destructor, non-array types |
---|
276 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p ); |
---|
277 | forall( dtype T | { void ^?{}( T & ); } ) void delete( T * ptr ); |
---|
278 | forall( dtype T, ttype Params | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest ); |
---|
279 | |
---|
280 | // Cforall allocation/deallocation and constructor/destructor, array types |
---|
281 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p ); |
---|
282 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] ); |
---|
283 | forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest ); |
---|
284 | |
---|
285 | //--------------------------------------- |
---|
286 | |
---|
287 | static inline { |
---|
288 | int strto( const char sptr[], char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); } |
---|
289 | unsigned int strto( const char sptr[], char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); } |
---|
290 | long int strto( const char sptr[], char ** eptr, int base ) { return strtol( sptr, eptr, base ); } |
---|
291 | unsigned long int strto( const char sptr[], char ** eptr, int base ) { return strtoul( sptr, eptr, base ); } |
---|
292 | long long int strto( const char sptr[], char ** eptr, int base ) { return strtoll( sptr, eptr, base ); } |
---|
293 | unsigned long long int strto( const char sptr[], char ** eptr, int base ) { return strtoull( sptr, eptr, base ); } |
---|
294 | |
---|
295 | float strto( const char sptr[], char ** eptr ) { return strtof( sptr, eptr ); } |
---|
296 | double strto( const char sptr[], char ** eptr ) { return strtod( sptr, eptr ); } |
---|
297 | long double strto( const char sptr[], char ** eptr ) { return strtold( sptr, eptr ); } |
---|
298 | } // distribution |
---|
299 | |
---|
300 | float _Complex strto( const char sptr[], char ** eptr ); |
---|
301 | double _Complex strto( const char sptr[], char ** eptr ); |
---|
302 | long double _Complex strto( const char sptr[], char ** eptr ); |
---|
303 | |
---|
304 | static inline { |
---|
305 | int ato( const char sptr[] ) { return (int)strtol( sptr, 0p, 10 ); } |
---|
306 | unsigned int ato( const char sptr[] ) { return (unsigned int)strtoul( sptr, 0p, 10 ); } |
---|
307 | long int ato( const char sptr[] ) { return strtol( sptr, 0p, 10 ); } |
---|
308 | unsigned long int ato( const char sptr[] ) { return strtoul( sptr, 0p, 10 ); } |
---|
309 | long long int ato( const char sptr[] ) { return strtoll( sptr, 0p, 10 ); } |
---|
310 | unsigned long long int ato( const char sptr[] ) { return strtoull( sptr, 0p, 10 ); } |
---|
311 | |
---|
312 | float ato( const char sptr[] ) { return strtof( sptr, 0p ); } |
---|
313 | double ato( const char sptr[] ) { return strtod( sptr, 0p ); } |
---|
314 | long double ato( const char sptr[] ) { return strtold( sptr, 0p ); } |
---|
315 | |
---|
316 | float _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); } |
---|
317 | double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); } |
---|
318 | long double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); } |
---|
319 | } // distribution |
---|
320 | |
---|
321 | //--------------------------------------- |
---|
322 | |
---|
323 | forall( otype E | { int ?<?( E, E ); } ) { |
---|
324 | E * bsearch( E key, const E * vals, size_t dim ); |
---|
325 | size_t bsearch( E key, const E * vals, size_t dim ); |
---|
326 | E * bsearchl( E key, const E * vals, size_t dim ); |
---|
327 | size_t bsearchl( E key, const E * vals, size_t dim ); |
---|
328 | E * bsearchu( E key, const E * vals, size_t dim ); |
---|
329 | size_t bsearchu( E key, const E * vals, size_t dim ); |
---|
330 | } // distribution |
---|
331 | |
---|
332 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) { |
---|
333 | E * bsearch( K key, const E * vals, size_t dim ); |
---|
334 | size_t bsearch( K key, const E * vals, size_t dim ); |
---|
335 | E * bsearchl( K key, const E * vals, size_t dim ); |
---|
336 | size_t bsearchl( K key, const E * vals, size_t dim ); |
---|
337 | E * bsearchu( K key, const E * vals, size_t dim ); |
---|
338 | size_t bsearchu( K key, const E * vals, size_t dim ); |
---|
339 | } // distribution |
---|
340 | |
---|
341 | forall( otype E | { int ?<?( E, E ); } ) { |
---|
342 | void qsort( E * vals, size_t dim ); |
---|
343 | } // distribution |
---|
344 | |
---|
345 | //--------------------------------------- |
---|
346 | |
---|
347 | extern "C" { // override C version |
---|
348 | void srandom( unsigned int seed ); |
---|
349 | long int random( void ); // GENERATES POSITIVE AND NEGATIVE VALUES |
---|
350 | // For positive values, use unsigned int, e.g., unsigned int r = random() % 100U; |
---|
351 | } // extern "C" |
---|
352 | |
---|
353 | static inline { |
---|
354 | long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u) |
---|
355 | long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u) |
---|
356 | unsigned long int random( void ) { return lrand48(); } |
---|
357 | unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u) |
---|
358 | 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) |
---|
359 | |
---|
360 | char random( void ) { return (unsigned long int)random(); } |
---|
361 | char random( char u ) { return random( (unsigned long int)u ); } // [0,u) |
---|
362 | char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u) |
---|
363 | int random( void ) { return (long int)random(); } |
---|
364 | int random( int u ) { return random( (long int)u ); } // [0,u] |
---|
365 | int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u) |
---|
366 | unsigned int random( void ) { return (unsigned long int)random(); } |
---|
367 | unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u] |
---|
368 | unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u) |
---|
369 | } // distribution |
---|
370 | |
---|
371 | float random( void ); // [0.0, 1.0) |
---|
372 | double random( void ); // [0.0, 1.0) |
---|
373 | float _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
374 | double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
375 | long double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
376 | |
---|
377 | //--------------------------------------- |
---|
378 | |
---|
379 | #include "common.hfa" |
---|
380 | |
---|
381 | //--------------------------------------- |
---|
382 | |
---|
383 | extern bool threading_enabled(void) OPTIONAL_THREAD; |
---|
384 | |
---|
385 | // Local Variables: // |
---|
386 | // mode: c // |
---|
387 | // tab-width: 4 // |
---|
388 | // End: // |
---|