Changes in libcfa/src/stdlib.hfa [aa0a1ad:6c5d92f]
- File:
-
- 1 edited
-
libcfa/src/stdlib.hfa (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/stdlib.hfa
raa0a1ad r6c5d92f 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Jan 2 22:53:57 202213 // Update Count : 5 9412 // Last Modified On : Tue Apr 20 21:20:03 2021 13 // Update Count : 575 14 14 // 15 15 … … 43 43 //--------------------------------------- 44 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 45 50 static inline forall( T & | sized(T) ) { 46 51 // CFA safe equivalents, i.e., implicit size specification 47 52 48 53 T * malloc( void ) { 49 if ( _Alignof(T) <= libAlign() ) return (T *) malloc(sizeof(T) ); // C allocation54 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C allocation 50 55 else return (T *)memalign( _Alignof(T), sizeof(T) ); 51 56 } // malloc 52 57 53 58 T * aalloc( size_t dim ) { 54 if ( _Alignof(T) <= libAlign() ) return (T *)aalloc( dim, sizeof(T) ); // C allocation 55 else return (T *)amemalign( _Alignof(T), dim, sizeof(T) ); 59 ARRAY_ALLOC$( aalloc, amemalign, dim ); 56 60 } // aalloc 57 61 58 62 T * calloc( size_t dim ) { 59 if ( _Alignof(T) <= libAlign() ) return (T *)calloc( dim, sizeof(T) ); // C allocation 60 else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) ); 63 ARRAY_ALLOC$( calloc, cmemalign, dim ); 61 64 } // calloc 62 65 63 66 T * resize( T * ptr, size_t size ) { // CFA resize, eliminate return-type cast 64 if ( _Alignof(T) <= libAlign() ) return (T *) resize( (void *)ptr, size ); // CFA resize65 else return (T *) resize( (void *)ptr, _Alignof(T), size ); // CFA resize67 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 66 69 } // resize 67 70 68 71 T * realloc( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast 69 if ( _Alignof(T) <= libAlign() ) return (T *) realloc( (void *)ptr, size ); // C realloc70 else return (T *) realloc( (void *)ptr, _Alignof(T), size ); // CFA realloc72 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 71 74 } // realloc 72 75 … … 166 169 return ret; 167 170 } 168 S_fill(T) ?`fill ( zero_t ) = void; // FIX ME: remove this once ticket 214 is resolved169 S_fill(T) ?`fill ( T * a ) { return (S_fill(T)){ 'T', '0', 0, a }; } // FIX ME: remove this once ticket 214 is resolved170 171 S_fill(T) ?`fill ( char c ) { return (S_fill(T)){ 'c', c }; } 172 S_fill(T) ?`fill ( T * a ) { return (S_fill(T)){ 'T', '0', 0, a }; } 171 173 S_fill(T) ?`fill ( T a[], size_t nmemb ) { return (S_fill(T)){ 'a', '0', nmemb * sizeof(T), a }; } 172 174 … … 360 362 361 363 static inline { 362 long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l + 1) + l; } // [l,u]363 long int random( long int u ) { return random( 0, u - 1); } // [0,u)364 long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u) 365 long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u) 364 366 unsigned long int random( void ) { return lrand48(); } 365 367 unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u) 366 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]368 unsigned long int random( unsigned long int l, unsigned long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u) 367 369 368 370 char random( void ) { return (unsigned long int)random(); } … … 385 387 //--------------------------------------- 386 388 387 struct PRNG {388 uint32_t callcnt; // call count389 uint32_t seed; // current seed390 uint32_t state; // random state391 }; // PRNG392 393 extern uint32_t prng( PRNG & prng ) __attribute__(( warn_unused_result )); // [0,UINT_MAX]394 static inline {395 void set_seed( PRNG & prng, uint32_t seed_ ) with( prng ) { state = seed = seed_; } // set seed396 void ?{}( PRNG & prng ) { set_seed( prng, rdtscl() ); } // random seed397 void ?{}( PRNG & prng, uint32_t seed ) { set_seed( prng, seed ); } // fixed seed398 uint32_t get_seed( PRNG & prng ) __attribute__(( warn_unused_result )) with( prng ) { return seed; } // get seed399 uint32_t prng( PRNG & prng, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( prng ) % u; } // [0,u)400 uint32_t prng( PRNG & prng, uint32_t l, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( prng, u - l + 1 ) + l; } // [l,u]401 uint32_t calls( PRNG & prng ) __attribute__(( warn_unused_result )) with( prng ) { return callcnt; }402 } // distribution403 404 extern void set_seed( uint32_t seed ); // set per thread seed405 extern uint32_t get_seed(); // get seed406 extern uint32_t prng( void ) __attribute__(( warn_unused_result )); // [0,UINT_MAX]407 static inline {408 uint32_t prng( uint32_t u ) __attribute__(( warn_unused_result ));409 uint32_t prng( uint32_t u ) { return prng() % u; } // [0,u)410 uint32_t prng( uint32_t l, uint32_t u ) __attribute__(( warn_unused_result ));411 uint32_t prng( uint32_t l, uint32_t u ) { return prng( u - l + 1 ) + l; } // [l,u]412 } // distribution413 414 //---------------------------------------415 416 389 extern bool threading_enabled( void ) OPTIONAL_THREAD; 417 390
Note:
See TracChangeset
for help on using the changeset viewer.