source: libcfa/src/stdlib.hfa@ 3318dff

Last change on this file since 3318dff was 76acb60, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

remove static from Exception macro

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