Tachyon (current)  Current Main Branch
util.h
Go to the documentation of this file.
1 /*
2  * util.h - This file contains prototypes and definitions for timers and RNGs
3  *
4  * (C) Copyright 1994-2022 John E. Stone
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * $Id: util.h,v 1.32 2022/03/07 06:31:06 johns Exp $
8  *
9  */
10 
17 #if !defined(RT_UTIL_H)
18 #define RT_UTIL_H 1
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #if !defined(USESINGLEFLT)
25 #define ACOS(x) acos(x)
26 #define COS(x) cos(x)
27 #define EXP(x) exp(x)
28 #define FABS(x) fabs(x)
29 #define POW(x, y) pow(x, y)
30 #define SIN(x) sin(x)
31 #define SQRT(x) sqrt(x)
32 /* Note: it may be necessary to define _GNU_SOURCE */
33 /* to obtain function prototypes for sincos() */
34 #if defined(__APPLE__)
35 #define SINCOS(ax, sx, cx) __sincos(ax, sx, cx)
36 #elif defined(_MSC_VER)
37 #define SINCOS(ax, sx, cx) (*(sx))=sin(ax); (*(cx))=cos(ax)
38 #else
39 #define SINCOS(ax, sx, cx) sincos(ax, sx, cx)
40 #endif
41 
42 #else
43 
44 #define ACOS(x) acosf(x)
45 #define COS(x) cosf(x)
46 #define EXP(x) expf(x)
47 #define FABS(x) fabsf(x)
48 #define POW(x, y) powf(x, y)
49 #define SIN(x) sinf(x)
50 #define SQRT(x) sqrtf(x)
51 /* Note: it may be necessary to define _GNU_SOURCE */
52 /* to obtain function prototypes for sincos() */
53 #if defined(__APPLE__)
54 #define SINCOS(ax, sx, cx) __sincosf(ax, sx, cx)
55 #elif defined(_MSC_VER)
56 #define SINCOS(ax, sx, cx) (*(sx))=sinf(ax); (*(cx))=cosf(ax)
57 #else
58 #define SINCOS(ax, sx, cx) sincosf(ax, sx, cx)
59 #endif
60 
61 #endif
62 
63 typedef void * rt_timerhandle; /* a timer handle */
64 rt_timerhandle rt_timer_create(void); /* create a timer (clears timer) */
65 void rt_timer_destroy(rt_timerhandle); /* create a timer (clears timer) */
66 void rt_timer_start(rt_timerhandle); /* start a timer (clears timer) */
67 void rt_timer_stop(rt_timerhandle); /* stop a timer */
68 double rt_timer_time(rt_timerhandle); /* report elapsed time in seconds */
69 double rt_timer_timenow(rt_timerhandle); /* report elapsed time in seconds */
70 
71 #define RT_RAND_MAX 4294967296.0 /* Max random value from rt_rand */
72 #define RT_RAND_MAX_INV 2.3283064365e-10 /* Max random value from rt_rand */
73 unsigned int rt_rand(unsigned int *); /* thread-safe 32-bit RNG */
74 
75 /* select the RNG to use as the basis for all of the floating point work */
76 #define RT_RNG_USE_KISS93 1
77 
78 #if defined(RT_RNG_USE_QUICK_AND_DIRTY)
79 
80 /* Quick and Dirty RNG */
81 typedef struct {
82  unsigned int randval;
84 #define RT_RNG_MAX 4294967296.0 /* max urand value: 2^32 */
85 
86 #elif defined(RT_RNG_USE_MERSENNE_TWISTER)
87 
88 /* Mersenne Twister */
89 typedef struct {
90  int mti; /* mti==N+1 means mt[N] is not initialized */
91  unsigned int mt[624]; /* N: the array for the state vector */
92  unsigned int mag01[2];
94 #define RT_RNG_MAX 4294967296.0 /* max urand value: 2^32 */
95 
96 #elif defined(RT_RNG_USE_KISS93)
97 
98 /* KISS93 */
99 typedef struct {
100  unsigned int x;
101  unsigned int y;
102  unsigned int z;
103  unsigned int w;
104  unsigned int c;
105  unsigned int k;
106  unsigned int m;
108 #define RT_RNG_MAX 4294967296.0 /* max urand value: 2^32 */
109 
110 #else
111 
112 /* KISS99 */
113 typedef struct {
114  unsigned int x;
115  unsigned int y;
116  unsigned int z;
117  unsigned int c;
119 #define RT_RNG_MAX 4294967296.0 /* max urand value: 2^32 */
120 
121 #endif
122 
123 void rng_urand_init(rng_urand_handle *rngh);
124 void rng_urand_seed(rng_urand_handle *rngh, unsigned int seed);
125 unsigned int rng_urand(rng_urand_handle *rngh);
126 
129 
130 void rng_frand_init(rng_frand_handle *rngh);
131 /* generates a random number on [0,1)-real-interval */
132 float rng_frand(rng_frand_handle *rngh);
133 void rng_frand_seed(rng_frand_handle *rngh, unsigned int seed);
134 
135 void rng_drand_init(rng_drand_handle *rngh);
136 /* generates a random number on [0,1)-real-interval */
137 double rng_drand(rng_frand_handle *rngh);
138 void rng_drand_seed(rng_frand_handle *rngh, unsigned int seed);
139 
140 /* routine to help create seeds for parallel runs */
141 unsigned int rng_seed_from_tid_nodeid(int tid, int node);
142 
143 /* TEA key mixing helper routines */
144 unsigned int tea2(unsigned int v0, unsigned int v1);
145 unsigned int tea4(unsigned int v0, unsigned int v1);
146 
147 /* routines for jittering AA, AO, and DoF samples */
148 void jitter_offset2f(unsigned int *pval, float *xy);
149 void jitter_disc2f(unsigned int *pval, float *xy);
150 void jitter_sphere3f(rng_frand_handle *rngh, float *dir);
151 
152 #ifdef __cplusplus
153 }
154 #endif
155 
156 #endif
double rng_drand(rng_frand_handle *rngh)
Definition: util.c:572
void rt_timer_start(rt_timerhandle)
Definition: util.c:168
float rng_frand(rng_frand_handle *rngh)
Definition: util.c:560
void rng_frand_seed(rng_frand_handle *rngh, unsigned int seed)
Definition: util.c:564
void * rt_timerhandle
Definition: util.h:63
double rt_timer_timenow(rt_timerhandle)
Definition: util.c:237
unsigned int c
Definition: util.h:104
void rt_timer_stop(rt_timerhandle)
Definition: util.c:177
rng_urand_handle rng_frand_handle
Definition: util.h:127
void rng_urand_seed(rng_urand_handle *rngh, unsigned int seed)
Definition: util.c:493
void jitter_offset2f(unsigned int *pval, float *xy)
Definition: util.c:751
rng_urand_handle rng_drand_handle
Definition: util.h:128
unsigned int y
Definition: util.h:101
unsigned int x
Definition: util.h:100
unsigned int rng_urand(rng_urand_handle *rngh)
Definition: util.c:501
void rng_urand_init(rng_urand_handle *rngh)
Definition: util.c:483
void rng_drand_init(rng_drand_handle *rngh)
Definition: util.c:568
void jitter_disc2f(unsigned int *pval, float *xy)
Definition: util.c:758
unsigned int rt_rand(unsigned int *)
Definition: util.c:324
void rng_frand_init(rng_frand_handle *rngh)
Definition: util.c:556
void jitter_sphere3f(rng_frand_handle *rngh, float *dir)
Definition: util.c:779
double rt_timer_time(rt_timerhandle)
Definition: util.c:186
unsigned int m
Definition: util.h:106
unsigned int w
Definition: util.h:103
void rt_timer_destroy(rt_timerhandle)
Definition: util.c:233
unsigned int k
Definition: util.h:105
rt_timerhandle rt_timer_create(void)
Definition: util.c:226
unsigned int rng_seed_from_tid_nodeid(int tid, int node)
Definition: util.c:583
void rng_drand_seed(rng_frand_handle *rngh, unsigned int seed)
Definition: util.c:576
unsigned int tea4(unsigned int v0, unsigned int v1)
Definition: util.c:626
unsigned int tea2(unsigned int v0, unsigned int v1)
Definition: util.c:614
unsigned int z
Definition: util.h:102