Ignore:
Timestamp:
Jan 19, 2022, 2:36:56 PM (4 years ago)
Author:
Michael Brooks <mlbrooks@…>
Branches:
ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
Children:
97c215f
Parents:
5235d49 (diff), 6a33e40 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.hfa

    r5235d49 r12c1eef  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Dec 29 15:30:58 2021
    13 // Update Count     : 591
     12// Last Modified On : Thu Jan 13 21:34:46 2022
     13// Update Count     : 636
    1414//
    1515
     
    2121#include <stdlib.h>                                                                             // *alloc, strto*, ato*
    2222#include <heap.hfa>
     23
    2324
    2425// Reduce includes by explicitly defining these routines.
     
    4344//---------------------------------------
    4445
    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 
    5046static inline forall( T & | sized(T) ) {
    5147        // CFA safe equivalents, i.e., implicit size specification
    5248
    5349        T * malloc( void ) {
    54                 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C allocation
     50                if ( _Alignof(T) <= libAlign() ) return (T *)malloc( sizeof(T) ); // C allocation
    5551                else return (T *)memalign( _Alignof(T), sizeof(T) );
    5652        } // malloc
    5753
    5854        T * aalloc( size_t dim ) {
    59                 ARRAY_ALLOC$( aalloc, amemalign, dim );
     55                if ( _Alignof(T) <= libAlign() ) return (T *)aalloc( dim, sizeof(T) ); // C allocation
     56                else return (T *)amemalign( _Alignof(T), dim, sizeof(T) );
    6057        } // aalloc
    6158
    6259        T * calloc( size_t dim ) {
    63                 ARRAY_ALLOC$( calloc, cmemalign, dim );
     60                if ( _Alignof(T) <= libAlign() ) return (T *)calloc( dim, sizeof(T) ); // C allocation
     61                else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) );
    6462        } // calloc
    6563
    6664        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
     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
    6967        } // resize
    7068
    7169        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
     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
    7472        } // realloc
    7573
     
    210208
    211209        forall( TT... | { T * alloc_internal$( void *, T *, size_t, size_t, S_fill(T), TT ); } ) {
    212 
    213210                T * alloc_internal$( void *       , T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill, T_resize Resize, TT rest) {
    214211                return alloc_internal$( Resize, (T*)0p, Align, Dim, Fill, rest);
     
    234231                return alloc_internal$( (void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), dim, (S_fill(T)){'0'}, all);
    235232            }
    236 
    237233        } // distribution TT
    238234} // distribution T
     
    388384//---------------------------------------
    389385
     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
    390405struct PRNG {
    391406        uint32_t callcnt;                                                                       // call count
     
    394409}; // PRNG
    395410
    396 extern uint32_t prng( PRNG & prng ) __attribute__(( warn_unused_result )); // [0,UINT_MAX]
     411void set_seed( PRNG & prng, uint32_t seed_ );
     412uint32_t prng( PRNG & prng ) __attribute__(( warn_unused_result )); // [0,UINT_MAX]
    397413static inline {
    398         void set_seed( PRNG & prng, uint32_t seed_ ) with( prng ) { state = seed = seed_; } // set seed
    399414        void ?{}( PRNG & prng ) { set_seed( prng, rdtscl() ); } // random seed
    400415        void ?{}( PRNG & prng, uint32_t seed ) { set_seed( prng, seed ); } // fixed seed
     
    405420} // distribution
    406421
    407 extern void set_seed( uint32_t seed );                                  // set per thread seed
    408 extern uint32_t get_seed();                                                             // get seed
    409 extern uint32_t prng( void ) __attribute__(( warn_unused_result )); // [0,UINT_MAX]
     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
     436void set_seed( uint32_t seed_ ) OPTIONAL_THREAD;
     437uint32_t get_seed() __attribute__(( warn_unused_result ));
     438uint32_t prng( void ) __attribute__(( warn_unused_result )) OPTIONAL_THREAD; // [0,UINT_MAX]
    410439static 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]
     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]
    415442} // distribution
    416443
Note: See TracChangeset for help on using the changeset viewer.