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 : Tue Apr 23 14:05:21 2024 |
---|
13 | // Update Count : 963 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include "bits/defs.hfa" // OPTIONAL_THREAD |
---|
19 | #include "bits/align.hfa" // libAlign |
---|
20 | #include "bits/random.hfa" // prng |
---|
21 | #include <Exception.hfa> |
---|
22 | #include <heap.hfa> |
---|
23 | |
---|
24 | #include <stdlib.h> // *alloc, strto*, ato* |
---|
25 | #include <errno.h> |
---|
26 | |
---|
27 | // Reduce includes by explicitly defining these routines. |
---|
28 | extern "C" { |
---|
29 | void * memalign( size_t alignment, size_t size ); // malloc.h |
---|
30 | void * pvalloc( size_t size ); // malloc.h |
---|
31 | void * memset( void * dest, int fill, size_t size ); // string.h |
---|
32 | void * memcpy( void * dest, const void * src, size_t size ); // string.h |
---|
33 | } // extern "C" |
---|
34 | |
---|
35 | //--------------------------------------- |
---|
36 | |
---|
37 | #ifndef EXIT_FAILURE |
---|
38 | #define EXIT_FAILURE 1 // failing exit status |
---|
39 | #define EXIT_SUCCESS 0 // successful exit status |
---|
40 | #endif // ! EXIT_FAILURE |
---|
41 | |
---|
42 | //--------------------------------------- |
---|
43 | |
---|
44 | #include "common.hfa" |
---|
45 | |
---|
46 | //--------------------------------------- |
---|
47 | |
---|
48 | static inline forall( T & | sized(T) ) { |
---|
49 | // CFA safe equivalents, i.e., implicit size specification, eliminate return-type cast |
---|
50 | |
---|
51 | T * malloc( void ) { |
---|
52 | if ( _Alignof(T) <= libAlign() ) return (T *)malloc( sizeof(T) ); // C allocation |
---|
53 | else return (T *)memalign( _Alignof(T), sizeof(T) ); |
---|
54 | } // malloc |
---|
55 | |
---|
56 | T * aalloc( size_t dim ) { |
---|
57 | if ( _Alignof(T) <= libAlign() ) return (T *)aalloc( dim, sizeof(T) ); // C allocation |
---|
58 | else return (T *)amemalign( _Alignof(T), dim, sizeof(T) ); |
---|
59 | } // aalloc |
---|
60 | |
---|
61 | T * calloc( size_t dim ) { |
---|
62 | if ( _Alignof(T) <= libAlign() ) return (T *)calloc( dim, sizeof(T) ); // C allocation |
---|
63 | else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) ); |
---|
64 | } // calloc |
---|
65 | |
---|
66 | T * resize( T * ptr, size_t size ) { |
---|
67 | if ( _Alignof(T) <= libAlign() ) return (T *)resize( (void *)ptr, size ); // C resize |
---|
68 | else return (T *)resize( (void *)ptr, _Alignof(T), size ); // CFA resize |
---|
69 | } // resize |
---|
70 | |
---|
71 | T * resize( T * ptr, size_t alignment, size_t size ) { |
---|
72 | return (T *)resize( (void *)ptr, alignment, size ); // CFA resize |
---|
73 | } // resize |
---|
74 | |
---|
75 | T * realloc( T * ptr, size_t size ) { // CFA realloc |
---|
76 | if ( _Alignof(T) <= libAlign() ) return (T *)realloc( (void *)ptr, size ); // C realloc |
---|
77 | else return (T *)realloc( (void *)ptr, _Alignof(T), size ); // CFA realloc |
---|
78 | } // realloc |
---|
79 | |
---|
80 | T * realloc( T * ptr, size_t alignment, size_t size ) { |
---|
81 | return (T *)realloc( (void *)ptr, alignment, size ); // CFA realloc |
---|
82 | } // realloc |
---|
83 | |
---|
84 | T * reallocarray( T * ptr, size_t dim ) { // CFA reallocarray |
---|
85 | if ( _Alignof(T) <= libAlign() ) return (T *)reallocarray( (void *)ptr, dim, sizeof(T) ); // C reallocarray |
---|
86 | else return (T *)reallocarray( (void *)ptr, _Alignof(T), dim ); // CFA reallocarray |
---|
87 | } // realloc |
---|
88 | |
---|
89 | T * reallocarray( T * ptr, size_t alignment, size_t dim ) { |
---|
90 | return (T *)reallocarray( (void *)ptr, alignment, dim ); // CFA reallocarray |
---|
91 | } // realloc |
---|
92 | |
---|
93 | T * memalign( size_t align ) { |
---|
94 | return (T *)memalign( align, sizeof(T) ); // C memalign |
---|
95 | } // memalign |
---|
96 | |
---|
97 | T * amemalign( size_t align, size_t dim ) { |
---|
98 | return (T *)amemalign( align, dim, sizeof(T) ); // CFA amemalign |
---|
99 | } // amemalign |
---|
100 | |
---|
101 | T * cmemalign( size_t align, size_t dim ) { |
---|
102 | return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign |
---|
103 | } // cmemalign |
---|
104 | |
---|
105 | T * aligned_alloc( size_t align ) { |
---|
106 | return (T *)aligned_alloc( align, sizeof(T) ); // C aligned_alloc |
---|
107 | } // aligned_alloc |
---|
108 | |
---|
109 | int posix_memalign( T ** ptr, size_t align ) { |
---|
110 | return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign |
---|
111 | } // posix_memalign |
---|
112 | |
---|
113 | T * valloc( void ) { |
---|
114 | return (T *)valloc( sizeof(T) ); // C valloc |
---|
115 | } // valloc |
---|
116 | |
---|
117 | T * pvalloc( void ) { |
---|
118 | return (T *)pvalloc( sizeof(T) ); // C pvalloc |
---|
119 | } // pvalloc |
---|
120 | } // distribution |
---|
121 | |
---|
122 | /* |
---|
123 | FIX ME : fix alloc interface after Ticker Number 214 is resolved, define and add union to S_fill. Then, modify |
---|
124 | postfix-fill functions to support T * with nmemb, char, and T object of any size. Finally, change alloc_internal. |
---|
125 | Or, just follow the instructions below for that. |
---|
126 | |
---|
127 | 1. Replace the current forall-block that contains defintions of S_fill and S_realloc with following: |
---|
128 | forall( T & | sized(T) ) { |
---|
129 | union U_fill { char c; T * a; T t; }; |
---|
130 | struct S_fill { char tag; U_fill(T) fill; }; |
---|
131 | struct S_realloc { inline T *; }; |
---|
132 | } |
---|
133 | |
---|
134 | 2. Replace all current postfix-fill functions with following for updated S_fill: |
---|
135 | S_fill(T) ?`fill( char a ) { S_fill(T) ret = {'c'}; ret.fill.c = a; return ret; } |
---|
136 | S_fill(T) ?`fill( T a ) { S_fill(T) ret = {'t'}; memcpy(&ret.fill.t, &a, sizeof(T)); return ret; } |
---|
137 | S_fill(T) ?`fill( T a[], size_t nmemb ) { S_fill(T) ret = {'a', nmemb}; ret.fill.a = a; return ret; } |
---|
138 | |
---|
139 | 3. Replace the alloc_internal$ function which is outside ttype forall-block with following function: |
---|
140 | T * alloc_internal$( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill) { |
---|
141 | T * ptr = NULL; |
---|
142 | size_t size = sizeof(T); |
---|
143 | size_t copy_end = 0; |
---|
144 | |
---|
145 | if(Resize) { |
---|
146 | ptr = (T*) (void *) resize( (int *)Resize, Align, Dim * size ); |
---|
147 | } else if (Realloc) { |
---|
148 | if (Fill.tag != '0') copy_end = min(malloc_size( Realloc ), Dim * size); |
---|
149 | ptr = (T*) (void *) realloc( (int *)Realloc, Align, Dim * size ); |
---|
150 | } else { |
---|
151 | ptr = (T*) (void *) memalign( Align, Dim * size ); |
---|
152 | } |
---|
153 | |
---|
154 | if(Fill.tag == 'c') { |
---|
155 | memset( (char *)ptr + copy_end, (int)Fill.fill.c, Dim * size - copy_end ); |
---|
156 | } else if(Fill.tag == 't') { |
---|
157 | for ( int i = copy_end; i <= Dim * size - size ; i += size ) { |
---|
158 | memcpy( (char *)ptr + i, &Fill.fill.t, size ); |
---|
159 | } |
---|
160 | } else if(Fill.tag == 'a') { |
---|
161 | memcpy( (char *)ptr + copy_end, Fill.fill.a, min(Dim * size - copy_end, size * Fill.nmemb) ); |
---|
162 | } |
---|
163 | |
---|
164 | return ptr; |
---|
165 | } // alloc_internal$ |
---|
166 | */ |
---|
167 | |
---|
168 | #pragma GCC diagnostic push |
---|
169 | #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
---|
170 | #pragma GCC diagnostic ignored "-Wuninitialized" |
---|
171 | |
---|
172 | struct T_align { size_t align; }; |
---|
173 | struct T_resize { void * addr; }; |
---|
174 | struct T_realloc { void * addr; }; |
---|
175 | forall( T & ) struct T_fill { |
---|
176 | // 'N' => no fill, 'c' => fill with character c, 'a' => fill first N array elements from another array, |
---|
177 | // 'A' => fill all array elements from another array, 'T' => fill using a T value. |
---|
178 | char tag; |
---|
179 | size_t nelem; // number of elements copied from "at" (used with tag 'a') |
---|
180 | // union { |
---|
181 | char c; |
---|
182 | T * at; |
---|
183 | char t[64]; // T t; |
---|
184 | // }; |
---|
185 | }; |
---|
186 | |
---|
187 | #pragma GCC diagnostic pop |
---|
188 | |
---|
189 | static inline { |
---|
190 | T_align ?`align( size_t a ) { return (T_align){ a }; } |
---|
191 | T_resize ?`resize( void * a ) { return (T_resize){ a }; } |
---|
192 | T_realloc ?`realloc( void * a ) { return (T_realloc){ a }; } |
---|
193 | } |
---|
194 | |
---|
195 | static inline forall( T & | sized(T) ) { |
---|
196 | T_fill(T) ?`fill( char c ) { return (T_fill(T)){ 'c', 0, c }; } |
---|
197 | T_fill(T) ?`fill( T t ) { |
---|
198 | T_fill(T) ret = { 'T' }; |
---|
199 | size_t size = sizeof(T); |
---|
200 | if ( size > sizeof(ret.t) ) { |
---|
201 | abort( "ERROR: const object of size greater than 50 bytes given for dynamic memory fill\n" ); |
---|
202 | } // if |
---|
203 | memcpy( &ret.t, &t, size ); |
---|
204 | return ret; |
---|
205 | } |
---|
206 | T_fill(T) ?`fill( T a[] ) { return (T_fill(T)){ 'A', 0, '\0', a }; } // FIX ME: remove this once ticket 214 is resolved |
---|
207 | T_fill(T) ?`fill( T a[], size_t nelem ) { return (T_fill(T)){ 'a', nelem * sizeof(T), '\0', a }; } |
---|
208 | |
---|
209 | // private interface |
---|
210 | T * alloc_internal$( size_t Dim, T_resize Resize, T_realloc Realloc, size_t Align, T_fill(T) Fill ) { |
---|
211 | T * ptr; |
---|
212 | size_t tsize = sizeof(T); |
---|
213 | size_t copy_end = 0; |
---|
214 | |
---|
215 | if ( Resize.addr ) { |
---|
216 | ptr = (T *)(void *)resize( Resize.addr, Align, Dim * tsize ); |
---|
217 | } else if ( Realloc.addr ) { |
---|
218 | if ( Fill.tag != 'N' ) copy_end = min(malloc_size( Realloc.addr ), Dim * tsize ); |
---|
219 | ptr = (T *)(void *)realloc( Realloc.addr, Align, Dim * tsize ); |
---|
220 | } else { |
---|
221 | ptr = (T *)(void *)memalign( Align, Dim * tsize ); |
---|
222 | } // if |
---|
223 | |
---|
224 | if ( Fill.tag == 'c' ) { |
---|
225 | memset( (char *)ptr + copy_end, (int)Fill.c, Dim * tsize - copy_end ); |
---|
226 | } else if ( Fill.tag == 'T' ) { |
---|
227 | for ( i; copy_end ~ Dim * tsize ~ tsize ) { |
---|
228 | assert( tsize <= sizeof(Fill.t) ); |
---|
229 | memcpy( (char *)ptr + i, &Fill.t, tsize ); |
---|
230 | } // for |
---|
231 | } else if ( Fill.tag == 'a' ) { |
---|
232 | memcpy( (char *)ptr + copy_end, Fill.at, min( Dim * tsize - copy_end, Fill.nelem ) ); |
---|
233 | } else if ( Fill.tag == 'A' ) { |
---|
234 | memcpy( (char *)ptr + copy_end, Fill.at, Dim * tsize ); |
---|
235 | } // if |
---|
236 | return ptr; |
---|
237 | } // alloc_internal$ |
---|
238 | |
---|
239 | // Dim is a fixed (optional first) parameter, and hence is not set using a postfix function. A dummy parameter is |
---|
240 | // being overwritten by the postfix argument in the ttype. |
---|
241 | forall( List ... | { T * alloc_internal$( size_t Dim, T_resize Resize, T_realloc Realloc, size_t Align, T_fill(T) Fill, List ); } ) { |
---|
242 | // middle interface |
---|
243 | T * alloc_internal$( size_t Dim, T_resize dummy, T_realloc Realloc, size_t Align, T_fill(T) Fill, T_resize Resize, List rest ) { |
---|
244 | return alloc_internal$( Dim, Resize, (T_realloc){0p}, Align, Fill, rest ); |
---|
245 | } |
---|
246 | T * alloc_internal$( size_t Dim, T_resize Resize, T_realloc dummy, size_t Align, T_fill(T) Fill, T_realloc Realloc, List rest ) { |
---|
247 | return alloc_internal$( Dim, (T_resize){0p}, Realloc, Align, Fill, rest ); |
---|
248 | } |
---|
249 | T * alloc_internal$( size_t Dim, T_resize Resize, T_realloc Realloc, size_t dummy, T_fill(T) Fill, T_align Align, List rest ) { |
---|
250 | return alloc_internal$( Dim, Resize, Realloc, Align.align, Fill, rest ); |
---|
251 | } |
---|
252 | T * alloc_internal$( size_t Dim, T_resize Resize, T_realloc Realloc, size_t Align, T_fill(T) dummy, T_fill(T) Fill, List rest ) { |
---|
253 | return alloc_internal$( Dim, Resize, Realloc, Align, Fill, rest ); |
---|
254 | } |
---|
255 | // public interface |
---|
256 | T * alloc( List rest ) { |
---|
257 | return alloc_internal$( (size_t)1, (T_resize){0p}, (T_realloc){0p}, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), (T_fill(T)){'N'}, rest ); |
---|
258 | } |
---|
259 | T * alloc( size_t Dim, List rest ) { |
---|
260 | return alloc_internal$( Dim, (T_resize){0p}, (T_realloc){0p}, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), (T_fill(T)){'N'}, rest ); |
---|
261 | } |
---|
262 | } // distribution List |
---|
263 | } // distribution T |
---|
264 | |
---|
265 | static inline forall( T & | sized(T) ) { |
---|
266 | // CFA safe initialization/copy, i.e., implicit size specification, non-array types |
---|
267 | T * memset( T * dest, char fill ) { // all combinations of pointer/reference |
---|
268 | return (T *)memset( dest, fill, sizeof(T) ); // C memset |
---|
269 | } // memset |
---|
270 | T * memset( T & dest, char fill ) { |
---|
271 | return (T *)memset( &dest, fill, sizeof(T) ); // C memset |
---|
272 | } // memset |
---|
273 | |
---|
274 | T * memcpy( T * dest, const T * src ) { // all combinations of pointer/reference |
---|
275 | return (T *)memcpy( dest, src, sizeof(T) ); // C memcpy |
---|
276 | } // memcpy |
---|
277 | T * memcpy( T & dest, const T & src ) { |
---|
278 | return (T *)memcpy( &dest, &src, sizeof(T) ); // C memcpy |
---|
279 | } // memcpy |
---|
280 | T * memcpy( T * dest, const T & src ) { |
---|
281 | return (T *)memcpy( dest, &src, sizeof(T) ); // C memcpy |
---|
282 | } // memcpy |
---|
283 | T * memcpy( T & dest, const T * src ) { |
---|
284 | return (T *)memcpy( &dest, src, sizeof(T) ); // C memcpy |
---|
285 | } // memcpy |
---|
286 | |
---|
287 | // CFA safe initialization/copy, i.e., implicit size specification, array types |
---|
288 | T * amemset( T dest[], char fill, size_t dim ) { |
---|
289 | return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset |
---|
290 | } // amemset |
---|
291 | |
---|
292 | T * amemcpy( T dest[], const T src[], size_t dim ) { |
---|
293 | return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy |
---|
294 | } // amemcpy |
---|
295 | } // distribution |
---|
296 | |
---|
297 | // CFA deallocation for multiple objects |
---|
298 | static inline forall( T & ) |
---|
299 | void free( T * ptr ) { |
---|
300 | free( (void *)ptr ); // C free |
---|
301 | } // free |
---|
302 | static inline forall( T &, List ... | { void free( List ); } ) |
---|
303 | void free( T * ptr, List rest ) { |
---|
304 | free( ptr ); |
---|
305 | free( rest ); |
---|
306 | } // free |
---|
307 | |
---|
308 | // CFA allocation/deallocation and constructor/destructor, non-array types |
---|
309 | static inline forall( T & | sized(T), Parms ... | { void ?{}( T &, Parms ); } ) |
---|
310 | T * new( Parms p ) { |
---|
311 | return &(*(T *)malloc()){ p }; // run constructor |
---|
312 | } // new |
---|
313 | |
---|
314 | static inline forall( T & | { void ^?{}( T & ); } ) |
---|
315 | void delete( T * ptr ) { |
---|
316 | // special case for 0-sized object => always call destructor |
---|
317 | if ( ptr || sizeof(ptr) == 0 ) { // ignore null but not 0-sized objects |
---|
318 | ^(*ptr){}; // run destructor |
---|
319 | } // if |
---|
320 | free( ptr ); // always call free |
---|
321 | } // delete |
---|
322 | static inline forall( T &, List ... | { void ^?{}( T & ); void delete( List ); } ) |
---|
323 | void delete( T * ptr, List rest ) { |
---|
324 | delete( ptr ); |
---|
325 | delete( rest ); |
---|
326 | } // delete |
---|
327 | |
---|
328 | // CFA allocation/deallocation and constructor/destructor, array types |
---|
329 | forall( T & | sized(T), Parms ... | { void ?{}( T &, Parms ); } ) T * anew( size_t dim, Parms p ); |
---|
330 | forall( T & | sized(T) | { void ^?{}( T & ); } ) void adelete( T arr[] ); |
---|
331 | forall( T & | sized(T) | { void ^?{}( T & ); }, List ... | { void adelete( List ); } ) void adelete( T arr[], List rest ); |
---|
332 | |
---|
333 | //--------------------------------------- |
---|
334 | |
---|
335 | // Check if all string characters are a specific kind, e.g., checkif( s, isblank ) |
---|
336 | bool checkif( const char s[], int (* kind)( int ) ); |
---|
337 | bool checkif( const char s[], int (* kind)( int, locale_t ), locale_t locale ); |
---|
338 | |
---|
339 | //--------------------------------------- |
---|
340 | |
---|
341 | static inline { |
---|
342 | int strto( const char sptr[], char * eptr[], int base ) { return (int)strtol( sptr, eptr, base ); } |
---|
343 | unsigned int strto( const char sptr[], char * eptr[], int base ) { return (unsigned int)strtoul( sptr, eptr, base ); } |
---|
344 | long int strto( const char sptr[], char * eptr[], int base ) { return strtol( sptr, eptr, base ); } |
---|
345 | unsigned long int strto( const char sptr[], char * eptr[], int base ) { return strtoul( sptr, eptr, base ); } |
---|
346 | long long int strto( const char sptr[], char * eptr[], int base ) { return strtoll( sptr, eptr, base ); } |
---|
347 | unsigned long long int strto( const char sptr[], char * eptr[], int base ) { return strtoull( sptr, eptr, base ); } |
---|
348 | |
---|
349 | float strto( const char sptr[], char * eptr[] ) { return strtof( sptr, eptr ); } |
---|
350 | double strto( const char sptr[], char * eptr[] ) { return strtod( sptr, eptr ); } |
---|
351 | long double strto( const char sptr[], char * eptr[] ) { return strtold( sptr, eptr ); } |
---|
352 | } // distribution |
---|
353 | |
---|
354 | float _Complex strto( const char sptr[], char * eptr[] ); |
---|
355 | double _Complex strto( const char sptr[], char * eptr[] ); |
---|
356 | long double _Complex strto( const char sptr[], char * eptr[] ); |
---|
357 | |
---|
358 | ExceptionDecl( out_of_range ); |
---|
359 | ExceptionDecl( invalid_argument ); |
---|
360 | |
---|
361 | forall( T | { T strto( const char sptr[], char * eptr[], int ); } ) |
---|
362 | T convert( const char sptr[] ); // integrals |
---|
363 | forall( T | { T strto( const char sptr[], char * eptr[] ); } ) |
---|
364 | T convert( const char sptr[] ); // floating-point (no base) |
---|
365 | |
---|
366 | static inline { |
---|
367 | int ato( const char sptr[] ) { return (int)strtol( sptr, 0p, 10 ); } |
---|
368 | unsigned int ato( const char sptr[] ) { return (unsigned int)strtoul( sptr, 0p, 10 ); } |
---|
369 | long int ato( const char sptr[] ) { return strtol( sptr, 0p, 10 ); } |
---|
370 | unsigned long int ato( const char sptr[] ) { return strtoul( sptr, 0p, 10 ); } |
---|
371 | long long int ato( const char sptr[] ) { return strtoll( sptr, 0p, 10 ); } |
---|
372 | unsigned long long int ato( const char sptr[] ) { return strtoull( sptr, 0p, 10 ); } |
---|
373 | |
---|
374 | float ato( const char sptr[] ) { return strtof( sptr, 0p ); } |
---|
375 | double ato( const char sptr[] ) { return strtod( sptr, 0p ); } |
---|
376 | long double ato( const char sptr[] ) { return strtold( sptr, 0p ); } |
---|
377 | |
---|
378 | float _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); } |
---|
379 | double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); } |
---|
380 | long double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); } |
---|
381 | } // distribution |
---|
382 | |
---|
383 | //--------------------------------------- |
---|
384 | |
---|
385 | forall( E | { int ?<?( E, E ); } ) { |
---|
386 | E * bsearch( E key, const E * vals, size_t dim ); |
---|
387 | size_t bsearch( E key, const E * vals, size_t dim ); |
---|
388 | E * bsearchl( E key, const E * vals, size_t dim ); |
---|
389 | size_t bsearchl( E key, const E * vals, size_t dim ); |
---|
390 | E * bsearchu( E key, const E * vals, size_t dim ); |
---|
391 | size_t bsearchu( E key, const E * vals, size_t dim ); |
---|
392 | } // distribution |
---|
393 | |
---|
394 | forall( K, E | { int ?<?( K, K ); K getKey( const E & ); } ) { |
---|
395 | E * bsearch( K key, const E * vals, size_t dim ); |
---|
396 | size_t bsearch( K key, const E * vals, size_t dim ); |
---|
397 | E * bsearchl( K key, const E * vals, size_t dim ); |
---|
398 | size_t bsearchl( K key, const E * vals, size_t dim ); |
---|
399 | E * bsearchu( K key, const E * vals, size_t dim ); |
---|
400 | size_t bsearchu( K key, const E * vals, size_t dim ); |
---|
401 | } // distribution |
---|
402 | |
---|
403 | forall( E | { int ?<?( E, E ); } ) { |
---|
404 | void qsort( E * vals, size_t dim ); |
---|
405 | } // distribution |
---|
406 | |
---|
407 | //--------------------------------------- |
---|
408 | |
---|
409 | extern "C" { // override C version |
---|
410 | void srandom( unsigned int seed ); |
---|
411 | long int random( void ); // GENERATES POSITIVE AND NEGATIVE VALUES |
---|
412 | // For positive values, use unsigned int, e.g., unsigned int r = random() % 100U; |
---|
413 | } // extern "C" |
---|
414 | |
---|
415 | static inline { |
---|
416 | long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l + 1) + l; } // [l,u] |
---|
417 | long int random( long int u ) { return random( 0, u - 1 ); } // [0,u) |
---|
418 | unsigned long int random( void ) { return lrand48(); } |
---|
419 | unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u) |
---|
420 | unsigned long int random( unsigned long int l, unsigned long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l + 1) + l; } // [l,u] |
---|
421 | |
---|
422 | char random( void ) { return (unsigned long int)random(); } |
---|
423 | char random( char u ) { return (unsigned long int)random( (unsigned long int)u ); } // [0,u) |
---|
424 | char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u) |
---|
425 | int random( void ) { return (long int)random(); } |
---|
426 | int random( int u ) { return (long int)random( (long int)u ); } // [0,u] |
---|
427 | int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u) |
---|
428 | unsigned int random( void ) { return (unsigned long int)random(); } |
---|
429 | unsigned int random( unsigned int u ) { return (unsigned long int)random( (unsigned long int)u ); } // [0,u] |
---|
430 | unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u) |
---|
431 | } // distribution |
---|
432 | |
---|
433 | float random( void ); // [0.0, 1.0) |
---|
434 | double random( void ); // [0.0, 1.0) |
---|
435 | float _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
436 | double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
437 | long double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
438 | |
---|
439 | //--------------------------------------- |
---|
440 | |
---|
441 | // Sequential Pseudo Random-Number Generator : generate repeatable sequence of values that appear random. |
---|
442 | // |
---|
443 | // Declaration : |
---|
444 | // PRNG sprng = { 1009 } - set starting seed versus random seed |
---|
445 | // |
---|
446 | // Interface : |
---|
447 | // set_seed( sprng, 1009 ) - set starting seed for ALL kernel threads versus random seed |
---|
448 | // get_seed( sprng ) - read seed |
---|
449 | // prng( sprng ) - generate random value in range [0,UINT_MAX] |
---|
450 | // prng( sprng, u ) - generate random value in range [0,u) |
---|
451 | // prng( sprng, l, u ) - generate random value in range [l,u] |
---|
452 | // calls( sprng ) - number of generated random value so far |
---|
453 | // |
---|
454 | // Examples : generate random number between 5-21 |
---|
455 | // prng( sprng ) % 17 + 5; values 0-16 + 5 = 5-21 |
---|
456 | // prng( sprng, 16 + 1 ) + 5; |
---|
457 | // prng( sprng, 5, 21 ); |
---|
458 | // calls( sprng ); |
---|
459 | |
---|
460 | forall( PRNG &, R ) |
---|
461 | trait basic_prng { |
---|
462 | void set_seed( PRNG & prng, R seed ); // set seed |
---|
463 | R get_seed( PRNG & prng ); // get seed |
---|
464 | R prng( PRNG & prng ); |
---|
465 | void ?{}( PRNG & prng ); // random seed |
---|
466 | void ?{}( PRNG & prng, R seed ); // fixed seed |
---|
467 | }; // basic_prng |
---|
468 | |
---|
469 | static inline forall( PRNG &, R | basic_prng( PRNG, R ) | { R ?%?( R, R ); } ) { |
---|
470 | R prng( PRNG & prng, R u ) { return prng( prng ) % u; } // [0,u) |
---|
471 | } |
---|
472 | static inline forall( PRNG &, R | basic_prng( PRNG, R ) | { R ?+?( R, R ); R ?-?( R, R ); R ?%?( R, R ); void ?{}( R &, one_t ); } ) { |
---|
473 | R prng( PRNG & prng, R l, R u ) { return prng( prng, u - l + (R){1} ) + l; } // [l,u] |
---|
474 | } |
---|
475 | |
---|
476 | struct PRNG32 { |
---|
477 | uint32_t callcnt; // call count |
---|
478 | uint32_t seed; // current seed |
---|
479 | PRNG_STATE_32_T state; // random state |
---|
480 | }; // PRNG32 |
---|
481 | |
---|
482 | static inline { |
---|
483 | void set_seed( PRNG32 & prng, uint32_t seed_ ) with( prng ) { seed = seed_; PRNG_SET_SEED_32( state, seed ); } |
---|
484 | uint32_t get_seed( PRNG32 & prng ) __attribute__(( warn_unused_result )) with( prng ) { return seed; } |
---|
485 | void ?{}( PRNG32 & prng, uint32_t seed ) with( prng ) { callcnt = 0; set_seed( prng, seed ); } // fixed seed |
---|
486 | void ?{}( PRNG32 & prng ) with( prng ) { ?{}( prng, rdtscl() ); } // random seed |
---|
487 | uint32_t prng( PRNG32 & prng ) __attribute__(( warn_unused_result )) with( prng ) { callcnt += 1; return PRNG_NAME_32( state ); } // [0,UINT_MAX] |
---|
488 | uint32_t prng( PRNG32 & prng, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( prng ) % u; } // [0,u) |
---|
489 | uint32_t prng( PRNG32 & prng, uint32_t l, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( prng, u - l + 1 ) + l; } // [l,u] |
---|
490 | uint32_t calls( PRNG32 & prng ) __attribute__(( warn_unused_result )) with( prng ) { return callcnt; } |
---|
491 | void copy( PRNG32 & dst, PRNG32 & src ) { dst = src; } // checkpoint PRNG state, use autogen assignment |
---|
492 | } // distribution |
---|
493 | void ?{}( PRNG32 &, PRNG32 & ) = void; // no copy, remove autogen copy constructor |
---|
494 | PRNG32 & ?=?( PRNG32 &, const PRNG32 ) = void; // no assignment, remove autogen assignment |
---|
495 | |
---|
496 | struct PRNG64 { |
---|
497 | uint64_t callcnt; // call count |
---|
498 | uint64_t seed; // current seed |
---|
499 | PRNG_STATE_64_T state; // random state |
---|
500 | }; // PRNG64 |
---|
501 | |
---|
502 | static inline { |
---|
503 | void set_seed( PRNG64 & prng, uint64_t seed_ ) with( prng ) { seed = seed_; PRNG_SET_SEED_64( state, seed ); } |
---|
504 | uint64_t get_seed( PRNG64 & prng ) __attribute__(( warn_unused_result )) with( prng ) { return seed; } |
---|
505 | void ?{}( PRNG64 & prng, uint64_t seed ) with( prng ) { callcnt = 0; set_seed( prng, seed ); } // fixed seed |
---|
506 | void ?{}( PRNG64 & prng ) with( prng ) { ?{}( prng, rdtscl() ); } // random seed |
---|
507 | uint64_t prng( PRNG64 & prng ) __attribute__(( warn_unused_result )) with( prng ) { callcnt += 1; return PRNG_NAME_64( state ); } // [0,UINT_MAX] |
---|
508 | uint64_t prng( PRNG64 & prng, uint64_t u ) __attribute__(( warn_unused_result )) { return prng( prng ) % u; } // [0,u) |
---|
509 | uint64_t prng( PRNG64 & prng, uint64_t l, uint64_t u ) __attribute__(( warn_unused_result )) { return prng( prng, u - l + 1 ) + l; } // [l,u] |
---|
510 | uint64_t calls( PRNG64 & prng ) __attribute__(( warn_unused_result )) with( prng ) { return callcnt; } |
---|
511 | void copy( PRNG64 & dst, PRNG64 & src ) { dst = src; } // checkpoint PRNG state, use autogen assignment |
---|
512 | } // distribution |
---|
513 | void ?{}( PRNG64 &, PRNG64 & ) = void; // no copy, remove autogen copy constructor |
---|
514 | PRNG64 & ?=?( PRNG64 &, const PRNG64 ) = void; // no assignment, remove autogen assignment |
---|
515 | |
---|
516 | // Set default random-generator size. |
---|
517 | #if defined( __x86_64__ ) || defined( __aarch64__ ) // 64-bit architecture |
---|
518 | #define PRNG PRNG64 |
---|
519 | #else // 32-bit architecture |
---|
520 | #define PRNG PRNG32 |
---|
521 | #endif // __x86_64__ |
---|
522 | |
---|
523 | // Concurrent Pseudo Random-Number Generator : generate repeatable sequence of values that appear random. |
---|
524 | // |
---|
525 | // Interface : |
---|
526 | // set_seed( 1009 ) - fixed seed for all kernel threads versus random seed |
---|
527 | // get_seed() - read seed |
---|
528 | // prng() - generate random value in range [0,UINT_MAX] |
---|
529 | // prng( u ) - generate random value in range [0,u) |
---|
530 | // prng( l, u ) - generate random value in range [l,u] |
---|
531 | // |
---|
532 | // Examples : generate random number between 5-21 |
---|
533 | // prng() % 17 + 5; values 0-16 + 5 = 5-21 |
---|
534 | // prng( 16 + 1 ) + 5; |
---|
535 | // prng( 5, 21 ); |
---|
536 | |
---|
537 | // Harmonize with concurrency/thread.hfa. |
---|
538 | void set_seed( size_t seed_ ) OPTIONAL_THREAD; // set global seed |
---|
539 | size_t get_seed() __attribute__(( warn_unused_result )); // get global seed |
---|
540 | size_t prng( void ) __attribute__(( warn_unused_result )) OPTIONAL_THREAD; // [0,UINT_MAX] |
---|
541 | static inline { |
---|
542 | size_t prng( size_t u ) __attribute__(( warn_unused_result )) { return prng() % u; } // [0,u) |
---|
543 | size_t prng( size_t l, size_t u ) __attribute__(( warn_unused_result )) { return prng( u - l + 1 ) + l; } // [l,u] |
---|
544 | } // distribution |
---|
545 | |
---|
546 | //--------------------------------------- |
---|
547 | |
---|
548 | extern bool threading_enabled( void ) OPTIONAL_THREAD; |
---|
549 | |
---|
550 | // Local Variables: // |
---|
551 | // mode: c // |
---|
552 | // tab-width: 4 // |
---|
553 | // End: // |
---|