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