Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.hfa

    r919a6b2 raa8e24c3  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Jan 13 21:34:46 2022
    13 // Update Count     : 636
     12// Last Modified On : Wed Dec 29 15:30:58 2021
     13// Update Count     : 591
    1414//
    1515
     
    2121#include <stdlib.h>                                                                             // *alloc, strto*, ato*
    2222#include <heap.hfa>
    23 
    2423
    2524// Reduce includes by explicitly defining these routines.
     
    4443//---------------------------------------
    4544
     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
    4650static inline forall( T & | sized(T) ) {
    4751        // CFA safe equivalents, i.e., implicit size specification
    4852
    4953        T * malloc( void ) {
    50                 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
    5155                else return (T *)memalign( _Alignof(T), sizeof(T) );
    5256        } // malloc
    5357
    5458        T * aalloc( size_t dim ) {
    55                 if ( _Alignof(T) <= libAlign() ) return (T *)aalloc( dim, sizeof(T) ); // C allocation
    56                 else return (T *)amemalign( _Alignof(T), dim, sizeof(T) );
     59                ARRAY_ALLOC$( aalloc, amemalign, dim );
    5760        } // aalloc
    5861
    5962        T * calloc( size_t dim ) {
    60                 if ( _Alignof(T) <= libAlign() ) return (T *)calloc( dim, sizeof(T) ); // C allocation
    61                 else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) );
     63                ARRAY_ALLOC$( calloc, cmemalign, dim );
    6264        } // calloc
    6365
    6466        T * resize( T * ptr, size_t size ) {                            // CFA resize, eliminate return-type cast
    65                 if ( _Alignof(T) <= libAlign() ) return (T *)resize( (void *)ptr, size ); // CFA resize
    66                 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
    6769        } // resize
    6870
    6971        T * realloc( T * ptr, size_t size ) {                           // CFA realloc, eliminate return-type cast
    70                 if ( _Alignof(T) <= libAlign() ) return (T *)realloc( (void *)ptr, size ); // C realloc
    71                 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
    7274        } // realloc
    7375
     
    208210
    209211        forall( TT... | { T * alloc_internal$( void *, T *, size_t, size_t, S_fill(T), TT ); } ) {
     212
    210213                T * alloc_internal$( void *       , T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill, T_resize Resize, TT rest) {
    211214                return alloc_internal$( Resize, (T*)0p, Align, Dim, Fill, rest);
     
    231234                return alloc_internal$( (void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), dim, (S_fill(T)){'0'}, all);
    232235            }
     236
    233237        } // distribution TT
    234238} // distribution T
     
    384388//---------------------------------------
    385389
    386 // Sequential Pseudo Random-Number Generator : generate repeatable sequence of values that appear random.
    387 //
    388 // Declaration :
    389 //   PRNG sprng = { 1009 } - set starting seed versus random seed
    390 //   
    391 // Interface :
    392 //   set_seed( sprng, 1009 ) - set starting seed for ALL kernel threads versus random seed
    393 //   get_seed( sprng ) - read seed
    394 //   prng( sprng ) - generate random value in range [0,UINT_MAX]
    395 //   prng( sprng, u ) - generate random value in range [0,u)
    396 //   prng( sprng, l, u ) - generate random value in range [l,u]
    397 //   calls( sprng ) - number of generated random value so far
    398 //
    399 // Examples : generate random number between 5-21
    400 //   prng( sprng ) % 17 + 5;    values 0-16 + 5 = 5-21
    401 //   prng( sprng, 16 + 1 ) + 5;
    402 //   prng( sprng, 5, 21 );
    403 //   calls( sprng );
    404 
    405390struct PRNG {
    406391        uint32_t callcnt;                                                                       // call count
     
    409394}; // PRNG
    410395
    411 void set_seed( PRNG & prng, uint32_t seed_ );
    412 uint32_t prng( PRNG & prng ) __attribute__(( warn_unused_result )); // [0,UINT_MAX]
     396extern uint32_t prng( PRNG & prng ) __attribute__(( warn_unused_result )); // [0,UINT_MAX]
    413397static inline {
     398        void set_seed( PRNG & prng, uint32_t seed_ ) with( prng ) { state = seed = seed_; } // set seed
    414399        void ?{}( PRNG & prng ) { set_seed( prng, rdtscl() ); } // random seed
    415400        void ?{}( PRNG & prng, uint32_t seed ) { set_seed( prng, seed ); } // fixed seed
     
    420405} // distribution
    421406
    422 // Concurrent Pseudo Random-Number Generator : generate repeatable sequence of values that appear random.
    423 //
    424 // Interface :
    425 //   set_seed( 1009 ) - fixed seed for all kernel threads versus random seed
    426 //   get_seed() - read seed
    427 //   prng() - generate random value in range [0,UINT_MAX]
    428 //   prng( u ) - generate random value in range [0,u)
    429 //   prng( l, u ) - generate random value in range [l,u]
    430 //
    431 // Examples : generate random number between 5-21
    432 //   prng() % 17 + 5;   values 0-16 + 5 = 5-21
    433 //   prng( 16 + 1 ) + 5;
    434 //   prng( 5, 21 );
    435 
    436 void set_seed( uint32_t seed_ ) OPTIONAL_THREAD;
    437 uint32_t get_seed() __attribute__(( warn_unused_result ));
    438 uint32_t prng( void ) __attribute__(( warn_unused_result )) OPTIONAL_THREAD; // [0,UINT_MAX]
     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]
    439410static inline {
    440         uint32_t prng( uint32_t u ) __attribute__(( warn_unused_result )) { return prng() % u; } // [0,u)
    441         uint32_t prng( uint32_t l, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( u - l + 1 ) + l; } // [l,u]
     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]
    442415} // distribution
    443416
Note: See TracChangeset for help on using the changeset viewer.