Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.hfa

    raa0a1ad r6c5d92f  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Jan  2 22:53:57 2022
    13 // Update Count     : 594
     12// Last Modified On : Tue Apr 20 21:20:03 2021
     13// Update Count     : 575
    1414//
    1515
     
    4343//---------------------------------------
    4444
     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
    4550static inline forall( T & | sized(T) ) {
    4651        // CFA safe equivalents, i.e., implicit size specification
    4752
    4853        T * malloc( void ) {
    49                 if ( _Alignof(T) <= libAlign() ) return (T *)malloc( sizeof(T) ); // C allocation
     54                if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C allocation
    5055                else return (T *)memalign( _Alignof(T), sizeof(T) );
    5156        } // malloc
    5257
    5358        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 );
    5660        } // aalloc
    5761
    5862        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 );
    6164        } // calloc
    6265
    6366        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 resize
    65                 else return (T *)resize( (void *)ptr, _Alignof(T), size ); // CFA resize
     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
    6669        } // resize
    6770
    6871        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 realloc
    70                 else return (T *)realloc( (void *)ptr, _Alignof(T), size ); // CFA realloc
     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
    7174        } // realloc
    7275
     
    166169                return ret;
    167170        }
    168         S_fill(T)               ?`fill ( zero_t ) = void; // FIX ME: remove this once ticket 214 is resolved
    169         S_fill(T)               ?`fill ( T * a )                                { return (S_fill(T)){ 'T', '0', 0, a }; } // FIX ME: remove this once ticket 214 is resolved
    170171        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 }; }
    171173        S_fill(T)               ?`fill ( T a[], size_t nmemb )  { return (S_fill(T)){ 'a', '0', nmemb * sizeof(T), a }; }
    172174
     
    360362
    361363static 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)
    364366        unsigned long int random( void ) { return lrand48(); }
    365367        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)
    367369
    368370        char random( void ) { return (unsigned long int)random(); }
     
    385387//---------------------------------------
    386388
    387 struct PRNG {
    388         uint32_t callcnt;                                                                       // call count
    389         uint32_t seed;                                                                          // current seed
    390         uint32_t state;                                                                         // random state
    391 }; // PRNG
    392 
    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 seed
    396         void ?{}( PRNG & prng ) { set_seed( prng, rdtscl() ); } // random seed
    397         void ?{}( PRNG & prng, uint32_t seed ) { set_seed( prng, seed ); } // fixed seed
    398         uint32_t get_seed( PRNG & prng ) __attribute__(( warn_unused_result )) with( prng ) { return seed; } // get seed
    399         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 } // distribution
    403 
    404 extern void set_seed( uint32_t seed );                                  // set per thread seed
    405 extern uint32_t get_seed();                                                             // get seed
    406 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 } // distribution
    413 
    414 //---------------------------------------
    415 
    416389extern bool threading_enabled( void ) OPTIONAL_THREAD;
    417390
Note: See TracChangeset for help on using the changeset viewer.