source: libcfa/src/stdlib.hfa@ db1ebed

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

change range of integral random( u, l ) to [l,u], first attempt at specialized PRNG

  • Property mode set to 100644
File size: 17.2 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 : Wed Dec 29 15:30:58 2021
13// Update Count : 591
14//
15
16#pragma once
17
18#include "bits/defs.hfa" // OPTIONAL_THREAD
19#include "bits/align.hfa" // libAlign
20
21#include <stdlib.h> // *alloc, strto*, ato*
22#include <heap.hfa>
23
24// Reduce includes by explicitly defining these routines.
25extern "C" {
26 void * memalign( size_t alignment, size_t size ); // malloc.h
27 void * pvalloc( size_t size ); // malloc.h
28 void * memset( void * dest, int fill, size_t size ); // string.h
29 void * memcpy( void * dest, const void * src, size_t size ); // string.h
30} // extern "C"
31
32//---------------------------------------
33
34#ifndef EXIT_FAILURE
35#define EXIT_FAILURE 1 // failing exit status
36#define EXIT_SUCCESS 0 // successful exit status
37#endif // ! EXIT_FAILURE
38
39//---------------------------------------
40
41#include "common.hfa"
42
43//---------------------------------------
44
45// Macro because of returns
46#define ARRAY_ALLOC$( allocation, alignment, dim ) \
47 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)allocation( dim, (size_t)sizeof(T) ); /* C allocation */ \
48 else return (T *)alignment( _Alignof(T), dim, sizeof(T) )
49
50static inline forall( T & | sized(T) ) {
51 // CFA safe equivalents, i.e., implicit size specification
52
53 T * malloc( void ) {
54 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C allocation
55 else return (T *)memalign( _Alignof(T), sizeof(T) );
56 } // malloc
57
58 T * aalloc( size_t dim ) {
59 ARRAY_ALLOC$( aalloc, amemalign, dim );
60 } // aalloc
61
62 T * calloc( size_t dim ) {
63 ARRAY_ALLOC$( calloc, cmemalign, dim );
64 } // calloc
65
66 T * resize( T * ptr, size_t size ) { // CFA resize, eliminate return-type cast
67 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)resize( (void *)ptr, size ); // CFA resize
68 else return (T *)(void *)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 *)(void *)realloc( (void *)ptr, size ); // C realloc
73 else return (T *)(void *)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
150typedef struct S_align { inline size_t; } T_align;
151typedef struct S_resize { inline void *; } T_resize;
152
153forall( 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
158static inline T_align ?`align ( size_t a ) { return (T_align){a}; }
159static inline T_resize ?`resize ( void * a ) { return (T_resize){a}; }
160
161static inline forall( T & | sized(T) ) {
162 S_fill(T) ?`fill ( T t ) {
163 S_fill(T) ret = { 't' };
164 size_t size = sizeof(T);
165 if ( size > sizeof(ret.t) ) {
166 abort( "ERROR: const object of size greater than 50 bytes given for dynamic memory fill\n" );
167 } // if
168 memcpy( &ret.t, &t, size );
169 return ret;
170 }
171 S_fill(T) ?`fill ( zero_t ) = void; // FIX ME: remove this once ticket 214 is resolved
172 S_fill(T) ?`fill ( T * a ) { return (S_fill(T)){ 'T', '0', 0, a }; } // FIX ME: remove this once ticket 214 is resolved
173 S_fill(T) ?`fill ( char c ) { return (S_fill(T)){ 'c', c }; }
174 S_fill(T) ?`fill ( T a[], size_t nmemb ) { return (S_fill(T)){ 'a', '0', nmemb * sizeof(T), a }; }
175
176 S_realloc(T) ?`realloc ( T * a ) { return (S_realloc(T)){a}; }
177
178 T * alloc_internal$( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill ) {
179 T * ptr = NULL;
180 size_t size = sizeof(T);
181 size_t copy_end = 0;
182
183 if ( Resize ) {
184 ptr = (T*) (void *) resize( (void *)Resize, Align, Dim * size );
185 } else if ( Realloc ) {
186 if ( Fill.tag != '0' ) copy_end = min(malloc_size( Realloc ), Dim * size );
187 ptr = (T *) (void *) realloc( (void *)Realloc, Align, Dim * size );
188 } else {
189 ptr = (T *) (void *) memalign( Align, Dim * size );
190 }
191
192 if ( Fill.tag == 'c' ) {
193 memset( (char *)ptr + copy_end, (int)Fill.c, Dim * size - copy_end );
194 } else if ( Fill.tag == 't' ) {
195 for ( int i = copy_end; i < Dim * size; i += size ) {
196 #pragma GCC diagnostic push
197 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
198 assert( size <= sizeof(Fill.t) );
199 memcpy( (char *)ptr + i, &Fill.t, size );
200 #pragma GCC diagnostic pop
201 }
202 } else if ( Fill.tag == 'a' ) {
203 memcpy( (char *)ptr + copy_end, Fill.at, min(Dim * size - copy_end, Fill.size) );
204 } else if ( Fill.tag == 'T' ) {
205 memcpy( (char *)ptr + copy_end, Fill.at, Dim * size );
206 }
207
208 return ptr;
209 } // alloc_internal$
210
211 forall( TT... | { T * alloc_internal$( void *, T *, size_t, size_t, S_fill(T), TT ); } ) {
212
213 T * alloc_internal$( void * , T * Realloc, 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 * Resize, 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
237 } // distribution TT
238} // distribution T
239
240static inline forall( T & | sized(T) ) {
241 // CFA safe initialization/copy, i.e., implicit size specification, non-array types
242 T * memset( T * dest, char fill ) {
243 return (T *)memset( dest, fill, sizeof(T) );
244 } // memset
245
246 T * memcpy( T * dest, const T * src ) {
247 return (T *)memcpy( dest, src, sizeof(T) );
248 } // memcpy
249
250 // CFA safe initialization/copy, i.e., implicit size specification, array types
251 T * amemset( T dest[], char fill, size_t dim ) {
252 return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
253 } // amemset
254
255 T * amemcpy( T dest[], const T src[], size_t dim ) {
256 return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
257 } // amemcpy
258} // distribution
259
260// CFA deallocation for multiple objects
261static inline forall( T & ) // FIX ME, problems with 0p in list
262void free( T * ptr ) {
263 free( (void *)ptr ); // C free
264} // free
265static inline forall( T &, TT... | { void free( TT ); } )
266void free( T * ptr, TT rest ) {
267 free( ptr );
268 free( rest );
269} // free
270
271// CFA allocation/deallocation and constructor/destructor, non-array types
272static inline forall( T & | sized(T), TT... | { void ?{}( T &, TT ); } )
273T * new( TT p ) {
274 return &(*(T *)malloc()){ p }; // run constructor
275} // new
276
277static inline forall( T & | { void ^?{}( T & ); } )
278void delete( T * ptr ) {
279 // special case for 0-sized object => always call destructor
280 if ( ptr || sizeof(ptr) == 0 ) { // ignore null but not 0-sized objects
281 ^(*ptr){}; // run destructor
282 } // if
283 free( ptr ); // always call free
284} // delete
285static inline forall( T &, TT... | { void ^?{}( T & ); void delete( TT ); } )
286void delete( T * ptr, TT rest ) {
287 delete( ptr );
288 delete( rest );
289} // delete
290
291// CFA allocation/deallocation and constructor/destructor, array types
292forall( T & | sized(T), TT... | { void ?{}( T &, TT ); } ) T * anew( size_t dim, TT p );
293forall( T & | sized(T) | { void ^?{}( T & ); } ) void adelete( T arr[] );
294forall( T & | sized(T) | { void ^?{}( T & ); }, TT... | { void adelete( TT ); } ) void adelete( T arr[], TT rest );
295
296//---------------------------------------
297
298static inline {
299 int strto( const char sptr[], char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
300 unsigned int strto( const char sptr[], char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
301 long int strto( const char sptr[], char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
302 unsigned long int strto( const char sptr[], char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
303 long long int strto( const char sptr[], char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
304 unsigned long long int strto( const char sptr[], char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
305
306 float strto( const char sptr[], char ** eptr ) { return strtof( sptr, eptr ); }
307 double strto( const char sptr[], char ** eptr ) { return strtod( sptr, eptr ); }
308 long double strto( const char sptr[], char ** eptr ) { return strtold( sptr, eptr ); }
309} // distribution
310
311float _Complex strto( const char sptr[], char ** eptr );
312double _Complex strto( const char sptr[], char ** eptr );
313long double _Complex strto( const char sptr[], char ** eptr );
314
315static inline {
316 int ato( const char sptr[] ) { return (int)strtol( sptr, 0p, 10 ); }
317 unsigned int ato( const char sptr[] ) { return (unsigned int)strtoul( sptr, 0p, 10 ); }
318 long int ato( const char sptr[] ) { return strtol( sptr, 0p, 10 ); }
319 unsigned long int ato( const char sptr[] ) { return strtoul( sptr, 0p, 10 ); }
320 long long int ato( const char sptr[] ) { return strtoll( sptr, 0p, 10 ); }
321 unsigned long long int ato( const char sptr[] ) { return strtoull( sptr, 0p, 10 ); }
322
323 float ato( const char sptr[] ) { return strtof( sptr, 0p ); }
324 double ato( const char sptr[] ) { return strtod( sptr, 0p ); }
325 long double ato( const char sptr[] ) { return strtold( sptr, 0p ); }
326
327 float _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
328 double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
329 long double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
330} // distribution
331
332//---------------------------------------
333
334forall( E | { int ?<?( E, E ); } ) {
335 E * bsearch( E key, const E * vals, size_t dim );
336 size_t bsearch( E key, const E * vals, size_t dim );
337 E * bsearchl( E key, const E * vals, size_t dim );
338 size_t bsearchl( E key, const E * vals, size_t dim );
339 E * bsearchu( E key, const E * vals, size_t dim );
340 size_t bsearchu( E key, const E * vals, size_t dim );
341} // distribution
342
343forall( K, E | { int ?<?( K, K ); K getKey( const E & ); } ) {
344 E * bsearch( K key, const E * vals, size_t dim );
345 size_t bsearch( K key, const E * vals, size_t dim );
346 E * bsearchl( K key, const E * vals, size_t dim );
347 size_t bsearchl( K key, const E * vals, size_t dim );
348 E * bsearchu( K key, const E * vals, size_t dim );
349 size_t bsearchu( K key, const E * vals, size_t dim );
350} // distribution
351
352forall( E | { int ?<?( E, E ); } ) {
353 void qsort( E * vals, size_t dim );
354} // distribution
355
356//---------------------------------------
357
358extern "C" { // override C version
359 void srandom( unsigned int seed );
360 long int random( void ); // GENERATES POSITIVE AND NEGATIVE VALUES
361 // For positive values, use unsigned int, e.g., unsigned int r = random() % 100U;
362} // extern "C"
363
364static inline {
365 long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l + 1) + l; } // [l,u]
366 long int random( long int u ) { return random( 0, u - 1 ); } // [0,u)
367 unsigned long int random( void ) { return lrand48(); }
368 unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u)
369 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]
370
371 char random( void ) { return (unsigned long int)random(); }
372 char random( char u ) { return random( (unsigned long int)u ); } // [0,u)
373 char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
374 int random( void ) { return (long int)random(); }
375 int random( int u ) { return random( (long int)u ); } // [0,u]
376 int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u)
377 unsigned int random( void ) { return (unsigned long int)random(); }
378 unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u]
379 unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
380} // distribution
381
382float random( void ); // [0.0, 1.0)
383double random( void ); // [0.0, 1.0)
384float _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
385double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
386long double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
387
388//---------------------------------------
389
390struct PRNG {
391 uint32_t callcnt; // call count
392 uint32_t seed; // current seed
393 uint32_t state; // random state
394}; // PRNG
395
396extern uint32_t prng( PRNG & prng ) __attribute__(( warn_unused_result )); // [0,UINT_MAX]
397static inline {
398 void set_seed( PRNG & prng, uint32_t seed_ ) with( prng ) { state = seed = seed_; } // set seed
399 void ?{}( PRNG & prng ) { set_seed( prng, rdtscl() ); } // random seed
400 void ?{}( PRNG & prng, uint32_t seed ) { set_seed( prng, seed ); } // fixed seed
401 uint32_t get_seed( PRNG & prng ) __attribute__(( warn_unused_result )) with( prng ) { return seed; } // get seed
402 uint32_t prng( PRNG & prng, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( prng ) % u; } // [0,u)
403 uint32_t prng( PRNG & prng, uint32_t l, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( prng, u - l + 1 ) + l; } // [l,u]
404 uint32_t calls( PRNG & prng ) __attribute__(( warn_unused_result )) with( prng ) { return callcnt; }
405} // distribution
406
407extern void set_seed( uint32_t seed ); // set per thread seed
408extern uint32_t get_seed(); // get seed
409extern uint32_t prng( void ) __attribute__(( warn_unused_result )); // [0,UINT_MAX]
410static inline {
411 uint32_t prng( uint32_t u ) __attribute__(( warn_unused_result ));
412 uint32_t prng( uint32_t u ) { return prng() % u; } // [0,u)
413 uint32_t prng( uint32_t l, uint32_t u ) __attribute__(( warn_unused_result ));
414 uint32_t prng( uint32_t l, uint32_t u ) { return prng( u - l + 1 ) + l; } // [l,u]
415} // distribution
416
417//---------------------------------------
418
419extern bool threading_enabled( void ) OPTIONAL_THREAD;
420
421// Local Variables: //
422// mode: c //
423// tab-width: 4 //
424// End: //
Note: See TracBrowser for help on using the repository browser.