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