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