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.c --
|
---|
8 | //
|
---|
9 | // Author : Peter A. Buhr
|
---|
10 | // Created On : Thu Jan 28 17:10:29 2016
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Tue Oct 22 08:57:52 2019
|
---|
13 | // Update Count : 478
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "stdlib.hfa"
|
---|
17 |
|
---|
18 | //---------------------------------------
|
---|
19 |
|
---|
20 | #define _XOPEN_SOURCE 600 // posix_memalign, *rand48
|
---|
21 | #include <string.h> // memcpy, memset
|
---|
22 | #include <malloc.h> // malloc_usable_size
|
---|
23 | //#include <math.h> // fabsf, fabs, fabsl
|
---|
24 | #include <complex.h> // _Complex_I
|
---|
25 | #include <assert.h>
|
---|
26 |
|
---|
27 | //---------------------------------------
|
---|
28 |
|
---|
29 | forall( dtype T | sized(T) ) {
|
---|
30 | T * alloc_set( T ptr[], size_t dim, char fill ) { // realloc array with fill
|
---|
31 | size_t olen = malloc_usable_size( ptr ); // current allocation
|
---|
32 | char * nptr = (char *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
|
---|
33 | size_t nlen = malloc_usable_size( nptr ); // new allocation
|
---|
34 | if ( nlen > olen ) { // larger ?
|
---|
35 | memset( nptr + olen, (int)fill, nlen - olen ); // initialize added storage
|
---|
36 | } // if
|
---|
37 | return (T *)nptr;
|
---|
38 | } // alloc_set
|
---|
39 |
|
---|
40 | T * alloc_align( T ptr[], size_t align ) { // aligned realloc array
|
---|
41 | char * nptr;
|
---|
42 | size_t alignment = malloc_alignment( ptr );
|
---|
43 | if ( align != alignment && (uintptr_t)ptr % align != 0 ) {
|
---|
44 | size_t olen = malloc_usable_size( ptr ); // current allocation
|
---|
45 | nptr = (char *)memalign( align, olen );
|
---|
46 | size_t nlen = malloc_usable_size( nptr ); // new allocation
|
---|
47 | size_t lnth = olen < nlen ? olen : nlen; // min
|
---|
48 | memcpy( nptr, ptr, lnth ); // initialize storage
|
---|
49 | free( ptr );
|
---|
50 | } else {
|
---|
51 | nptr = (char *)ptr;
|
---|
52 | } // if
|
---|
53 | return (T *)nptr;
|
---|
54 | } // alloc_align
|
---|
55 |
|
---|
56 | T * alloc_align( T ptr[], size_t align, size_t dim ) { // aligned realloc array
|
---|
57 | char * nptr;
|
---|
58 | size_t alignment = malloc_alignment( ptr );
|
---|
59 | if ( align != alignment ) {
|
---|
60 | size_t olen = malloc_usable_size( ptr ); // current allocation
|
---|
61 | nptr = (char *)memalign( align, dim * sizeof(T) );
|
---|
62 | size_t nlen = malloc_usable_size( nptr ); // new allocation
|
---|
63 | size_t lnth = olen < nlen ? olen : nlen; // min
|
---|
64 | memcpy( nptr, ptr, lnth ); // initialize storage
|
---|
65 | free( ptr );
|
---|
66 | } else {
|
---|
67 | nptr = (char *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
|
---|
68 | } // if
|
---|
69 | return (T *)nptr;
|
---|
70 | } // alloc_align
|
---|
71 |
|
---|
72 | T * alloc_align_set( T ptr[], size_t align, char fill ) { // aligned realloc with fill
|
---|
73 | size_t olen = malloc_usable_size( ptr ); // current allocation
|
---|
74 | char * nptr = alloc_align( ptr, align );
|
---|
75 | size_t nlen = malloc_usable_size( nptr ); // new allocation
|
---|
76 | if ( nlen > olen ) { // larger ?
|
---|
77 | memset( nptr + olen, (int)fill, nlen - olen ); // initialize added storage
|
---|
78 | } // if
|
---|
79 | return (T *)nptr;
|
---|
80 | } // alloc_align_set
|
---|
81 | } // distribution
|
---|
82 |
|
---|
83 | // allocation/deallocation and constructor/destructor, non-array types
|
---|
84 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
|
---|
85 | T * new( Params p ) {
|
---|
86 | return &(*malloc()){ p }; // run constructor
|
---|
87 | } // new
|
---|
88 |
|
---|
89 | forall( dtype T | sized(T) | { void ^?{}( T & ); } )
|
---|
90 | void delete( T * ptr ) {
|
---|
91 | if ( ptr ) { // ignore null
|
---|
92 | ^(*ptr){}; // run destructor
|
---|
93 | free( ptr );
|
---|
94 | } // if
|
---|
95 | } // delete
|
---|
96 |
|
---|
97 | forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } )
|
---|
98 | void delete( T * ptr, Params rest ) {
|
---|
99 | if ( ptr ) { // ignore null
|
---|
100 | ^(*ptr){}; // run destructor
|
---|
101 | free( ptr );
|
---|
102 | } // if
|
---|
103 | delete( rest );
|
---|
104 | } // delete
|
---|
105 |
|
---|
106 |
|
---|
107 | // allocation/deallocation and constructor/destructor, array types
|
---|
108 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
|
---|
109 | T * anew( size_t dim, Params p ) {
|
---|
110 | T * arr = alloc( dim );
|
---|
111 | for ( unsigned int i = 0; i < dim; i += 1 ) {
|
---|
112 | (arr[i]){ p }; // run constructor
|
---|
113 | } // for
|
---|
114 | return arr;
|
---|
115 | } // anew
|
---|
116 |
|
---|
117 | forall( dtype T | sized(T) | { void ^?{}( T & ); } )
|
---|
118 | void adelete( size_t dim, T arr[] ) {
|
---|
119 | if ( arr ) { // ignore null
|
---|
120 | for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned
|
---|
121 | ^(arr[i]){}; // run destructor
|
---|
122 | } // for
|
---|
123 | free( arr );
|
---|
124 | } // if
|
---|
125 | } // adelete
|
---|
126 |
|
---|
127 | forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } )
|
---|
128 | void adelete( size_t dim, T arr[], Params rest ) {
|
---|
129 | if ( arr ) { // ignore null
|
---|
130 | for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned
|
---|
131 | ^(arr[i]){}; // run destructor
|
---|
132 | } // for
|
---|
133 | free( arr );
|
---|
134 | } // if
|
---|
135 | adelete( rest );
|
---|
136 | } // adelete
|
---|
137 |
|
---|
138 | //---------------------------------------
|
---|
139 |
|
---|
140 | float _Complex strto( const char * sptr, char ** eptr ) {
|
---|
141 | float re, im;
|
---|
142 | char * eeptr;
|
---|
143 | re = strtof( sptr, &eeptr );
|
---|
144 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
|
---|
145 | im = strtof( eeptr, &eeptr );
|
---|
146 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
|
---|
147 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
|
---|
148 | return re + im * _Complex_I;
|
---|
149 | } // strto
|
---|
150 |
|
---|
151 | double _Complex strto( const char * sptr, char ** eptr ) {
|
---|
152 | double re, im;
|
---|
153 | char * eeptr;
|
---|
154 | re = strtod( sptr, &eeptr );
|
---|
155 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
|
---|
156 | im = strtod( eeptr, &eeptr );
|
---|
157 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
|
---|
158 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
|
---|
159 | return re + im * _Complex_I;
|
---|
160 | } // strto
|
---|
161 |
|
---|
162 | long double _Complex strto( const char * sptr, char ** eptr ) {
|
---|
163 | long double re, im;
|
---|
164 | char * eeptr;
|
---|
165 | re = strtold( sptr, &eeptr );
|
---|
166 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
|
---|
167 | im = strtold( eeptr, &eeptr );
|
---|
168 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
|
---|
169 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
|
---|
170 | return re + im * _Complex_I;
|
---|
171 | } // strto
|
---|
172 |
|
---|
173 | //---------------------------------------
|
---|
174 |
|
---|
175 | forall( otype E | { int ?<?( E, E ); } ) {
|
---|
176 | E * bsearch( E key, const E * vals, size_t dim ) {
|
---|
177 | int cmp( const void * t1, const void * t2 ) {
|
---|
178 | return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
|
---|
179 | } // cmp
|
---|
180 | return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
|
---|
181 | } // bsearch
|
---|
182 |
|
---|
183 | size_t bsearch( E key, const E * vals, size_t dim ) {
|
---|
184 | E * result = bsearch( key, vals, dim );
|
---|
185 | return result ? result - vals : dim; // pointer subtraction includes sizeof(E)
|
---|
186 | } // bsearch
|
---|
187 |
|
---|
188 | size_t bsearchl( E key, const E * vals, size_t dim ) {
|
---|
189 | size_t l = 0, m, h = dim;
|
---|
190 | while ( l < h ) {
|
---|
191 | m = (l + h) / 2;
|
---|
192 | if ( (E &)(vals[m]) < key ) { // cast away const
|
---|
193 | l = m + 1;
|
---|
194 | } else {
|
---|
195 | h = m;
|
---|
196 | } // if
|
---|
197 | } // while
|
---|
198 | return l;
|
---|
199 | } // bsearchl
|
---|
200 |
|
---|
201 | E * bsearchl( E key, const E * vals, size_t dim ) {
|
---|
202 | size_t posn = bsearchl( key, vals, dim );
|
---|
203 | return (E *)(&vals[posn]); // cast away const
|
---|
204 | } // bsearchl
|
---|
205 |
|
---|
206 | size_t bsearchu( E key, const E * vals, size_t dim ) {
|
---|
207 | size_t l = 0, m, h = dim;
|
---|
208 | while ( l < h ) {
|
---|
209 | m = (l + h) / 2;
|
---|
210 | if ( ! ( key < (E &)(vals[m]) ) ) { // cast away const
|
---|
211 | l = m + 1;
|
---|
212 | } else {
|
---|
213 | h = m;
|
---|
214 | } // if
|
---|
215 | } // while
|
---|
216 | return l;
|
---|
217 | } // bsearchu
|
---|
218 |
|
---|
219 | E * bsearchu( E key, const E * vals, size_t dim ) {
|
---|
220 | size_t posn = bsearchu( key, vals, dim );
|
---|
221 | return (E *)(&vals[posn]);
|
---|
222 | } // bsearchu
|
---|
223 |
|
---|
224 |
|
---|
225 | void qsort( E * vals, size_t dim ) {
|
---|
226 | int cmp( const void * t1, const void * t2 ) {
|
---|
227 | return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
|
---|
228 | } // cmp
|
---|
229 | qsort( vals, dim, sizeof(E), cmp );
|
---|
230 | } // qsort
|
---|
231 | } // distribution
|
---|
232 |
|
---|
233 |
|
---|
234 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
|
---|
235 | E * bsearch( K key, const E * vals, size_t dim ) {
|
---|
236 | int cmp( const void * t1, const void * t2 ) {
|
---|
237 | return *(K *)t1 < getKey( *(E *)t2 ) ? -1 : getKey( *(E *)t2 ) < *(K *)t1 ? 1 : 0;
|
---|
238 | } // cmp
|
---|
239 | return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
|
---|
240 | } // bsearch
|
---|
241 |
|
---|
242 | size_t bsearch( K key, const E * vals, size_t dim ) {
|
---|
243 | E * result = bsearch( key, vals, dim );
|
---|
244 | return result ? result - vals : dim; // pointer subtraction includes sizeof(E)
|
---|
245 | } // bsearch
|
---|
246 |
|
---|
247 | size_t bsearchl( K key, const E * vals, size_t dim ) {
|
---|
248 | size_t l = 0, m, h = dim;
|
---|
249 | while ( l < h ) {
|
---|
250 | m = (l + h) / 2;
|
---|
251 | if ( getKey( vals[m] ) < key ) {
|
---|
252 | l = m + 1;
|
---|
253 | } else {
|
---|
254 | h = m;
|
---|
255 | } // if
|
---|
256 | } // while
|
---|
257 | return l;
|
---|
258 | } // bsearchl
|
---|
259 |
|
---|
260 | E * bsearchl( K key, const E * vals, size_t dim ) {
|
---|
261 | size_t posn = bsearchl( key, vals, dim );
|
---|
262 | return (E *)(&vals[posn]); // cast away const
|
---|
263 | } // bsearchl
|
---|
264 |
|
---|
265 | size_t bsearchu( K key, const E * vals, size_t dim ) {
|
---|
266 | size_t l = 0, m, h = dim;
|
---|
267 | while ( l < h ) {
|
---|
268 | m = (l + h) / 2;
|
---|
269 | if ( ! ( key < getKey( vals[m] ) ) ) {
|
---|
270 | l = m + 1;
|
---|
271 | } else {
|
---|
272 | h = m;
|
---|
273 | } // if
|
---|
274 | } // while
|
---|
275 | return l;
|
---|
276 | } // bsearchu
|
---|
277 |
|
---|
278 | E * bsearchu( K key, const E * vals, size_t dim ) {
|
---|
279 | size_t posn = bsearchu( key, vals, dim );
|
---|
280 | return (E *)(&vals[posn]);
|
---|
281 | } // bsearchu
|
---|
282 | } // distribution
|
---|
283 |
|
---|
284 | //---------------------------------------
|
---|
285 |
|
---|
286 | extern "C" { // override C version
|
---|
287 | void srandom( unsigned int seed ) { srand48( (long int)seed ); }
|
---|
288 | long int random( void ) { return mrand48(); }
|
---|
289 | } // extern "C"
|
---|
290 |
|
---|
291 | float random( void ) { return (float)drand48(); } // cast otherwise float uses lrand48
|
---|
292 | double random( void ) { return drand48(); }
|
---|
293 | float _Complex random( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
|
---|
294 | double _Complex random( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); }
|
---|
295 | long double _Complex random( void ) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
|
---|
296 |
|
---|
297 | //---------------------------------------
|
---|
298 |
|
---|
299 | bool threading_enabled(void) __attribute__((weak)) {
|
---|
300 | return false;
|
---|
301 | }
|
---|
302 |
|
---|
303 | // Local Variables: //
|
---|
304 | // tab-width: 4 //
|
---|
305 | // End: //
|
---|