Changeset c655650


Ignore:
Timestamp:
Feb 12, 2022, 1:53:14 PM (2 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
Children:
470618c0
Parents:
aac37fa
Message:

update fast PRNG code

Location:
libcfa/src/concurrency
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/kernel/startup.cfa

    raac37fa rc655650  
    1818
    1919// C Includes
    20 #include <errno.h>              // errno
     20#include <errno.h>                                                                              // errno
    2121#include <signal.h>
    22 #include <string.h>             // strerror
    23 #include <unistd.h>             // sysconf
     22#include <string.h>                                                                             // strerror
     23#include <unistd.h>                                                                             // sysconf
    2424
    2525extern "C" {
    26       #include <limits.h>       // PTHREAD_STACK_MIN
    27         #include <unistd.h>       // syscall
    28         #include <sys/eventfd.h>  // eventfd
    29       #include <sys/mman.h>     // mprotect
    30       #include <sys/resource.h> // getrlimit
     26        #include <limits.h>                                                                     // PTHREAD_STACK_MIN
     27        #include <unistd.h>                                                                     // syscall
     28        #include <sys/eventfd.h>                                                        // eventfd
     29        #include <sys/mman.h>                                                           // mprotect
     30        #include <sys/resource.h>                                                       // getrlimit
    3131}
    3232
    3333// CFA Includes
    3434#include "kernel_private.hfa"
    35 #include "startup.hfa"          // STARTUP_PRIORITY_XXX
     35#include "startup.hfa"                                                                  // STARTUP_PRIORITY_XXX
    3636#include "limits.hfa"
    3737#include "math.hfa"
     
    102102extern void __wake_proc(processor *);
    103103extern int cfa_main_returned;                                                   // from interpose.cfa
    104 extern uint32_t __global_random_seed;
     104uint32_t __global_random_prime = 4_294_967_291u, __global_random_mask = false;
    105105
    106106//-----------------------------------------------------------------------------
     
    490490        preferred = ready_queue_new_preferred();
    491491        last_proc = 0p;
    492         random_state = __global_random_seed;
     492        random_state = __global_random_mask ? __global_random_prime : __global_random_prime ^ rdtscl();
    493493        #if defined( __CFA_WITH_VERIFY__ )
    494494                canary = 0x0D15EA5E0D15EA5Ep;
  • libcfa/src/concurrency/thread.cfa

    raac37fa rc655650  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jan 15 14:34:58 2022
    13 // Update Count     : 45
     12// Last Modified On : Sat Feb 12 08:45:33 2022
     13// Update Count     : 65
    1414//
    1515
     
    2525#include "invoke.h"
    2626
    27 extern uint32_t __global_random_seed;
     27extern uint32_t __global_random_seed, __global_random_prime, __global_random_mask;
    2828
    2929//-----------------------------------------------------------------------------
     
    4545        preferred = ready_queue_new_preferred();
    4646        last_proc = 0p;
    47         random_state = __global_random_seed;
     47        random_state = __global_random_mask ? __global_random_prime : __global_random_prime ^ rdtscl();
    4848        #if defined( __CFA_WITH_VERIFY__ )
    4949                canary = 0x0D15EA5E0D15EA5Ep;
     
    178178        active_thread()->random_state = __global_random_seed = seed;
    179179        GENERATOR( active_thread()->random_state );
     180        __global_random_prime = active_thread()->random_state;
     181        __global_random_mask = true;
    180182} // set_seed
    181183
  • libcfa/src/concurrency/thread.hfa

    raac37fa rc655650  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Feb  9 22:10:14 2022
    13 // Update Count     : 14
     12// Last Modified On : Fri Feb 11 16:34:07 2022
     13// Update Count     : 20
    1414//
    1515
     
    131131
    132132//----------
     133// prng
    133134static inline {
    134135        uint32_t prng( thread$ & th ) __attribute__(( warn_unused_result )) { return LCG( th.random_state ); } // [0,UINT_MAX]
    135136        uint32_t prng( thread$ & th, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( th ) % u; } // [0,u)
    136137        uint32_t prng( thread$ & th, uint32_t l, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( th, u - l + 1 ) + l; } // [l,u]
    137 } // prng
     138        forall( T & | is_thread(T) ) {
     139                uint32_t prng( T & th ) __attribute__(( warn_unused_result )) { return prng( (thread &)th ); } // [0,UINT_MAX]
     140                uint32_t prng( T & th, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( th ) % u; } // [0,u)
     141                uint32_t prng( T & th, uint32_t l, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( th, u - l + 1 ) + l; } // [l,u]
     142        } // distribution
     143} // distribution
    138144
    139145// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.