|
36 | 36 | #define CONCATENATE_INNER(arg1, arg2) arg1 ## arg2 |
37 | 37 | #define CONCATENATE(arg1, arg2) CONCATENATE_INNER(arg1, arg2) |
38 | 38 |
|
| 39 | +/* Portable thread-safe pseudo-random number generator. */ |
| 40 | +#ifdef _WIN32 |
| 41 | + // Use rand_s. |
| 42 | + #include <errno.h> |
| 43 | + unsigned int thread_safe_rand(unsigned int *seed_state) { |
| 44 | + unsigned int result; |
| 45 | + errno_t err = rand_s(&result); |
| 46 | + return result; |
| 47 | + } |
| 48 | +#else /* #ifdef _WIN32 */ |
| 49 | + // Use rand_r. |
| 50 | + unsigned int thread_safe_rand(unsigned int *seed_state) { |
| 51 | + return rand_r(seed_state); |
| 52 | + } |
| 53 | +#endif /* #ifdef _WIN32 */ |
| 54 | + |
39 | 55 | /* Functions to initialize bit pseudo-random generators and generate bits. */ |
40 | 56 | #define BITSEED bitseed |
41 | 57 | #define BITSEEDTYPE cpfloat_bitseed_t |
|
46 | 62 | #define ADVANCEBIT(seed, thread, nloc) \ |
47 | 63 | pcg32_advance_r(seed, thread * nloc - 1); |
48 | 64 | #define GENBIT(seed) (pcg32_random_r(seed) & (1U << 31)) |
49 | | -#else /* #ifdef PCG_VARIANTS_H_INCLUDED */ |
| 65 | +#else /* #ifdef PCG_VARIANTS_H_INCLUDED */ |
50 | 66 | #ifdef _OPENMP |
51 | 67 | #define INITBIT(seed) *seed = time(NULL); |
52 | 68 | #define GEN_SINGLE_BIT(seed) (rand_r(seed) & (1U << 30)) |
53 | 69 | #define PRNG_ADVANCE_BIT prng_advance_bit |
54 | 70 | static inline BITTYPE PRNG_ADVANCE_BIT(BITSEEDTYPE *seed, size_t delta) { |
55 | 71 | for (size_t i=0; i<delta; i++) |
56 | | - rand_r(seed); |
| 72 | + thread_safe_rand(seed); |
57 | 73 | return GEN_SINGLE_BIT(seed); |
58 | 74 | } |
59 | 75 | #define ADVANCEBIT(seed, thread, nloc) PRNG_ADVANCE_BIT(seed, thread); |
60 | 76 | #define GENBIT(seed) PRNG_ADVANCE_BIT(seed, nthreads) |
61 | | -#else /* #ifdef _OPENMP */ |
| 77 | +#else /* #ifdef _OPENMP */ |
62 | 78 | #define INITBIT(seed) srand(time(NULL)); |
63 | 79 | #define GEN_SINGLE_BIT(seed) (rand() & (1U << 30)) |
64 | 80 | #define GENBIT(seed) (GEN_SINGLE_BIT(seed)) |
65 | 81 | #endif /* #ifdef _OPENMP */ |
66 | | -#endif /* #ifdef PCG_VARIANTS_H_INCLUDED */ |
| 82 | +#endif /* #ifdef PCG_VARIANTS_H_INCLUDED */ |
67 | 83 |
|
68 | 84 | #define PRNG_BIT_INIT \ |
69 | 85 | if (fpopts->BITSEED == NULL) { \ |
|
0 commit comments