source: libcfa/src/stdlib.hfa@ 8fd53b6e

Last change on this file since 8fd53b6e was 710d0c8c, checked in by Peter A. Buhr <pabuhr@…>, 19 months ago

formatting, add missing CFA reallocarray routine

  • Property mode set to 100644
File size: 21.8 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
[710d0c8c]12// Last Modified On : Mon Apr 15 10:51:38 2024
13// Update Count : 815
[bd85400]14//
15
[53a6c2a]16#pragma once
[17e5e2b]17
[94429f8]18#include "bits/defs.hfa" // OPTIONAL_THREAD
19#include "bits/align.hfa" // libAlign
[12b5e94a]20#include "bits/random.hfa" // prng
[b5e725a]21#include <Exception.hfa>
22#include <heap.hfa>
[2026bb6]23
[d46ed6e]24#include <stdlib.h> // *alloc, strto*, ato*
[76acb60]25#include <errno.h>
[2210cfc]26
[ca7949b]27// Reduce includes by explicitly defining these routines.
[3ce0d440]28extern "C" {
[4e7c0fc0]29 void * memalign( size_t alignment, size_t size ); // malloc.h
30 void * pvalloc( size_t size ); // malloc.h
[b9c04946]31 void * memset( void * dest, int fill, size_t size ); // string.h
[57fc7d8]32 void * memcpy( void * dest, const void * src, size_t size ); // string.h
[3ce0d440]33} // extern "C"
[e672372]34
[bd85400]35//---------------------------------------
36
[45161b4d]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
[c354108]44#include "common.hfa"
45
46//---------------------------------------
47
[fd54fef]48static inline forall( T & | sized(T) ) {
[4803a901]49 // CFA safe equivalents, i.e., implicit size specification
[3ce0d440]50
[74b19fb]51 T * malloc( void ) {
[aa0a1ad]52 if ( _Alignof(T) <= libAlign() ) return (T *)malloc( sizeof(T) ); // C allocation
[68f0c4e]53 else return (T *)memalign( _Alignof(T), sizeof(T) );
[74b19fb]54 } // malloc
55
[856fe3e]56 T * aalloc( size_t dim ) {
[aa0a1ad]57 if ( _Alignof(T) <= libAlign() ) return (T *)aalloc( dim, sizeof(T) ); // C allocation
58 else return (T *)amemalign( _Alignof(T), dim, sizeof(T) );
[856fe3e]59 } // aalloc
60
[74b19fb]61 T * calloc( size_t dim ) {
[aa0a1ad]62 if ( _Alignof(T) <= libAlign() ) return (T *)calloc( dim, sizeof(T) ); // C allocation
63 else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) );
[74b19fb]64 } // calloc
65
[710d0c8c]66 T * resize( T * ptr, size_t size ) { // CFA resize
[aa0a1ad]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
[856fe3e]69 } // resize
70
[710d0c8c]71 T * realloc( T * ptr, size_t size ) { // CFA realloc
[aa0a1ad]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
[74b19fb]74 } // realloc
75
[710d0c8c]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
[74b19fb]81 T * memalign( size_t align ) {
[cafb687]82 return (T *)memalign( align, sizeof(T) ); // C memalign
[74b19fb]83 } // memalign
84
[856fe3e]85 T * amemalign( size_t align, size_t dim ) {
86 return (T *)amemalign( align, dim, sizeof(T) ); // CFA amemalign
87 } // amemalign
88
[d74369b]89 T * cmemalign( size_t align, size_t dim ) {
90 return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign
91 } // cmemalign
92
[74b19fb]93 T * aligned_alloc( size_t align ) {
[cafb687]94 return (T *)aligned_alloc( align, sizeof(T) ); // C aligned_alloc
[74b19fb]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
[ada0246d]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
[55acc3a]108} // distribution
109
[ceb7db8]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:
[fd54fef]115 forall( T & | sized(T) ) {
[b6a71bc]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 *; };
[ceb7db8]119 }
120
121 2. Replace all current postfix-fill functions with following for updated S_fill:
[b6a71bc]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; }
[ceb7db8]125
[6c5d92f]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) {
[ceb7db8]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;
[6c5d92f]152 } // alloc_internal$
[ceb7db8]153*/
154
[b6a71bc]155typedef struct S_align { inline size_t; } T_align;
156typedef struct S_resize { inline void *; } T_resize;
[ceb7db8]157
[fd54fef]158forall( T & ) {
[b6a71bc]159 struct S_fill { char tag; char c; size_t size; T * at; char t[50]; };
160 struct S_realloc { inline T *; };
[ceb7db8]161}
162
[b6a71bc]163static inline T_align ?`align( size_t a ) { return (T_align){a}; }
164static inline T_resize ?`resize( void * a ) { return (T_resize){a}; }
[74b19fb]165
[b6a71bc]166extern "C" ssize_t write(int fd, const void *buf, size_t count);
[fd54fef]167static inline forall( T & | sized(T) ) {
[ceb7db8]168 S_fill(T) ?`fill ( T t ) {
169 S_fill(T) ret = { 't' };
170 size_t size = sizeof(T);
[3d3d75e]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
[ceb7db8]174 memcpy( &ret.t, &t, size );
175 return ret;
176 }
[b6a71bc]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 }; }
[ceb7db8]181
[b6a71bc]182 S_realloc(T) ?`realloc ( T * a ) { return (S_realloc(T)){a}; }
[ceb7db8]183
[6c5d92f]184 T * alloc_internal$( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill ) {
[ceb7db8]185 T * ptr = NULL;
186 size_t size = sizeof(T);
187 size_t copy_end = 0;
[f67b983]188
189 if ( Resize ) {
[b6a71bc]190 ptr = (T*)(void *)resize( (void *)Resize, Align, Dim * size );
[f67b983]191 } else if ( Realloc ) {
[3d3d75e]192 if ( Fill.tag != '0' ) copy_end = min(malloc_size( Realloc ), Dim * size );
[b6a71bc]193 ptr = (T *)(void *)realloc( (void *)Realloc, Align, Dim * size );
[cfbc703d]194 } else {
[b6a71bc]195 ptr = (T *)(void *) memalign( Align, Dim * size );
[ceb7db8]196 }
197
[3d3d75e]198 if ( Fill.tag == 'c' ) {
[ceb7db8]199 memset( (char *)ptr + copy_end, (int)Fill.c, Dim * size - copy_end );
[3d3d75e]200 } else if ( Fill.tag == 't' ) {
[f6a4917]201 for ( i; copy_end ~ Dim * size ~ size ) {
[3d3d75e]202 #pragma GCC diagnostic push
203 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
[d1b70d4]204 assert( size <= sizeof(Fill.t) );
205 memcpy( (char *)ptr + i, &Fill.t, size );
[3d3d75e]206 #pragma GCC diagnostic pop
[ceb7db8]207 }
[3d3d75e]208 } else if ( Fill.tag == 'a' ) {
[ceb7db8]209 memcpy( (char *)ptr + copy_end, Fill.at, min(Dim * size - copy_end, Fill.size) );
[3d3d75e]210 } else if ( Fill.tag == 'T' ) {
211 memcpy( (char *)ptr + copy_end, Fill.at, Dim * size );
[ceb7db8]212 }
213
214 return ptr;
[6c5d92f]215 } // alloc_internal$
[ceb7db8]216
[6c5d92f]217 forall( TT... | { T * alloc_internal$( void *, T *, size_t, size_t, S_fill(T), TT ); } ) {
[58e97d9]218 T * alloc_internal$( void *, T *, size_t Align, size_t Dim, S_fill(T) Fill, T_resize Resize, TT rest ) {
[6c5d92f]219 return alloc_internal$( Resize, (T*)0p, Align, Dim, Fill, rest);
[ceb7db8]220 }
221
[58e97d9]222 T * alloc_internal$( void *, T *, size_t Align, size_t Dim, S_fill(T) Fill, S_realloc(T) Realloc, TT rest ) {
[6c5d92f]223 return alloc_internal$( (void*)0p, Realloc, Align, Dim, Fill, rest);
[ceb7db8]224 }
225
[58e97d9]226 T * alloc_internal$( void * Resize, T * Realloc, size_t, size_t Dim, S_fill(T) Fill, T_align Align, TT rest ) {
[6c5d92f]227 return alloc_internal$( Resize, Realloc, Align, Dim, Fill, rest);
[ceb7db8]228 }
229
[58e97d9]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 );
[ceb7db8]232 }
233
234 T * alloc( TT all ) {
[58e97d9]235 return alloc_internal$( (void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), (size_t)1, (S_fill(T)){'0'}, all );
[ceb7db8]236 }
237
238 T * alloc( size_t dim, TT all ) {
[58e97d9]239 return alloc_internal$( (void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), dim, (S_fill(T)){'0'}, all );
[ceb7db8]240 }
241 } // distribution TT
242} // distribution T
[3ce0d440]243
[fd54fef]244static inline forall( T & | sized(T) ) {
[4803a901]245 // CFA safe initialization/copy, i.e., implicit size specification, non-array types
[b9c04946]246 T * memset( T * dest, char fill ) {
247 return (T *)memset( dest, fill, sizeof(T) );
[3ce0d440]248 } // memset
249
250 T * memcpy( T * dest, const T * src ) {
251 return (T *)memcpy( dest, src, sizeof(T) );
252 } // memcpy
253
[4803a901]254 // CFA safe initialization/copy, i.e., implicit size specification, array types
[b9c04946]255 T * amemset( T dest[], char fill, size_t dim ) {
256 return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
257 } // amemset
[3ce0d440]258
[b9c04946]259 T * amemcpy( T dest[], const T src[], size_t dim ) {
[3ce0d440]260 return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
[b9c04946]261 } // amemcpy
[3ce0d440]262} // distribution
[f3fc631f]263
[4803a901]264// CFA deallocation for multiple objects
[fd54fef]265static inline forall( T & ) // FIX ME, problems with 0p in list
[4803a901]266void free( T * ptr ) {
267 free( (void *)ptr ); // C free
268} // free
[fd54fef]269static inline forall( T &, TT... | { void free( TT ); } )
[4803a901]270void free( T * ptr, TT rest ) {
271 free( ptr );
[94429f8]272 free( rest );
273} // free
274
[4803a901]275// CFA allocation/deallocation and constructor/destructor, non-array types
[fd54fef]276static inline forall( T & | sized(T), TT... | { void ?{}( T &, TT ); } )
[94429f8]277T * new( TT p ) {
[09ee131]278 return &(*(T *)malloc()){ p }; // run constructor
[94429f8]279} // new
280
[fd54fef]281static inline forall( T & | { void ^?{}( T & ); } )
[94429f8]282void delete( T * ptr ) {
[0f7a0ea]283 // special case for 0-sized object => always call destructor
284 if ( ptr || sizeof(ptr) == 0 ) { // ignore null but not 0-sized objects
[94429f8]285 ^(*ptr){}; // run destructor
286 } // if
[4803a901]287 free( ptr ); // always call free
[94429f8]288} // delete
[fd54fef]289static inline forall( T &, TT... | { void ^?{}( T & ); void delete( TT ); } )
[94429f8]290void delete( T * ptr, TT rest ) {
291 delete( ptr );
292 delete( rest );
293} // delete
[627f585]294
[4803a901]295// CFA allocation/deallocation and constructor/destructor, array types
[fd54fef]296forall( T & | sized(T), TT... | { void ?{}( T &, TT ); } ) T * anew( size_t dim, TT p );
297forall( T & | sized(T) | { void ^?{}( T & ); } ) void adelete( T arr[] );
298forall( T & | sized(T) | { void ^?{}( T & ); }, TT... | { void adelete( TT ); } ) void adelete( T arr[], TT rest );
[54af365]299//---------------------------------------
300
[8f650f0]301// Check if all string characters are a specific kind, e.g., checkif( s, isblank )
302bool checkif( const char s[], int (* kind)( int ) );
303bool checkif( const char s[], int (* kind)( int, locale_t ), locale_t locale );
[6065b3aa]304
[bd85400]305//---------------------------------------
306
[57fc7d8]307static inline {
[76acb60]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 ); }
[57fc7d8]318} // distribution
[e672372]319
[76acb60]320float _Complex strto( const char sptr[], char * eptr[] );
321double _Complex strto( const char sptr[], char * eptr[] );
322long double _Complex strto( const char sptr[], char * eptr[] );
323
[b5e725a]324ExceptionDecl( out_of_range );
325ExceptionDecl( invalid_argument );
[76acb60]326
327forall( T | { T strto( const char sptr[], char * eptr[], int ); } )
[54af365]328T convert( const char sptr[] ); // integrals
329forall( T | { T strto( const char sptr[], char * eptr[] ); } )
330T convert( const char sptr[] ); // floating-point (no base)
[bd85400]331
[57fc7d8]332static inline {
[e3fea42]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 ); }
[57fc7d8]347} // distribution
[e672372]348
[bd85400]349//---------------------------------------
350
[fd54fef]351forall( E | { int ?<?( E, E ); } ) {
[3ce0d440]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
[9c47a47]359
[fd54fef]360forall( K, E | { int ?<?( K, K ); K getKey( const E & ); } ) {
[3ce0d440]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
[bd85400]368
[fd54fef]369forall( E | { int ?<?( E, E ); } ) {
[b9c04946]370 void qsort( E * vals, size_t dim );
371} // distribution
372
[bd85400]373//---------------------------------------
374
[bbe1a87]375extern "C" { // override C version
376 void srandom( unsigned int seed );
[4e7c0fc0]377 long int random( void ); // GENERATES POSITIVE AND NEGATIVE VALUES
378 // For positive values, use unsigned int, e.g., unsigned int r = random() % 100U;
[bbe1a87]379} // extern "C"
380
381static inline {
[aa8e24c3]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)
[bbe1a87]384 unsigned long int random( void ) { return lrand48(); }
385 unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u)
[aa8e24c3]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]
[bbe1a87]387
388 char random( void ) { return (unsigned long int)random(); }
[24d6572]389 char random( char u ) { return (unsigned long int)random( (unsigned long int)u ); } // [0,u)
[bbe1a87]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(); }
[24d6572]392 int random( int u ) { return (long int)random( (long int)u ); } // [0,u]
[bbe1a87]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(); }
[24d6572]395 unsigned int random( unsigned int u ) { return (unsigned long int)random( (unsigned long int)u ); } // [0,u]
[bbe1a87]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
399float random( void ); // [0.0, 1.0)
400double random( void ); // [0.0, 1.0)
401float _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
402double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
403long double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
[bd85400]404
405//---------------------------------------
406
[1959528]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
[a892e61]411//
[1959528]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
[8a97248]426forall( PRNG &, R )
427trait basic_prng {
[20cf96d]428 void set_seed( PRNG & prng, R seed ); // set seed
429 R get_seed( PRNG & prng ); // get seed
[d2ad151]430 R prng( PRNG & prng );
431 void ?{}( PRNG & prng ); // random seed
[20cf96d]432 void ?{}( PRNG & prng, R seed ); // fixed seed
[d2ad151]433}; // basic_prng
434
[20cf96d]435static inline forall( PRNG &, R | basic_prng( PRNG, R ) | { R ?%?( R, R ); } ) {
[d2ad151]436 R prng( PRNG & prng, R u ) { return prng( prng ) % u; } // [0,u)
437}
[20cf96d]438static inline forall( PRNG &, R | basic_prng( PRNG, R ) | { R ?+?( R, R ); R ?-?( R, R ); R ?%?( R, R ); void ?{}( R &, one_t ); } ) {
[d2ad151]439 R prng( PRNG & prng, R l, R u ) { return prng( prng, u - l + (R){1} ) + l; } // [l,u]
440}
441
442struct PRNG32 {
[aa8e24c3]443 uint32_t callcnt; // call count
444 uint32_t seed; // current seed
[dd46fd3]445 PRNG_STATE_32_T state; // random state
[3770b87]446}; // PRNG32
[d2ad151]447
448static inline {
[261e107]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; }
[5f31bf0]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
[dd46fd3]453 uint32_t prng( PRNG32 & prng ) __attribute__(( warn_unused_result )) with( prng ) { callcnt += 1; return PRNG_NAME_32( state ); } // [0,UINT_MAX]
[ac8b016]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]
[d2ad151]456 uint32_t calls( PRNG32 & prng ) __attribute__(( warn_unused_result )) with( prng ) { return callcnt; }
[3770b87]457 void copy( PRNG32 & dst, PRNG32 & src ) { dst = src; } // checkpoint PRNG state, use autogen assignment
[d2ad151]458} // distribution
[3770b87]459void ?{}( PRNG32 &, PRNG32 & ) = void; // no copy, remove autogen copy constructor
460PRNG32 & ?=?( PRNG32 &, const PRNG32 ) = void; // no assignment, remove autogen assignment
[d2ad151]461
462struct PRNG64 {
463 uint64_t callcnt; // call count
464 uint64_t seed; // current seed
[dd46fd3]465 PRNG_STATE_64_T state; // random state
[3770b87]466}; // PRNG64
[aa8e24c3]467
468static inline {
[261e107]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; }
[5f31bf0]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
[dd46fd3]473 uint64_t prng( PRNG64 & prng ) __attribute__(( warn_unused_result )) with( prng ) { callcnt += 1; return PRNG_NAME_64( state ); } // [0,UINT_MAX]
[ac8b016]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]
[d2ad151]476 uint64_t calls( PRNG64 & prng ) __attribute__(( warn_unused_result )) with( prng ) { return callcnt; }
[3770b87]477 void copy( PRNG64 & dst, PRNG64 & src ) { dst = src; } // checkpoint PRNG state, use autogen assignment
[aa8e24c3]478} // distribution
[3770b87]479void ?{}( PRNG64 &, PRNG64 & ) = void; // no copy, remove autogen copy constructor
480PRNG64 & ?=?( PRNG64 &, const PRNG64 ) = void; // no assignment, remove autogen assignment
[aa8e24c3]481
[b5e725a]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
[1959528]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
[d2ad151]503// Harmonize with concurrency/thread.hfa.
[d8bdf13]504void set_seed( size_t seed_ ) OPTIONAL_THREAD; // set global seed
505size_t get_seed() __attribute__(( warn_unused_result )); // get global seed
[20cf96d]506size_t prng( void ) __attribute__(( warn_unused_result )) OPTIONAL_THREAD; // [0,UINT_MAX]
[aa8e24c3]507static inline {
[20cf96d]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]
[aa8e24c3]510} // distribution
511
512//---------------------------------------
513
[94429f8]514extern bool threading_enabled( void ) OPTIONAL_THREAD;
[2026bb6]515
[bd85400]516// Local Variables: //
517// mode: c //
518// tab-width: 4 //
519// End: //
Note: See TracBrowser for help on using the repository browser.