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 Jul 12 08:03:58 2018 |
---|
13 | // Update Count : 337 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <stdlib.h> // allocation, strto*, ato* |
---|
19 | extern "C" { |
---|
20 | void * memalign( size_t align, size_t size ); // malloc.h |
---|
21 | void * memset( void * dest, int c, size_t size ); // string.h |
---|
22 | void * memcpy( void * dest, const void * src, size_t size ); // string.h |
---|
23 | } // extern "C" |
---|
24 | |
---|
25 | //--------------------------------------- |
---|
26 | |
---|
27 | #ifndef EXIT_FAILURE |
---|
28 | #define EXIT_FAILURE 1 // failing exit status |
---|
29 | #define EXIT_SUCCESS 0 // successful exit status |
---|
30 | #endif // ! EXIT_FAILURE |
---|
31 | |
---|
32 | //--------------------------------------- |
---|
33 | |
---|
34 | static inline forall( dtype T | sized(T) ) { |
---|
35 | // C dynamic allocation |
---|
36 | |
---|
37 | T * malloc( void ) { |
---|
38 | // printf( "* malloc\n" ); |
---|
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 | //printf( "X2\n" ); |
---|
51 | return (T *)(void *)calloc( dim, sizeof(T) ); // C calloc |
---|
52 | } // calloc |
---|
53 | |
---|
54 | T * realloc( T * ptr, size_t size ) { |
---|
55 | //printf( "X3\n" ); |
---|
56 | return (T *)(void *)realloc( (void *)ptr, size ); |
---|
57 | } // realloc |
---|
58 | |
---|
59 | T * memalign( size_t align ) { |
---|
60 | //printf( "X4\n" ); |
---|
61 | return (T *)memalign( align, sizeof(T) ); |
---|
62 | } // memalign |
---|
63 | |
---|
64 | T * aligned_alloc( size_t align ) { |
---|
65 | //printf( "X5\n" ); |
---|
66 | return (T *)aligned_alloc( align, sizeof(T) ); |
---|
67 | } // aligned_alloc |
---|
68 | |
---|
69 | int posix_memalign( T ** ptr, size_t align ) { |
---|
70 | //printf( "X6\n" ); |
---|
71 | return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign |
---|
72 | } // posix_memalign |
---|
73 | |
---|
74 | |
---|
75 | // Cforall dynamic allocation |
---|
76 | |
---|
77 | T * alloc( void ) { |
---|
78 | //printf( "X7\n" ); |
---|
79 | return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
80 | } // alloc |
---|
81 | |
---|
82 | T * alloc( char fill ) { |
---|
83 | //printf( "X8\n" ); |
---|
84 | T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
85 | return (T *)memset( ptr, (int)fill, sizeof(T) ); // initial with fill value |
---|
86 | } // alloc |
---|
87 | |
---|
88 | T * alloc( size_t dim ) { |
---|
89 | //printf( "X9\n" ); |
---|
90 | return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc |
---|
91 | } // alloc |
---|
92 | |
---|
93 | T * alloc( size_t dim, char fill ) { |
---|
94 | //printf( "X10\n" ); |
---|
95 | T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc |
---|
96 | return (T *)memset( ptr, (int)fill, dim * sizeof(T) ); // initial with fill value |
---|
97 | } // alloc |
---|
98 | |
---|
99 | T * alloc( T ptr[], size_t dim ) { |
---|
100 | //printf( "X11\n" ); |
---|
101 | return (T *)(void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc |
---|
102 | } // alloc |
---|
103 | } // distribution |
---|
104 | |
---|
105 | |
---|
106 | forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ); |
---|
107 | |
---|
108 | |
---|
109 | static inline forall( dtype T | sized(T) ) { |
---|
110 | T * align_alloc( size_t align ) { |
---|
111 | //printf( "X13\n" ); |
---|
112 | return (T *)memalign( align, sizeof(T) ); |
---|
113 | } // align_alloc |
---|
114 | |
---|
115 | T * align_alloc( size_t align, char fill ) { |
---|
116 | //printf( "X14\n" ); |
---|
117 | T * ptr = (T *)memalign( align, sizeof(T) ); |
---|
118 | return (T *)memset( ptr, (int)fill, sizeof(T) ); |
---|
119 | } // align_alloc |
---|
120 | |
---|
121 | T * align_alloc( size_t align, size_t dim ) { |
---|
122 | //printf( "X15\n" ); |
---|
123 | return (T *)memalign( align, dim * sizeof(T) ); |
---|
124 | } // align_alloc |
---|
125 | |
---|
126 | T * align_alloc( size_t align, size_t dim, char fill ) { |
---|
127 | //printf( "X16\n" ); |
---|
128 | T * ptr = (T *)memalign( align, dim * sizeof(T) ); |
---|
129 | return (T *)memset( ptr, (int)fill, dim * sizeof(T) ); |
---|
130 | } // align_alloc |
---|
131 | } // distribution |
---|
132 | |
---|
133 | |
---|
134 | static inline forall( dtype T | sized(T) ) { |
---|
135 | // data, non-array types |
---|
136 | |
---|
137 | T * memset( T * dest, char c ) { |
---|
138 | //printf( "X17\n" ); |
---|
139 | return (T *)memset( dest, c, sizeof(T) ); |
---|
140 | } // memset |
---|
141 | |
---|
142 | T * memcpy( T * dest, const T * src ) { |
---|
143 | //printf( "X18\n" ); |
---|
144 | return (T *)memcpy( dest, src, sizeof(T) ); |
---|
145 | } // memcpy |
---|
146 | } // distribution |
---|
147 | |
---|
148 | static inline forall( dtype T | sized(T) ) { |
---|
149 | // data, array types |
---|
150 | |
---|
151 | T * memset( T dest[], size_t dim, char c ) { |
---|
152 | //printf( "X19\n" ); |
---|
153 | return (T *)(void *)memset( dest, c, dim * sizeof(T) ); // C memset |
---|
154 | } // memset |
---|
155 | |
---|
156 | T * memcpy( T dest[], const T src[], size_t dim ) { |
---|
157 | //printf( "X20\n" ); |
---|
158 | return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy |
---|
159 | } // memcpy |
---|
160 | } // distribution |
---|
161 | |
---|
162 | // allocation/deallocation and constructor/destructor, non-array types |
---|
163 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p ); |
---|
164 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr ); |
---|
165 | forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest ); |
---|
166 | |
---|
167 | // allocation/deallocation and constructor/destructor, array types |
---|
168 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p ); |
---|
169 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] ); |
---|
170 | forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest ); |
---|
171 | |
---|
172 | //--------------------------------------- |
---|
173 | |
---|
174 | static inline { |
---|
175 | int strto( const char * sptr, char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); } |
---|
176 | unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); } |
---|
177 | long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); } |
---|
178 | unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); } |
---|
179 | long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); } |
---|
180 | unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); } |
---|
181 | |
---|
182 | float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); } |
---|
183 | double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); } |
---|
184 | long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); } |
---|
185 | } // distribution |
---|
186 | |
---|
187 | float _Complex strto( const char * sptr, char ** eptr ); |
---|
188 | double _Complex strto( const char * sptr, char ** eptr ); |
---|
189 | long double _Complex strto( const char * sptr, char ** eptr ); |
---|
190 | |
---|
191 | static inline { |
---|
192 | int ato( const char * sptr ) {return (int)strtol( sptr, 0, 10 ); } |
---|
193 | unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); } |
---|
194 | long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); } |
---|
195 | unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); } |
---|
196 | long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); } |
---|
197 | unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); } |
---|
198 | |
---|
199 | float ato( const char * sptr ) { return strtof( sptr, 0 ); } |
---|
200 | double ato( const char * sptr ) { return strtod( sptr, 0 ); } |
---|
201 | long double ato( const char * sptr ) { return strtold( sptr, 0 ); } |
---|
202 | |
---|
203 | float _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } |
---|
204 | double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } |
---|
205 | long double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } |
---|
206 | } // distribution |
---|
207 | |
---|
208 | //--------------------------------------- |
---|
209 | |
---|
210 | forall( otype E | { int ?<?( E, E ); } ) { |
---|
211 | E * bsearch( E key, const E * vals, size_t dim ); |
---|
212 | size_t bsearch( E key, const E * vals, size_t dim ); |
---|
213 | E * bsearchl( E key, const E * vals, size_t dim ); |
---|
214 | size_t bsearchl( E key, const E * vals, size_t dim ); |
---|
215 | E * bsearchu( E key, const E * vals, size_t dim ); |
---|
216 | size_t bsearchu( E key, const E * vals, size_t dim ); |
---|
217 | |
---|
218 | void qsort( E * vals, size_t dim ); |
---|
219 | } // distribution |
---|
220 | |
---|
221 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) { |
---|
222 | E * bsearch( K key, const E * vals, size_t dim ); |
---|
223 | size_t bsearch( K key, const E * vals, size_t dim ); |
---|
224 | E * bsearchl( K key, const E * vals, size_t dim ); |
---|
225 | size_t bsearchl( K key, const E * vals, size_t dim ); |
---|
226 | E * bsearchu( K key, const E * vals, size_t dim ); |
---|
227 | size_t bsearchu( K key, const E * vals, size_t dim ); |
---|
228 | } // distribution |
---|
229 | |
---|
230 | //--------------------------------------- |
---|
231 | |
---|
232 | extern "C" { // override C version |
---|
233 | void srandom( unsigned int seed ); |
---|
234 | long int random( void ); |
---|
235 | } // extern "C" |
---|
236 | |
---|
237 | static inline { |
---|
238 | long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u) |
---|
239 | long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u) |
---|
240 | unsigned long int random( void ) { return lrand48(); } |
---|
241 | 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) |
---|
242 | unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u) |
---|
243 | |
---|
244 | char random( void ) { return (unsigned long int)random(); } |
---|
245 | char random( char u ) { return random( (unsigned long int)u ); } // [0,u) |
---|
246 | char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u) |
---|
247 | int random( void ) { return (long int)random(); } |
---|
248 | int random( int u ) { return random( (long int)u ); } // [0,u] |
---|
249 | int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u) |
---|
250 | unsigned int random( void ) { return (unsigned long int)random(); } |
---|
251 | unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u] |
---|
252 | unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u) |
---|
253 | } // distribution |
---|
254 | |
---|
255 | float random( void ); // [0.0, 1.0) |
---|
256 | double random( void ); // [0.0, 1.0) |
---|
257 | float _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
258 | double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
259 | long double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
260 | |
---|
261 | //--------------------------------------- |
---|
262 | |
---|
263 | #include "common" |
---|
264 | |
---|
265 | // Local Variables: // |
---|
266 | // mode: c // |
---|
267 | // tab-width: 4 // |
---|
268 | // End: // |
---|