mmc_rand_common.h
Go to the documentation of this file.
1 /***************************************************************************/
32 /***************************************************************************/
38 #ifndef _MMC_RNG_COMMON_H
39 #define _MMC_RNG_COMMON_H
40 
41 #define TWO_PI (M_PI*2.0)
42 #define EPS 1e-6f
43 #define EPS2 0.1*EPS
44 
46 inlinefun float rand_next_scatlen(RandType t[RAND_BUF_LEN]) {
47  return -logf(rand_uniform01(t) + EPS);
48 }
50 inlinefun float rand_next_aangle(RandType t[RAND_BUF_LEN]) {
51  return rand_uniform01(t);
52 }
54 inlinefun float rand_next_zangle(RandType t[RAND_BUF_LEN]) {
55  return rand_uniform01(t);
56 }
58 inlinefun float rand_next_reflect(RandType t[RAND_BUF_LEN]) {
59  return rand_uniform01(t);
60 }
62 inlinefun float rand_do_roulette(RandType t[RAND_BUF_LEN]) {
63  return rand_uniform01(t);
64 }
65 #ifdef MMC_USE_SSE_MATH
66 
67 #define MATH_BLOCK 8
68 
70 inlinefun void rand_next_aangle_sincos(RandType t[RAND_BUF_LEN], float* si, float* co) {
71  static __thread V4SF sine[MATH_BLOCK], cosine[MATH_BLOCK];
72  static __thread int pos = (MATH_BLOCK << 2);
73 
74  if (pos >= (MATH_BLOCK << 2)) {
75  V4SF ran[MATH_BLOCK];
76  int i, j;
77 
78  for (i = 0; i < MATH_BLOCK; i++)
79  for (j = 0; j < 4; j++) {
80  ran[i].f[j] = TWO_PI * rand_uniform01(t);
81  }
82 
83  for (i = 0; i < MATH_BLOCK; i++) {
84  sincos_ps(ran[i].v, &(sine[i].v), &(cosine[i].v));
85  }
86 
87  pos = 0;
88  }
89 
90  *si = sine[0].f[pos];
91  *co = cosine[0].f[pos++];
92 }
93 
95 inlinefun float rand_next_scatlen_ps(RandType t[RAND_BUF_LEN]) {
96  static __thread V4SF logval[MATH_BLOCK];
97  static __thread int pos = (MATH_BLOCK << 2);
98  float res;
99 
100  if (pos >= (MATH_BLOCK << 2)) {
101  V4SF ran[MATH_BLOCK];
102  int i, j;
103 
104  for (i = 0; i < MATH_BLOCK; i++)
105  for (j = 0; j < 4; j++) {
106  ran[i].f[j] = rand_uniform01(t);
107  }
108 
109  for (i = 0; i < MATH_BLOCK; i++) {
110  logval[i].v = log_ps(ran[i].v);
111  }
112 
113  pos = 0;
114  }
115 
116  res = ((logval[0].f[pos] != logval[0].f[pos]) ? LOG_RNG_MAX : (-logval[0].f[pos]));
117  pos++;
118  return res;
119 }
120 #endif
121 
122 #endif
inlinefun float rand_next_scatlen(RandType t[RAND_BUF_LEN])
generate [0,1] random number for the next scattering length
Definition: mmc_rand_common.h:46
inlinefun float rand_do_roulette(RandType t[RAND_BUF_LEN])
generate random number for the next zenith angle
Definition: mmc_rand_common.h:62
inlinefun float rand_next_reflect(RandType t[RAND_BUF_LEN])
generate random number for reflection test
Definition: mmc_rand_common.h:58
inlinefun float rand_next_zangle(RandType t[RAND_BUF_LEN])
generate random number for the next zenith angle
Definition: mmc_rand_common.h:54
inlinefun float rand_next_aangle(RandType t[RAND_BUF_LEN])
generate [0,1] random number for the next arimuthal angle
Definition: mmc_rand_common.h:50