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 Jul 27 07:21:36 2018 |
---|
13 | // Update Count : 345 |
---|
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 fill, 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 fill ) { |
---|
130 | return (T *)memset( dest, fill, 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 * amemset( T dest[], char fill, size_t dim ) { |
---|
142 | return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset |
---|
143 | } // amemset |
---|
144 | |
---|
145 | T * amemcpy( T dest[], const T src[], size_t dim ) { |
---|
146 | return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy |
---|
147 | } // amemcpy |
---|
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 | } // distribution |
---|
206 | |
---|
207 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) { |
---|
208 | E * bsearch( K key, const E * vals, size_t dim ); |
---|
209 | size_t bsearch( K key, const E * vals, size_t dim ); |
---|
210 | E * bsearchl( K key, const E * vals, size_t dim ); |
---|
211 | size_t bsearchl( K key, const E * vals, size_t dim ); |
---|
212 | E * bsearchu( K key, const E * vals, size_t dim ); |
---|
213 | size_t bsearchu( K key, const E * vals, size_t dim ); |
---|
214 | } // distribution |
---|
215 | |
---|
216 | forall( otype E | { int ?<?( E, E ); } ) { |
---|
217 | void qsort( E * vals, size_t dim ); |
---|
218 | } // distribution |
---|
219 | |
---|
220 | //--------------------------------------- |
---|
221 | |
---|
222 | extern "C" { // override C version |
---|
223 | void srandom( unsigned int seed ); |
---|
224 | long int random( void ); |
---|
225 | } // extern "C" |
---|
226 | |
---|
227 | static inline { |
---|
228 | long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u) |
---|
229 | long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u) |
---|
230 | unsigned long int random( void ) { return lrand48(); } |
---|
231 | 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) |
---|
232 | unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u) |
---|
233 | |
---|
234 | char random( void ) { return (unsigned long int)random(); } |
---|
235 | char random( char u ) { return random( (unsigned long int)u ); } // [0,u) |
---|
236 | char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u) |
---|
237 | int random( void ) { return (long int)random(); } |
---|
238 | int random( int u ) { return random( (long int)u ); } // [0,u] |
---|
239 | int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u) |
---|
240 | unsigned int random( void ) { return (unsigned long int)random(); } |
---|
241 | unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u] |
---|
242 | unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u) |
---|
243 | } // distribution |
---|
244 | |
---|
245 | float random( void ); // [0.0, 1.0) |
---|
246 | double random( void ); // [0.0, 1.0) |
---|
247 | float _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
248 | double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
249 | long double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
250 | |
---|
251 | //--------------------------------------- |
---|
252 | |
---|
253 | #include "common.hfa" |
---|
254 | |
---|
255 | // Local Variables: // |
---|
256 | // mode: c // |
---|
257 | // tab-width: 4 // |
---|
258 | // End: // |
---|