pike.git / src / stuff.c

version» Context lines:

pike.git/src/stuff.c:95:      /*    * This rounds an integer up to the next power of two. For x a power    * of two, this will just return the same again.    */   unsigned INT32 find_next_power(unsigned INT32 x)   {    if( x == 0 ) return 1;    return 1<<(my_log2(x-1)+1);   } -  - #if HAS___BUILTIN_IA32_RDRAND32_STEP - static int use_rdrnd; - #endif -  - /* Bob Jenkins small noncryptographic PRNG */ -  - typedef unsigned long int u4; - static u4 rnd_a; - static u4 rnd_b; - static u4 rnd_c; - static u4 rnd_d; -  - #define rot(x,k) (((x)<<(k))|((x)>>(32-(k)))) - static u4 ranval(void) { -  u4 e = rnd_a - rot(rnd_b, 27); -  rnd_a = rnd_b ^ rot(rnd_c, 17); -  rnd_b = rnd_c + rnd_d; -  rnd_c = rnd_d + e; -  rnd_d = e + rnd_a; -  return rnd_d; - } -  - static void raninit( u4 seed ) { -  u4 i; -  rnd_a = 0xf1ea5eed, rnd_b = rnd_c = rnd_d = seed; -  for (i=0; i<20; ++i) { -  (void)ranval(); -  } - } -  - PMOD_EXPORT void my_srand(INT32 seed) - { - #if HAS___BUILTIN_IA32_RDRAND32_STEP -  unsigned int ignore, cpuid_ecx; -  -  /* NOTE: some versions of valgrind falsely advertise -  * RDRAND support in cpuid. -  */ - # if defined(USE_VALGRIND) -  if (!PIKE_MEM_CHECKER()) - # endif -  if( !use_rdrnd ) -  { -  INT32 cpuid[4]; -  x86_get_cpuid (1, cpuid); -  if( cpuid[3] & bit_RDRND_2 ) -  use_rdrnd = 1; -  } -  /* We still do the initialization here, since rdrnd might stop -  working if the hardware random unit in the CPU fails (according -  to intel documentation). -  -  -  This is likely to be rather rare. But the cost is not exactly -  high. -  -  Source: -  -  http://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide -  */ - #endif -  -  raninit(seed); - } -  - static unsigned INT32 low_rand(void) - { - #if HAS___BUILTIN_IA32_RDRAND32_STEP -  if( use_rdrnd ) -  { -  unsigned int rnd; -  unsigned int cnt = 0; -  do{ -  if( __builtin_ia32_rdrand32_step( &rnd ) ) -  return rnd; -  } while(cnt++ < 100); -  -  /* hardware random unit most likely not healthy. -  Switch to software random. */ -  use_rdrnd = 0; -  } - #endif -  return ranval(); - } -  - PMOD_EXPORT unsigned INT32 my_rand(unsigned INT32 limit) - { -  if(limit==0xffffffff) -  return low_rand(); -  return low_rand()%limit; - } +