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