Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.hfa

    r6c5d92f raa0a1ad  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Apr 20 21:20:03 2021
    13 // Update Count     : 575
     12// Last Modified On : Sun Jan  2 22:53:57 2022
     13// Update Count     : 594
    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 
    5045static inline forall( T & | sized(T) ) {
    5146        // CFA safe equivalents, i.e., implicit size specification
    5247
    5348        T * malloc( void ) {
    54                 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C allocation
     49                if ( _Alignof(T) <= libAlign() ) return (T *)malloc( sizeof(T) ); // C allocation
    5550                else return (T *)memalign( _Alignof(T), sizeof(T) );
    5651        } // malloc
    5752
    5853        T * aalloc( size_t dim ) {
    59                 ARRAY_ALLOC$( aalloc, amemalign, dim );
     54                if ( _Alignof(T) <= libAlign() ) return (T *)aalloc( dim, sizeof(T) ); // C allocation
     55                else return (T *)amemalign( _Alignof(T), dim, sizeof(T) );
    6056        } // aalloc
    6157
    6258        T * calloc( size_t dim ) {
    63                 ARRAY_ALLOC$( calloc, cmemalign, dim );
     59                if ( _Alignof(T) <= libAlign() ) return (T *)calloc( dim, sizeof(T) ); // C allocation
     60                else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) );
    6461        } // calloc
    6562
    6663        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
     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
    6966        } // resize
    7067
    7168        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
     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
    7471        } // realloc
    7572
     
    169166                return ret;
    170167        }
     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
    171170        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 }; }
    173171        S_fill(T)               ?`fill ( T a[], size_t nmemb )  { return (S_fill(T)){ 'a', '0', nmemb * sizeof(T), a }; }
    174172
     
    362360
    363361static inline {
    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)
     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)
    366364        unsigned long int random( void ) { return lrand48(); }
    367365        unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,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)
     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]
    369367
    370368        char random( void ) { return (unsigned long int)random(); }
     
    387385//---------------------------------------
    388386
     387struct PRNG {
     388        uint32_t callcnt;                                                                       // call count
     389        uint32_t seed;                                                                          // current seed
     390        uint32_t state;                                                                         // random state
     391}; // PRNG
     392
     393extern uint32_t prng( PRNG & prng ) __attribute__(( warn_unused_result )); // [0,UINT_MAX]
     394static 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
     404extern void set_seed( uint32_t seed );                                  // set per thread seed
     405extern uint32_t get_seed();                                                             // get seed
     406extern uint32_t prng( void ) __attribute__(( warn_unused_result )); // [0,UINT_MAX]
     407static 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
    389416extern bool threading_enabled( void ) OPTIONAL_THREAD;
    390417
Note: See TracChangeset for help on using the changeset viewer.