Changeset 20cf96d for libcfa/src


Ignore:
Timestamp:
Nov 22, 2022, 11:03:19 PM (2 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, master
Children:
78de1e5
Parents:
29702ad
Message:

changes to PRNG types to eliminate casts

Location:
libcfa/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/thread.cfa

    r29702ad r20cf96d  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Nov 20 17:17:50 2022
    13 // Update Count     : 80
     12// Last Modified On : Tue Nov 22 22:18:37 2022
     13// Update Count     : 81
    1414//
    1515
     
    227227//-----------------------------------------------------------------------------
    228228
    229 void set_seed( uint64_t seed ) {
     229void set_seed( size_t seed ) {
    230230        PRNG_ARG_T & state = active_thread()->random_state;
    231231        state = __global_random_seed = seed;
     
    235235} // set_seed
    236236
    237 uint64_t prng( void ) {                                                                 // [0,UINT_MAX]
     237size_t prng( void ) {                                                                   // [0,UINT_MAX]
    238238        PRNG_ARG_T & state = active_thread()->random_state;
    239239        return PRNG_NAME( state );
  • libcfa/src/concurrency/thread.hfa

    r29702ad r20cf96d  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Nov 19 16:41:27 2022
    13 // Update Count     : 30
     12// Last Modified On : Tue Nov 22 22:18:34 2022
     13// Update Count     : 35
    1414//
    1515
     
    142142//----------
    143143// prng
     144void set_seed( size_t seed );
    144145static inline {
    145         uint64_t prng( thread$ & th ) __attribute__(( warn_unused_result )) { return PRNG_NAME( th.random_state ); } // [0,UINT_MAX]
    146         uint64_t prng( thread$ & th, uint64_t u ) __attribute__(( warn_unused_result )) { return prng( th ) % u; } // [0,u)
    147         uint64_t prng( thread$ & th, uint64_t l, uint64_t u ) __attribute__(( warn_unused_result )) { return prng( th, u - l + 1 ) + l; } // [l,u]
     146        size_t prng( thread$ & th ) __attribute__(( warn_unused_result )) { return PRNG_NAME( th.random_state ); } // [0,UINT_MAX]
     147        size_t prng( thread$ & th, size_t u ) __attribute__(( warn_unused_result )) { return prng( th ) % u; } // [0,u)
     148        size_t prng( thread$ & th, size_t l, size_t u ) __attribute__(( warn_unused_result )) { return prng( th, u - l + 1 ) + l; } // [l,u]
    148149        forall( T & | is_thread(T) ) {
    149                 uint64_t prng( T & th ) __attribute__(( warn_unused_result )) { return prng( (thread &)th ); } // [0,UINT_MAX]
    150                 uint64_t prng( T & th, uint64_t u ) __attribute__(( warn_unused_result )) { return prng( th ) % u; } // [0,u)
    151                 uint64_t prng( T & th, uint64_t l, uint64_t u ) __attribute__(( warn_unused_result )) { return prng( th, u - l + 1 ) + l; } // [l,u]
     150                size_t prng( T & th ) __attribute__(( warn_unused_result )) { return prng( (thread &)th ); } // [0,UINT_MAX]
     151                size_t prng( T & th, size_t u ) __attribute__(( warn_unused_result )) { return prng( th ) % u; } // [0,u)
     152                size_t prng( T & th, size_t l, size_t u ) __attribute__(( warn_unused_result )) { return prng( th, u - l + 1 ) + l; } // [l,u]
    152153        } // distribution
    153154} // distribution
  • libcfa/src/stdlib.cfa

    r29702ad r20cf96d  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Nov 19 16:42:26 2022
    13 // Update Count     : 612
     12// Last Modified On : Tue Nov 22 22:18:36 2022
     13// Update Count     : 613
    1414//
    1515
     
    229229__attribute__((visibility("hidden"))) PRNG_ARG_T __global_random_state; // sequential only
    230230
    231 void set_seed( uint64_t seed ) { __global_random_state = __global_random_seed = seed; PRNG_NAME( __global_random_state ); }
    232 uint64_t get_seed() { return __global_random_seed; }
    233 uint64_t prng( void ) { return PRNG_NAME( __global_random_state ); } // [0,UINT_MAX]
     231void set_seed( size_t seed ) { __global_random_state = __global_random_seed = seed; PRNG_NAME( __global_random_state ); }
     232size_t get_seed() { return __global_random_seed; }
     233size_t prng( void ) { return PRNG_NAME( __global_random_state ); } // [0,UINT_MAX]
    234234
    235235//---------------------------------------
  • libcfa/src/stdlib.hfa

    r29702ad r20cf96d  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Nov 20 17:12:37 2022
    13 // Update Count     : 730
     12// Last Modified On : Tue Nov 22 22:48:59 2022
     13// Update Count     : 736
    1414//
    1515
     
    404404//   calls( sprng );
    405405
    406 trait basic_prng( PRNG &, S, R ) {
    407         void set_seed( PRNG & prng, S seed );                           // set seed
    408         S get_seed( PRNG & prng );                                                      // get seed
     406trait basic_prng( PRNG &, R ) {
     407        void set_seed( PRNG & prng, R seed );                           // set seed
     408        R get_seed( PRNG & prng );                                                      // get seed
    409409        R prng( PRNG & prng );
    410410        void ?{}( PRNG & prng );                                                        // random seed
    411         void ?{}( PRNG & prng, S seed );                                        // fixed seed
     411        void ?{}( PRNG & prng, R seed );                                        // fixed seed
    412412}; // basic_prng
    413413
    414 static inline forall( PRNG &, S, R | basic_prng( PRNG, S, R ) | { R ?%?( R, R ); } ) {
     414static inline forall( PRNG &, R | basic_prng( PRNG, R ) | { R ?%?( R, R ); } ) {
    415415        R prng( PRNG & prng, R u ) { return prng( prng ) % u; } // [0,u)
    416416}
    417 static inline forall( PRNG &, S, R | basic_prng( PRNG, S, R ) | { R ?+?( R, R ); R ?-?( R, R ); R ?%?( R, R ); void ?{}( R &, one_t ); } ) {
     417static inline forall( PRNG &, R | basic_prng( PRNG, R ) | { R ?+?( R, R ); R ?-?( R, R ); R ?%?( R, R ); void ?{}( R &, one_t ); } ) {
    418418        R prng( PRNG & prng, R l, R u ) { return prng( prng, u - l + (R){1} ) + l; } // [l,u]
    419419}
     
    429429        uint32_t get_seed( PRNG32 & prng ) __attribute__(( warn_unused_result )) with( prng ) { return seed; } // get seed
    430430        uint32_t prng( PRNG32 & prng ) __attribute__(( warn_unused_result )) with( prng ) { callcnt += 1; return PRNG_NAME( state ); } // [0,UINT_MAX]
     431        uint32_t prng( PRNG32 & prng, size_t u ) __attribute__(( warn_unused_result )) { return prng( prng ) % u; } // [0,u)
     432        uint32_t prng( PRNG32 & prng, size_t l, size_t u ) __attribute__(( warn_unused_result )) { return prng( prng, u - l + 1 ) + l; } // [l,u]
    431433        uint32_t calls( PRNG32 & prng ) __attribute__(( warn_unused_result )) with( prng ) { return callcnt; }
    432434        void ?{}( PRNG32 & prng ) with( prng ) { callcnt = 0; set_seed( prng, rdtscl() ); } // random seed
     
    444446        uint64_t get_seed( PRNG64 & prng ) __attribute__(( warn_unused_result )) with( prng ) { return seed; } // get seed
    445447        uint64_t prng( PRNG64 & prng ) __attribute__(( warn_unused_result )) with( prng ) { callcnt += 1; return PRNG_NAME( state ); } // [0,UINT_MAX]
     448        uint64_t prng( PRNG64 & prng, size_t u ) __attribute__(( warn_unused_result )) { return prng( prng ) % u; } // [0,u)
     449        uint64_t prng( PRNG64 & prng, size_t l, size_t u ) __attribute__(( warn_unused_result )) { return prng( prng, u - l + 1 ) + l; } // [l,u]
    446450        uint64_t calls( PRNG64 & prng ) __attribute__(( warn_unused_result )) with( prng ) { return callcnt; }
    447451        void ?{}( PRNG64 & prng ) with( prng ) { callcnt = 0; set_seed( prng, rdtscl() ); } // random seed
     
    464468
    465469// Harmonize with concurrency/thread.hfa.
    466 void set_seed( uint64_t seed_ ) OPTIONAL_THREAD;
    467 uint64_t get_seed() __attribute__(( warn_unused_result ));
    468 uint64_t prng( void ) __attribute__(( warn_unused_result )) OPTIONAL_THREAD; // [0,UINT_MAX]
    469 static inline {
    470         uint64_t prng( uint64_t u ) __attribute__(( warn_unused_result )) { return prng() % u; } // [0,u)
    471         uint64_t prng( uint64_t l, uint64_t u ) __attribute__(( warn_unused_result )) { return prng( u - l + 1 ) + l; } // [l,u]
     470void set_seed( size_t seed_ ) OPTIONAL_THREAD;
     471size_t get_seed() __attribute__(( warn_unused_result ));
     472size_t prng( void ) __attribute__(( warn_unused_result )) OPTIONAL_THREAD; // [0,UINT_MAX]
     473static inline {
     474        size_t prng( size_t u ) __attribute__(( warn_unused_result )) { return prng() % u; } // [0,u)
     475        size_t prng( size_t l, size_t u ) __attribute__(( warn_unused_result )) { return prng( u - l + 1 ) + l; } // [l,u]
    472476} // distribution
    473477
Note: See TracChangeset for help on using the changeset viewer.