source: libcfa/src/stdlib.hfa@ b200492

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since b200492 was 919a6b2, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

formatting

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