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